PostGIS  3.0.6dev-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, xstr(POSTGIS_REVISION));
31  printf(_("USAGE: pgsql2shp [<options>] <database> [<schema>.]<table>\n"
32  " pgsql2shp [<options>] <database> <query>\n"
33  "\n"
34  "OPTIONS:\n" ));
35  printf(_(" -f <filename> Use this option to specify the name of the file to create.\n" ));
36  printf(_(" -h <host> Allows you to specify connection to a database on a\n"
37  " machine other than the default.\n" ));
38  printf(_(" -p <port> Allows you to specify a database port other than the default.\n" ));
39  printf(_(" -P <password> Connect to the database with the specified password.\n" ));
40  printf(_(" -u <user> Connect to the database as the specified user.\n" ));
41  printf(_(" -g <geometry_column> Specify the geometry column to be exported.\n" ));
42  printf(_(" -b Use a binary cursor.\n" ));
43  printf(_(" -r Raw mode. Do not assume table has been created by the loader. This would\n"
44  " not unescape attribute names and will not skip the 'gid' attribute.\n" ));
45  printf(_(" -k Keep PostgreSQL identifiers case.\n" ));
46  printf(_(" -m <filename> Specify a file containing a set of mappings of (long) column\n"
47  " names to 10 character DBF column names. The content of the file is one or\n"
48  " more lines of two names separated by white space and no trailing or\n"
49  " leading space. For example:\n"
50  " COLUMNNAME DBFFIELD1\n"
51  " AVERYLONGCOLUMNNAME DBFFIELD2\n" ));
52  printf(_(" -? Display this help screen.\n\n" ));
53  exit(status);
54 }
55 
56 int
57 main(int argc, char **argv)
58 {
59  SHPDUMPERCONFIG *config;
60  SHPDUMPERSTATE *state;
61 
62  int ret, c, i;
63 
64  /* If no options are specified, display usage */
65  if (argc == 1)
66  {
67  usage(0); /* TODO: should this exit with error ? */
68  }
69 
70  /* Parse command line options and set configuration */
71  config = malloc(sizeof(SHPDUMPERCONFIG));
73 
74  while ((c = pgis_getopt(argc, argv, "bf:h:du:p:P:g:rkm:")) != EOF)
75  {
76  switch (c)
77  {
78  case 'b':
79  config->binary = 1;
80  break;
81  case 'f':
82  config->shp_file = pgis_optarg;
83  break;
84  case 'h':
85  config->conn->host = pgis_optarg;
86  break;
87  case 'd':
88  config->dswitchprovided = 1;
89  break;
90  case 'r':
91  config->includegid = 1;
92  config->unescapedattrs = 1;
93  break;
94  case 'u':
95  config->conn->username = pgis_optarg;
96  break;
97  case 'p':
98  config->conn->port = pgis_optarg;
99  break;
100  case 'P':
101  config->conn->password = pgis_optarg;
102  break;
103  case 'g':
104  config->geo_col_name = pgis_optarg;
105  break;
106  case 'm':
108  break;
109  case 'k':
110  config->keep_fieldname_case = 1;
111  break;
112  default:
113  usage(pgis_optopt == '?' ? 0 : 1);
114  }
115  }
116 
117 
118  /* Determine the database name from the next argument, if no database, exit. */
119  if (pgis_optind < argc)
120  {
121  config->conn->database = argv[pgis_optind];
122  pgis_optind++;
123  }
124  else
125  {
126  usage(1);
127  }
128 
129 
130  /* Determine the table and schema names from the next argument if supplied, otherwise if
131  it's a user-defined query then set that instead */
132  if (pgis_optind < argc)
133  {
134  /* User-defined queries begin with SELECT */
135  if (!strncmp(argv[pgis_optind], "SELECT ", 7) ||
136  !strncmp(argv[pgis_optind], "select ", 7))
137  {
138  config->usrquery = argv[pgis_optind];
139  }
140  else
141  {
142  /* Schema qualified table name */
143  char *strptr = argv[pgis_optind];
144  char *chrptr = strchr(strptr, '.');
145 
146  /* OK, this is a schema-qualified table name... */
147  if (chrptr)
148  {
149  if ( chrptr == strptr )
150  {
151  /* table is ".something" display help */
152  usage(0);
153  exit(0);
154  }
155  /* Null terminate at the '.' */
156  *chrptr = '\0';
157  /* Copy in the parts */
158  config->schema = strdup(strptr);
159  config->table = strdup(chrptr+1);
160  }
161  else
162  {
163  config->table = strdup(strptr);
164  }
165  }
166  }
167  else
168  {
169  usage(1);
170  }
171 
172  state = ShpDumperCreate(config);
173 
174  ret = ShpDumperConnectDatabase(state);
175  if (ret != SHPDUMPEROK)
176  {
177  fprintf(stderr, "%s\n", state->message);
178  fflush(stderr);
179  exit(1);
180  }
181 
182  /* Display a warning if the -d switch is used with PostGIS >= 1.0 */
183  if (state->pgis_major_version > 0 && state->config->dswitchprovided)
184  {
185  fprintf(stderr, _("WARNING: -d switch is useless when dumping from postgis-1.0.0+\n"));
186  fflush(stderr);
187  }
188 
189  /* Open the table ready to return rows */
190  fprintf(stdout, _("Initializing... \n"));
191  fflush(stdout);
192 
193  ret = ShpDumperOpenTable(state);
194  if (ret != SHPDUMPEROK)
195  {
196  fprintf(stderr, "%s\n", state->message);
197  fflush(stderr);
198 
199  if (ret == SHPDUMPERERR)
200  exit(1);
201  }
202 
203  fprintf(stdout, _("Done (postgis major version: %d).\n"), state->pgis_major_version);
204  fprintf(stdout, _("Output shape: %s\n"), shapetypename(state->outshptype));
205  fprintf(stdout, _("Dumping: "));
206  fflush(stdout);
207 
208  for (i = 0; i < ShpDumperGetRecordCount(state); i++)
209  {
210  /* Mimic existing behaviour */
211  if (!(state->currow % state->config->fetchsize))
212  {
213  fprintf(stdout, "X");
214  fflush(stdout);
215  }
216 
217  ret = ShpLoaderGenerateShapeRow(state);
218  if (ret != SHPDUMPEROK)
219  {
220  fprintf(stderr, "%s\n", state->message);
221  fflush(stderr);
222 
223  if (ret == SHPDUMPERERR)
224  exit(1);
225  }
226  }
227 
228  fprintf(stdout, _(" [%d rows].\n"), ShpDumperGetRecordCount(state));
229  fflush(stdout);
230 
231  ret = ShpDumperCloseTable(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  ShpDumperDestroy(state);
242 
243  return 0;
244 }
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:57
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]