| 1 | /********************************************************************** |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * PostGIS - Spatial Types for PostgreSQL |
|---|
| 5 | * http://postgis.refractions.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 | #include <stdio.h> |
|---|
| 16 | #include <string.h> |
|---|
| 17 | #include <stdlib.h> |
|---|
| 18 | #include <ctype.h> |
|---|
| 19 | #include <unistd.h> |
|---|
| 20 | #include <errno.h> |
|---|
| 21 | #include <sys/types.h> |
|---|
| 22 | #include <sys/stat.h> |
|---|
| 23 | #include <iconv.h> |
|---|
| 24 | |
|---|
| 25 | #include "shapefil.h" |
|---|
| 26 | #include "getopt.h" |
|---|
| 27 | |
|---|
| 28 | #include "../liblwgeom/liblwgeom.h" |
|---|
| 29 | |
|---|
| 30 | #include "stringbuffer.h" |
|---|
| 31 | |
|---|
| 32 | #define RCSID "$Id$" |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | /* |
|---|
| 36 | * Loader policy constants |
|---|
| 37 | */ |
|---|
| 38 | |
|---|
| 39 | #define POLICY_NULL_ABORT 0x0 |
|---|
| 40 | #define POLICY_NULL_INSERT 0x1 |
|---|
| 41 | #define POLICY_NULL_SKIP 0x2 |
|---|
| 42 | |
|---|
| 43 | |
|---|
| 44 | /* |
|---|
| 45 | * Error message handling |
|---|
| 46 | */ |
|---|
| 47 | |
|---|
| 48 | #define SHPLOADERMSGLEN 1024 |
|---|
| 49 | |
|---|
| 50 | #define SHPLOADEROK -1 |
|---|
| 51 | #define SHPLOADERERR 0 |
|---|
| 52 | #define SHPLOADERWARN 1 |
|---|
| 53 | |
|---|
| 54 | #define SHPLOADERRECDELETED 2 |
|---|
| 55 | #define SHPLOADERRECISNULL 3 |
|---|
| 56 | |
|---|
| 57 | /* |
|---|
| 58 | * Shapefile (dbf) field name are at most 10chars + 1 NULL. |
|---|
| 59 | * Postgresql field names are at most 63 bytes + 1 NULL. |
|---|
| 60 | */ |
|---|
| 61 | #define MAXFIELDNAMELEN 64 |
|---|
| 62 | #define MAXVALUELEN 1024 |
|---|
| 63 | |
|---|
| 64 | /* |
|---|
| 65 | * Default geometry column name |
|---|
| 66 | */ |
|---|
| 67 | #define GEOMETRY_DEFAULT "the_geom" |
|---|
| 68 | #define GEOGRAPHY_DEFAULT "geog" |
|---|
| 69 | |
|---|
| 70 | /* |
|---|
| 71 | * Default character encoding |
|---|
| 72 | */ |
|---|
| 73 | #define ENCODING_DEFAULT "WINDOWS-1252" |
|---|
| 74 | |
|---|
| 75 | /* |
|---|
| 76 | * Structure to hold the loader configuration options |
|---|
| 77 | */ |
|---|
| 78 | |
|---|
| 79 | typedef struct shp_loader_config |
|---|
| 80 | { |
|---|
| 81 | /* load mode: c = create, d = delete, a = append, p = prepare */ |
|---|
| 82 | char opt; |
|---|
| 83 | |
|---|
| 84 | /* table to load into */ |
|---|
| 85 | char *table; |
|---|
| 86 | |
|---|
| 87 | /* schema to load into */ |
|---|
| 88 | char *schema; |
|---|
| 89 | |
|---|
| 90 | /* geometry column name to use */ |
|---|
| 91 | char *geom; |
|---|
| 92 | |
|---|
| 93 | /* the shape file (without the .shp extension) */ |
|---|
| 94 | char *shp_file; |
|---|
| 95 | |
|---|
| 96 | /* 0 = SQL inserts, 1 = dump */ |
|---|
| 97 | int dump_format; |
|---|
| 98 | |
|---|
| 99 | /* 0 = MULTIPOLYGON/MULTILINESTRING, 1 = force to POLYGON/LINESTRING */ |
|---|
| 100 | int simple_geometries; |
|---|
| 101 | |
|---|
| 102 | /* 0 = geometry, 1 = geography */ |
|---|
| 103 | int geography; |
|---|
| 104 | |
|---|
| 105 | /* 0 = columnname, 1 = "columnName" */ |
|---|
| 106 | int quoteidentifiers; |
|---|
| 107 | |
|---|
| 108 | /* 0 = allow int8 fields, 1 = no int8 fields */ |
|---|
| 109 | int forceint4; |
|---|
| 110 | |
|---|
| 111 | /* 0 = no index, 1 = create index after load */ |
|---|
| 112 | int createindex; |
|---|
| 113 | |
|---|
| 114 | /* 0 = load DBF file only, 1 = load everything */ |
|---|
| 115 | int readshape; |
|---|
| 116 | |
|---|
| 117 | /* iconv encoding name */ |
|---|
| 118 | char *encoding; |
|---|
| 119 | |
|---|
| 120 | /* how to handle nulls */ |
|---|
| 121 | int null_policy; |
|---|
| 122 | |
|---|
| 123 | /* SRID specified */ |
|---|
| 124 | int sr_id; |
|---|
| 125 | |
|---|
| 126 | /* 0 = new style (PostGIS 1.x) geometries, 1 = old style (PostGIS 0.9.x) geometries */ |
|---|
| 127 | int hwgeom; |
|---|
| 128 | |
|---|
| 129 | } SHPLOADERCONFIG; |
|---|
| 130 | |
|---|
| 131 | |
|---|
| 132 | /* |
|---|
| 133 | * Structure to holder the current loader state |
|---|
| 134 | */ |
|---|
| 135 | |
|---|
| 136 | typedef struct shp_loader_state |
|---|
| 137 | { |
|---|
| 138 | /* Configuration for this state */ |
|---|
| 139 | SHPLOADERCONFIG *config; |
|---|
| 140 | |
|---|
| 141 | /* Shapefile handle */ |
|---|
| 142 | SHPHandle hSHPHandle; |
|---|
| 143 | |
|---|
| 144 | /* Shapefile type */ |
|---|
| 145 | int shpfiletype; |
|---|
| 146 | |
|---|
| 147 | /* Data file handle */ |
|---|
| 148 | DBFHandle hDBFHandle; |
|---|
| 149 | |
|---|
| 150 | /* Number of rows in the shapefile */ |
|---|
| 151 | int num_entities; |
|---|
| 152 | |
|---|
| 153 | /* Number of fields in the shapefile */ |
|---|
| 154 | int num_fields; |
|---|
| 155 | |
|---|
| 156 | /* Number of rows in the DBF file */ |
|---|
| 157 | int num_records; |
|---|
| 158 | |
|---|
| 159 | /* Pointer to an array of field names */ |
|---|
| 160 | char **field_names; |
|---|
| 161 | |
|---|
| 162 | /* Field type */ |
|---|
| 163 | DBFFieldType *types; |
|---|
| 164 | |
|---|
| 165 | /* Arrays of field widths and precisions */ |
|---|
| 166 | int *widths; |
|---|
| 167 | int *precisions; |
|---|
| 168 | |
|---|
| 169 | /* String containing colume name list in the form "(col1, col2, col3 ... , colN)" */ |
|---|
| 170 | char *col_names; |
|---|
| 171 | |
|---|
| 172 | /* String containing the PostGIS geometry type, e.g. POINT, POLYGON etc. */ |
|---|
| 173 | char *pgtype; |
|---|
| 174 | |
|---|
| 175 | /* PostGIS geometry type (numeric version) */ |
|---|
| 176 | unsigned int wkbtype; |
|---|
| 177 | |
|---|
| 178 | /* Number of dimensions to output */ |
|---|
| 179 | int pgdims; |
|---|
| 180 | |
|---|
| 181 | /* 0 = simple geometry, 1 = multi geometry */ |
|---|
| 182 | int istypeM; |
|---|
| 183 | |
|---|
| 184 | /* Last (error) message */ |
|---|
| 185 | char message[SHPLOADERMSGLEN]; |
|---|
| 186 | |
|---|
| 187 | } SHPLOADERSTATE; |
|---|
| 188 | |
|---|
| 189 | |
|---|
| 190 | typedef struct shp_connection_state |
|---|
| 191 | { |
|---|
| 192 | /* PgSQL username to log in with */ |
|---|
| 193 | char *username; |
|---|
| 194 | |
|---|
| 195 | /* PgSQL password to log in with */ |
|---|
| 196 | char *password; |
|---|
| 197 | |
|---|
| 198 | /* PgSQL database to connect to */ |
|---|
| 199 | char *database; |
|---|
| 200 | |
|---|
| 201 | /* PgSQL port to connect to */ |
|---|
| 202 | char *port; |
|---|
| 203 | |
|---|
| 204 | /* PgSQL server to connect to */ |
|---|
| 205 | char *host; |
|---|
| 206 | |
|---|
| 207 | } SHPCONNECTIONCONFIG; |
|---|
| 208 | |
|---|
| 209 | /* Externally accessible functions */ |
|---|
| 210 | void strtolower(char *s); |
|---|
| 211 | void set_config_defaults(SHPLOADERCONFIG *config); |
|---|
| 212 | |
|---|
| 213 | SHPLOADERSTATE *ShpLoaderCreate(SHPLOADERCONFIG *config); |
|---|
| 214 | int ShpLoaderOpenShape(SHPLOADERSTATE *state); |
|---|
| 215 | int ShpLoaderGetSQLHeader(SHPLOADERSTATE *state, char **strheader); |
|---|
| 216 | int ShpLoaderGetSQLCopyStatement(SHPLOADERSTATE *state, char **strheader); |
|---|
| 217 | int ShpLoaderGetRecordCount(SHPLOADERSTATE *state); |
|---|
| 218 | int ShpLoaderGenerateSQLRowStatement(SHPLOADERSTATE *state, int item, char **strrecord); |
|---|
| 219 | int ShpLoaderGetSQLFooter(SHPLOADERSTATE *state, char **strfooter); |
|---|
| 220 | void ShpLoaderDestroy(SHPLOADERSTATE *state); |
|---|