1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
/**********************************************************************
*
* PostGIS - Spatial Types for PostgreSQL
* http://postgis.net
* Copyright 2001-2003 Refractions Research Inc.
* Copyright 2009 Paul Ramsey <pramsey@cleverelephant.ca>
* Copyright 2009 Mark Cave-Ayland <mark.cave-ayland@siriusit.co.uk>
*
* This is free software; you can redistribute and/or modify it under
* the terms of the GNU General Public Licence. See the COPYING file.
*
**********************************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <errno.h>
#include <math.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <iconv.h>
#include "libpq-fe.h"
#include "shapefil.h"
#include "shpcommon.h"
#include "getopt.h"
#define P2S_RCSID "$Id$"
/*
* Error message handling
*/
#define SHPDUMPERMSGLEN 1024
#define SHPDUMPEROK -1
#define SHPDUMPERERR 0
#define SHPDUMPERWARN 1
/*
* Structure to hold the dumper configuration options
*/
typedef struct shp_dumper_config
{
/* Parameters used to connect to the database */
SHPCONNECTIONCONFIG *conn;
/* table to load into */
char *table;
/* schema to load into */
char *schema;
/* user-specified query, if supplied */
char *usrquery;
/* 0=use normal cursor, 1=use binary cursor */
int binary;
/* Name of the output shapefile */
char *shp_file;
/* TODO: rename? 0=switch not provided, 1=switch provided */
int dswitchprovided;
/* TODO: replace and combine with below 0=do not include gid column in shapefile, 1=include gid column in shapefile */
int includegid;
/* TODO: 0=escape column names, 1=do not escape column names */
int unescapedattrs;
/* Name of geometry/geography database column */
char *geo_col_name;
/* 0=do not keep fieldname case, 1=keep fieldname case */
int keep_fieldname_case;
/* Number of rows to fetch in a cursor batch */
int fetchsize;
/* Name of the column map file if specified */
char *column_map_filename;
/* 0=normal output to stdout, 1=no output to stdout */
int quiet;
} SHPDUMPERCONFIG;
/*
* Structure to holder the current dumper state
*/
typedef struct shp_dumper_state
{
/* Configuration for this state */
SHPDUMPERCONFIG *config;
/* Database connection being used */
PGconn *conn;
/* Version of PostGIS being used */
int pgis_major_version;
/* OID for geometries */
int geom_oid;
/* OID for geographies */
int geog_oid;
/* Schema of current working table */
char *schema;
/* Name of current working table */
char *table;
/* Name of geography/geometry column in current working table */
char *geo_col_name;
/* DBF fieldnames for all non-spatial fields */
char **dbffieldnames;
/* DBF field types for all non-spatial fields */
int *dbffieldtypes;
/* PostgreSQL column names for all non-spatial fields */
char **pgfieldnames;
/* PostgreSQL column lengths for all non-spatial fields */
int *pgfieldlens;
/* PostgreSQL column typmods for all non-spatial fields */
int *pgfieldtypmods;
/* Number of non-spatial fields in DBF output file */
int fieldcount;
/* Number of rows in the database table */
int num_records;
/* Name of the current shapefile */
char *shp_file;
/* Handle of the current DBF file */
DBFHandle dbf;
/* Handle of the current SHP file */
SHPHandle shp;
/* Indicate output geometry type: s=2d, z=3dz or 4d, m=3dm */
char outtype;
/* Shapefile/DBF geometry type */
int outshptype;
/* Number of rows in source database table */
int rowcount;
/* The main query being used for the table scan */
char *main_scan_query;
/* The current row number */
int currow;
/* The result set for the current FETCH batch */
PGresult *fetchres;
/* The row number within the current FETCH batch */
int curresrow;
/* The number of rows within the current FETCH batch */
int currescount;
/* The query being used to fetch records from the table */
char *fetch_query;
/* Last (error) message */
char message[SHPDUMPERMSGLEN];
/* Column map */
colmap column_map;
} SHPDUMPERSTATE;
/* Externally accessible functions */
void set_dumper_config_defaults(SHPDUMPERCONFIG *config);
char *shapetypename(int num);
SHPDUMPERSTATE *ShpDumperCreate(SHPDUMPERCONFIG *config);
char *ShpDumperGetConnectionStringFromConn(SHPCONNECTIONCONFIG *config);
int ShpDumperConnectDatabase(SHPDUMPERSTATE *state);
int ShpDumperOpenTable(SHPDUMPERSTATE *state);
int ShpDumperGetRecordCount(SHPDUMPERSTATE *state);
int ShpLoaderGenerateShapeRow(SHPDUMPERSTATE *state);
int ShpDumperCloseTable(SHPDUMPERSTATE *state);
void ShpDumperDestroy(SHPDUMPERSTATE *state);
char *quote_identifier(const char *s);
|