~ubuntu-branches/ubuntu/precise/spatialite/precise

« back to all changes in this revision

Viewing changes to spatialite-tools/spatialite_tool.c

  • Committer: Package Import Robot
  • Author(s): David Paleino, Francesco Paolo Lovergine, David Paleino
  • Date: 2011-11-21 12:10:43 UTC
  • mfrom: (4.1.3 sid)
  • Revision ID: package-import@ubuntu.com-20111121121043-0g14o2uf0r343a82
Tags: 3.0.0~beta20110817-3
[ Francesco Paolo Lovergine ]
* Fixed linking order for sqlite3 in debian patch 00-systemlibs.patch.
  (closes: #638929)

[ David Paleino ]
* Conditionally disable full EPSG initialization (for srs_init.c)
  on powerpc, and document what projections are available on that
  architecture (Closes: #649302)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
/ spatialite_tool
 
3
/
 
4
/ an utility CLI tool for Shapefile import / export
 
5
/
 
6
/ version 1.0, 2008 Decmber 11
 
7
/
 
8
/ Author: Sandro Furieri a.furieri@lqt.it
 
9
/
 
10
/ Copyright (C) 2008  Alessandro Furieri
 
11
/
 
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.
 
16
/
 
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.
 
21
/
 
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/>.
 
24
/
 
25
*/
 
26
 
 
27
#if defined(_WIN32) && !defined(__MINGW32__)
 
28
/* MSVC strictly requires this include [off_t] */
 
29
#include <sys/types.h>
 
30
#endif
 
31
 
 
32
#include <stdlib.h>
 
33
#include <stdio.h>
 
34
#include <string.h>
 
35
#include <errno.h>
 
36
 
 
37
#ifdef SPATIALITE_AMALGAMATION
 
38
#include <spatialite/sqlite3.h>
 
39
#else
 
40
#include <sqlite3.h>
 
41
#endif
 
42
 
 
43
#include <spatialite/gaiaaux.h>
 
44
#include <spatialite/gaiageo.h>
 
45
#include <spatialite.h>
 
46
 
 
47
#if defined(_WIN32) && !defined(__MINGW32__)
 
48
#define strcasecmp      _stricmp
 
49
#endif /* not WIN32 */
 
50
 
 
51
#define ARG_NONE                0
 
52
#define ARG_CMD                 1
 
53
#define ARG_SHP                 2
 
54
#define ARG_TXT                 3
 
55
#define ARG_DBF                 4
 
56
#define ARG_DB                  5
 
57
#define ARG_TABLE               6
 
58
#define ARG_COL                 7
 
59
#define ARG_CS                  8
 
60
#define ARG_SRID                9
 
61
#define ARG_TYPE                10
 
62
 
 
63
static void
 
64
spatialite_autocreate (sqlite3 * db)
 
65
{
 
66
/* attempting to perform self-initialization for a newly created DB */
 
67
    int ret;
 
68
    char sql[1024];
 
69
    char *err_msg = NULL;
 
70
    int count;
 
71
    int i;
 
72
    char **results;
 
73
    int rows;
 
74
    int columns;
 
75
 
 
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);
 
79
    if (ret != SQLITE_OK)
 
80
        return;
 
81
    if (rows < 1)
 
82
        ;
 
83
    else
 
84
      {
 
85
          for (i = 1; i <= rows; i++)
 
86
              count = atoi (results[(i * columns) + 0]);
 
87
      }
 
88
    sqlite3_free_table (results);
 
89
 
 
90
    if (count > 0)
 
91
        return;
 
92
 
 
93
/* all right, it's empty: proceding to initialize */
 
94
    strcpy (sql, "SELECT InitSpatialMetadata()");
 
95
    ret = sqlite3_exec (db, sql, NULL, NULL, &err_msg);
 
96
    if (ret != SQLITE_OK)
 
97
      {
 
98
          fprintf (stderr, "InitSpatialMetadata() error: %s\n", err_msg);
 
99
          sqlite3_free (err_msg);
 
100
          return;
 
101
      }
 
102
}
 
103
 
 
104
static void
 
105
do_import_dbf (char *db_path, char *dbf_path, char *table, char *charset)
 
106
{
 
107
/* importing some DBF */
 
108
    int ret;
 
109
    int rows;
 
110
    sqlite3 *handle;
 
111
/* initializing SpatiaLite */
 
112
    spatialite_init (0);
 
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  */
 
118
    ret =
 
119
        sqlite3_open_v2 (db_path, &handle,
 
120
                         SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
 
121
    if (ret != SQLITE_OK)
 
122
      {
 
123
          fprintf (stderr, "cannot open '%s': %s\n", db_path,
 
124
                   sqlite3_errmsg (handle));
 
125
          sqlite3_close (handle);
 
126
          return;
 
127
      }
 
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,
 
131
                 dbf_path);
 
132
    else
 
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));
 
