Beispiel #1
0
int pj_apply_gridshift( projCtx ctx, const char *nadgrids, int inverse, 
                        long point_count, int point_offset,
                        double *x, double *y, double *z )

{
    PJ_GRIDINFO **gridlist;
    int           grid_count;
    int           ret;
    
    gridlist = pj_gridlist_from_nadgrids( ctx, nadgrids, &grid_count );

    if( gridlist == NULL || grid_count == 0 )
        return ctx->last_errno;

    ret = pj_apply_gridshift_3( ctx, gridlist, grid_count, inverse, 
                                point_count, point_offset, x, y, z );

    /* 
    ** Note this frees the array of grid list pointers, but not the grids
    ** which is as intended.  The grids themselves live on.
    */
    pj_dalloc( gridlist );

    return ret;
}
Beispiel #2
0
PJ_GRIDINFO *pj_gc_findgrid( projCtx ctx, PJ_GridCatalog *catalog, int after,
                             LP location, double date,
                             PJ_Region *optimal_region,
                             double *grid_date ) 
{
    int iEntry;
    PJ_GridCatalogEntry *entry = NULL;

    for( iEntry = 0; iEntry < catalog->entry_count; iEntry++ ) 
    {
        entry = catalog->entries + iEntry;

        if( (after && entry->date < date) 
            || (!after && entry->date > date) )
            continue;

        if( location.lam < entry->region.ll_long
            || location.lam > entry->region.ur_long
            || location.phi < entry->region.ll_lat
            || location.phi > entry->region.ur_lat )
            continue;

        if( entry->available == -1 )
            continue;

        break;
    }

    if( iEntry == catalog->entry_count )
    {
        if( grid_date )
            *grid_date = 0.0;
        if( optimal_region != NULL )
            memset( optimal_region, 0, sizeof(PJ_Region));
        return NULL;
    }

    if( grid_date )
        *grid_date = entry->date;

    if( optimal_region )
    {
        
    }

    if( entry->gridinfo == NULL )
    {
        PJ_GRIDINFO **gridlist = NULL;
        int grid_count = 0;
        gridlist = pj_gridlist_from_nadgrids( ctx, entry->definition, 
                                              &grid_count);
        if( grid_count == 1 )
            entry->gridinfo = gridlist[0];
    }
    
    return entry->gridinfo;
}
Beispiel #3
0
int pj_apply_gridshift_2( PJ *defn, int inverse, 
                          long point_count, int point_offset,
                          double *x, double *y, double *z )

{
    if( defn->gridlist == NULL )
    {
        defn->gridlist = 
            pj_gridlist_from_nadgrids( pj_get_ctx( defn ),
                                       pj_param(defn->ctx, defn->params,"snadgrids").s,
                                       &(defn->gridlist_count) );

        if( defn->gridlist == NULL || defn->gridlist_count == 0 )
            return defn->ctx->last_errno;
    }
     
    return pj_apply_gridshift_3( pj_get_ctx( defn ),
                                 defn->gridlist, defn->gridlist_count, inverse, 
                                 point_count, point_offset, x, y, z );
}
Beispiel #4
0
int proj_vgrid_init(PJ* P, const char *grids) {
/**********************************************

  Initizalize and populate gridlist.

    Takes a PJ-object and the plus-parameter
    name that is used in the proj-string to
    specify the grids to load, e.g. "+grids".
    The + should be left out here.

    Returns the number of loaded grids.

***********************************************/

    /* prepend "s" to the "grids" string to allow usage with pj_param */
    char *sgrids = (char *) pj_malloc( (strlen(grids)+1+1) *sizeof(char) );
    sprintf(sgrids, "%s%s", "s", grids);

    if (P->vgridlist_geoid == NULL) {
        P->vgridlist_geoid = pj_gridlist_from_nadgrids(
            P->ctx,
            pj_param(P->ctx, P->params, sgrids).s,
            &(P->vgridlist_geoid_count)
        );

        if( P->vgridlist_geoid == NULL || P->vgridlist_geoid_count == 0 ) {
            pj_dealloc(sgrids);
            return 0;
        }
    }

    if (P->vgridlist_geoid_count == 0) {
        proj_errno_set(P, PJD_ERR_FAILED_TO_LOAD_GRID);
    }

    pj_dealloc(sgrids);
    return P->vgridlist_geoid_count;
}
Beispiel #5
0
int pj_apply_gridshift( const char *nadgrids, int inverse, 
                        long point_count, int point_offset,
                        double *x, double *y, double *z )

{
    int grid_count = 0;
    PJ_GRIDINFO   **tables;
    int  i;
    int debug_flag = getenv( "PROJ_DEBUG" ) != NULL;
    static int debug_count = 0;

    pj_errno = 0;

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

    for( i = 0; i < point_count; i++ )
    {
        long io = i * point_offset;
        LP   input, output;
        int  itable;

        input.phi = y[io];
        input.lam = x[io];
        output.phi = HUGE_VAL;
        output.lam = HUGE_VAL;

        /* keep trying till we find a table that works */
        for( itable = 0; itable < grid_count; itable++ )
        {
            PJ_GRIDINFO *gi = tables[itable];
            struct CTABLE *ct = gi->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-1) * ct->del.phi < input.phi
                || ct->ll.lam + (ct->lim.lam-1) * ct->del.lam < input.lam )
                continue;

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

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

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

                    break;
                }

                /* we found a more refined child node to use */
                if( child != NULL )
                {
                    gi = child;
                    ct = child->ct;
                }
            }

            /* load the grid shift info if we don't have it. */
            if( ct->cvs == NULL && !pj_gridinfo_load( gi ) )
            {
                pj_errno = -38;
                return pj_errno;
            }
            
            output = nad_cvt( input, inverse, ct );
            if( output.lam != HUGE_VAL )
            {
                if( debug_flag && debug_count++ < 20 )
                    fprintf( stderr,
                             "pj_apply_gridshift(): used %s\n",
                             ct->id );
                break;
            }
        }

        if( output.lam == HUGE_VAL )
        {
            if( debug_flag )
            {
                fprintf( stderr, 
                         "pj_apply_gridshift(): failed to find a grid shift table for\n"
                         "                      location (%.7fdW,%.7fdN)\n",
                         x[io] * RAD_TO_DEG, 
                         y[io] * RAD_TO_DEG );
                fprintf( stderr, 
                         "   tried: %s\n", nadgrids );
            }
        
            pj_errno = -38;
            return pj_errno;
        }
        else
        {
            y[io] = output.phi;
            x[io] = output.lam;
        }
    }

    return 0;
}
Beispiel #6
0
int pj_apply_vgridshift( PJ *defn, const char *listname,
                         PJ_GRIDINFO ***gridlist_p,
                         int *gridlist_count_p,
                         int inverse,
                         long point_count, int point_offset,
                         double *x, double *y, double *z )

{
    int  i;
    static int debug_count = 0;
    PJ_GRIDINFO **tables;
    struct CTABLE ct;

    if( *gridlist_p == NULL )
    {
        *gridlist_p =
            pj_gridlist_from_nadgrids( pj_get_ctx(defn),
                                       pj_param(defn->ctx,defn->params,listname).s,
                                       gridlist_count_p );

        if( *gridlist_p == NULL || *gridlist_count_p == 0 )
            return defn->ctx->last_errno;
    }

    if( *gridlist_count_p == 0 )
    {
        pj_ctx_set_errno( defn->ctx, PJD_ERR_FAILED_TO_LOAD_GRID);
        return PJD_ERR_FAILED_TO_LOAD_GRID;
    }

    tables = *gridlist_p;
    defn->ctx->last_errno = 0;

    for( i = 0; i < point_count; i++ )
    {
        double value;
        long io = i * point_offset;
        LP   input;

        input.phi = y[io];
        input.lam = x[io];

        value = read_vgrid_value(defn, input, gridlist_count_p, tables, &ct);

        if( inverse )
            z[io] -= value;
        else
            z[io] += value;
        if( value != HUGE_VAL )
        {
            if( debug_count++ < 20 ) {
                proj_log_trace(defn, "pj_apply_gridshift(): used %s", ct.id);
                break;
            }
        }

        if( value == HUGE_VAL )
        {
            int itable;
            char gridlist[3000];

            proj_log_debug(defn,
                "pj_apply_vgridshift(): failed to find a grid shift table for\n"
                "                       location (%.7fdW,%.7fdN)",
                x[io] * RAD_TO_DEG,
                y[io] * RAD_TO_DEG );

            gridlist[0] = '\0';
            for( itable = 0; itable < *gridlist_count_p; itable++ )
            {
                PJ_GRIDINFO *gi = tables[itable];
                if( strlen(gridlist) + strlen(gi->gridname) > sizeof(gridlist)-100 )
                {
                    strcat( gridlist, "..." );
                    break;
                }

                if( itable == 0 )
                    sprintf( gridlist, "   tried: %s", gi->gridname );
                else
                    sprintf( gridlist+strlen(gridlist), ",%s", gi->gridname );
            }

            proj_log_debug(defn, "%s", gridlist);
            pj_ctx_set_errno( defn->ctx, PJD_ERR_GRID_AREA );

            return PJD_ERR_GRID_AREA;
        }
    }

    return 0;
}
int pj_apply_vgridshift(PJ *defn, const char *listname, PJ_GRIDINFO ***gridlist_p, int *gridlist_count_p, int inverse,
                        long point_count, int point_offset, double *x, double *y, double *z)

