22 #include <grass/gis.h> 23 #include <grass/gprojects.h> 24 #include <grass/glocale.h> 27 #define FINDERFUNC set_proj_lib 28 #define PERMANENT "PERMANENT" 31 static void alloc_options(
char *);
61 int pj_get_kv(
struct pj_info *info,
const struct Key_Value *in_proj_keys,
62 const struct Key_Value *in_units_keys)
68 char buffa[300], factbuff[50];
70 char proj_in[250], *datum, *params;
88 strcpy(factbuff, str);
89 if (strlen(factbuff) > 0)
90 sscanf(factbuff,
"%lf", &(info->meters));
94 sprintf(proj_in,
"%s", str);
98 sprintf(info->proj,
"%s", str);
100 if (strlen(info->proj) <= 0)
101 sprintf(info->proj,
"ll");
108 for (i = 0; i < in_proj_keys->nitems; i++) {
110 if (strcmp(in_proj_keys->key[i],
"name") == 0) {
115 else if (strcmp(in_proj_keys->key[i],
"init") == 0) {
120 else if (strcmp(in_proj_keys->key[i],
"zone") == 0) {
127 else if (strcmp(in_proj_keys->key[i],
"datum") == 0
128 || strcmp(in_proj_keys->key[i],
"dx") == 0
129 || strcmp(in_proj_keys->key[i],
"dy") == 0
130 || strcmp(in_proj_keys->key[i],
"dz") == 0
131 || strcmp(in_proj_keys->key[i],
"datumparams") == 0
132 || strcmp(in_proj_keys->key[i],
"nadgrids") == 0
133 || strcmp(in_proj_keys->key[i],
"towgs84") == 0
134 || strcmp(in_proj_keys->key[i],
"ellps") == 0
135 || strcmp(in_proj_keys->key[i],
"a") == 0
136 || strcmp(in_proj_keys->key[i],
"b") == 0
137 || strcmp(in_proj_keys->key[i],
"es") == 0
138 || strcmp(in_proj_keys->key[i],
"f") == 0
139 || strcmp(in_proj_keys->key[i],
"rf") == 0) {
145 else if (strcmp(in_proj_keys->key[i],
"proj") == 0) {
146 if (strcmp(in_proj_keys->value[i],
"ll") == 0)
147 sprintf(buffa,
"proj=longlat");
149 sprintf(buffa,
"proj=%s", in_proj_keys->value[i]);
155 else if (strcmp(in_proj_keys->value[i],
"defined") == 0)
156 sprintf(buffa,
"%s", in_proj_keys->key[i]);
159 sprintf(buffa,
"%s=%s",
160 in_proj_keys->key[i], in_proj_keys->value[i]);
162 alloc_options(buffa);
167 if (sscanf(str,
"%d", &(info->zone)) != 1) {
170 if (info->zone < 0) {
173 info->zone = -info->zone;
176 sprintf(buffa,
"south");
177 alloc_options(buffa);
180 sprintf(buffa,
"zone=%d", info->zone);
181 alloc_options(buffa);
189 sprintf(buffa,
"ellps=%s", str);
190 alloc_options(buffa);
193 sprintf(buffa,
"a=%.16g", a);
194 alloc_options(buffa);
198 sprintf(buffa,
"b=%.16g", a);
200 sprintf(buffa,
"rf=%.16g", rf);
201 alloc_options(buffa);
207 sprintf(buffa,
"no_defs");
208 alloc_options(buffa);
213 sprintf(buffa,
"%s", params);
214 alloc_options(buffa);
220 else if (datum !=
NULL) {
223 sprintf(buffa,
"%s", params);
224 alloc_options(buffa);
232 sprintf(buffa,
"datum=%s", datum);
233 alloc_options(buffa);
244 pjc = proj_context_create();
245 if (!(pj = proj_create_argv(pjc, nopt, opt_in))) {
250 if (!(pj = pj_init(nopt, opt_in))) {
253 _(
"Unable to initialise PROJ with the following parameter list:"));
254 for (i = 0; i < nopt; i++) {
257 sprintf(err,
" +%s", opt_in[i]);
262 G_warning(_(
"The PROJ error message: %s"), pj_strerrno(pj_errno));
268 int perr = proj_errno(pj);
277 for (i = 0; i < nopt; i++)
278 deflen += strlen(opt_in[i]) + 2;
280 info->def = G_malloc(deflen + 1);
282 sprintf(buffa,
"+%s ", opt_in[0]);
283 strcpy(info->def, buffa);
286 for (i = 1; i < nopt; i++) {
287 sprintf(buffa,
"+%s ", opt_in[i]);
288 strcat(info->def, buffa);
295 static void alloc_options(
char *buffa)
299 nsize = strlen(buffa);
300 opt_in[nopt++] = (
char *)G_malloc(nsize + 1);
301 sprintf(opt_in[nopt - 1],
"%s", buffa);
328 char zonebuff[50], buffa[300];
338 info->proj[0] =
'\0';
346 if ((str ==
NULL) || (str[0] ==
'\0')) {
350 sprintf(info->proj,
"ll");
351 sprintf(buffa,
"proj=latlong ellps=WGS84");
352 alloc_options(buffa);
360 while (s = strtok(s,
" \t\n"), s) {
361 if (strncmp(s,
"+unfact=", 8) == 0) {
363 info->meters = atof(s);
366 if (strncmp(s,
"+", 1) == 0)
368 if (nsize = strlen(s), nsize) {
370 fprintf(stderr,
"nopt = %d, s=%s\n", nopt, str);
374 if (strncmp(
"zone=", s, 5) == 0) {
375 sprintf(zonebuff,
"%s", s + 5);
376 sscanf(zonebuff,
"%d", &(info->zone));
379 if (strncmp(s,
"init=", 5) == 0) {
383 if (strncmp(
"proj=", s, 5) == 0) {
384 sprintf(info->proj,
"%s", s + 5);
385 if (strcmp(info->proj,
"ll") == 0)
386 sprintf(buffa,
"proj=latlong");
388 sprintf(buffa,
"%s", s);
391 sprintf(buffa,
"%s", s);
393 alloc_options(buffa);
401 pjc = proj_context_create();
402 if (!(pj = proj_create_argv(pjc, nopt, opt_in))) {
403 G_warning(_(
"Unable to initialize pj cause: %s"),
404 proj_errno_string(proj_context_errno(pjc)));
411 if (!(pj = pj_init(nopt, opt_in))) {
412 G_warning(_(
"Unable to initialize pj cause: %s"),
413 pj_strerrno(pj_errno));
420 for (i = 0; i < nopt; i++)
421 deflen += strlen(opt_in[i]) + 2;
423 info->def = G_malloc(deflen + 1);
425 sprintf(buffa,
"+%s ", opt_in[0]);
426 strcpy(info->def, buffa);
429 for (i = 1; i < nopt; i++) {
430 sprintf(buffa,
"+%s ", opt_in[i]);
431 strcat(info->def, buffa);
465 sprintf(pjnew->proj,
"ll");
466 if ((pjnew->pj = pj_latlong_from_proj(pjold->pj)) ==
NULL)
469 deftmp = pj_get_def(pjnew->pj, 1);
483 static char *buf =
NULL;
484 static size_t buf_len;
485 size_t len = strlen(gisbase) +
sizeof(GRIDDIR) + strlen(name) + 1;
491 buf = G_malloc(buf_len);
494 sprintf(buf,
"%s%s/%s", gisbase, GRIDDIR, name);
517 fprintf(stderr,
"%s: %s\n", _(
"Input Projection Parameters"),
519 fprintf(stderr,
"%s: %.16g\n", _(
"Input Unit Factor"),
529 fprintf(stderr,
"%s: %s\n", _(
"Output Projection Parameters"),
531 fprintf(stderr,
"%s: %.16g\n", _(
"Output Unit Factor"),
const char * G_find_key_value(const char *key, const struct Key_Value *kv)
Find given key (case sensitive)
int GPJ__get_datum_params(const struct Key_Value *projinfo, char **datumname, char **params)
Extract the datum transformation-related parameters from a set of general PROJ_INFO parameters...
int GPJ_get_equivalent_latlong(struct pj_info *pjnew, struct pj_info *pjold)
Define a latitude / longitude co-ordinate system with the same ellipsoid and datum parameters as an e...
int pj_print_proj_params(const struct pj_info *iproj, const struct pj_info *oproj)
Print projection parameters as used by PROJ.4 for input and output co-ordinate systems.
char * G_store(const char *s)
Copy string to allocated memory.
int GPJ__get_ellipsoid_params(const struct Key_Value *proj_keys, double *a, double *e2, double *rf)
Get the ellipsoid parameters from proj keys structure.
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)
const char * set_proj_lib(const char *name)
int pj_get_kv(struct pj_info *info, const struct Key_Value *in_proj_keys, const struct Key_Value *in_units_keys)
Create a pj_info struct Co-ordinate System definition from a set of PROJ_INFO / PROJ_UNITS-style key-...
int GPJ_get_default_datum_params_by_name(const char *name, char **params)
"Last resort" function to retrieve a "default" set of datum parameters for a datum (N...
void G_free(void *buf)
Free allocated memory.
int pj_get_string(struct pj_info *info, char *str)
Create a pj_info struct Co-ordinate System definition from a string with a sequence of key=value pair...
const char * G_gisbase(void)
Get full path name of the top level module directory.
void G_warning(const char *msg,...)
Print a warning message to stderr.