139
}
 
140
 
 
141
static void
 
142
do_import_shp (char *db_path, char *shp_path, char *table, char *charset,
 
143
               int srid, char *column, int coerce2d, int compressed)
 
144
{
 
145
/* importing some SHP */
 
146
    int ret;
 
147
    int rows;
 
148
    sqlite3 *handle;
 
149
/* initializing SpatiaLite */
 
150
    spatialite_init (0);
 
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  */
 
156
    ret =
 
157
        sqlite3_open_v2 (db_path, &handle,
 
158
                         SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
 
159
    if (ret != SQLITE_OK)
 
160
      {
 
161
          fprintf (stderr, "cannot open '%s': %s\n", db_path,
 
162
                   sqlite3_errmsg (handle));
 
163
          sqlite3_close (handle);
 
164
          return;
 
165
      }
 
166
    spatialite_autocreate (handle);
 
167
    if (load_shapefile
 
168
        (handle, shp_path, table, charset, srid, column, coerce2d, compressed,
 
169
         0, &rows))
 
170
        fprintf (stderr, "Inserted %d rows into '%s' from '%s.shp'\n", rows,
 
171
                 table, shp_path);
 
172
    else
 
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));
 
179
}
 
180
 
 
181
static void
 
182
do_export (char *db_path, char *shp_path, char *table, char *column,
 
183
           char *charset, char *type)
 
184
{
 
185
/* exporting some SHP */
 
186
    int ret;
 
187
    int rows;
 
188
    sqlite3 *handle;
 
189
/* initializing SpatiaLite */
 
190
    spatialite_init (0);
 
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)
 
198
      {
 
199
          fprintf (stderr, "cannot open '%s': %s\n", db_path,
 
200
                   sqlite3_errmsg (handle));
 
201
          sqlite3_close (handle);
 
202
          return;
 
203
      }
 
204
    if (dump_shapefile
 
205
        (handle, table, column, shp_path, charset, type, 0, &rows))
 
206
        fprintf (stderr, "Exported %d rows into '%s.shp' from '%s'\n", rows,
 
207
                 shp_path, table);
 
208
    else
 
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));
 
215
}
 
216
 
 
217
static void
 
218
do_help ()
 
