PostGIS  2.2.7dev-r@@SVN_REVISION@@
shp2pgsql-core.h
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  * Copyright 2009 Paul Ramsey <pramsey@cleverelephant.ca>
7  * Copyright 2009 Mark Cave-Ayland <mark.cave-ayland@siriusit.co.uk>
8  *
9  * This is free software; you can redistribute and/or modify it under
10  * the terms of the GNU General Public Licence. See the COPYING file.
11  *
12  **********************************************************************/
13 
14 /* Standard headers */
15 #include <stdio.h>
16 #include <string.h>
17 #include <stdlib.h>
18 #include <locale.h>
19 #include <ctype.h>
20 #include <unistd.h>
21 #include <errno.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <iconv.h>
25 
26 #include "shapefil.h"
27 #include "shpcommon.h"
28 #include "getopt.h"
29 
30 #include "../liblwgeom/stringbuffer.h"
31 
32 #define S2P_RCSID "$Id: shp2pgsql-core.h 13497 2015-05-13 18:29:52Z pramsey $"
33 
34 /* Number of digits of precision in WKT produced. */
35 #define WKT_PRECISION 15
36 
37 /* Loader policy constants */
38 #define POLICY_NULL_ABORT 0x0
39 #define POLICY_NULL_INSERT 0x1
40 #define POLICY_NULL_SKIP 0x2
41 
42 /* Forced dimensionality constants */
43 #define FORCE_OUTPUT_DISABLE 0x0
44 #define FORCE_OUTPUT_2D 0x1
45 #define FORCE_OUTPUT_3DZ 0x2
46 #define FORCE_OUTPUT_3DM 0x3
47 #define FORCE_OUTPUT_4D 0x4
48 
49 /* Error message handling */
50 #define SHPLOADERMSGLEN 1024
51 
52 /* Error status codes */
53 #define SHPLOADEROK -1
54 #define SHPLOADERERR 0
55 #define SHPLOADERWARN 1
56 
57 /* Record status codes */
58 #define SHPLOADERRECDELETED 2
59 #define SHPLOADERRECISNULL 3
60 
61 /*
62  * Shapefile (dbf) field name are at most 10chars + 1 NULL.
63  * Postgresql field names are at most 63 bytes + 1 NULL.
64  */
65 #define MAXFIELDNAMELEN 64
66 #define MAXVALUELEN 1024
67 
68 /*
69  * Default geometry column name
70  */
71 #define GEOMETRY_DEFAULT "geom"
72 #define GEOGRAPHY_DEFAULT "geog"
73 
74 /*
75  * Default character encoding
76  */
77 #define ENCODING_DEFAULT "UTF-8"
78 
79 /*
80  * Structure to hold the loader configuration options
81  */
82 typedef struct shp_loader_config
83 {
84  /* load mode: c = create, d = delete, a = append, p = prepare */
85  char opt;
86 
87  /* table to load into */
88  char *table;
89 
90  /* schema to load into */
91  char *schema;
92 
93  /* geometry/geography column name specified by the user, may be null. */
94  char *geo_col;
95 
96  /* the shape file (without the .shp extension) */
97  char *shp_file;
98 
99  /* 0 = SQL inserts, 1 = dump */
101 
102  /* 0 = MULTIPOLYGON/MULTILINESTRING, 1 = force to POLYGON/LINESTRING */
104 
105  /* 0 = geometry, 1 = geography */
107 
108  /* 0 = columnname, 1 = "columnName" */
110 
111  /* 0 = allow int8 fields, 1 = no int8 fields */
113 
114  /* 0 = no index, 1 = create index after load */
116 
117  /* 0 = load DBF file only, 1 = load everything */
119 
120  /* Override the output geometry type (a FORCE_OUTPUT_* constant) */
122 
123  /* iconv encoding name */
124  char *encoding;
125 
126  /* tablespace name for the table */
127  char *tablespace;
128 
129  /* tablespace name for the indexes */
131 
132  /* how to handle nulls */
134 
135  /* SRID specified */
136  int sr_id;
137 
138  /* SRID of the shape file */
140 
141  /* 0 = WKB (more precise), 1 = WKT (may have coordinate drift). */
142  int use_wkt;
143 
144  /* whether to do a single transaction or run each statement on its own */
146 
147  /* Name of the column map file if specified */
149 
151 
152 
153 /*
154  * Structure to holder the current loader state
155  */
156 typedef struct shp_loader_state
157 {
158  /* Configuration for this state */
160 
161  /* Shapefile handle */
163 
164  /* Shapefile type */
166 
167  /* Data file handle */
168  DBFHandle hDBFHandle;
169 
170  /* Number of rows in the shapefile */
172 
173  /* Number of fields in the shapefile */
175 
176  /* Number of rows in the DBF file */
178 
179  /* Pointer to an array of field names */
180  char **field_names;
181 
182  /* Field type */
183  DBFFieldType *types;
184 
185  /* Arrays of field widths and precisions */
186  int *widths;
188 
189  /* Pointer to an array of PostgreSQL field types */
190  char **pgfieldtypes;
191 
192  /* String containing colume name list in the form "(col1, col2, col3 ... , colN)" */
193  char *col_names;
194 
195  /* String containing the PostGIS geometry type, e.g. POINT, POLYGON etc. */
196  char *pgtype;
197 
198  /* Flag for whether the output geometry has Z coordinates or not. */
199  int has_z;
200 
201  /* Flag for whether the output geometry has M coordinates or not. */
202  int has_m;
203 
204  /* Number of dimensions to output */
205  int pgdims;
206 
207  /* Last (error) message */
209 
210  /* SRID of the shape file. If not reprojecting, will be the same as to_srid. */
212 
213  /* SRID of the table. If not reprojecting, will be the same as from_srid. */
214  int to_srid;
215 
216  /* geometry/geography column name to use. Will be set to the default if the config did
217  not specify a column name. */
218  char *geo_col;
219 
220  /* Column map */
222 
224 
225 
226 /* Externally accessible functions */
227 void strtolower(char *s);
229 
232 int ShpLoaderGetSQLHeader(SHPLOADERSTATE *state, char **strheader);
233 int ShpLoaderGetSQLCopyStatement(SHPLOADERSTATE *state, char **strheader);
235 int ShpLoaderGenerateSQLRowStatement(SHPLOADERSTATE *state, int item, char **strrecord);
236 int ShpLoaderGetSQLFooter(SHPLOADERSTATE *state, char **strfooter);
237 void ShpLoaderDestroy(SHPLOADERSTATE *state);
struct shp_loader_config SHPLOADERCONFIG
int ShpLoaderGetSQLFooter(SHPLOADERSTATE *state, char **strfooter)
SHPLOADERCONFIG * config
DBFFieldType * types
int ShpLoaderOpenShape(SHPLOADERSTATE *state)
struct shp_loader_state SHPLOADERSTATE
int ShpLoaderGetSQLHeader(SHPLOADERSTATE *state, char **strheader)
DBFHandle hDBFHandle
void ShpLoaderDestroy(SHPLOADERSTATE *state)
void set_loader_config_defaults(SHPLOADERCONFIG *config)
int ShpLoaderGetRecordCount(SHPLOADERSTATE *state)
SHPLOADERSTATE * ShpLoaderCreate(SHPLOADERCONFIG *config)
char * s
Definition: cu_in_wkt.c:23
SHPHandle hSHPHandle
char message[SHPLOADERMSGLEN]
int ShpLoaderGetSQLCopyStatement(SHPLOADERSTATE *state, char **strheader)
int ShpLoaderGenerateSQLRowStatement(SHPLOADERSTATE *state, int item, char **strrecord)
void strtolower(char *s)
#define SHPLOADERMSGLEN