File: spatialite_tool.c

package info (click to toggle)
spatialite-tools 4.1.1-4
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,752 kB
  • ctags: 2,040
  • sloc: ansic: 25,564; sh: 10,934; xml: 1,354; makefile: 82
file content (543 lines) | stat: -rw-r--r-- 14,018 bytes parent folder | download
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/* 
/ spatialite_tool
/
/ an utility CLI tool for Shapefile import / export
/
/ version 1.0, 2008 Decmber 11
/
/ Author: Sandro Furieri a.furieri@lqt.it
/
/ Copyright (C) 2008  Alessandro Furieri
/
/    This program is free software: you can redistribute it and/or modify
/    it under the terms of the GNU General Public License as published by
/    the Free Software Foundation, either version 3 of the License, or
/    (at your option) any later version.
/
/    This program is distributed in the hope that it will be useful,
/    but WITHOUT ANY WARRANTY; without even the implied warranty of
/    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
/    GNU General Public License for more details.
/
/    You should have received a copy of the GNU General Public License
/    along with this program.  If not, see <http://www.gnu.org/licenses/>.
/
*/

#if defined(_WIN32) && !defined(__MINGW32__)
/* MSVC strictly requires this include [off_t] */
#include <sys/types.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#if defined(_WIN32) && !defined(__MINGW32__)
#include "config-msvc.h"
#else
#include "config.h"
#endif

#ifdef SPATIALITE_AMALGAMATION
#include <spatialite/sqlite3.h>
#else
#include <sqlite3.h>
#endif

#include <spatialite/gaiaaux.h>
#include <spatialite/gaiageo.h>
#include <spatialite.h>

#if defined(_WIN32) && !defined(__MINGW32__)
#define strcasecmp	_stricmp
#endif /* not WIN32 */

#define ARG_NONE		0
#define ARG_CMD			1
#define ARG_SHP			2
#define ARG_TXT			3
#define ARG_DBF			4
#define ARG_DB			5
#define ARG_TABLE		6
#define ARG_COL			7
#define ARG_CS			8
#define ARG_SRID		9
#define ARG_TYPE		10

static void
spatialite_autocreate (sqlite3 * db)
{
/* attempting to perform self-initialization for a newly created DB */
    int ret;
    char sql[1024];
    char *err_msg = NULL;
    int count;
    int i;
    char **results;
    int rows;
    int columns;

/* checking if this DB is really empty */
    strcpy (sql, "SELECT Count(*) from sqlite_master");
    ret = sqlite3_get_table (db, sql, &results, &rows, &columns, NULL);
    if (ret != SQLITE_OK)
	return;
    if (rows < 1)
	;
    else
      {
	  for (i = 1; i <= rows; i++)
	      count = atoi (results[(i * columns) + 0]);
      }
    sqlite3_free_table (results);

    if (count > 0)
	return;

/* all right, it's empty: proceding to initialize */
    strcpy (sql, "SELECT InitSpatialMetadata(1)");
    ret = sqlite3_exec (db, sql, NULL, NULL, &err_msg);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "InitSpatialMetadata() error: %s\n", err_msg);
	  sqlite3_free (err_msg);
	  return;
      }
}

static void
do_import_dbf (char *db_path, char *dbf_path, char *table, char *charset)
{
/* importing some DBF */
    int ret;
    int rows;
    sqlite3 *handle;
    void *cache;

/* showing the SQLite version */
    fprintf (stderr, "SQLite version: %s\n", sqlite3_libversion ());
/* showing the SpatiaLite version */
    fprintf (stderr, "SpatiaLite version: %s\n", spatialite_version ());
/* trying to connect the SpatiaLite DB  */
    ret =
	sqlite3_open_v2 (db_path, &handle,
			 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "cannot open '%s': %s\n", db_path,
		   sqlite3_errmsg (handle));
	  sqlite3_close (handle);
	  return;
      }
    cache = spatialite_alloc_connection ();
    spatialite_init_ex (handle, cache, 0);
    spatialite_autocreate (handle);
    if (load_dbf (handle, dbf_path, table, charset, 0, &rows, NULL))
	fprintf (stderr, "Inserted %d rows into '%s' from '%s'\n", rows, table,
		 dbf_path);
    else
	fprintf (stderr, "Some ERROR occurred\n");
