PostGIS  2.1.10dev-r@@SVN_REVISION@@
int main ( int  argc,
char **  argv 
)

Definition at line 56 of file pgsql2shp-cli.c.

References _, shp_dumper_config::binary, shp_dumper_config::column_map_filename, shp_dumper_state::config, shp_dumper_config::conn, shp_dumper_state::currow, shp_connection_state::database, shp_dumper_config::dswitchprovided, shp_dumper_config::fetchsize, shp_dumper_config::geo_col_name, shp_connection_state::host, shp_dumper_config::includegid, shp_dumper_config::keep_fieldname_case, shp_dumper_state::message, shp_dumper_state::outshptype, shp_connection_state::password, pgis_getopt(), shp_dumper_state::pgis_major_version, pgis_optarg, pgis_optind, pgis_optopt, shp_connection_state::port, shp_dumper_config::schema, set_dumper_config_defaults(), shapetypename(), shp_dumper_config::shp_file, ShpDumperCloseTable(), ShpDumperConnectDatabase(), ShpDumperCreate(), ShpDumperDestroy(), SHPDUMPERERR, ShpDumperGetRecordCount(), SHPDUMPEROK, ShpDumperOpenTable(), ShpLoaderGenerateShapeRow(), shp_dumper_config::table, shp_dumper_config::unescapedattrs, usage(), shp_connection_state::username, and shp_dumper_config::usrquery.

