[Proj] BUG in pj_get_def

Eric Miller EMiller at dfg.ca.gov
Tue Sep 14 13:02:44 EDT 2004


It seems the utility function pj_get_def returns unused parameters.  The
simple program below demonstrates.

#include <stdio.h>
#include <stdlib.h>
#include "proj_api.h"

int main (void) {
	const char *before = "+proj=aea +lat_0=0 +lon_0=-120 +lat_1=34
"
			"+lat_2=40.5 +y_0=-4000000 +datum=NAD83
+nodefs";
	char *after = NULL;
	projPJ prj = pj_init_plus(before);
	if (prj == NULL) {
		fprintf(stderr, "pj_init_plus failed: %s\n",
pj_strerrno(pj_errno));
		exit(EXIT_FAILURE);
	}
	after = pj_get_def(prj, 0);
	if (after) {
		printf("before = '%s'\n", before);
		printf("after = '%s'\n", after);
	}
	else {
		fprintf(stderr, "pj_get_def failed: %s\n",
pj_strerrno(pj_errno));
		exit(EXIT_FAILURE);
	}
	return 0;
}

Outputting:
before = '+proj=aea +lat_0=0 +lon_0=-120 +lat_1=34 +lat_2=40.5
+y_0=-4000000 +datum=NAD83 +nodefs'
after = ' +proj=aea +lat_0=0 +lon_0=-120 +lat_1=34 +lat_2=40.5
+y_0=-4000000 +datum=NAD83 +nodefs +lat_1=29.5 +lat_2=45.5 +ellps=GRS80
+towgs84=0,0,0'

The extra "lat_1" and "lat_2" parameters are spurious.

I added a check for the "used" flag in the modified function below.  It
seems to correct the problem.

char *pj_get_def( PJ *P, int options )

{
    paralist *t;
    int l;
    char *definition;
    int  def_max = 10;

    definition = (char *) pj_malloc(def_max);
    definition[0] = '\0';

    for (t = P->params; t; t = t->next)
    {
        if (t->used) {
            l = strlen(t->param) + 1;
            if( strlen(definition) + l + 5 > def_max )
            {
                char *def2;

                def_max = def_max * 2 + l + 5;
                def2 = (char *) pj_malloc(def_max);
                strcpy( def2, definition );
                pj_dalloc( definition );
                definition = def2;
            }

            strcat( definition, " +" );
            strcat( definition, t->param );
        }
    }

    return definition;
}


Eric G. Miller
GIS Analyst
Wildlife and Habitat Data Analysis Branch
Department of Fish and Game



More information about the Proj mailing list