[GRASS-SVN] r74220 - grass/trunk/general/g.proj

svn_grass at osgeo.org svn_grass at osgeo.org
Sun Mar 10 11:08:56 PDT 2019


Author: mmetz
Date: 2019-03-10 11:08:56 -0700 (Sun, 10 Mar 2019)
New Revision: 74220

Added:
   grass/trunk/general/g.proj/list_codes.c
Modified:
   grass/trunk/general/g.proj/local_proto.h
   grass/trunk/general/g.proj/main.c
Log:
g.proj: code cleanup for new option list_codes

Added: grass/trunk/general/g.proj/list_codes.c
===================================================================
--- grass/trunk/general/g.proj/list_codes.c	                        (rev 0)
+++ grass/trunk/general/g.proj/list_codes.c	2019-03-10 18:08:56 UTC (rev 74220)
@@ -0,0 +1,194 @@
+#include <string.h>
+#include <unistd.h>
+#include <grass/gis.h>
+#include <grass/glocale.h>
+#include <grass/config.h>
+
+#ifdef HAVE_PROJ_H
+#include <proj.h>
+#if PROJ_VERSION_MAJOR >= 6
+#define USE_PROJDB
+#endif
+#else
+#include <proj_api.h>
+#endif
+
+char *get_authority_names(void)
+{
+    char *authnames;
+
+#ifdef USE_PROJDB
+    /* PROJ 6 */
+    int i, len;
+    PROJ_STRING_LIST authlist = proj_get_authorities_from_database(NULL);
+
+    len = 0;
+    for (i = 0; authlist[i]; i++) {
+	len += strlen(authlist[i]) + 1;
+    }
+    if (len > 0) {
+	authnames = G_malloc((len + 1) * sizeof(char)); /* \0 */
+	*authnames = '\0';
+	for (i = 0; authlist[i]; i++) {
+	    if (i > 0)
+		strcat(authnames, ",");
+	    strcat(authnames, authlist[i]);
+	}
+    }
+    else {
+	authnames = G_store("");
+    }
+#else
+    /* PROJ 4, 5 */
+    /* there are various init files in share/proj/:
+     * EPSG,GL27,IGNF,ITRF2000,ITRF2008,ITRF2014,nad27,nad83,esri
+     * but they have different formats: bothering only with EPSG here */ 
+    authnames = G_store("EPSG");
+#endif
+
+    return authnames;
+}
+
+void list_codes(char *authname)
+{
+#ifdef USE_PROJDB
+    /* PROJ 6+ */
+    int i, crs_cnt;
+    PROJ_CRS_INFO **proj_crs_info;
+    
+    crs_cnt = 0;
+    proj_crs_info = proj_get_crs_info_list_from_database(NULL,
+			authname, NULL, &crs_cnt);
+    if (crs_cnt < 1)
+	G_fatal_error(_("No codes found for authority %s"),
+		      authname);
+	    
+    for (i = 0; i < crs_cnt; i++) {
+	const char *proj_definition;
+	char emptystr;
+	PJ *pj;
+
+	emptystr = '\0';
+	pj = proj_create_from_database(NULL,
+				       proj_crs_info[i]->auth_name,
+				       proj_crs_info[i]->code,
+				       PJ_CATEGORY_CRS,
+				       0, NULL);
+	proj_definition = proj_as_proj_string(NULL, pj, PJ_PROJ_5, NULL);
+	if (!proj_definition) {
+	    /* what to do with a CRS without proj string ? */
+	    G_debug(1, "No proj string for %s:%s",
+		    proj_crs_info[i]->auth_name,
+		    proj_crs_info[i]->code);
+	    proj_definition = &emptystr;
+	}
+
+	if (proj_definition) {
+	    fprintf(stdout, "%s|%s|%s\n", proj_crs_info[i]->code,
+					  proj_crs_info[i]->name,
+					  proj_definition);
+	}
+
+	proj_destroy(pj);
+    }
+#else
+    char pathname[GPATH_MAX];
+    char code[GNAME_MAX], name[GNAME_MAX], proj_def[GNAME_MAX];
+    FILE *fp;
+    char buf[4096];
+    int line;
+
+#ifdef HAVE_PROJ_H
+    /* PROJ 5 */
+    PJ_INIT_INFO init_info;
+
+    if (G_strcasecmp(authname, "EPSG") == 0)
+	authname = "epsg";
+
+    init_info = proj_init_info(authname);
+    sprintf(pathname, init_info.filename);
+    
+    if (access(pathname, F_OK) != 0)
+	G_fatal_error(_("Unable to find init file %s"), authname);
+
+#else
+    /* PROJ 4 */
+    /* can't use pj_find_file() from the old proj api
+     * because it does not exist in PROJ 4 */
+    char *grass_proj_share;
+    
+    authname = listcodes->answer;
+    if (G_strcasecmp(authname, "EPSG") == 0)
+	authname = "epsg";
+
+    grass_proj_share = getenv("GRASS_PROJSHARE");
+    if (!grass_proj_share)
+	G_fatal_error(_("Environment variable GRASS_PROJSHARE is not set"));
+    sprintf(pathname, "%s/%s", grass_proj_share, authname);
+    G_convert_dirseps_to_host(pathname);
+#endif
+
+    /* PROJ 4 / 5 */
+    
+    /* the init files do not have a common structure, thus restrict to epsg 
+     * see pj_init.c get_init_string() in PROJ 4 / 5 for a 
+     * generic init file parser, however without descriptive name */
+    if (strcmp(authname, "epsg") != 0)
+	G_fatal_error(_("Only epsg file is currently supported"));
+    
+    /* open the init file */
+    fp = fopen(pathname, "r");
+    if (!fp) {
+	G_fatal_error(_("Unable to open init file <%s>"), authname);
+    }
+    name[0] = '\0';
+    /* print list of codes, names, definitions */
+    for (line = 1; G_getl2(buf, sizeof(buf), fp); line++) {
+	int buflen;
+
+	G_strip(buf);
+	buflen = strlen(buf);
+	    
+	if (*buf == '\0' || 
+	    (buflen >= 10 && strncmp(buf, "<metadata>", 10) == 0)) {
+	    name[0] = '\0'; 
+	    continue;
+	}
+
+	if (strncmp(buf, "<metadata>", strlen("<metadata>")) == 0)
+	    continue;
+
+	/* name: line starts with '# ' */
+	/* code and definition in next line */
+
+	if (*buf == '#' && buflen > 2) {
+	    sprintf(name, buf + 2);
+	    continue;
+	}
+
+	if (*buf == '<') {
+	    int i, j;
+	    
+	    i = 0;
+	    while (i < buflen && buf[i] != '>')
+		i++;
+	    buf[i] = '\0';
+	    sprintf(code, buf + 1);
+	    i++;
+	    j = i;
+	    while (i < buflen && buf[i] != '<')
+		i++;
+	    if (i < buflen && buf[i] == '<')
+		buf[i] = '\0';
+	    sprintf(proj_def, buf + j);
+	    G_strip(proj_def);
+
+	    fprintf(stdout, "%s|%s|%s\n", code, name, proj_def);
+	    name[0] = '\0';
+	}
+    }
+    fclose(fp);
+#endif
+}
+
+