/* disconnecting the SpatiaLite DB */
    ret = sqlite3_close (handle);
    if (ret != SQLITE_OK)
	fprintf (stderr, "sqlite3_close() error: %s\n",
		 sqlite3_errmsg (handle));
    spatialite_cleanup_ex (cache);
}

static void
do_import_shp (char *db_path, char *shp_path, char *table, char *charset,
	       int srid, char *column, int coerce2d, int compressed)
{
/* importing some SHP */
    int ret;
    int rows;
    sqlite3 *handle;
    void *cache;

/* showing the SQLite version */
    fprintf (stderr, "SQLite version: %s\n", sqlite3_libversion ());
/* showing the SpatiaLite version */
    fprintf (stderr, "SpatiaLite version: %s\n", spatialite_version ());
/* trying to connect the SpatiaLite DB  */
    ret =
	sqlite3_open_v2 (db_path, &handle,
			 SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "cannot open '%s': %s\n", db_path,
		   sqlite3_errmsg (handle));
	  sqlite3_close (handle);
	  return;
      }
    cache = spatialite_alloc_connection ();
    spatialite_init_ex (handle, cache, 0);
    spatialite_autocreate (handle);
    if (load_shapefile
	(handle, shp_path, table, charset, srid, column, coerce2d, compressed,
	 0, 0, &rows, NULL))
	fprintf (stderr, "Inserted %d rows into '%s' from '%s.shp'\n", rows,
		 table, shp_path);
    else
	fprintf (stderr, "Some ERROR occurred\n");
/* disconnecting the SpatiaLite DB */
    ret = sqlite3_close (handle);
    if (ret != SQLITE_OK)
	fprintf (stderr, "sqlite3_close() error: %s\n",
		 sqlite3_errmsg (handle));
    spatialite_cleanup_ex (cache);
}

static void
do_export (char *db_path, char *shp_path, char *table, char *column,
	   char *charset, char *type)
{
/* exporting some SHP */
    int ret;
    int rows;
    sqlite3 *handle;
    void *cache;

/* showing the SQLite version */
    fprintf (stderr, "SQLite version: %s\n", sqlite3_libversion ());
/* showing the SpatiaLite version */
    fprintf (stderr, "SpatiaLite version: %s\n", spatialite_version ());
/* trying to connect the SpatiaLite DB  */
    ret = sqlite3_open_v2 (db_path, &handle, SQLITE_OPEN_READONLY, NULL);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "cannot open '%s': %s\n", db_path,
		   sqlite3_errmsg (handle));
	  sqlite3_close (handle);
	  return;
      }
    cache = spatialite_alloc_connection ();
    spatialite_init_ex (handle, cache, 0);
    if (dump_shapefile
	(handle, table, column, shp_path, charset, type, 0, &rows, NULL))
	fprintf (stderr, "Exported %d rows into '%s.shp' from '%s'\n", rows,
		 shp_path, table);
    else
	fprintf (stderr, "Some ERROR occurred\n");
/* disconnecting the SpatiaLite DB */
    ret = sqlite3_close (handle);
    if (ret != SQLITE_OK)
	fprintf (stderr, "sqlite3_close() error: %s\n",
		 sqlite3_errmsg (handle));
    spatialite_cleanup_ex (cache);
}

