PostGIS  3.4.0dev-r@@SVN_REVISION@@
pgsql2shp-cli.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  * Copyright 2001-2003 Refractions Research Inc.
6  *
7  * This is free software; you can redistribute and/or modify it under
8  * the terms of the GNU General Public Licence. See the COPYING file.
9  *
10  **********************************************************************
11  *
12  * PostGIS to Shapefile converter
13  *
14  * Original Author: Jeff Lounsbury <jeffloun@refractions.net>
15  * Maintainer: Sandro Santilli <strk@keybit.bet>
16  *
17  **********************************************************************/
18 
19 #include "pgsql2shp-core.h"
20 #include "../postgis_config.h"
21 
22 #define xstr(s) str(s)
23 #define str(s) #s
24 
25 static void
26 usage(int status)
27 {
28  /* TODO: if status != 0 print all to stderr */
29 
30  printf(_( "RELEASE: %s (%s)\n" ), POSTGIS_LIB_VERSION,
31  xstr(POSTGIS_REVISION));
32  printf(_("USAGE: pgsql2shp [<options>] <database> [<schema>.]<table>\n"
33  " pgsql2shp [<options>] <database> <query>\n"
34  "\n"
35  "OPTIONS:\n" ));
36  printf(_(" -f <filename> Use this option to specify the name of the file to create.\n" ));
37  printf(_(" -h <host> Allows you to specify connection to a database on a\n"
38  " machine other than the default.\n" ));
39  printf(_(" -p <port> Allows you to specify a database port other than the default.\n" ));
40  printf(_(" -P <password> Connect to the database with the specified password.\n" ));
41  printf(_(" -u <user> Connect to the database as the specified user.\n" ));
42  printf(_(" -g <geometry_column> Specify the geometry column to be exported.\n" ));
43  printf(_(" -b Use a binary cursor.\n" ));
44  printf(_(" -r Raw mode. Do not assume table has been created by the loader. This would\n"
45  " not unescape attribute names and will not skip the 'gid' attribute.\n" ));
46  printf(_(" -k Keep PostgreSQL identifiers case.\n" ));
47  printf(_(" -m <filename> Specify a file containing a set of mappings of (long) column\n"
48  " names to 10 character DBF column names. The content of the file is one or\n"
49  " more lines of two names separated by white space and no trailing or\n"
50  " leading space. For example:\n"
51  " COLUMNNAME DBFFIELD1\n"
52  " AVERYLONGCOLUMNNAME DBFFIELD2\n" ));
53  printf(_(" -q Quiet mode. No messages to stdout.\n" ));
54  printf(_(" -? Display this help screen.\n\n" ));
55  exit(status);
56 }
57 
58 int
59 main(int argc, char **argv)
60 {
61  SHPDUMPERCONFIG *config;
62  SHPDUMPERSTATE *state;
63 
64  int ret, c, i;
65 
66  /* If no options are specified, display usage */
67  if (argc == 1)
68  {
69  usage(0); /* TODO: should this exit with error ? */
70  }
71 
72  /* Parse command line options and set configuration */
73  config = malloc(sizeof(SHPDUMPERCONFIG));
75 
76  while ((c = pgis_getopt(argc, argv, "bf:h:du:p:P:g:rkm:q")) != EOF)
77  {
78  switch (c)
79  {
80  case 'b':
81  config->binary = 1;
82  break;
83  case 'f':
84  config->shp_file = pgis_optarg;
85  break;
86  case 'h':
87  config->conn->host = pgis_optarg;
88  break;
89  case 'd':
90  config->dswitchprovided = 1;
91  break;
92  case 'r':
93  config->includegid = 1;
94  config->unescapedattrs = 1;
95  break;
96  case 'u':
97  config->conn->username = pgis_optarg;
98  break;
99  case 'p':
100  config->conn->port = pgis_optarg;
101  break;
102  case 'P':
103  config->conn->password = pgis_optarg;
104  break;
105  case 'g':
106  config->geo_col_name = pgis_optarg;
107  break;
108  case 'm':
110  break;
111  case 'k':
112  config->keep_fieldname_case = 1;
113  break;
114  case 'q':
115  config->quiet = 1;
116  break;
117  default:
118  usage(pgis_optopt == '?' ? 0 : 1);
119  }
120  }
121 
122 
123  /* Determine the database name from the next argument, if no database, exit. */
124  if (pgis_optind < argc)
125  {
126  config->conn->database = argv[pgis_optind];
127  pgis_optind++;
128  }
129  else
130  {
131  usage(1);
132  }
133 
134 
135  /* Determine the table and schema names from the next argument if supplied, otherwise if
136  it's a user-defined query then set that instead */
137  if (pgis_optind < argc)
138  {
139  /* User-defined queries begin with SELECT or WITH */
140  if ( !strncmp(argv[pgis_optind], "SELECT ", 7) ||
141  !strncmp(argv[pgis_optind], "select ", 7) ||
142  !strncmp(argv[pgis_optind], "WITH ", 5) ||
143  !strncmp(argv[pgis_optind], "with ", 5)
144  )
145  {
146  config->usrquery = argv[pgis_optind];
147  }
148  else
149  {
150  /* Schema qualified table name */
151  char *strptr = argv[pgis_optind];
152  char *chrptr = strchr(strptr, '.');
153 
154  /* OK, this is a schema-qualified table name... */
155  if (chrptr)
156  {
157  if ( chrptr == strptr )
158  {
159  /* table is ".something" display help */
160  usage(0);
161  exit(0);
162  }
163  /* Null terminate at the '.' */
164  *chrptr = '\0';
165  /* Copy in the parts */
166  config->schema = strdup(strptr);
167  config->table = strdup(chrptr+1);
168  }
169  else
170  {
171  config->table = strdup(strptr);
172  }
173  }
174  }
175  else
176  {
177  usage(1);
178  }
179 
180  state = ShpDumperCreate(config);
181 
182  ret = ShpDumperConnectDatabase(state);
183  if (ret != SHPDUMPEROK)
184  {
185  fprintf(stderr, "%s\n", state->message);
186  fflush(stderr);
187  exit(1);
188  }
189 
190  /* Display a warning if the -d switch is used with PostGIS >= 1.0 */
191  if (state->pgis_major_version > 0 && state->config->dswitchprovided)
192  {
193  fprintf(stderr, _("WARNING: -d switch is useless when dumping from postgis-1.0.0+\n"));
194  fflush(stderr);
195  }
196 
197  /* Open the table ready to return rows */
198  if (!state->config->quiet)
199  {
200  fprintf(stdout, _("Initializing... \n"));
201  fflush(stdout);
202  }
203 
204  ret = ShpDumperOpenTable(state);
205  if (ret != SHPDUMPEROK)
206  {
207  fprintf(stderr, "%s\n", state->message);
208  fflush(stderr);
209 
210  if (ret == SHPDUMPERERR)
211  exit(1);
212  }
213 
214  if (!state->config->quiet)
215  {
216  fprintf(stdout, _("Done (postgis major version: %d).\n"), state->pgis_major_version);
217  fprintf(stdout, _("Output shape: %s\n"), shapetypename(state->outshptype));
218  fprintf(stdout, _("Dumping: "));
219  fflush(stdout);
220  }
221 
222  for (i = 0; i < ShpDumperGetRecordCount(state); i++)
223  {
224  /* Mimic existing behaviour */
225  if (!state->config->quiet && !(state->currow % state->config->fetchsize))
226  {
227  fprintf(stdout, "X");
228  fflush(stdout);
229  }
230 
231  ret = ShpLoaderGenerateShapeRow(state);
232  if (ret != SHPDUMPEROK)
233  {
234  fprintf(stderr, "%s\n", state->message);
235  fflush(stderr);
236 
237  if (ret == SHPDUMPERERR)
238  exit(1);
239  }
240  }
241 
242  if (!state->config->quiet)
243  {
244  fprintf(stdout, _(" [%d rows].\n"), ShpDumperGetRecordCount(state));
245  fflush(stdout);
246  }
247 
248  ret = ShpDumperCloseTable(state);
249  if (ret != SHPDUMPEROK)
250  {
251  fprintf(stderr, "%s\n", state->message);
252  fflush(stderr);
253 
254  if (ret == SHPDUMPERERR)
255  exit(1);
256  }
257 
258  ShpDumperDestroy(state);
259 
260  return 0;
261 }
int pgis_optind
Definition: getopt.c:39
int pgis_optopt
Definition: getopt.c:40
int pgis_getopt(int argc, char **argv, char *opts)
Definition: getopt.c:44
char * pgis_optarg
Definition: getopt.c:41
void * malloc(YYSIZE_T)
int main(int argc, char **argv)
Definition: pgsql2shp-cli.c:59
static void usage(int status)
Definition: pgsql2shp-cli.c:26
#define xstr(s)
Definition: pgsql2shp-cli.c:22
void set_dumper_config_defaults(SHPDUMPERCONFIG *config)
int ShpDumperGetRecordCount(SHPDUMPERSTATE *state)
void ShpDumperDestroy(SHPDUMPERSTATE *state)
char * shapetypename(int num)
SHPDUMPERSTATE * ShpDumperCreate(SHPDUMPERCONFIG *config)
int ShpDumperConnectDatabase(SHPDUMPERSTATE *state)
int ShpLoaderGenerateShapeRow(SHPDUMPERSTATE *state)
int ShpDumperCloseTable(SHPDUMPERSTATE *state)
int ShpDumperOpenTable(SHPDUMPERSTATE *state)
#define SHPDUMPEROK
#define SHPDUMPERERR
#define _(String)
Definition: shpcommon.h:24
#define POSTGIS_LIB_VERSION
Definition: sqldefines.h:13
char * column_map_filename
SHPCONNECTIONCONFIG * conn
SHPDUMPERCONFIG * config
char message[SHPDUMPERMSGLEN]