From: Paul Ramsey Date: Mon, 21 Dec 2009 23:22:21 +0000 (+0000) Subject: Add support for geography type to command-line loader (#251) X-Git-Tag: 1.5.0b1~61 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a7523c42e02b2500969bdb171b1d44f66e1cf696;p=postgis Add support for geography type to command-line loader (#251) git-svn-id: http://svn.osgeo.org/postgis/trunk@5040 b70326c6-7e19-0410-871a-916f4a2858ee --- diff --git a/loader/shp2pgsql-cli.c b/loader/shp2pgsql-cli.c index c924b836f..73af13f21 100644 --- a/loader/shp2pgsql-cli.c +++ b/loader/shp2pgsql-cli.c @@ -22,7 +22,7 @@ usage() printf("RCSID: %s RELEASE: %s\n", RCSID, POSTGIS_VERSION); printf("USAGE: shp2pgsql [] [.]\n"); printf("OPTIONS:\n"); - printf(" -s Set the SRID field. If not specified it defaults to -1.\n"); + printf(" -s 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 Specify the name of the geometry column\n"); + printf(" -g 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 Specify the character encoding of Shape's\n"); printf(" attribute column. (default : \"ASCII\")\n"); #endif - printf(" -N Specify NULL geometries handling policy (insert,skip,abort)\n"); + printf(" -N 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); diff --git a/loader/shp2pgsql-core.c b/loader/shp2pgsql-core.c index da5dfa8f0..263f8e671 100644 --- a/loader/shp2pgsql-core.c +++ b/loader/shp2pgsql-core.c @@ -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) { diff --git a/loader/shp2pgsql-core.h b/loader/shp2pgsql-core.h index 783eff7fb..84aac8ee8 100644 --- a/loader/shp2pgsql-core.h +++ b/loader/shp2pgsql-core.h @@ -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; diff --git a/loader/shp2pgsql-gui.c b/loader/shp2pgsql-gui.c index 30ecfc3f3..27c99793c 100644 --- a/loader/shp2pgsql-gui.c +++ b/loader/shp2pgsql-gui.c @@ -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);