219
{
 
220
/* printing the argument list */
 
221
    fprintf (stderr, "\n\nusage: spatitalite_tool CMD ARGLIST\n");
 
222
    fprintf (stderr,
 
223
             "==============================================================\n");
 
224
    fprintf (stderr, "CMD has to be one of the followings:\n");
 
225
    fprintf (stderr, "------------------------------------\n");
 
226
    fprintf (stderr,
 
227
             "-h or --help                      print this help message\n");
 
228
    fprintf (stderr,
 
229
             "-i or --import                    import [CSV/TXT, DBF or SHP]\n");
 
230
    fprintf (stderr,
 
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");
 
235
    fprintf (stderr,
 
236
             "-shp or --shapefile pathname      the shapefile path [NO SUFFIX]\n");
 
237
    fprintf (stderr,
 
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");
 
243
    fprintf (stderr,
 
244
             "--type         [POINT | LINESTRING | POLYGON | MULTIPOINT]\n");
 
245
    fprintf (stderr, "\noptional ARGs for SHP import are:\n");
 
246
    fprintf (stderr, "---------------------------------\n");
 
247
    fprintf (stderr,
 
248
             "-2 or --coerce-2d                  coerce to 2D geoms [x,y]\n");
 
249
    fprintf (stderr,
 
250
             "-k or --compressed                 apply geometry compression\n");
 
251
    fprintf (stderr, "\nexamples:\n");
 
252
    fprintf (stderr, "---------\n");
 
253
    fprintf (stderr,
 
254
             "spatialite_tool -i -dbf abc.dbf -d db.sqlite -t tbl -c CP1252\n");
 
255
    fprintf (stderr,
 
256
             "spatialite_tool -i -shp abc -d db.sqlite -t tbl -c CP1252 [-s 4326] [-g geom]\n");
 
257
    fprintf (stderr,
 
258
             "spatialite_tool -i -shp abc -d db.sqlite -t tbl -c CP1252 [-s 4326] [-2] [-k]\n");
 
259
    fprintf (stderr,
 
260
             "spatialite_tool -e -shp abc -d db.sqlite -t tbl -g geom -c CP1252 [--type POINT]\n");
 
261
}
 
262
 
 
263
int
 
264
main (int argc, char *argv[])
 
265
{
 
266
/* the MAIN function simply perform arguments checking */
 
267
    int i;
 
268
    int next_arg = ARG_NONE;
 
269
    char *shp_path = NULL;
 
270
    char *dbf_path = NULL;
 
271
    char *db_path = NULL;
 
272
    char *table = NULL;
 
273
    char *column = NULL;
 
274
    char *charset = NULL;
 
275
    char *type = NULL;
 
276
    int srid = -1;
 
277
    int import = 0;
 
278
    int export = 0;
 
279
    int in_shp = 0;
 
280
    int in_dbf = 0;
 
281
    int coerce2d = 0;
 
282
    int compressed = 0;
 
283
    int error = 0;
 
284
    for (i = 1; i < argc; i++)
 
285
      {
 
286
          /* parsing the invocation arguments */
 
287
          if (next_arg != ARG_NONE)
 
288
            {
 
289
                switch (next_arg)
 
290
                  {
 
291
                  case ARG_SHP:
 
292
                      shp_path = argv[i];
 
293
                      break;
 
294
                  case ARG_DBF:
 
295
                      dbf_path = argv[i];
 
296
                      break;
 
297
                  case ARG_DB:
 
298
                      db_path = argv[i];
 
299
                      break;
 
300
                  case ARG_TABLE:
 
301
                      table = argv[i];
 
302
                      break;
 
303
                  case ARG_COL:
 
304
                      column = argv[i];
 
305
                      break;
 
306
                  case ARG_CS:
 
307
                      charset = argv[i];
 
308
                      break;
 
309
                  case ARG_SRID:
 
310
                      srid = atoi (argv[i]);
 
311
                      break;
 
312
                  case ARG_TYPE:
 
313
                      type = argv[i];
 
314
                      break;
 
315
                  };
 
316
                next_arg = ARG_NONE;
 
317
                continue;
 
318
            }
 
319
          if (strcasecmp (argv[i], "--help") == 0
 
320
              || strcmp (argv[i], "-h") == 0)
 
321
            {
 
322
                do_help ();
 
323
                return -1;
 
324
            }
 
325
          if (strcasecmp (argv[i], "--shapefile") == 0)
 
326
            {
 
327
                next_arg = ARG_SHP;
 
328
                in_shp = 1;
 
329
                continue;
 
330
            }
 
331
          if (strcmp (argv[i], "-shp") == 0)
 
332
            {
 
333
                next_arg = ARG_SHP;
 
334
                in_shp = 1;
 
335
                continue;
 
336
            }
 
337
          if (strcasecmp (argv[i], "--dbf-path") == 0)
 
338
            {
 
339
                next_arg = ARG_DBF;
 
340
                in_dbf = 1;
 
341
                continue;
 
342
            }
 
343
          if (strcmp (argv[i], "-dbf") == 0)
 
344
            {
 
345
                next_arg = ARG_DBF;
 
346
                in_dbf = 1;
 
347
                continue;
 
348
            }
 
349
          if (strcasecmp (argv[i], "--db-path") == 0)
 
350
            {
 
351
                next_arg = ARG_DB;
 
352
                continue;
 
353
            }
 
354
          if (strcmp (argv[i], "-d") == 0)
 
355
            {
 
356
                next_arg = ARG_DB;
 
357
                continue;
 
358
            }
 
359
          if (strcasecmp (argv[i], "--table") == 0)
 
360
            {
 
361
                next_arg = ARG_TABLE;
 
362
                continue;
 
363
            }
 
364
          if (strcmp (argv[i], "-t") == 0)
 
365
            {
 
366
                next_arg = ARG_TABLE;
 
367
                continue;
 
368
            }
 
369
          if (strcasecmp (argv[i], "--geometry-column") == 0)
 
370
            {
 
371
                next_arg = ARG_COL;
 
372
                continue;
 
373
            }
 
374
          if (strcmp (argv[i], "-g") == 0)
 
375
            {
 
376
                next_arg = ARG_COL;
 
377
                continue;
 
378
            }
 
379
          if (strcasecmp (argv[i], "--charset") == 0)
 
380
            {
 
381
                next_arg = ARG_CS;
 
382
                continue;
 
383
            }
 
384
          if (strcasecmp (argv[i], "-c") == 0)
 
385
            {
 
386
                next_arg = ARG_CS;
 
387
                continue;
 
388
            }
 
389
          if (strcasecmp (argv[i], "--srid") == 0)
 
390
            {
 
391
                next_arg = ARG_SRID;
 
392
                continue;
 
393
            }
 
394
          if (strcasecmp (argv[i], "-s") == 0)
 
395
            {
 
396
                next_arg = ARG_SRID;
 
397
                continue;
 
398
            }
 
399
          if (strcasecmp (argv[i], "--type") == 0)
 
400
            {
 
401
                next_arg = ARG_TYPE;
 
402
                continue;
 
403
            }
 
404
          if (strcasecmp (argv[i], "--import") == 0 ||
 
405
              strcasecmp (argv[i], "-i") == 0)
 
406
            {
 
407
                import = 1;
 
408
                continue;
 
409
            }
 
410
          if (strcasecmp (argv[i], "--export-shp") == 0 ||
 
411
              strcasecmp (argv[i], "-e") == 0)
 
412
            {
 
413
                export = 1;
 
414
                continue;
 
415
            }
 
416
          if (strcasecmp (argv[i], "--coerce-2d") == 0 ||
 
417
              strcasecmp (argv[i], "-2") == 0)
 
418
            {
 
419
                coerce2d = 1;
 
420
                continue;
 
421
            }
 
422
          if (strcasecmp (argv[i], "--compressed-geometries") == 0 ||
 
423
              strcasecmp (argv[i], "-k") == 0)
 
424
            {
 
425
                coerce2d = 1;
 
426
                continue;
 
427
            }
 
428
          fprintf (stderr, "unknown argument: %s\n", argv[i]);
 
429
          error = 1;
 
430
      }
 
431
    if ((import + export) != 1)
 
432
      {
 
433
          fprintf (stderr, "undefined CMD\n");
 
434
          error = 1;
 
435
      }
 
436
    if (error)
 
437
      {
 
438
          do_help ();
 
439
          return -1;
 
440
      }
 
441
/* checking the arguments */
 
442
    if (import)
 
443
      {
 
444
          /* import SHP */
 
445
          if (!db_path)
 
446
            {
 
447
                fprintf (stderr,
 
448
                         "did you forget setting the --db-path argument ?\n");
 
449
                error = 1;
 
450
            }
 
451
          if ((in_shp + in_dbf) != 1)
 
452
            {
 
453
                fprintf (stderr, "undefined IMPORT source: SHP or DBF ?\n");
 
454
                error = 1;
 
455
            }
 
456
          if (in_shp && !shp_path)
 
457
            {
 
458
                fprintf (stderr,
 
459
                         "did you forget setting the --shapefile argument ?\n");
 
460
                error = 1;
 
461
            }
 
462
          if (in_dbf && !dbf_path)
 
463
            {
 
464
                fprintf (stderr,
 
465
                         "did you forget setting the --dbf-path argument ?\n");
 
466
                error = 1;
 
467
            }
 
468
          if (!table)
 
469
            {
 
470
                fprintf (stderr,
 
471
                         "did you forget setting the --table argument ?\n");
 
472
                error = 1;
 
473
            }
 
474
          if (!charset)
 
475
            {
 
476
                fprintf (stderr,
 
477
                         "did you forget setting the --charset argument ?\n");
 
478
                error = 1;
 
479
            }
 
480
      }
 
481
    if (export)
 
482
      {
 
483
          /* export SHP */
 
484
          if (!db_path)
 
485
            {
 
486
                fprintf (stderr,
 
487
                         "did you forget setting the --db-path argument ?\n");
 
488
                error = 1;
 
489
            }
 
490
          if (!shp_path)
 
491
            {
 
492
                fprintf (stderr,
 
493
                         "did you forget setting the --shapefile argument ?\n");
 
494
                error = 1;
 
495
            }
 
496
          if (!table)
 
497
            {
 
498
                fprintf (stderr,
 
499
                         "did you forget setting the --table argument ?\n");
 
500
                error = 1;
 
501
            }
 
502
          if (!column)
 
503
            {
 
504
                fprintf (stderr,
 
505
                         "did you forget setting the --geometry-column argument ?\n");
 
506
                error = 1;
 
507
            }
 
508
          if (!charset)
 
509
            {
 
510
                fprintf (stderr,
 
511
                         "did you forget setting the --charset argument ?\n");
 
512
                error = 1;
 
513
            }
 
514
      }
 
515
    if (error)
 
516
      {
 
517
          do_help ();
 
518
          return -1;
 
519
      }
 
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);
 
525
    if (export)
 
526
        do_export (db_path, shp_path, table, column, charset, type);
 
527
    return 0;
 
528
}