]> granicus.if.org Git - postgis/commitdiff
Add support for geography type to command-line loader (#251)
authorPaul Ramsey <pramsey@cleverelephant.ca>
Mon, 21 Dec 2009 23:22:21 +0000 (23:22 +0000)
committerPaul Ramsey <pramsey@cleverelephant.ca>
Mon, 21 Dec 2009 23:22:21 +0000 (23:22 +0000)
git-svn-id: http://svn.osgeo.org/postgis/trunk@5040 b70326c6-7e19-0410-871a-916f4a2858ee

loader/shp2pgsql-cli.c
loader/shp2pgsql-core.c
loader/shp2pgsql-core.h
loader/shp2pgsql-gui.c

index c924b836ffcb99b70fcb18ed87fb7e1b78b0c3a3..73af13f21b1ba4734964f4ec08e6e2efe8ed53f7 100644 (file)
@@ -22,7 +22,7 @@ usage()
        printf("RCSID: %s RELEASE: %s\n", RCSID, POSTGIS_VERSION);
        printf("USAGE: shp2pgsql [<options>] <shapefile> [<schema>.]<table>\n");
        printf("OPTIONS:\n");
-       printf("  -s <srid>  Set the SRID field. If not specified it defaults to -1.\n");
+       printf("  -s <srid>  Set the SRID field. Defaults to -1.\n");
        printf("  (-d|a|c|p) These are mutually exclusive options:\n");
        printf("      -d  Drops the table, then recreates it and populates\n");
        printf("          it with current shape file data.\n");
@@ -31,20 +31,21 @@ usage()
        printf("      -c  Creates a new table and populates it, this is the\n");
        printf("          default if you do not specify any options.\n");
        printf("      -p  Prepare mode, only creates the table.\n");
-       printf("  -g <geometry_column> Specify the name of the geometry column\n");
+       printf("  -g <geocolumn> Specify the name of the geometry/geography column\n");
        printf("     (mostly useful in append mode).\n");
-       printf("  -D  Use postgresql dump format (defaults to sql insert statments.\n");
+       printf("  -D  Use postgresql dump format (defaults to SQL insert statments.\n");
+       printf("  -G  Use geography type (requires lon/lat data).\n");
        printf("  -k  Keep postgresql identifiers case.\n");
        printf("  -i  Use int4 type for all integer dbf fields.\n");
-       printf("  -I  Create a GiST index on the geometry column.\n");
+       printf("  -I  Create a spatial index on the geocolumn.\n");
        printf("  -S  Generate simple geometries instead of MULTI geometries.\n");
 #ifdef HAVE_ICONV
        printf("  -W <encoding> Specify the character encoding of Shape's\n");
        printf("     attribute column. (default : \"ASCII\")\n");
 #endif
-       printf("  -N <policy> Specify NULL geometries handling policy (insert,skip,abort)\n");
+       printf("  -N <policy> NULL geometries handling policy (insert*,skip,abort)\n");
        printf("  -n  Only import DBF file.\n");
-       printf("  -? Display this help screen\n");
+       printf("  -?  Display this help screen.\n");
 }
 
 
@@ -68,7 +69,7 @@ main (int argc, char **argv)
        config = malloc(sizeof(SHPLOADERCONFIG));
        set_config_defaults(config);
 
-       while ((c = pgis_getopt(argc, argv, "kcdapDs:Sg:iW:wIN:n")) != EOF)
+       while ((c = pgis_getopt(argc, argv, "kcdapGDs:Sg:iW:wIN:n")) != EOF)
        {
                switch (c)
                {
@@ -92,6 +93,10 @@ main (int argc, char **argv)
                                config->dump_format = 1;
                                break;
 
+                       case 'G':
+                               config->geography = 1;
+                               break;
+
                        case 'S':
                                config->simple_geometries = 1;
                                break;
@@ -207,6 +212,14 @@ main (int argc, char **argv)
                        strtolower(config->schema);
        }
 
+       /* Make the geocolumn name consistent with the load type (geometry or geography) */
+       if( config->geography )
+       {
+               if(config->geom) free(config->geom);
+               config->geom = strdup(GEOGRAPHY_DEFAULT);
+       }
+
+
        /* Create the shapefile state object */
        state = ShpLoaderCreate(config);
 
index da5dfa8f09a74901eb5a1f5dec3df3bfdecadaf4..263f8e671feac86c62ac52b33ca0d5a8227b58ff 100644 (file)
@@ -849,7 +849,7 @@ set_config_defaults(SHPLOADERCONFIG *config)
        config->opt = 'c';
        config->schema = NULL;
        config->table = NULL;
-       config->geom = strdup("the_geom");
+       config->geom = strdup(GEOMETRY_DEFAULT);
        config->readshape = 1;
        config->sr_id = -1;
        config->hwgeom = 0;
@@ -1222,7 +1222,6 @@ ShpLoaderOpenShape(SHPLOADERSTATE *state)
        return ret;
 }
 
