22 #include <grass/gis.h>
23 #include <grass/gprojects.h>
24 #include <grass/glocale.h>
27 #define FINDERFUNC set_proj_share
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 #if PROJ_VERSION_MAJOR >= 6
248 alloc_options(
"type=crs");
250 pjc = proj_context_create();
251 if (!(pj = proj_create_argv(pjc, nopt, opt_in))) {
256 if (!(pj = pj_init(nopt, opt_in))) {
259 _(
"Unable to initialise PROJ with the following parameter list:"));
260 for (i = 0; i < nopt; i++) {
263 sprintf(
err,
" +%s", opt_in[i]);
268 G_warning(_(
"The PROJ error message: %s"), pj_strerrno(pj_errno));
274 int perr = proj_errno(pj);
283 for (i = 0; i < nopt; i++)
284 deflen += strlen(opt_in[i]) + 2;
286 info->def = G_malloc(deflen + 1);
288 sprintf(buffa,
"+%s ", opt_in[0]);
289 strcpy(info->def, buffa);
292 for (i = 1; i < nopt; i++) {
293 sprintf(buffa,
"+%s ", opt_in[i]);
294 strcat(info->def, buffa);
301 static void alloc_options(
char *buffa)
305 nsize = strlen(buffa);
306 opt_in[nopt++] = (
char *)G_malloc(nsize + 1);
307 sprintf(opt_in[nopt - 1],
"%s", buffa);
334 char zonebuff[50], buffa[300];
344 info->proj[0] =
'\0';
352 if ((str ==
NULL) || (str[0] ==
'\0')) {
356 sprintf(info->proj,
"ll");
357 sprintf(buffa,
"proj=latlong ellps=WGS84");
358 alloc_options(buffa);
366 while (s = strtok(s,
" \t\n"), s) {
367 if (strncmp(s,
"+unfact=", 8) == 0) {
369 info->meters = atof(s);
372 if (strncmp(s,
"+", 1) == 0)
374 if (nsize = strlen(s), nsize) {
376 fprintf(stderr,
"nopt = %d, s=%s\n", nopt, str);
380 if (strncmp(
"zone=", s, 5) == 0) {
381 sprintf(zonebuff,
"%s", s + 5);
382 sscanf(zonebuff,
"%d", &(info->zone));
385 if (strncmp(s,
"init=", 5) == 0) {
389 if (strncmp(
"proj=", s, 5) == 0) {
390 sprintf(info->proj,
"%s", s + 5);
391 if (strcmp(info->proj,
"ll") == 0)
392 sprintf(buffa,
"proj=latlong");
394 sprintf(buffa,
"%s", s);
397 sprintf(buffa,
"%s", s);
399 alloc_options(buffa);
407 #if PROJ_VERSION_MAJOR >= 6
411 alloc_options(
"type=crs");
413 pjc = proj_context_create();
414 if (!(pj = proj_create_argv(pjc, nopt, opt_in))) {
415 G_warning(_(
"Unable to initialize pj cause: %s"),
416 proj_errno_string(proj_context_errno(pjc)));
423 if (!(pj = pj_init(nopt, opt_in))) {
424 G_warning(_(
"Unable to initialize pj cause: %s"),
425 pj_strerrno(pj_errno));
432 for (i = 0; i < nopt; i++)
433 deflen += strlen(opt_in[i]) + 2;
435 info->def = G_malloc(deflen + 1);
437 sprintf(buffa,
"+%s ", opt_in[0]);
438 strcpy(info->def, buffa);
441 for (i = 1; i < nopt; i++) {
442 sprintf(buffa,
"+%s ", opt_in[i]);
443 strcat(info->def, buffa);
477 sprintf(pjnew->proj,
"ll");
478 if ((pjnew->pj = pj_latlong_from_proj(pjold->pj)) ==
NULL)
481 deftmp = pj_get_def(pjnew->pj, 1);
497 static char *buf =
NULL;
498 const char *projshare;
499 static size_t buf_len = 0;
502 projshare = getenv(
"GRASS_PROJSHARE");
506 len = strlen(projshare) + strlen(
name) + 2;
512 buf = G_malloc(buf_len);
515 sprintf(buf,
"%s/%s", projshare,
name);
538 fprintf(stderr,
"%s: %s\n", _(
"Input Projection Parameters"),
540 fprintf(stderr,
"%s: %.16g\n", _(
"Input Unit Factor"),
550 fprintf(stderr,
"%s: %s\n", _(
"Output Projection Parameters"),
552 fprintf(stderr,
"%s: %.16g\n", _(
"Output Unit Factor"),
void G_free(void *buf)
Free 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.
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 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.
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...
const char * set_proj_share(const char *name)
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...
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
void G_warning(const char *msg,...)
Print a warning message to stderr.
const char * G_find_key_value(const char *key, const struct Key_Value *kv)
Find given key (case sensitive)
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....
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.
char * G_store(const char *s)
Copy string to allocated memory.
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)