static void
do_help ()
{
/* printing the argument list */
    fprintf (stderr, "\n\nusage: spatitalite_tool CMD ARGLIST\n");
    fprintf (stderr,
	     "==============================================================\n");
    fprintf (stderr, "CMD has to be one of the followings:\n");
    fprintf (stderr, "------------------------------------\n");
    fprintf (stderr,
	     "-h or --help                      print this help message\n");
    fprintf (stderr,
	     "-i or --import                    import [CSV/TXT, DBF or SHP]\n");
    fprintf (stderr,
	     "-e or --export-shp                exporting some shapefile\n");
    fprintf (stderr, "\nsupported ARGs are:\n");
    fprintf (stderr, "-------------------\n");
    fprintf (stderr, "-dbf or --dbf-path pathname       the full DBF path\n");
    fprintf (stderr,
	     "-shp or --shapefile pathname      the shapefile path [NO SUFFIX]\n");
    fprintf (stderr,
	     "-d or --db-path pathname          the SpatiaLite db path\n");
    fprintf (stderr, "-t or --table table_name          the db geotable\n");
    fprintf (stderr, "-g or --geometry-column col_name  the Geometry column\n");
    fprintf (stderr, "-c or --charset charset_name      a charset name\n");
    fprintf (stderr, "-s or --srid SRID                 the SRID\n");
    fprintf (stderr,
	     "--type         [POINT | LINESTRING | POLYGON | MULTIPOINT]\n");
    fprintf (stderr, "\noptional ARGs for SHP import are:\n");
    fprintf (stderr, "---------------------------------\n");
    fprintf (stderr,
	     "-2 or --coerce-2d                  coerce to 2D geoms [x,y]\n");
    fprintf (stderr,
	     "-k or --compressed                 apply geometry compression\n");
    fprintf (stderr, "\nexamples:\n");
    fprintf (stderr, "---------\n");
    fprintf (stderr,
	     "spatialite_tool -i -dbf abc.dbf -d db.sqlite -t tbl -c CP1252\n");
    fprintf (stderr,
	     "spatialite_tool -i -shp abc -d db.sqlite -t tbl -c CP1252 [-s 4326] [-g geom]\n");
    fprintf (stderr,
	     "spatialite_tool -i -shp abc -d db.sqlite -t tbl -c CP1252 [-s 4326] [-2] [-k]\n");
    fprintf (stderr,
	     "spatialite_tool -e -shp abc -d db.sqlite -t tbl -g geom -c CP1252 [--type POINT]\n");
}