-
 /* Return a pointer to an allocated string containing the header for the specified loader state */
 int
 ShpLoaderGetSQLHeader(SHPLOADERSTATE *state, char **strheader)
@@ -1263,7 +1262,7 @@ ShpLoaderGetSQLHeader(SHPLOADERSTATE *state, char **strheader)
                 */
                if (state->config->schema)
                {
-                       if (state->config->readshape == 1)
+                       if (state->config->readshape == 1 && (! state->config->geography) )
                        {
                                vasbappend(sb, "SELECT DropGeometryColumn('%s','%s','%s');\n",
                                        state->config->schema, state->config->table, state->config->geom);
@@ -1274,7 +1273,7 @@ ShpLoaderGetSQLHeader(SHPLOADERSTATE *state, char **strheader)
                }
                else
                {
-                       if (state->config->readshape == 1)
+                       if (state->config->readshape == 1  && (! state->config->geography) )
                        {
                                vasbappend(sb, "SELECT DropGeometryColumn('','%s','%s');\n",
                                        state->config->table, state->config->geom);
@@ -1362,10 +1361,29 @@ ShpLoaderGetSQLHeader(SHPLOADERSTATE *state, char **strheader)
                                        return SHPLOADERERR;
                        }
                }
+               
+               /* Add the geography column directly to the table definition, we don't 
+                  need to do an AddGeometryColumn() call. */
+               if(state->config->readshape == 1 && state->config->geography)
+               {
+                       char *dimschar;
+                       if( state->pgdims == 4 )
+                               dimschar = "ZM";
+                       else
+                               dimschar = "";
+                       if(state->config->sr_id != -1 && state->config->sr_id != 4326)
+                       {
+                               snprintf(state->message, SHPLOADERMSGLEN, "Invalid SRID for geography type: %x", state->config->sr_id);
+                               stringbuffer_destroy(sb);
+                               return SHPLOADERERR;
+                       }
+                       vasbappend(sb, ",\n\"%s\" geography(%s%s,%d)", state->config->geom, state->pgtype, dimschar, 4326);
+               }
+               
                vasbappend(sb, ");\n");
        
                /* Create the geometry column with an addgeometry call */
-               if (state->config->readshape == 1)
+               if (state->config->readshape == 1 && (!state->config->geography))
                {
                        if (state->config->schema) 
                        {
index 783eff7fbcaf5aa5d57ea2f1d35f6d39c39c199a..84aac8ee896f891aef1e346b61547a55808746ba 100644 (file)
@@ -68,7 +68,7 @@
  * Default geometry column name
  */
 #define GEOMETRY_DEFAULT "the_geom"
-#define GEOGRAPHY_DEFAULT "the_geog"
+#define GEOGRAPHY_DEFAULT "geog"
 
 /*
  * Structure to hold the loader configuration options 
@@ -96,6 +96,9 @@ typedef struct shp_loader_config
 
        /* 0 = MULTIPOLYGON/MULTILINESTRING, 1 = force to POLYGON/LINESTRING */
        int simple_geometries;
+       
+       /* 0 = geometry, 1 = geography */
+       int geography;
 
        /* 0 = columnname, 1 = "columnName" */
        int quoteidentifiers;
index 30ecfc3f3cd7c5f0c77535beadab258feaaa8bf9..27c99793c263bde58f0ae339fc365b614f6c635c 100644 (file)
@@ -172,7 +172,7 @@ pgui_set_config_from_ui()
                config->schema = strdup(pg_schema);
 
        if (strlen(pg_geom) == 0)
-               config->geom = strdup("the_geom");
+               config->geom = strdup(GEOMETRY_DEFAULT);
        else
                config->geom = strdup(pg_geom);