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