57 {
58  SHPDUMPERCONFIG *config;
59  SHPDUMPERSTATE *state;
60 
61  int ret, c, i;
62 
63  /* If no options are specified, display usage */
64  if (argc == 1)
65  {
66  usage(0); /* TODO: should this exit with error ? */
67  }
68 
69  /* Parse command line options and set configuration */
70  config = malloc(sizeof(SHPDUMPERCONFIG));
72 
73  while ((c = pgis_getopt(argc, argv, "bf:h:du:p:P:g:rkm:")) != EOF)
74  {
75  switch (c)
76  {
77  case 'b':
78  config->binary = 1;
79  break;
80  case 'f':
81  config->shp_file = pgis_optarg;
82  break;
83  case 'h':
84  config->conn->host = pgis_optarg;
85  break;
86  case 'd':
87  config->dswitchprovided = 1;
88  break;
89  case 'r':
90  config->includegid = 1;
91  config->unescapedattrs = 1;
92  break;
93  case 'u':
94  config->conn->username = pgis_optarg;
95  break;
96  case 'p':
97  config->conn->port = pgis_optarg;
98  break;
99  case 'P':
100  config->conn->password = pgis_optarg;
101  break;
102  case 'g':
103  config->geo_col_name = pgis_optarg;
104  break;
105  case 'm':
107  break;
108  case 'k':
109  config->keep_fieldname_case = 1;
110  break;
111  default:
112  usage(pgis_optopt == '?' ? 0 : 1);
113  }
114  }
115 
116 
117  /* Determine the database name from the next argument, if no database, exit. */
118  if (pgis_optind < argc)
119  {
120  config->conn->database = argv[pgis_optind];
121  pgis_optind++;
122  }
123  else
124  {
125  usage(1);
126  }
127 
128 
129  /* Determine the table and schema names from the next argument if supplied, otherwise if
130  it's a user-defined query then set that instead */
131  if (pgis_optind < argc)
132  {
133  /* User-defined queries begin with SELECT */
134  if (!strncmp(argv[pgis_optind], "SELECT ", 7) ||
135  !strncmp(argv[pgis_optind], "select ", 7))
136  {
137  config->usrquery = argv[pgis_optind];
138  }
139  else
140  {
141  /* Schema qualified table name */
142  char *strptr = argv[pgis_optind];
143  char *chrptr = strchr(strptr, '.');
144 
145  /* OK, this is a schema-qualified table name... */
146  if (chrptr)
147  {
148  if ( chrptr == strptr )
149  {
150  /* table is ".something" display help */
151  usage(0);
152  exit(0);
153  }
154  /* Null terminate at the '.' */
155  *chrptr = '\0';
156  /* Copy in the parts */
157  config->schema = strdup(strptr);
158  config->table = strdup(chrptr+1);
159  }
160  else
161  {
162  config->table = strdup(strptr);
163  }
164  }
165  }
166  else
167  {
168  usage(1);
169  }
170 
171  state = ShpDumperCreate(config);
172 
173  ret = ShpDumperConnectDatabase(state);
174  if (ret != SHPDUMPEROK)
175  {
176  fprintf(stderr, "%s\n", state->message);
177  fflush(stderr);
178  exit(1);
179  }
180 
181  /* Display a warning if the -d switch is used with PostGIS >= 1.0 */
182  if (state->pgis_major_version > 0 && state->config->dswitchprovided)
183  {
184  fprintf(stderr, _("WARNING: -d switch is useless when dumping from postgis-1.0.0+\n"));
185  fflush(stderr);
186  }
187 
188  /* Open the table ready to return rows */
189  fprintf(stdout, _("Initializing... \n"));
190  fflush(stdout);
191 
192  ret = ShpDumperOpenTable(state);
193  if (ret != SHPDUMPEROK)
194  {
195  fprintf(stderr, "%s\n", state->message);
196  fflush(stderr);
197 
198  if (ret == SHPDUMPERERR)
199  exit(1);
200  }
201 
202  fprintf(stdout, _("Done (postgis major version: %d).\n"), state->pgis_major_version);
203  fprintf(stdout, _("Output shape: %s\n"), shapetypename(state->outshptype));
204  fprintf(stdout, _("Dumping: "));
205  fflush(stdout);
206 
207  for (i = 0; i < ShpDumperGetRecordCount(state); i++)
208  {
209  /* Mimic existing behaviour */
210  if (!(state->currow % state->config->fetchsize))
211  {
212  fprintf(stdout, "X");
213  fflush(stdout);
214  }
215 
216  ret = ShpLoaderGenerateShapeRow(state);
217  if (ret != SHPDUMPEROK)
218  {
219  fprintf(stderr, "%s\n", state->message);
220  fflush(stderr);
221 
222  if (ret == SHPDUMPERERR)
223  exit(1);
224  }
225  }
226 
227  fprintf(stdout, _(" [%d rows].\n"), ShpDumperGetRecordCount(state));
228  fflush(stdout);
229 
230  ret = ShpDumperCloseTable(state);
231  if (ret != SHPDUMPEROK)
232  {
233  fprintf(stderr, "%s\n", state->message);
234  fflush(stderr);
235 
236  if (ret == SHPDUMPERERR)
237  exit(1);
238  }
239 
240  ShpDumperDestroy(state);
241 
242  return 0;
243 }
char * column_map_filename
int ShpDumperOpenTable(SHPDUMPERSTATE *state)
#define _(String)
Definition: shpcommon.h:23
static void usage(int status)
Definition: pgsql2shp-cli.c:25
SHPDUMPERCONFIG * config
int ShpLoaderGenerateShapeRow(SHPDUMPERSTATE *state)
SHPDUMPERSTATE * ShpDumperCreate(SHPDUMPERCONFIG *config)
int ShpDumperCloseTable(SHPDUMPERSTATE *state)
int pgis_optopt
Definition: getopt.c:44
void set_dumper_config_defaults(SHPDUMPERCONFIG *config)
int pgis_optind
Definition: getopt.c:43
#define SHPDUMPERERR
SHPCONNECTIONCONFIG * conn
void ShpDumperDestroy(SHPDUMPERSTATE *state)
int ShpDumperGetRecordCount(SHPDUMPERSTATE *state)
#define SHPDUMPEROK
int ShpDumperConnectDatabase(SHPDUMPERSTATE *state)
char * pgis_optarg
Definition: getopt.c:45
int pgis_getopt(int argc, char **argv, char *opts)
Definition: getopt.c:48
char message[SHPDUMPERMSGLEN]
char * shapetypename(int num)

Here is the call graph for this function: