inline bool pj_gridlist_merge_gridfile(std::string const& gridname, StreamPolicy const& stream_policy, srs::shared_grids & grids, std::vector<std::size_t> & gridindexes) { // Try to find in the existing list of loaded grids. Add all // matching grids as with NTv2 we can get many grids from one // file (one shared gridname). { boost::shared_lock<boost::shared_mutex> lock(grids.mutex); if (pj_gridlist_find_all(gridname, grids.gridinfo, gridindexes)) return true; } // Try to load the named grid. typename StreamPolicy::stream_type is; stream_policy.open(is, gridname); pj_gridinfo new_grids; if (! pj_gridinfo_init(gridname, is, new_grids)) { return false; } // Add the grid now that it is loaded. std::size_t orig_size = 0; std::size_t new_size = 0; { boost::unique_lock<boost::shared_mutex> lock(grids.mutex); // Try to find in the existing list of loaded grids again // in case other thread already added it. if (pj_gridlist_find_all(gridname, grids.gridinfo, gridindexes)) return true; orig_size = grids.gridinfo.size(); new_size = orig_size + new_grids.size(); grids.gridinfo.resize(new_size); for (std::size_t i = 0 ; i < new_grids.size() ; ++ i) new_grids[i].swap(grids.gridinfo[i + orig_size]); } pj_gridlist_add_seq_inc(gridindexes, orig_size, new_size); return true; }
PJ_GRID_INFO proj_grid_info(const char *gridname) { /****************************************************************************** Information about a named datum grid. Returns PJ_GRID_INFO struct. ******************************************************************************/ PJ_GRID_INFO grinfo; /*PJ_CONTEXT *ctx = proj_context_create(); */ PJ_CONTEXT *ctx = pj_get_default_ctx(); PJ_GRIDINFO *gridinfo = pj_gridinfo_init(ctx, gridname); memset(&grinfo, 0, sizeof(PJ_GRID_INFO)); /* in case the grid wasn't found */ if (gridinfo->filename == NULL) { pj_gridinfo_free(ctx, gridinfo); strcpy(grinfo.format, "missing"); return grinfo; } /* The string copies below are automatically null-terminated due to */ /* the memset above, so strncpy is safe */ /* name of grid */ strncpy (grinfo.gridname, gridname, sizeof(grinfo.gridname) - 1); /* full path of grid */ pj_find_file(ctx, gridname, grinfo.filename, sizeof(grinfo.filename) - 1); /* grid format */ strncpy (grinfo.format, gridinfo->format, sizeof(grinfo.format) - 1); /* grid size */ grinfo.n_lon = gridinfo->ct->lim.lam; grinfo.n_lat = gridinfo->ct->lim.phi; /* cell size */ grinfo.cs_lon = gridinfo->ct->del.lam; grinfo.cs_lat = gridinfo->ct->del.phi; /* bounds of grid */ grinfo.lowerleft = gridinfo->ct->ll; grinfo.upperright.lam = grinfo.lowerleft.lam + grinfo.n_lon*grinfo.cs_lon; grinfo.upperright.phi = grinfo.lowerleft.phi + grinfo.n_lat*grinfo.cs_lat; pj_gridinfo_free(ctx, gridinfo); return grinfo; }
static int pj_gridlist_merge_gridfile( const char *gridname ) { int i, got_match=0; PJ_GRIDINFO *this_grid, *tail = NULL; /* -------------------------------------------------------------------- */ /* Try to find in the existing list of loaded grids. Add all */ /* matching grids as with NTv2 we can get many grids from one */ /* file (one shared gridname). */ /* -------------------------------------------------------------------- */ for( this_grid = grid_list; this_grid != NULL; this_grid = this_grid->next) { if( strcmp(this_grid->gridname,gridname) == 0 ) { got_match = 1; /* dont add to the list if it is invalid. */ if( this_grid->ct == NULL ) return 0; /* do we need to grow the list? */ if( last_nadgrids_count >= last_nadgrids_max - 2 ) { PJ_GRIDINFO **new_list; int new_max = last_nadgrids_max + 20; new_list = (PJ_GRIDINFO **) pj_malloc(sizeof(void*) * new_max); if( last_nadgrids_list != NULL ) { memcpy( new_list, last_nadgrids_list, sizeof(void*) * last_nadgrids_max ); pj_dalloc( last_nadgrids_list ); } last_nadgrids_list = new_list; last_nadgrids_max = new_max; } /* add to the list */ last_nadgrids_list[last_nadgrids_count++] = this_grid; last_nadgrids_list[last_nadgrids_count] = NULL; } tail = this_grid; } if( got_match ) return 1; /* -------------------------------------------------------------------- */ /* Try to load the named grid. */ /* -------------------------------------------------------------------- */ this_grid = pj_gridinfo_init( gridname ); if( this_grid == NULL ) { /* we should get at least a stub grid with a missing "ct" member */ assert( FALSE ); return 0; } if( tail != NULL ) tail->next = this_grid; else grid_list = this_grid; /* -------------------------------------------------------------------- */ /* Recurse to add the grid now that it is loaded. */ /* -------------------------------------------------------------------- */ return pj_gridlist_merge_gridfile( gridname ); }