PostGIS  3.1.6dev-r@@SVN_REVISION@@
shp2pgsql-cli.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://www.postgis.org
5  * Copyright 2008 OpenGeo.org
6  * Copyright 2009 Mark Cave-Ayland <mark.cave-ayland@siriusit.co.uk>
7  *
8  * This is free software; you can redistribute and/or modify it under
9  * the terms of the GNU General Public Licence. See the COPYING file.
10  *
11  * Maintainer: Paul Ramsey <pramsey@opengeo.org>
12  *
13  **********************************************************************/
14 
15 #include "../postgis_config.h"
16 
17 #include "shp2pgsql-core.h"
18 #include "../liblwgeom/liblwgeom.h" /* for SRID_UNKNOWN */
19 
20 #define xstr(s) str(s)
21 #define str(s) #s
22 
23 static void
25 {
26  printf(_( "RELEASE: %s (%s)\n" ),
27  POSTGIS_LIB_VERSION, xstr(POSTGIS_REVISION));
28  printf(_( "USAGE: shp2pgsql [<options>] <shapefile> [[<schema>.]<table>]\n"
29  "OPTIONS:\n" ));
30  printf(_( " -s [<from>:]<srid> Set the SRID field. Defaults to %d.\n"
31  " Optionally reprojects from given SRID.\n"),
32  SRID_UNKNOWN);
33  printf(_( " (-d|a|c|p) These are mutually exclusive options:\n"
34  " -d Drops the table, then recreates it and populates\n"
35  " it with current shape file data.\n"
36  " -a Appends shape file into current table, must be\n"
37  " exactly the same table schema.\n"
38  " -c Creates a new table and populates it, this is the\n"
39  " default if you do not specify any options.\n"
40  " -p Prepare mode, only creates the table.\n" ));
41  printf(_( " -g <geocolumn> Specify the name of the geometry/geography column\n"
42  " (mostly useful in append mode).\n" ));
43  printf(_( " -D Use postgresql dump format (defaults to SQL insert statements).\n" ));
44  printf(_( " -e Execute each statement individually, do not use a transaction.\n"
45  " Not compatible with -D.\n" ));
46  printf(_( " -G Use geography type (requires lon/lat data or -s to reproject).\n" ));
47  printf(_( " -k Keep postgresql identifiers case.\n" ));
48  printf(_( " -i Use int4 type for all integer dbf fields.\n" ));
49  printf(_( " -I Create a spatial index on the geocolumn.\n" ));
50  printf(_(" -m <filename> Specify a file containing a set of mappings of (long) column\n"
51  " names to 10 character DBF column names. The content of the file is one or\n"
52  " more lines of two names separated by white space and no trailing or\n"
53  " leading space. For example:\n"
54  " COLUMNNAME DBFFIELD1\n"
55  " AVERYLONGCOLUMNNAME DBFFIELD2\n" ));
56  printf(_( " -S Generate simple geometries instead of MULTI geometries.\n" ));
57  printf(_( " -t <dimensionality> Force geometry to be one of '2D', '3DZ', '3DM', or '4D'\n" ));
58 
59  printf(_( " -w Output WKT instead of WKB. Note that this can result in\n"
60  " coordinate drift.\n" ));
61  printf(_( " -W <encoding> Specify the character encoding of Shape's\n"
62  " attribute column. (default: \"UTF-8\")\n" ));
63  printf(_( " -N <policy> NULL geometries handling policy (insert*,skip,abort).\n" ));
64  printf(_( " -n Only import DBF file.\n" ));
65  printf(_( " -T <tablespace> Specify the tablespace for the new table.\n"
66  " Note that indexes will still use the default tablespace unless the\n"
67  " -X flag is also used.\n"));
68  printf(_( " -X <tablespace> Specify the tablespace for the table's indexes.\n"
69  " This applies to the primary key, and the spatial index if\n"
70  " the -I flag is used.\n" ));
71  printf(_( " -? Display this help screen.\n" ));
72  printf( "\n" );
73  printf(_( " An argument of `--' disables further option processing.\n" ));
74  printf(_( " (useful for unusual file names starting with '-')\n" ));
75 }
76 
77 
78 int
79 main (int argc, char **argv)
80 {
81  SHPLOADERCONFIG *config;
82  SHPLOADERSTATE *state;
83  char *header, *footer, *record;
84  int c;
85  int ret, i;
86 
87 #ifdef ENABLE_NLS
88  setlocale (LC_ALL, "");
89  bindtextdomain (PACKAGE, PGSQL_LOCALEDIR);
90  textdomain (PACKAGE);
91 #endif
92 
93  /* If no options are specified, display usage */
94  if (argc == 1)
95  {
96  usage();
97  exit(0);
98  }
99 
100  /* Parse command line options and set configuration */
101  config = malloc(sizeof(SHPLOADERCONFIG));
103 
104  /* Keep the flag list alphabetic so it's easy to see what's left. */
105  while ((c = pgis_getopt(argc, argv, "-acdeg:ikm:nps:t:wDGIN:ST:W:X:")) != EOF)
106  {
107  // can not do this inside the switch case
108  if ('-' == c)
109  break;
110 
111  switch (c)
112  {
113  case 'c':
114  case 'd':
115  case 'a':
116  case 'p':
117  config->opt = c;
118  break;
119 
120  case 'D':
121  config->dump_format = 1;
122  break;
123 
124  case 'G':
125  config->geography = 1;
126  break;
127 
128  case 'S':
129  config->simple_geometries = 1;
130  break;
131 
132  case 's':
133  if (pgis_optarg)
134  {
135  char *ptr = strchr(pgis_optarg, ':');
136  if (ptr)
137  {
138  *ptr++ = '\0';
139  sscanf(pgis_optarg, "%d", &config->shp_sr_id);
140  sscanf(ptr, "%d", &config->sr_id);
141  }
142  else
143  {
144  /* Only TO_SRID specified */
145  sscanf(pgis_optarg, "%d", &config->sr_id);
146  }
147  }
148  else
149  {
150  /* With -s, user must specify TO_SRID or FROM_SRID:TO_SRID */
151  fprintf(stderr, "The -s parameter must be specified in the form [FROM_SRID:]TO_SRID\n");
152  exit(1);
153  }
154  break;
155  case 'g':
156  config->geo_col = pgis_optarg;
157  break;
158  case 'm':
160  break;
161 
162  case 'k':
163  config->quoteidentifiers = 1;
164  break;
165 
166  case 'i':
167  config->forceint4 = 1;
168  break;
169 
170  case 'I':
171  config->createindex = 1;
172  break;
173 
174  case 'w':
175  config->use_wkt = 1;
176  break;
177 
178  case 'n':
179  config->readshape = 0;
180  break;
181 
182  case 'W':
183  config->encoding = strdup(pgis_optarg);
184  break;
185 
186  case 'N':
187  switch (pgis_optarg[0])
188  {
189  case 'a':
190  config->null_policy = POLICY_NULL_ABORT;
191  break;
192  case 'i':
194  break;
195  case 's':
196  config->null_policy = POLICY_NULL_SKIP;
197  break;
198  default:
199  fprintf(stderr, "Unsupported NULL geometry handling policy.\nValid policies: insert, skip, abort\n");
200  exit(1);
201  }
202  break;
203 
204  case 't':
205  if (strcasecmp(pgis_optarg, "2D") == 0)
206  {
207  config->force_output = FORCE_OUTPUT_2D;
208  }
209  else if (strcasecmp(pgis_optarg, "3DZ") == 0 )
210  {
211  config->force_output = FORCE_OUTPUT_3DZ;
212  }
213  else if (strcasecmp(pgis_optarg, "3DM") == 0 )
214  {
215  config->force_output = FORCE_OUTPUT_3DM;
216  }
217  else if (strcasecmp(pgis_optarg, "4D") == 0 )
218  {
219  config->force_output = FORCE_OUTPUT_4D;
220  }
221  else
222  {
223  fprintf(stderr, "Unsupported output type: %s\nValid output types are 2D, 3DZ, 3DM and 4D\n", pgis_optarg);
224  exit(1);
225  }
226  break;
227 
228  case 'T':
229  config->tablespace = pgis_optarg;
230  break;
231 
232  case 'X':
233  config->idxtablespace = pgis_optarg;
234  break;
235 
236  case 'e':
237  config->usetransaction = 0;
238  break;
239 
240  case '?':
241  usage();
242  exit(0);
243 
244  default:
245  usage();
246  exit(0);
247  }
248  }
249 
250  /* Once we have parsed the arguments, make sure certain combinations are valid */
251  if (config->dump_format && !config->usetransaction)
252  {
253  fprintf(stderr, "Invalid argument combination - cannot use both -D and -e\n");
254  exit(1);
255  }
256 
257  /* Determine the shapefile name from the next argument, if no shape file, exit. */
258  if (pgis_optind < argc)
259  {
260  config->shp_file = argv[pgis_optind];
261  pgis_optind++;
262  }
263  else
264  {
265  usage();
266  exit(0);
267  }
268 
269  /* Determine the table and schema names from the next argument */
270  if (pgis_optind < argc)
271  {
272  char *strptr = argv[pgis_optind];
273  char *chrptr = strchr(strptr, '.');
274 
275  /* OK, this is a schema-qualified table name... */
276  if (chrptr)
277  {
278  if ( chrptr == strptr )
279  {
280  /* ".something" ??? */
281  usage();
282  exit(0);
283  }
284  /* Null terminate at the '.' */
285  *chrptr = '\0';
286  /* Copy in the parts */
287  config->schema = strdup(strptr);
288  config->table = strdup(chrptr+1);
289  }
290  else
291  {
292  config->table = strdup(strptr);
293  }
294  }
295 
296  /* If the table parameter is not provided, use the shape file name as a proxy value.
297  Strip out the .shp and the leading path information first. */
298  if ( config->shp_file && config->table == NULL)
299  {
300  char *shp_file = strdup(config->shp_file);
301  char *ptr;
302 
303  /* Remove the extension, if present */
304  for ( ptr = shp_file + strlen(shp_file); ptr > shp_file; ptr-- )
305  {
306  if ( *ptr == '.' )
307  {
308  *ptr = '\0';
309  break;
310  }
311  }
312 
313  /* The remaining non-path section is the table name */
314  for ( ptr = shp_file + strlen(shp_file); ptr > shp_file; ptr-- )
315  {
316  if ( *ptr == '/' || *ptr == '\\' )
317  {
318  ptr++;
319  break;
320  }
321  }
322  config->table = strdup(ptr);
323  free(shp_file);
324  }
325 
326 
327  /* Transform table name to lower case if no quoting specified */
328  if (!config->quoteidentifiers)
329  {
330  if ( config->table )
331  strtolower(config->table);
332  if ( config->schema )
333  strtolower(config->schema);
334  }
335 
336  /* Create the shapefile state object */
337  state = ShpLoaderCreate(config);
338 
339  /* Open the shapefile */
340  ret = ShpLoaderOpenShape(state);
341  if (ret != SHPLOADEROK)
342  {
343  fprintf(stderr, "%s\n", state->message);
344 
345  if (ret == SHPLOADERERR)
346  exit(1);
347  }
348 
349  /* If reading the whole shapefile, display its type */
350  if (state->config->readshape)
351  {
352  fprintf(stderr, "Shapefile type: %s\n", SHPTypeName(state->shpfiletype));
353  fprintf(stderr, "Postgis type: %s[%d]\n", state->pgtype, state->pgdims);
354  }
355 
356  /* Print the header to stdout */
357  ret = ShpLoaderGetSQLHeader(state, &header);
358  if (ret != SHPLOADEROK)
359  {
360  fprintf(stderr, "%s\n", state->message);
361 
362  if (ret == SHPLOADERERR)
363  exit(1);
364  }
365 
366  printf("%s", header);
367  free(header);
368 
369  /* If we are not in "prepare" mode, go ahead and write out the data. */
370  if ( state->config->opt != 'p' )
371  {
372 
373  /* If in COPY mode, output the COPY statement */
374  if (state->config->dump_format)
375  {
376  ret = ShpLoaderGetSQLCopyStatement(state, &header);
377  if (ret != SHPLOADEROK)
378  {
379  fprintf(stderr, "%s\n", state->message);
380 
381  if (ret == SHPLOADERERR)
382  exit(1);
383  }
384 
385  printf("%s", header);
386  free(header);
387  }
388 
389  /* Main loop: iterate through all of the records and send them to stdout */
390  for (i = 0; i < ShpLoaderGetRecordCount(state); i++)
391  {
392  ret = ShpLoaderGenerateSQLRowStatement(state, i, &record);
393 
394  switch (ret)
395  {
396  case SHPLOADEROK:
397  /* Simply display the geometry */
398  printf("%s\n", record);
399  free(record);
400  break;
401 
402  case SHPLOADERERR:
403  /* Display the error message then stop */
404  fprintf(stderr, "%s\n", state->message);
405  exit(1);
406  break;
407 
408  case SHPLOADERWARN:
409  /* Display the warning, but continue */
410  fprintf(stderr, "%s\n", state->message);
411  printf("%s\n", record);
412  free(record);
413  break;
414 
415  case SHPLOADERRECDELETED:
416  /* Record is marked as deleted - ignore */
417  break;
418 
419  case SHPLOADERRECISNULL:
420  /* Record is NULL and should be ignored according to NULL policy */
421  break;
422  }
423  }
424 
425  /* If in COPY mode, terminate the COPY statement */
426  if (state->config->dump_format)
427  printf("\\.\n");
428 
429  }
430 
431  /* Print the footer to stdout */
432  ret = ShpLoaderGetSQLFooter(state, &footer);
433  if (ret != SHPLOADEROK)
434  {
435  fprintf(stderr, "%s\n", state->message);
436 
437  if (ret == SHPLOADERERR)
438  exit(1);
439  }
440 
441  printf("%s", footer);
442  free(footer);
443 
444 
445  /* Free the state object */
446  ShpLoaderDestroy(state);
447 
448  /* Free configuration variables */
449  if (config->schema)
450  free(config->schema);
451  if (config->table)
452  free(config->table);
453  if (config->encoding)
454  free(config->encoding);
455  free(config);
456 
457  return 0;
458 }
int pgis_optind
Definition: getopt.c:39
int pgis_getopt(int argc, char **argv, char *opts)
Definition: getopt.c:44
char * pgis_optarg
Definition: getopt.c:41
int main(int argc, char *argv[])
#define SRID_UNKNOWN
Unknown SRID value.
Definition: liblwgeom.h:229
void * malloc(YYSIZE_T)
void free(void *)
const char SHPAPI_CALL1 * SHPTypeName(int nSHPType);const char SHPAPI_CALL1(*) SHPPartTypeName(int nPartType
Definition: shpopen.c:2553
static void usage()
Definition: shp2pgsql-cli.c:24
#define xstr(s)
Definition: shp2pgsql-cli.c:20
int ShpLoaderGetRecordCount(SHPLOADERSTATE *state)
void strtolower(char *s)
void ShpLoaderDestroy(SHPLOADERSTATE *state)
int ShpLoaderGetSQLCopyStatement(SHPLOADERSTATE *state, char **strheader)
int ShpLoaderOpenShape(SHPLOADERSTATE *state)
int ShpLoaderGenerateSQLRowStatement(SHPLOADERSTATE *state, int item, char **strrecord)
void set_loader_config_defaults(SHPLOADERCONFIG *config)
int ShpLoaderGetSQLFooter(SHPLOADERSTATE *state, char **strfooter)
SHPLOADERSTATE * ShpLoaderCreate(SHPLOADERCONFIG *config)
int ShpLoaderGetSQLHeader(SHPLOADERSTATE *state, char **strheader)
#define FORCE_OUTPUT_4D
#define POLICY_NULL_ABORT
#define FORCE_OUTPUT_2D
#define SHPLOADERRECISNULL
#define SHPLOADERWARN
#define FORCE_OUTPUT_3DM
#define POLICY_NULL_SKIP
#define POLICY_NULL_INSERT
#define SHPLOADERRECDELETED
#define SHPLOADERERR
#define SHPLOADEROK
#define FORCE_OUTPUT_3DZ
#define _(String)
Definition: shpcommon.h:24
#define POSTGIS_LIB_VERSION
Definition: sqldefines.h:13
char message[SHPLOADERMSGLEN]
SHPLOADERCONFIG * config