[GRASS-SVN] r74191 - in grass/trunk: include lib/proj

svn_grass at osgeo.org svn_grass at osgeo.org
Fri Mar 8 11:29:27 PST 2019


Author: mmetz
Date: 2019-03-08 11:29:27 -0800 (Fri, 08 Mar 2019)
New Revision: 74191

Modified:
   grass/trunk/include/gprojects.h
   grass/trunk/lib/proj/convert.c
   grass/trunk/lib/proj/do_proj.c
   grass/trunk/lib/proj/get_proj.c
Log:
libproj: use proj_create_crs_to_crs() if possible

Modified: grass/trunk/include/gprojects.h
===================================================================
--- grass/trunk/include/gprojects.h	2019-03-08 19:22:55 UTC (rev 74190)
+++ grass/trunk/include/gprojects.h	2019-03-08 19:29:27 UTC (rev 74191)
@@ -51,6 +51,7 @@
     int zone;
     char proj[100];
     char *def;
+    char *srid;
 };
 
 struct gpj_datum

Modified: grass/trunk/lib/proj/convert.c
===================================================================
--- grass/trunk/lib/proj/convert.c	2019-03-08 19:22:55 UTC (rev 74190)
+++ grass/trunk/lib/proj/convert.c	2019-03-08 19:29:27 UTC (rev 74191)
@@ -38,7 +38,7 @@
     char    *id;        /* units keyword */
     char    *to_meter;  /* multiply by value to get meters */
     char    *name;      /* comments */
-    double   factor;       /* to_meter factor in actual numbers */
+    double   factor;    /* to_meter factor in actual numbers */
 };
 
 struct gpj_units

Modified: grass/trunk/lib/proj/do_proj.c
===================================================================
--- grass/trunk/lib/proj/do_proj.c	2019-03-08 19:22:55 UTC (rev 74190)
+++ grass/trunk/lib/proj/do_proj.c	2019-03-08 19:29:27 UTC (rev 74191)
@@ -89,17 +89,39 @@
 	G_fatal_error(_("Input coordinate system is NULL"));
 
 #ifdef HAVE_PROJ_H
+    info_trans->pj = NULL;
     if (!info_trans->def) {
-	if (info_out->pj != NULL && info_out->def != NULL)
-	    G_asprintf(&(info_trans->def), "+proj=pipeline +step +inv %s +step %s",
-		       info_in->def, info_out->def);
-	else
-	    /* assume info_out to be ll equivalent of info_in */
-	    G_asprintf(&(info_trans->def), "+proj=pipeline +step +inv %s",
-		       info_in->def);
+	if (info_in->srid && info_out->pj && info_out->srid) {
+	    /* ask PROJ for the best pipeline */
+	    info_trans->pj = proj_create_crs_to_crs(PJ_DEFAULT_CTX,
+	                                            info_in->srid,
+						    info_out->srid,
+						    NULL);
+
+	    if (info_trans->pj == NULL)
+		G_warning(_("proj_create_crs_to_crs() failed for '%s' and '%s'"),
+		          info_in->srid, info_out->srid);
+	    else {
+		char *str = proj_as_proj_string(NULL, info_trans->pj,
+		                                PJ_PROJ_5, NULL);
+
+		if (str)
+		    info-trans->def = G_store(str);
+	    }
+	}
+	if (info_trans->pj == NULL) {
+	    if (info_out->pj != NULL && info_out->def != NULL)
+		G_asprintf(&(info_trans->def), "+proj=pipeline +step +inv %s +step %s",
+			   info_in->def, info_out->def);
+	    else
+		/* assume info_out to be ll equivalent of info_in */
+		G_asprintf(&(info_trans->def), "+proj=pipeline +step +inv %s",
+			   info_in->def);
+	}
     }
 
-    info_trans->pj = proj_create(PJ_DEFAULT_CTX, info_trans->def);
+    if (info_trans->pj == NULL)
+	info_trans->pj = proj_create(PJ_DEFAULT_CTX, info_trans->def);
     if (info_trans->pj == NULL) {
 	G_warning(_("proj_create() failed for '%s'"), info_trans->def);
 	return -1;

Modified: grass/trunk/lib/proj/get_proj.c
===================================================================
--- grass/trunk/lib/proj/get_proj.c	2019-03-08 19:22:55 UTC (rev 74190)
+++ grass/trunk/lib/proj/get_proj.c	2019-03-08 19:29:27 UTC (rev 74191)
@@ -81,6 +81,7 @@
     info->proj[0] = '\0';
     info->def = NULL;
     info->pj = NULL;
+    info->srid = NULL;
 
     str = G_find_key_value("meters", in_units_keys);
     if (str != NULL) {
@@ -98,6 +99,10 @@
     }
     if (strlen(info->proj) <= 0)
 	sprintf(info->proj, "ll");
+    str = G_find_key_value("init", in_proj_keys);
+    if (str != NULL) {
+	info->srid = G_store(str);
+    }
 
     nopt = 0;
     for (i = 0; i < in_proj_keys->nitems; i++) {
@@ -105,6 +110,11 @@
 	if (strcmp(in_proj_keys->key[i], "name") == 0) {
 	    continue;
 
+	    /* init is here ignored */
+	}
+	else if (strcmp(in_proj_keys->key[i], "init") == 0) {
+	    continue;
+
 	    /* zone handled separately at end of loop */
 	}
 	else if (strcmp(in_proj_keys->key[i], "zone") == 0) {
@@ -328,6 +338,7 @@
     info->proj[0] = '\0';
     info->meters = 1.0;
     info->def = NULL;
+    info->srid = NULL;
     info->pj = NULL;
     
     nopt = 0;
@@ -365,6 +376,10 @@
 			sscanf(zonebuff, "%d", &(info->zone));
 		    }
 
+		    if (strncmp(s, "init=", 5) == 0) {
+			info->srid = G_store(s + 6);
+		    }
+
 		    if (strncmp("proj=", s, 5) == 0) {
 			sprintf(info->proj, "%s", s + 5);
 			if (strcmp(info->proj, "ll") == 0)
@@ -385,7 +400,8 @@
 #ifdef HAVE_PROJ_H
     pjc = proj_context_create();
     if (!(pj = proj_create_argv(pjc, nopt, opt_in))) {
-	G_warning(_("Unable to initialize pj"));
+	G_warning(_("Unable to initialize pj cause: %s"),
+	          proj_errno_string(proj_context_errno(pjc)));
 	return -1;
     }
 #else



More information about the grass-commit mailing list