diff options
Diffstat (limited to 'src/pj_apply_gridshift.c')
| -rw-r--r-- | src/pj_apply_gridshift.c | 216 |
1 files changed, 26 insertions, 190 deletions
diff --git a/src/pj_apply_gridshift.c b/src/pj_apply_gridshift.c index 7860cbdd..287ffb07 100644 --- a/src/pj_apply_gridshift.c +++ b/src/pj_apply_gridshift.c @@ -6,10 +6,10 @@ * NAD83 or the reverse). This module is responsible for keeping * a list of loaded grids, and calling with each one that is * allowed for a given datum (expressed as the nadgrids= parameter). - * Author: Frank Warmerdam, warmerda@home.com + * Author: Frank Warmerdam, warmerdam@pobox.com * ****************************************************************************** - * Copyright (c) 2000, Frank Warmerdam + * Copyright (c) 2000, Frank Warmerdam <warmerdam@pobox.com> * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -31,6 +31,9 @@ ****************************************************************************** * * $Log$ + * Revision 1.5 2003/03/15 06:02:02 warmerda + * preliminary NTv2 support, major restructure of datum shifting + * * Revision 1.4 2002/07/08 02:32:05 warmerda * ensure clean C++ builds * @@ -51,190 +54,6 @@ #include <string.h> #include <math.h> -#define GRID_MAX 100 - -/* used only by pj_get_grid() and pj_deallocate_grids() */ -static int grid_count = 0; -static char **grid_names = NULL; -static struct CTABLE **grid_list = NULL; - -/* used only by pj_load_nadgrids() and pj_deallocate_grids() */ -static struct CTABLE **last_nadgrids_list = NULL; -static char *last_nadgrids = NULL; - -/************************************************************************/ -/* pj_deallocate_grids() */ -/* */ -/* Deallocate all loaded grids. */ -/************************************************************************/ - -void pj_deallocate_grids() - -{ - if( grid_count > 0 ) - { - int i; - - for( i = 0; i < grid_count; i++ ) - { - if( grid_list[i] != NULL ) - nad_free( grid_list[i] ); - pj_dalloc( grid_names[i] ); - } - - pj_dalloc( grid_names ); - pj_dalloc( grid_list ); - - grid_names = NULL; - grid_list = NULL; - - grid_count = 0; - } - - if( last_nadgrids != NULL ) - { - pj_dalloc( last_nadgrids ); - last_nadgrids = NULL; - - pj_dalloc( last_nadgrids_list ); - last_nadgrids_list = NULL; - } -} - -/************************************************************************/ -/* pj_get_grid() */ -/* */ -/* Find the requested grid in the list, or if not present, try */ -/* and load it. On failure returns NULL and sets pj_errno. */ -/************************************************************************/ - -static struct CTABLE *pj_get_grid( const char *name ) - -{ - int i; - -/* -------------------------------------------------------------------- */ -/* First look in the existing list. */ -/* -------------------------------------------------------------------- */ - for( i = 0; i < grid_count; i++ ) - { - if( strcmp( grid_names[i], name ) == 0 ) - { - if( grid_list[i] == NULL ) - pj_errno = -38; - - return grid_list[i]; - } - } - -/* -------------------------------------------------------------------- */ -/* Add entry for this file in the grid list. */ -/* -------------------------------------------------------------------- */ - if( grid_count == 0 ) - { - grid_names = (char **) pj_malloc(sizeof(char *) * GRID_MAX); - memset( grid_names, 0, sizeof(char *) * GRID_MAX ); - grid_list = (struct CTABLE **) - pj_malloc(sizeof(struct CTABLE *) * GRID_MAX ); - memset( grid_list, 0, sizeof(struct CTABLE *) * GRID_MAX ); - } - else if( grid_count >= GRID_MAX ) - { - pj_errno = -38; - return NULL; - } - - grid_count++; - - grid_names[grid_count-1] = (char *) pj_malloc(strlen(name)+1); - strcpy( grid_names[grid_count-1], name ); - -/* -------------------------------------------------------------------- */ -/* Read the file. */ -/* -------------------------------------------------------------------- */ - grid_list[grid_count-1] = nad_init( (char *) name ); - - return grid_list[grid_count-1]; -} - -/************************************************************************/ -/* pj_load_nadgrids() */ -/* */ -/* This functions loads the list of grids corresponding to a */ -/* particular nadgrids string into a list, and returns it. The */ -/* list is kept around till a request is made with a different */ -/* string in order to cut down on the string parsing cost, and */ -/* the cost of building the list of tables each time. */ -/************************************************************************/ - -static struct CTABLE **pj_load_nadgrids( const char *nadgrids ) - -{ - int nadgrids_count = 0; - const char *s; - - pj_errno = 0; - - if( last_nadgrids != NULL - && strcmp(nadgrids,last_nadgrids) == 0 ) - return last_nadgrids_list; - -/* -------------------------------------------------------------------- */ -/* Free old one, if any, and make space for new list. */ -/* -------------------------------------------------------------------- */ - if( last_nadgrids != NULL ) - { - pj_dalloc(last_nadgrids); - } - - last_nadgrids = (char *) pj_malloc(strlen(nadgrids)+1); - strcpy( last_nadgrids, nadgrids ); - - if( last_nadgrids_list == NULL ) - last_nadgrids_list = (struct CTABLE **) - pj_malloc(sizeof(struct CTABLE *) * GRID_MAX); - -/* -------------------------------------------------------------------- */ -/* Loop processing names out of nadgrids one at a time. */ -/* -------------------------------------------------------------------- */ - for( s = nadgrids; *s != '\0'; ) - { - int end_char; - char name[128]; - - for( end_char = 0; - s[end_char] != '\0' && s[end_char] != ','; - end_char++ ) {} - - if( end_char > sizeof(name) ) - { - pj_errno = -38; - return NULL; - } - - strncpy( name, s, end_char ); - name[end_char] = '\0'; - - s += end_char; - if( *s == ',' ) - s++; - - - last_nadgrids_list[nadgrids_count] = pj_get_grid( name ); - if( last_nadgrids_list[nadgrids_count] == NULL ) - { - pj_errno = -38; - return NULL; - } - - nadgrids_count++; - } - - last_nadgrids_list[nadgrids_count] = NULL; - - return last_nadgrids_list; -} - /************************************************************************/ /* pj_apply_gridshift() */ /************************************************************************/ @@ -244,10 +63,11 @@ int pj_apply_gridshift( const char *nadgrids, int inverse, double *x, double *y, double *z ) { - struct CTABLE **tables = pj_load_nadgrids( nadgrids ); + int grid_count = 0; + PJ_GRIDINFO **tables = pj_gridlist_from_nadgrids( nadgrids, &grid_count); int i; - if( tables == NULL ) + if( tables == NULL || grid_count == 0 ) return pj_errno; for( i = 0; i < point_count; i++ ) @@ -260,9 +80,25 @@ int pj_apply_gridshift( const char *nadgrids, int inverse, input.lam = x[io]; /* keep trying till we find a table that works */ - for( itable = 0; tables[itable] != NULL; itable++ ) + for( itable = 0; itable < grid_count; itable++ ) { - output = nad_cvt( input, inverse, tables[itable] ); + struct CTABLE *ct = tables[itable]->ct; + + /* skip tables that don't match our point at all. */ + if( ct->ll.phi > input.phi || ct->ll.lam > input.lam + || ct->ll.phi + ct->lim.phi * ct->del.phi < input.phi + || ct->ll.lam + ct->lim.lam * ct->del.lam < input.lam ) + continue; + + /* load the grid shift info if we don't have it. */ + if( tables[itable]->ct->cvs == NULL + && !pj_gridinfo_load( tables[itable] ) ) + { + pj_errno = -38; + return pj_errno; + } + + output = nad_cvt( input, inverse, tables[itable]->ct ); if( output.lam != HUGE_VAL ) break; } |
