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 *);
59 int pj_get_kv(
struct pj_info *info,
const struct Key_Value *in_proj_keys,
60 const struct Key_Value *in_units_keys)
66 char buffa[300], factbuff[50];
67 char proj_in[50], *datum, *params;
77 strcpy(factbuff, str);
78 if (strlen(factbuff) > 0)
79 sscanf(factbuff,
"%lf", &(info->meters));
83 sprintf(proj_in,
"%s", str);
87 sprintf(info->proj,
"%s", str);
89 if (strlen(info->proj) <= 0)
90 sprintf(info->proj,
"ll");
93 for (i = 0; i < in_proj_keys->nitems; i++) {
95 if (strcmp(in_proj_keys->key[i],
"name") == 0) {
100 else if (strcmp(in_proj_keys->key[i],
"zone") == 0) {
107 else if (strcmp(in_proj_keys->key[i],
"datum") == 0
108 || strcmp(in_proj_keys->key[i],
"dx") == 0
109 || strcmp(in_proj_keys->key[i],
"dy") == 0
110 || strcmp(in_proj_keys->key[i],
"dz") == 0
111 || strcmp(in_proj_keys->key[i],
"datumparams") == 0
112 || strcmp(in_proj_keys->key[i],
"nadgrids") == 0
113 || strcmp(in_proj_keys->key[i],
"towgs84") == 0
114 || strcmp(in_proj_keys->key[i],
"ellps") == 0
115 || strcmp(in_proj_keys->key[i],
"a") == 0
116 || strcmp(in_proj_keys->key[i],
"b") == 0
117 || strcmp(in_proj_keys->key[i],
"es") == 0
118 || strcmp(in_proj_keys->key[i],
"f") == 0
119 || strcmp(in_proj_keys->key[i],
"rf") == 0) {
125 else if (strcmp(in_proj_keys->key[i],
"proj") == 0) {
126 if (strcmp(in_proj_keys->value[i],
"ll") == 0)
127 sprintf(buffa,
"proj=longlat");
129 sprintf(buffa,
"proj=%s", in_proj_keys->value[i]);
135 else if (strcmp(in_proj_keys->value[i],
"defined") == 0)
136 sprintf(buffa,
"%s", in_proj_keys->key[i]);
139 sprintf(buffa,
"%s=%s",
140 in_proj_keys->key[i], in_proj_keys->value[i]);
142 alloc_options(buffa);
147 if (sscanf(str,
"%d", &(info->zone)) != 1) {
150 if (info->zone < 0) {
153 info->zone = -info->zone;
156 sprintf(buffa,
"south");
157 alloc_options(buffa);
160 sprintf(buffa,
"zone=%d", info->zone);
161 alloc_options(buffa);
169 sprintf(buffa,
"ellps=%s", str);
170 alloc_options(buffa);
173 sprintf(buffa,
"a=%.16g", a);
174 alloc_options(buffa);
178 sprintf(buffa,
"b=%.16g", a);
180 sprintf(buffa,
"rf=%.16g", rf);
181 alloc_options(buffa);
187 sprintf(buffa,
"no_defs");
188 alloc_options(buffa);
193 sprintf(buffa,
"%s", params);
194 alloc_options(buffa);
200 else if (datum !=
NULL) {
203 sprintf(buffa,
"%s", params);
204 alloc_options(buffa);
212 sprintf(buffa,
"datum=%s", datum);
213 alloc_options(buffa);
226 if (!(pj = pj_init(nopt1, opt_in))) {
228 _(
"Unable to initialise PROJ.4 with the following parameter list:"));
229 for (i = 0; i < nopt1; i++) {
232 sprintf(err,
" +%s", opt_in[i]);
236 G_warning(_(
"The error message: %s"), pj_strerrno(pj_errno));
244 static void alloc_options(
char *buffa)
248 nsize = strlen(buffa);
249 opt_in[nopt1++] = (
char *)G_malloc(nsize + 1);
250 sprintf(opt_in[nopt1 - 1],
"%s", buffa);
260 char zonebuff[50], buffa[300];
264 info->proj[0] =
'\0';
267 if ((str ==
NULL) || (str[0] ==
'\0')) {
271 sprintf(info->proj,
"ll");
272 sprintf(buffa,
"proj=latlong ellps=WGS84");
273 nsize = strlen(buffa);
274 opt_in[nopt] = (
char *)G_malloc(nsize + 1);
275 sprintf(opt_in[nopt++],
"%s", buffa);
283 while (s = strtok(s,
" \t\n"), s) {
284 if (strncmp(s,
"+unfact=", 8) == 0) {
286 info->meters = atof(s);
289 if (strncmp(s,
"+", 1) == 0)
291 if (nsize = strlen(s), nsize) {
293 fprintf(stderr,
"nopt = %d, s=%s\n", nopt, str);
297 if (strncmp(
"zone=", s, 5) == 0) {
298 sprintf(zonebuff,
"%s", s + 5);
299 sscanf(zonebuff,
"%d", &(info->zone));
302 if (strncmp(
"proj=", s, 5) == 0) {
303 sprintf(info->proj,
"%s", s + 5);
304 if (strcmp(info->proj,
"ll") == 0)
305 sprintf(buffa,
"proj=latlong");
307 sprintf(buffa,
"%s", s);
310 sprintf(buffa,
"%s", s);
312 nsize = strlen(buffa);
313 opt_in[nopt] = (
char *)G_malloc(nsize + 1);
314 sprintf(opt_in[nopt++],
"%s", buffa);
324 if (!(pj = pj_init(nopt, opt_in))) {
325 G_warning(_(
"Unable to initialize pj cause: %s"),
326 pj_strerrno(pj_errno));
354 sprintf(pjnew->proj,
"ll");
355 if ((pjnew->pj = pj_latlong_from_proj(pjold->pj)) ==
NULL)
367 static char *buf =
NULL;
368 static size_t buf_len;
369 size_t len = strlen(gisbase) +
sizeof(GRIDDIR) + strlen(name) + 1;
375 buf = G_malloc(buf_len);
378 sprintf(buf,
"%s%s/%s", gisbase, GRIDDIR, name);
399 str = pj_get_def(iproj->pj, 1);
401 fprintf(stderr,
"%s: %s\n", _(
"Input Projection Parameters"),
404 fprintf(stderr,
"%s: %.16g\n", _(
"Input Unit Factor"),
412 str = pj_get_def(oproj->pj, 1);
414 fprintf(stderr,
"%s: %s\n", _(
"Output Projection Parameters"),
417 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.
int GPJ__get_ellipsoid_params(const struct Key_Value *proj_keys, double *a, double *e2, double *rf)
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)
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.