Modified: grass/trunk/general/g.proj/local_proto.h
===================================================================
--- grass/trunk/general/g.proj/local_proto.h	2019-03-10 09:22:35 UTC (rev 74219)
+++ grass/trunk/general/g.proj/local_proto.h	2019-03-10 18:08:56 UTC (rev 74220)
@@ -1,12 +1,5 @@
 #include <grass/config.h>
 
-#ifdef HAVE_PROJ_H
-#include <proj.h>
-#if PROJ_VERSION_MAJOR >= 6
-#define USE_PROJDB
-#endif
-#endif
-
 extern struct Key_Value *projinfo, *projunits, *projepsg;
 extern struct Cell_head cellhd;
 
@@ -34,3 +27,7 @@
 /* create.c */
 void create_location(const char *);
 void modify_projinfo();
+
+/* list_codes.c */
+char *get_authority_names(void);
+void list_codes(char *);

Modified: grass/trunk/general/g.proj/main.c
===================================================================
--- grass/trunk/general/g.proj/main.c	2019-03-10 09:22:35 UTC (rev 74219)
+++ grass/trunk/general/g.proj/main.c	2019-03-10 18:08:56 UTC (rev 74220)
@@ -17,56 +17,15 @@
 
 #include <stdlib.h>
 #include <string.h>
-#include <unistd.h>
 #include <grass/gis.h>
 #include <grass/glocale.h>
 #include <grass/config.h>
 
-#ifndef HAVE_PROJ_H
-#include <proj_api.h>
-#endif
 #include "local_proto.h"
 
 struct Key_Value *projinfo, *projunits, *projepsg;
 struct Cell_head cellhd;
 
