From: Mark Cave-Ayland Date: Mon, 26 Mar 2012 16:23:58 +0000 (+0000) Subject: Rework code from r9204 (bug #900: shp2pgsql: a switch to drop M from 4d imports)... X-Git-Tag: 2.0.0rc1~23 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=cfb86ebcc9976c6e864ee4d7de728e27ea51ee37;p=postgis Rework code from r9204 (bug #900: shp2pgsql: a switch to drop M from 4d imports) to fix bug #1710: shp2pgsql -t option is not working properly. The original implementation set the has_z/has_m flags directly based upon the -t parameter; however this lost the information as to how many dimensions were specified. Resolve this by specifying a new force_output configuration variable and setting it to an enumerated constant, so that we can then implement a simple switch() to set both the output dimension and output flags correctly. git-svn-id: http://svn.osgeo.org/postgis/trunk@9548 b70326c6-7e19-0410-871a-916f4a2858ee --- diff --git a/loader/shp2pgsql-cli.c b/loader/shp2pgsql-cli.c index 618d14f4e..d6021cb14 100644 --- a/loader/shp2pgsql-cli.c +++ b/loader/shp2pgsql-cli.c @@ -197,23 +197,26 @@ main (int argc, char **argv) break; case 't': - if ( strcasecmp(pgis_optarg,"2D") == 0 ) + if (strcasecmp(pgis_optarg, "2D") == 0) { - config->want_z = config->want_m = 0; + config->force_output = FORCE_OUTPUT_2D; } - else if ( strcasecmp(pgis_optarg,"3DZ") == 0 ) + else if (strcasecmp(pgis_optarg, "3DZ") == 0 ) { - config->want_z = 1; - config->want_m = 0; + config->force_output = FORCE_OUTPUT_3DZ; } - else if ( strcasecmp(pgis_optarg,"3DM") == 0 ) + else if (strcasecmp(pgis_optarg, "3DM") == 0 ) { - config->want_z = 0; - config->want_m = 1; + config->force_output = FORCE_OUTPUT_3DM; } - else if ( strcasecmp(pgis_optarg,"4D") == 0 ) + else if (strcasecmp(pgis_optarg, "4D") == 0 ) { - config->want_z = config->want_m = 1; + config->force_output = FORCE_OUTPUT_4D; + } + else + { + fprintf(stderr, "Unsupported output type: %s\nValid output types are 2D, 3DZ, 3DM and 4D\n", pgis_optarg); + exit(1); } break; diff --git a/loader/shp2pgsql-core.c b/loader/shp2pgsql-core.c index 9f8b1be84..d563b82e1 100644 --- a/loader/shp2pgsql-core.c +++ b/loader/shp2pgsql-core.c @@ -762,8 +762,7 @@ set_loader_config_defaults(SHPLOADERCONFIG *config) config->forceint4 = 0; config->createindex = 0; config->readshape = 1; - config->want_m = -1; - config->want_z = -1; + config->force_output = FORCE_OUTPUT_DISABLE; config->encoding = strdup(ENCODING_DEFAULT); config->null_policy = POLICY_NULL_INSERT; config->sr_id = SRID_UNKNOWN; @@ -1006,13 +1005,36 @@ ShpLoaderOpenShape(SHPLOADERSTATE *state) break; } - /* Force M-handling if configured to do so */ - if (state->config->want_m != -1) - state->has_m = state->config->want_m; + /* Force Z/M-handling if configured to do so */ + switch(state->config->force_output) + { + case FORCE_OUTPUT_2D: + state->has_z = 0; + state->has_m = 0; + state->pgdims = 2; + break; + + case FORCE_OUTPUT_3DZ: + state->has_z = 1; + state->has_m = 0; + state->pgdims = 3; + break; + + case FORCE_OUTPUT_3DM: + state->has_z = 0; + state->has_m = 1; + state->pgdims = 3; + break; - /* Force Z-handling if configured to do so */ - if (state->config->want_z != -1) - state->has_z = state->config->want_z; + case FORCE_OUTPUT_4D: + state->has_z = 1; + state->has_m = 1; + state->pgdims = 4; + break; + default: + /* Simply use the auto-detected values above */ + break; + } /* If in simple geometry mode, alter names for CREATE TABLE by skipping MULTI */ if (state->config->simple_geometries) diff --git a/loader/shp2pgsql-core.h b/loader/shp2pgsql-core.h index 80621cb5a..34ae5b451 100644 --- a/loader/shp2pgsql-core.h +++ b/loader/shp2pgsql-core.h @@ -40,6 +40,12 @@ #define POLICY_NULL_INSERT 0x1 #define POLICY_NULL_SKIP 0x2 +/* Forced dimensionality constants */ +#define FORCE_OUTPUT_DISABLE 0x0 +#define FORCE_OUTPUT_2D 0x1 +#define FORCE_OUTPUT_3DZ 0x2 +#define FORCE_OUTPUT_3DM 0x3 +#define FORCE_OUTPUT_4D 0x4 /* Error message handling */ #define SHPLOADERMSGLEN 1024 @@ -112,9 +118,8 @@ typedef struct shp_loader_config /* 0 = load DBF file only, 1 = load everything */ int readshape; - /* 0 = load all coordinates, 1 = skip M dimension */ - int want_m; - int want_z; + /* Override the output geometry type (a FORCE_OUTPUT_* constant) */ + int force_output; /* iconv encoding name */ char *encoding; @@ -188,10 +193,10 @@ typedef struct shp_loader_state /* String containing the PostGIS geometry type, e.g. POINT, POLYGON etc. */ char *pgtype; - /* Flag for whether the geometry has Z coordinates or not. */ + /* Flag for whether the output geometry has Z coordinates or not. */ int has_z; - /* Flag for whether the geometry has M coordinates or not. */ + /* Flag for whether the output geometry has M coordinates or not. */ int has_m; /* Number of dimensions to output */