Statistics
| Revision:

svn-gvsig-desktop / tags / v1_10_0_Build_1261 / libraries / libjni-proj4 / src / nad_init.c @ 33914

History | View | Annotate | Download (5.76 KB)

1
/******************************************************************************
2
 * $Id: nad_init.c,v 1.8 2003/03/17 18:56:01 warmerda Exp $
3
 *
4
 * Project:  PROJ.4
5
 * Purpose:  Load datum shift files into memory.
6
 * Author:   Frank Warmerdam, warmerdam@pobox.com
7
 *
8
 ******************************************************************************
9
 * Copyright (c) 2000, Frank Warmerdam
10
 *
11
 * Permission is hereby granted, free of charge, to any person obtaining a
12
 * copy of this software and associated documentation files (the "Software"),
13
 * to deal in the Software without restriction, including without limitation
14
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15
 * and/or sell copies of the Software, and to permit persons to whom the
16
 * Software is furnished to do so, subject to the following conditions:
17
 *
18
 * The above copyright notice and this permission notice shall be included
19
 * in all copies or substantial portions of the Software.
20
 *
21
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27
 * DEALINGS IN THE SOFTWARE.
28
 ******************************************************************************
29
 *
30
 * $Log: nad_init.c,v $
31
 * Revision 1.8  2003/03/17 18:56:01  warmerda
32
 * implement delayed loading of ctable format files
33
 *
34
 * Revision 1.7  2003/03/15 06:02:02  warmerda
35
 * preliminary NTv2 support, major restructure of datum shifting
36
 *
37
 * Revision 1.6  2002/07/08 02:32:05  warmerda
38
 * ensure clean C++ builds
39
 *
40
 * Revision 1.5  2002/04/30 16:26:07  warmerda
41
 * trip newlines of ctable id field
42
 *
43
 * Revision 1.4  2001/08/17 17:28:37  warmerda
44
 * removed use of emess()
45
 *
46
 * Revision 1.3  2001/04/05 19:31:54  warmerda
47
 * substantially reorganized and added NTv1 support
48
 *
49
 */
50

    
51
#define PJ_LIB__
52

    
53
#include <projects.h>
54
#include <stdio.h>
55
#include <errno.h>
56
#include <assert.h>
57
#include <string.h>
58

    
59
/************************************************************************/
60
/*                          nad_ctable_load()                           */
61
/*                                                                      */
62
/*      Load the data portion of a ctable formatted grid.               */
63
/************************************************************************/
64

    
65
int nad_ctable_load( struct CTABLE *ct, FILE *fid )
66

    
67
{
68
    int  a_size;
69

    
70
    fseek( fid, sizeof(struct CTABLE), SEEK_SET );
71

    
72
    /* read all the actual shift values */
73
    a_size = ct->lim.lam * ct->lim.phi;
74
    ct->cvs = (FLP *) pj_malloc(sizeof(FLP) * a_size);
75
    if( ct->cvs == NULL 
76
        || fread(ct->cvs, sizeof(FLP), a_size, fid) != a_size )
77
    {
78
        pj_errno = -38;
79
        return 0;
80
    }
81

    
82
    return 1;
83
} 
84

    
85
/************************************************************************/
86
/*                          nad_ctable_init()                           */
87
/*                                                                      */
88
/*      Read the header portion of a "ctable" format grid.              */
89
/************************************************************************/
90

    
91
struct CTABLE *nad_ctable_init( FILE * fid )
92
{
93
    struct CTABLE *ct;
94
    int                id_end;
95

    
96
    /* read the table header */
97
    ct = (struct CTABLE *) pj_malloc(sizeof(struct CTABLE));
98
    if( ct == NULL 
99
        || fread( ct, sizeof(struct CTABLE), 1, fid ) != 1 )
100
    {
101
        pj_errno = -38;
102
        return NULL;
103
    }
104

    
105
    /* do some minimal validation to ensure the structure isn't corrupt */
106
    if( ct->lim.lam < 1 || ct->lim.lam > 100000 
107
        || ct->lim.phi < 1 || ct->lim.phi > 100000 )
108
    {
109
        pj_errno = -38;
110
        return NULL;
111
    }
112
    
113
    /* trim white space and newlines off id */
114
    for( id_end = strlen(ct->id)-1; id_end > 0; id_end-- )
115
    {
116
        if( ct->id[id_end] == '\n' || ct->id[id_end] == ' ' )
117
            ct->id[id_end] = '\0';
118
        else
119
            break;
120
    }
121

    
122
    ct->cvs = NULL;
123

    
124
    return ct;
125
}
126

    
127
/************************************************************************/
128
/*                              nad_init()                              */
129
/*                                                                      */
130
/*      Read a datum shift file in any of the supported binary formats. */
131
/************************************************************************/
132

    
133
struct CTABLE *nad_init(char *name) 
134
{
135
    char         fname[MAX_PATH_FILENAME+1];
136
    struct CTABLE *ct;
137
    FILE         *fid;
138
    char        header[512];
139

    
140
    errno = pj_errno = 0;
141

    
142
/* -------------------------------------------------------------------- */
143
/*      Open the file using the usual search rules.                     */
144
/* -------------------------------------------------------------------- */
145
    strcpy(fname, name);
146
    if (!(fid = pj_open_lib(fname, "rb"))) {
147
        pj_errno = errno;
148
        return 0;
149
    }
150
    
151
    ct = nad_ctable_init( fid );
152
    if( ct != NULL )
153
    {
154
        if( !nad_ctable_load( ct, fid ) )
155
        {
156
            nad_free( ct );
157
            ct = NULL;
158
        }
159
    }
160

    
161
    fclose(fid);
162
    return ct;
163
}
164

    
165
/************************************************************************/
166
/*                              nad_free()                              */
167
/*                                                                      */
168
/*      Free a CTABLE grid shift structure produced by nad_init().      */
169
/************************************************************************/
170

    
171
void nad_free(struct CTABLE *ct) 
172
{
173
    if (ct) {
174
        if( ct->cvs != NULL )
175
            pj_dalloc(ct->cvs);
176

    
177
        pj_dalloc(ct);
178
    }
179
}