int
main (int argc, char *argv[])
{
/* the MAIN function simply perform arguments checking */
    int i;
    int next_arg = ARG_NONE;
    char *shp_path = NULL;
    char *dbf_path = NULL;
    char *db_path = NULL;
    char *table = NULL;
    char *column = NULL;
    char *charset = NULL;
    char *type = NULL;
    int srid = -1;
    int import = 0;
    int export = 0;
    int in_shp = 0;
    int in_dbf = 0;
    int coerce2d = 0;
    int compressed = 0;
    int error = 0;
    for (i = 1; i < argc; i++)
      {
	  /* parsing the invocation arguments */
	  if (next_arg != ARG_NONE)
	    {
		switch (next_arg)
		  {
		  case ARG_SHP:
		      shp_path = argv[i];
		      break;
		  case ARG_DBF:
		      dbf_path = argv[i];
		      break;
		  case ARG_DB:
		      db_path = argv[i];
		      break;
		  case ARG_TABLE:
		      table = argv[i];
		      break;
		  case ARG_COL:
		      column = argv[i];
		      break;
		  case ARG_CS:
		      charset = argv[i];
		      break;
		  case ARG_SRID:
		      srid = atoi (argv[i]);
		      break;
		  case ARG_TYPE:
		      type = argv[i];
		      break;
		  };
		next_arg = ARG_NONE;
		continue;
	    }
	  if (strcasecmp (argv[i], "--help") == 0
	      || strcmp (argv[i], "-h") == 0)
	    {
		do_help ();
		return -1;
	    }
	  if (strcasecmp (argv[i], "--shapefile") == 0)
	    {
		next_arg = ARG_SHP;
		in_shp = 1;
		continue;
	    }
	  if (strcmp (argv[i], "-shp") == 0)
	    {
		next_arg = ARG_SHP;
		in_shp = 1;
		continue;
	    }
	  if (strcasecmp (argv[i], "--dbf-path") == 0)
	    {
		next_arg = ARG_DBF;
		in_dbf = 1;
		continue;
	    }
	  if (strcmp (argv[i], "-dbf") == 0)
	    {
		next_arg = ARG_DBF;
		in_dbf = 1;
		continue;
	    }
	  if (strcasecmp (argv[i], "--db-path") == 0)
	    {
		next_arg = ARG_DB;
		continue;
	    }
	  if (strcmp (argv[i], "-d") == 0)
	    {
		next_arg = ARG_DB;
		continue;
	    }
	  if (strcasecmp (argv[i], "--table") == 0)
	    {
		next_arg = ARG_TABLE;
		continue;
	    }
	  if (strcmp (argv[i], "-t") == 0)
	    {
		next_arg = ARG_TABLE;
		continue;
	    }
	  if (strcasecmp (argv[i], "--geometry-column") == 0)
	    {
		next_arg = ARG_COL;
		continue;
	    }
	  if (strcmp (argv[i], "-g") == 0)
	    {
		next_arg = ARG_COL;
		continue;
	    }
	  if (strcasecmp (argv[i], "--charset") == 0)
	    {
		next_arg = ARG_CS;
		continue;
	    }
	  if (strcasecmp (argv[i], "-c") == 0)
	    {
		next_arg = ARG_CS;
		continue;
	    }
	  if (strcasecmp (argv[i], "--srid") == 0)
	    {
		next_arg = ARG_SRID;
		continue;
	    }
	  if (strcasecmp (argv[i], "-s") == 0)
	    {
		next_arg = ARG_SRID;
		continue;
	    }
	  if (strcasecmp (argv[i], "--type") == 0)
	    {
		next_arg = ARG_TYPE;
		continue;
	    }
	  if (strcasecmp (argv[i], "--import") == 0 ||
	      strcasecmp (argv[i], "-i") == 0)
	    {
		import = 1;
		continue;
	    }
	  if (strcasecmp (argv[i], "--export-shp") == 0 ||
	      strcasecmp (argv[i], "-e") == 0)
	    {
		export = 1;
		continue;
	    }
	  if (strcasecmp (argv[i], "--coerce-2d") == 0 ||
	      strcasecmp (argv[i], "-2") == 0)
	    {
		coerce2d = 1;
		continue;
	    }
	  if (strcasecmp (argv[i], "--compressed-geometries") == 0 ||
	      strcasecmp (argv[i], "-k") == 0)
	    {
		coerce2d = 1;
		continue;
	    }
	  fprintf (stderr, "unknown argument: %s\n", argv[i]);
	  error = 1;
      }
    if ((import + export) != 1)
      {
	  fprintf (stderr, "undefined CMD\n");
	  error = 1;
      }
    if (error)
      {
	  do_help ();
	  return -1;
      }
/* checking the arguments */
    if (import)
      {
	  /* import SHP */
	  if (!db_path)
	    {
		fprintf (stderr,
			 "did you forget setting the --db-path argument ?\n");
		error = 1;
	    }
	  if ((in_shp + in_dbf) != 1)
	    {
		fprintf (stderr, "undefined IMPORT source: SHP or DBF ?\n");
		error = 1;
	    }
	  if (in_shp && !shp_path)
	    {
		fprintf (stderr,
			 "did you forget setting the --shapefile argument ?\n");
		error = 1;
	    }
	  if (in_dbf && !dbf_path)
	    {
		fprintf (stderr,
			 "did you forget setting the --dbf-path argument ?\n");
		error = 1;
	    }
	  if (!table)
	    {
		fprintf (stderr,
			 "did you forget setting the --table argument ?\n");
		error = 1;
	    }
	  if (!charset)
	    {
		fprintf (stderr,
			 "did you forget setting the --charset argument ?\n");
		error = 1;
	    }
      }
    if (export)
      {
	  /* export SHP */
	  if (!db_path)
	    {
		fprintf (stderr,
			 "did you forget setting the --db-path argument ?\n");
		error = 1;
	    }
	  if (!shp_path)
	    {
		fprintf (stderr,
			 "did you forget setting the --shapefile argument ?\n");
		error = 1;
	    }
	  if (!table)
	    {
		fprintf (stderr,
			 "did you forget setting the --table argument ?\n");
		error = 1;
	    }
	  if (!column)
	    {
		fprintf (stderr,
			 "did you forget setting the --geometry-column argument ?\n");
		error = 1;
	    }
	  if (!charset)
	    {
		fprintf (stderr,
			 "did you forget setting the --charset argument ?\n");
		error = 1;
	    }
      }
    if (error)
      {
	  do_help ();
	  return -1;
      }
    if (import && in_dbf)
	do_import_dbf (db_path, dbf_path, table, charset);
    if (import && in_shp)
	do_import_shp (db_path, shp_path, table, charset, srid, column,
		       coerce2d, compressed);
    if (export)
	do_export (db_path, shp_path, table, column, charset, type);
    return 0;
}