{
	int i;
	static int debug_count = 0;
	PJ_GRIDINFO **tables;

	if (*gridlist_p == NULL) {
		*gridlist_p =
		    pj_gridlist_from_nadgrids(pj_get_ctx(defn), pj_param(defn->ctx, defn->params, listname).s, gridlist_count_p);

		if (*gridlist_p == NULL || *gridlist_count_p == 0)
			return defn->ctx->last_errno;
	}

	if (*gridlist_count_p == 0) {
		pj_ctx_set_errno(defn->ctx, -38);
		return -38;
	}

	tables = *gridlist_p;
	defn->ctx->last_errno = 0;

	for (i = 0; i < point_count; i++) {
		long io = i * point_offset;
		LP input;
		int itable;
		double value = HUGE_VAL;

		input.phi = y[io];
		input.lam = x[io];

		/* keep trying till we find a table that works */
		for (itable = 0; itable < *gridlist_count_p; itable++) {
			PJ_GRIDINFO *gi = tables[itable];
			struct CTABLE *ct = gi->ct;
			double grid_x, grid_y;
			int grid_ix, grid_iy;
			float *cvs;

			/* 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 - 1) * ct->del.phi < input.phi ||
			    ct->ll.lam + (ct->lim.lam - 1) * ct->del.lam < input.lam)
				continue;

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

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

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

					break;
				}

				/* we found a more refined child node to use */
				if (child != NULL) {
					gi = child;
					ct = child->ct;
				}
			}

			/* load the grid shift info if we don't have it. */
			if (ct->cvs == NULL && !pj_gridinfo_load(pj_get_ctx(defn), gi)) {
				pj_ctx_set_errno(defn->ctx, -38);
				return -38;
			}

			/* Interpolation a location within the grid */
			grid_x = (input.lam - ct->ll.lam) / ct->del.lam;
			grid_y = (input.phi - ct->ll.phi) / ct->del.phi;
			grid_ix = (int)floor(grid_x);
			grid_iy = (int)floor(grid_y);
			grid_x -= grid_ix;
			grid_y -= grid_iy;

			cvs = (float *)ct->cvs;
			value = cvs[grid_ix + grid_iy * ct->lim.lam] * (1.0 - grid_x) * (1.0 - grid_y) +
			        cvs[grid_ix + 1 + grid_iy * ct->lim.lam] * (grid_x) * (1.0 - grid_y) +
			        cvs[grid_ix + (grid_iy + 1) * ct->lim.lam] * (1.0 - grid_x) * (grid_y) +
			        cvs[grid_ix + 1 + (grid_iy + 1) * ct->lim.lam] * (grid_x) * (grid_y);

			if (value > 1000 || value < -1000) /* nodata? */
				value = HUGE_VAL;
			else {
				if (inverse)
					z[io] -= value;
				else
					z[io] += value;
			}

			if (value != HUGE_VAL) {
				if (debug_count++ < 20)
					pj_log(defn->ctx, PJ_LOG_DEBUG_MINOR, "pj_apply_gridshift(): used %s", ct->id);
				break;
			}
		}

		if (value == HUGE_VAL) {
			char gridlist[3000];

			pj_log(defn->ctx, PJ_LOG_DEBUG_MAJOR,
			       "pj_apply_vgridshift(): failed to find a grid shift table for\n"
			       "                       location (%.7fdW,%.7fdN)",
			       x[io] * RAD_TO_DEG, y[io] * RAD_TO_DEG);

			gridlist[0] = '\0';
			for (itable = 0; itable < *gridlist_count_p; itable++) {
				PJ_GRIDINFO *gi = tables[itable];
				if (strlen(gridlist) + strlen(gi->gridname) > sizeof(gridlist) - 100) {
					strcat(gridlist, "...");
					break;
				}

				if (itable == 0)
					sprintf(gridlist, "   tried: %s", gi->gridname);
				else
					sprintf(gridlist + strlen(gridlist), ",%s", gi->gridname);
			}
			pj_log(defn->ctx, PJ_LOG_DEBUG_MAJOR, "%s", gridlist);

			pj_ctx_set_errno(defn->ctx, PJD_ERR_GRID_AREA);
			return PJD_ERR_GRID_AREA;
		}
	}

	return 0;
}