-static char *get_authority_names()
-{
-    char *authnames;
-
-#ifdef USE_PROJDB
-    /* PROJ 6 */
-    int i, len;
-    PROJ_STRING_LIST authlist = proj_get_authorities_from_database(NULL);
-
-    len = 0;
-    for (i = 0; authlist[i]; i++) {
-	len += strlen(authlist[i]) + 1;
-    }
-    if (len > 0) {
-	authnames = G_malloc((len + 1) * sizeof(char)); /* \0 */
-	*authnames = '\0';
-	for (i = 0; authlist[i]; i++) {
-	    if (i > 0)
-		strcat(authnames, ",");
-	    strcat(authnames, authlist[i]);
-	}
-    }
-    else {
-	authnames = G_store("");
-    }
-#else
-    /* PROJ 4, 5 */
-    /* there are various init files in share/proj/:
-     * EPSG,GL27,IGNF,ITRF2000,ITRF2008,ITRF2014,nad27,nad83,esri
-     * but they have different formats: bothering only with EPSG here */ 
-    authnames = G_store("EPSG");
-#endif
-
-    return authnames;
-}
-
-
 int main(int argc, char *argv[])
 {
     struct Flag *printinfo,	/* Print contents of PROJ_INFO & PROJ_UNITS */
@@ -259,147 +218,7 @@
 
     /* list codes for given authority */
     if (listcodes->answer) {
-#ifdef USE_PROJDB
-	/* PROJ 6+ */
-	int i, crs_cnt;
-	PROJ_CRS_INFO **proj_crs_info;
-	
-	crs_cnt = 0;
-	proj_crs_info = proj_get_crs_info_list_from_database(NULL,
-			    listcodes->answer, NULL, &crs_cnt);
-	if (crs_cnt < 1)
-	    G_fatal_error(_("No codes found for authority %s"),
-		          listcodes->answer);
-		
-	for (i = 0; i < crs_cnt; i++) {
-	    const char *proj_definition;
-	    char emptystr;
-	    PJ *pj;
-
-	    emptystr = '\0';
-	    pj = proj_create_from_database(NULL,
-	                                   proj_crs_info[i]->auth_name,
-	                                   proj_crs_info[i]->code,
-					   PJ_CATEGORY_CRS,
-					   0, NULL);
-	    proj_definition = proj_as_proj_string(NULL, pj, PJ_PROJ_5, NULL);
-	    if (!proj_definition) {
-		/* what to do with a CRS without proj string ? */
-		G_debug(1, "No proj string for %s:%s",
-			proj_crs_info[i]->auth_name,
-			proj_crs_info[i]->code);
-		proj_definition = &emptystr;
-	    }
-
-	    if (proj_definition) {
-		fprintf(stdout, "%s|%s|%s\n", proj_crs_info[i]->code,
-					      proj_crs_info[i]->name,
-					      proj_definition);
-	    }
-
-	    proj_destroy(pj);
-	}
-#else
-	char pathname[GPATH_MAX];
-	char *authname;
-	char code[GNAME_MAX], name[GNAME_MAX], proj_def[GNAME_MAX];
-	FILE *fp;
-	char buf[4096];
-	int line, have_name;
-
-#ifdef HAVE_PROJ_H
-	/* PROJ 5 */
-	PJ_INIT_INFO init_info;
-
-	authname = listcodes->answer;
-	if (G_strcasecmp(authname, "EPSG") == 0)
-	    authname = "epsg";
-
-	init_info = proj_init_info(authname);
-	sprintf(pathname, init_info.filename);
-	
-	if (access(pathname, F_OK) != 0)
-	    G_fatal_error(_("Unable to find init file %s"), authname);
-
-#else
-	/* PROJ 4 */
-	/* can't use pj_find_file() from the old proj api
-	 * because it does not exist in PROJ 4 */
-	char *grass_proj_share;
-	
-	authname = listcodes->answer;
-	if (G_strcasecmp(authname, "EPSG") == 0)
-	    authname = "epsg";
-
-	grass_proj_share = getenv("GRASS_PROJSHARE");
-	if (!grass_proj_share)
-	    G_fatal_error(_("Environment variable GRASS_PROJSHARE is not set"));
-	sprintf(pathname, "%s/%s", grass_proj_share, authname);
-	G_convert_dirseps_to_host(pathname);
-#endif
-
-	/* PROJ 4 / 5 */
-	
-	/* the init files do not have a common structure, thus restrict to epsg */
-	if (strcmp(authname, "epsg") != 0)
-	    G_fatal_error(_("Only epsg file is currently supported"));
-	
-	/* open the init file */
-	fp = fopen(pathname, "r");
-	if (!fp) {
-	    G_fatal_error(_("Unable to open init file <%s>"), authname);
-	}
-	have_name = 0;
-	/* print list of codes, names, definitions */
-	for (line = 1; G_getl2(buf, sizeof(buf), fp); line++) {
-
-	    G_strip(buf);
-	    if (*buf == '\0')
-		continue;
-
-	    if (strncmp(buf, "<metadata>", strlen("<metadata>")) == 0)
-		continue;
-
-	    /* name: line starts with '# ' */
-	    /* code and definition in next line */
-
-	    if (!have_name) {
-		if (*buf != '#')
-		    continue;
-		sprintf(name, buf + 2);
-		have_name = 1;
-	    }
-	    if (have_name && *buf == '#') {
-		sprintf(name, buf + 2);
-		continue;
-	    }
-
-	    if (have_name && *buf == '<') {
-		int i, j, buflen;
-		
-		buflen = strlen(buf);
-		
-		i = 0;
-		while (i < buflen && buf[i] != '>')
-		    i++;
-		buf[i] = '\0';
-		sprintf(code, buf + 1);
-		i++;
-		j = i;
-		while (i < buflen && buf[i] != '<')
-		    i++;
-		if (i < buflen && buf[i] == '<')
-		    buf[i] = '\0';
-		sprintf(proj_def, buf + j);
-		G_strip(proj_def);
-
-		fprintf(stdout, "%s|%s|%s\n", code, name, proj_def);
-		have_name = 0;
-		name[0] = '\0';
-	    }
-	}
-	fclose(fp);
-#endif
+	list_codes(listcodes->answer);
 	exit(EXIT_SUCCESS);
     }
 



More information about the grass-commit mailing list