4
/ an utility CLI tool for Shapefile import / export
6
/ version 1.0, 2008 Decmber 11
8
/ Author: Sandro Furieri a.furieri@lqt.it
10
/ Copyright (C) 2008 Alessandro Furieri
12
/ This program is free software: you can redistribute it and/or modify
13
/ it under the terms of the GNU General Public License as published by
14
/ the Free Software Foundation, either version 3 of the License, or
15
/ (at your option) any later version.
17
/ This program is distributed in the hope that it will be useful,
18
/ but WITHOUT ANY WARRANTY; without even the implied warranty of
19
/ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20
/ GNU General Public License for more details.
22
/ You should have received a copy of the GNU General Public License
23
/ along with this program. If not, see <http://www.gnu.org/licenses/>.
27
#if defined(_WIN32) && !defined(__MINGW32__)
28
/* MSVC strictly requires this include [off_t] */
29
#include <sys/types.h>
37
#ifdef SPATIALITE_AMALGAMATION
38
#include <spatialite/sqlite3.h>
43
#include <spatialite/gaiaaux.h>
44
#include <spatialite/gaiageo.h>
45
#include <spatialite.h>
47
#if defined(_WIN32) && !defined(__MINGW32__)
48
#define strcasecmp _stricmp
49
#endif /* not WIN32 */
64
spatialite_autocreate (sqlite3 * db)
66
/* attempting to perform self-initialization for a newly created DB */
76
/* checking if this DB is really empty */
77
strcpy (sql, "SELECT Count(*) from sqlite_master");
78
ret = sqlite3_get_table (db, sql, &results, &rows, &columns, NULL);
85
for (i = 1; i <= rows; i++)
86
count = atoi (results[(i * columns) + 0]);
88
sqlite3_free_table (results);
93
/* all right, it's empty: proceding to initialize */
94
strcpy (sql, "SELECT InitSpatialMetadata()");
95
ret = sqlite3_exec (db, sql, NULL, NULL, &err_msg);
98
fprintf (stderr, "InitSpatialMetadata() error: %s\n", err_msg);
99
sqlite3_free (err_msg);
105
do_import_dbf (char *db_path, char *dbf_path, char *table, char *charset)
107
/* importing some DBF */
111
/* initializing SpatiaLite */
113
/* showing the SQLite version */
114
fprintf (stderr, "SQLite version: %s\n", sqlite3_libversion ());
115
/* showing the SpatiaLite version */
116
fprintf (stderr, "SpatiaLite version: %s\n", spatialite_version ());
117
/* trying to connect the SpatiaLite DB */
119
sqlite3_open_v2 (db_path, &handle,
120
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
121
if (ret != SQLITE_OK)
123
fprintf (stderr, "cannot open '%s': %s\n", db_path,
124
sqlite3_errmsg (handle));
125
sqlite3_close (handle);
128
spatialite_autocreate (handle);
129
if (load_dbf (handle, dbf_path, table, charset, 0, &rows))
130
fprintf (stderr, "Inserted %d rows into '%s' from '%s'\n", rows, table,
133
fprintf (stderr, "Some ERROR occurred\n");
134
/* disconnecting the SpatiaLite DB */
135
ret = sqlite3_close (handle);
136
if (ret != SQLITE_OK)
137
fprintf (stderr, "sqlite3_close() error: %s\n",
138
sqlite3_errmsg (handle));
142
do_import_shp (char *db_path, char *shp_path, char *table, char *charset,
143
int srid, char *column, int coerce2d, int compressed)
145
/* importing some SHP */
149
/* initializing SpatiaLite */
151
/* showing the SQLite version */
152
fprintf (stderr, "SQLite version: %s\n", sqlite3_libversion ());
153
/* showing the SpatiaLite version */
154
fprintf (stderr, "SpatiaLite version: %s\n", spatialite_version ());
155
/* trying to connect the SpatiaLite DB */
157
sqlite3_open_v2 (db_path, &handle,
158
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
159
if (ret != SQLITE_OK)
161
fprintf (stderr, "cannot open '%s': %s\n", db_path,
162
sqlite3_errmsg (handle));
163
sqlite3_close (handle);
166
spatialite_autocreate (handle);
168
(handle, shp_path, table, charset, srid, column, coerce2d, compressed,
170
fprintf (stderr, "Inserted %d rows into '%s' from '%s.shp'\n", rows,
173
fprintf (stderr, "Some ERROR occurred\n");
174
/* disconnecting the SpatiaLite DB */
175
ret = sqlite3_close (handle);
176
if (ret != SQLITE_OK)
177
fprintf (stderr, "sqlite3_close() error: %s\n",
178
sqlite3_errmsg (handle));
182
do_export (char *db_path, char *shp_path, char *table, char *column,
183
char *charset, char *type)
185
/* exporting some SHP */
189
/* initializing SpatiaLite */
191
/* showing the SQLite version */
192
fprintf (stderr, "SQLite version: %s\n", sqlite3_libversion ());
193
/* showing the SpatiaLite version */
194
fprintf (stderr, "SpatiaLite version: %s\n", spatialite_version ());
195
/* trying to connect the SpatiaLite DB */
196
ret = sqlite3_open_v2 (db_path, &handle, SQLITE_OPEN_READONLY, NULL);
197
if (ret != SQLITE_OK)
199
fprintf (stderr, "cannot open '%s': %s\n", db_path,
200
sqlite3_errmsg (handle));
201
sqlite3_close (handle);
205
(handle, table, column, shp_path, charset, type, 0, &rows))
206
fprintf (stderr, "Exported %d rows into '%s.shp' from '%s'\n", rows,
209
fprintf (stderr, "Some ERROR occurred\n");
210
/* disconnecting the SpatiaLite DB */
211
ret = sqlite3_close (handle);
212
if (ret != SQLITE_OK)
213
fprintf (stderr, "sqlite3_close() error: %s\n",
214
sqlite3_errmsg (handle));
220
/* printing the argument list */
221
fprintf (stderr, "\n\nusage: spatitalite_tool CMD ARGLIST\n");
223
"==============================================================\n");
224
fprintf (stderr, "CMD has to be one of the followings:\n");
225
fprintf (stderr, "------------------------------------\n");
227
"-h or --help print this help message\n");
229
"-i or --import import [CSV/TXT, DBF or SHP]\n");
231
"-e or --export-shp exporting some shapefile\n");
232
fprintf (stderr, "\nsupported ARGs are:\n");
233
fprintf (stderr, "-------------------\n");
234
fprintf (stderr, "-dbf or --dbf-path pathname the full DBF path\n");
236
"-shp or --shapefile pathname the shapefile path [NO SUFFIX]\n");
238
"-d or --db-path pathname the SpatiaLite db path\n");
239
fprintf (stderr, "-t or --table table_name the db geotable\n");
240
fprintf (stderr, "-g or --geometry-column col_name the Geometry column\n");
241
fprintf (stderr, "-c or --charset charset_name a charset name\n");
242
fprintf (stderr, "-s or --srid SRID the SRID\n");
244
"--type [POINT | LINESTRING | POLYGON | MULTIPOINT]\n");
245
fprintf (stderr, "\noptional ARGs for SHP import are:\n");
246
fprintf (stderr, "---------------------------------\n");
248
"-2 or --coerce-2d coerce to 2D geoms [x,y]\n");
250
"-k or --compressed apply geometry compression\n");
251
fprintf (stderr, "\nexamples:\n");
252
fprintf (stderr, "---------\n");
254
"spatialite_tool -i -dbf abc.dbf -d db.sqlite -t tbl -c CP1252\n");
256
"spatialite_tool -i -shp abc -d db.sqlite -t tbl -c CP1252 [-s 4326] [-g geom]\n");
258
"spatialite_tool -i -shp abc -d db.sqlite -t tbl -c CP1252 [-s 4326] [-2] [-k]\n");
260
"spatialite_tool -e -shp abc -d db.sqlite -t tbl -g geom -c CP1252 [--type POINT]\n");
264
main (int argc, char *argv[])
266
/* the MAIN function simply perform arguments checking */
268
int next_arg = ARG_NONE;
269
char *shp_path = NULL;
270
char *dbf_path = NULL;
271
char *db_path = NULL;
274
char *charset = NULL;
284
for (i = 1; i < argc; i++)
286
/* parsing the invocation arguments */
287
if (next_arg != ARG_NONE)
310
srid = atoi (argv[i]);
319
if (strcasecmp (argv[i], "--help") == 0
320
|| strcmp (argv[i], "-h") == 0)
325
if (strcasecmp (argv[i], "--shapefile") == 0)
331
if (strcmp (argv[i], "-shp") == 0)
337
if (strcasecmp (argv[i], "--dbf-path") == 0)
343
if (strcmp (argv[i], "-dbf") == 0)
349
if (strcasecmp (argv[i], "--db-path") == 0)
354
if (strcmp (argv[i], "-d") == 0)
359
if (strcasecmp (argv[i], "--table") == 0)
361
next_arg = ARG_TABLE;
364
if (strcmp (argv[i], "-t") == 0)
366
next_arg = ARG_TABLE;
369
if (strcasecmp (argv[i], "--geometry-column") == 0)
374
if (strcmp (argv[i], "-g") == 0)
379
if (strcasecmp (argv[i], "--charset") == 0)
384
if (strcasecmp (argv[i], "-c") == 0)
389
if (strcasecmp (argv[i], "--srid") == 0)
394
if (strcasecmp (argv[i], "-s") == 0)
399
if (strcasecmp (argv[i], "--type") == 0)
404
if (strcasecmp (argv[i], "--import") == 0 ||
405
strcasecmp (argv[i], "-i") == 0)
410
if (strcasecmp (argv[i], "--export-shp") == 0 ||
411
strcasecmp (argv[i], "-e") == 0)
416
if (strcasecmp (argv[i], "--coerce-2d") == 0 ||
417
strcasecmp (argv[i], "-2") == 0)
422
if (strcasecmp (argv[i], "--compressed-geometries") == 0 ||
423
strcasecmp (argv[i], "-k") == 0)
428
fprintf (stderr, "unknown argument: %s\n", argv[i]);
431
if ((import + export) != 1)
433
fprintf (stderr, "undefined CMD\n");
441
/* checking the arguments */
448
"did you forget setting the --db-path argument ?\n");
451
if ((in_shp + in_dbf) != 1)
453
fprintf (stderr, "undefined IMPORT source: SHP or DBF ?\n");
456
if (in_shp && !shp_path)
459
"did you forget setting the --shapefile argument ?\n");
462
if (in_dbf && !dbf_path)
465
"did you forget setting the --dbf-path argument ?\n");
471
"did you forget setting the --table argument ?\n");
477
"did you forget setting the --charset argument ?\n");
487
"did you forget setting the --db-path argument ?\n");
493
"did you forget setting the --shapefile argument ?\n");
499
"did you forget setting the --table argument ?\n");
505
"did you forget setting the --geometry-column argument ?\n");
511
"did you forget setting the --charset argument ?\n");
520
if (import && in_dbf)
521
do_import_dbf (db_path, dbf_path, table, charset);
522
if (import && in_shp)
523
do_import_shp (db_path, shp_path, table, charset, srid, column,
524
coerce2d, compressed);
526
do_export (db_path, shp_path, table, column, charset, type);