Statistics
| Revision:

svn-gvsig-desktop / tags / v1_9_Build_1222 / libraries / libjni-proj4 / src / pj_apply_gridshift.c @ 41849

History | View | Annotate | Download (6.08 KB)

1
/******************************************************************************
2
 * $Id: pj_apply_gridshift.c,v 1.8 2003/03/20 21:29:41 warmerda Exp $
3
 *
4
 * Project:  PROJ.4
5
 * Purpose:  Apply datum shifts based on grid shift files (normally NAD27 to
6
 *           NAD83 or the reverse).  This module is responsible for keeping
7
 *           a list of loaded grids, and calling with each one that is 
8
 *           allowed for a given datum (expressed as the nadgrids= parameter).
9
 * Author:   Frank Warmerdam, warmerdam@pobox.com
10
 *
11
 ******************************************************************************
12
 * Copyright (c) 2000, Frank Warmerdam <warmerdam@pobox.com>
13
 *
14
 * Permission is hereby granted, free of charge, to any person obtaining a
15
 * copy of this software and associated documentation files (the "Software"),
16
 * to deal in the Software without restriction, including without limitation
17
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18
 * and/or sell copies of the Software, and to permit persons to whom the
19
 * Software is furnished to do so, subject to the following conditions:
20
 *
21
 * The above copyright notice and this permission notice shall be included
22
 * in all copies or substantial portions of the Software.
23
 *
24
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
25
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
27
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
29
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
30
 * DEALINGS IN THE SOFTWARE.
31
 ******************************************************************************
32
 *
33
 * $Log: pj_apply_gridshift.c,v $
34
 * Revision 1.8  2003/03/20 21:29:41  warmerda
35
 * Fixed bug in checking against grid bounds.
36
 *
37
 * Revision 1.7  2003/03/17 19:45:23  warmerda
38
 * improved error handling
39
 *
40
 * Revision 1.6  2003/03/17 18:56:34  warmerda
41
 * implement heirarchical NTv2 gridinfos
42
 *
43
 * Revision 1.5  2003/03/15 06:02:02  warmerda
44
 * preliminary NTv2 support, major restructure of datum shifting
45
 *
46
 * Revision 1.4  2002/07/08 02:32:05  warmerda
47
 * ensure clean C++ builds
48
 *
49
 * Revision 1.3  2002/04/30 16:27:27  warmerda
50
 * improve debug output
51
 *
52
 * Revision 1.2  2001/03/15 16:57:55  warmerda
53
 * fixed intermittent problem in pj_load_nadgrids()
54
 *
55
 * Revision 1.1  2000/07/06 23:32:27  warmerda
56
 * New
57
 *
58
 */
59

    
60
#define PJ_LIB__
61

    
62
#include <projects.h>
63
#include <string.h>
64
#include <math.h>
65

    
66
/************************************************************************/
67
/*                         pj_apply_gridshift()                         */
68
/************************************************************************/
69

    
70
int pj_apply_gridshift( const char *nadgrids, int inverse, 
71
                        long point_count, int point_offset,
72
                        double *x, double *y, double *z )
73

    
74
{
75
    int grid_count = 0;
76
    PJ_GRIDINFO   **tables;
77
    int  i;
78
    int debug_flag = getenv( "PROJ_DEBUG" ) != NULL;
79
    static int debug_count = 0;
80

    
81
    pj_errno = 0;
82

    
83
    tables = pj_gridlist_from_nadgrids( nadgrids, &grid_count);
84
    if( tables == NULL || grid_count == 0 )
85
        return pj_errno;
86

    
87
    for( i = 0; i < point_count; i++ )
88
    {
89
        long io = i * point_offset;
90
        LP   input, output;
91
        int  itable;
92

    
93
        input.phi = y[io];
94
        input.lam = x[io];
95
        output.phi = HUGE_VAL;
96
        output.lam = HUGE_VAL;
97

    
98
        /* keep trying till we find a table that works */
99
        for( itable = 0; itable < grid_count; itable++ )
100
        {
101
            PJ_GRIDINFO *gi = tables[itable];
102
            struct CTABLE *ct = gi->ct;
103

    
104
            /* skip tables that don't match our point at all.  */
105
            if( ct->ll.phi > input.phi || ct->ll.lam > input.lam
106
                || ct->ll.phi + (ct->lim.phi-1) * ct->del.phi < input.phi
107
                || ct->ll.lam + (ct->lim.lam-1) * ct->del.lam < input.lam )
108
                continue;
109

    
110
            /* If we have child nodes, check to see if any of them apply. */
111
            if( gi->child != NULL )
112
            {
113
                PJ_GRIDINFO *child;
114

    
115
                for( child = gi->child; child != NULL; child = child->next )
116
                {
117
                    struct CTABLE *ct1 = child->ct;
118

    
119
                    if( ct1->ll.phi > input.phi || ct1->ll.lam > input.lam
120
                      || ct1->ll.phi+(ct1->lim.phi-1)*ct1->del.phi < input.phi
121
                      || ct1->ll.lam+(ct1->lim.lam-1)*ct1->del.lam < input.lam)
122
                        continue;
123

    
124
                    break;
125
                }
126

    
127
                /* we found a more refined child node to use */
128
                if( child != NULL )
129
                {
130
                    gi = child;
131
                    ct = child->ct;
132
                }
133
            }
134

    
135
            /* load the grid shift info if we don't have it. */
136
            if( ct->cvs == NULL && !pj_gridinfo_load( gi ) )
137
            {
138
                pj_errno = -38;
139
                return pj_errno;
140
            }
141
            
142
            output = nad_cvt( input, inverse, ct );
143
            if( output.lam != HUGE_VAL )
144
            {
145
                if( debug_flag && debug_count++ < 20 )
146
                    fprintf( stderr,
147
                             "pj_apply_gridshift(): used %s\n",
148
                             ct->id );
149
                break;
150
            }
151
        }
152

    
153
        if( output.lam == HUGE_VAL )
154
        {
155
            if( debug_flag )
156
            {
157
                fprintf( stderr, 
158
                         "pj_apply_gridshift(): failed to find a grid shift table for\n"
159
                         "                      location (%.7fdW,%.7fdN)\n",
160
                         x[io] * RAD_TO_DEG, 
161
                         y[io] * RAD_TO_DEG );
162
                fprintf( stderr, 
163
                         "   tried: %s\n", nadgrids );
164
            }
165
        
166
            pj_errno = -38;
167
            return pj_errno;
168
        }
169
        else
170
        {
171
            y[io] = output.phi;
172
            x[io] = output.lam;
173
        }
174
    }
175

    
176
    return 0;
177
}
178