File: check_gpkgConvert.c

package info (click to toggle)
spatialite 5.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 74,240 kB
  • sloc: ansic: 587,051; makefile: 8,583; sh: 4,276; yacc: 1,973; xml: 717
file content (311 lines) | stat: -rw-r--r-- 8,517 bytes parent folder | download | duplicates (3)
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
/*

 check_gpkgConvert.c - Test case for GeoPackage Extensions

 Author: Sandro Furieri <a.furieri@lqt.it>

 ------------------------------------------------------------------------------
 
 Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
 The contents of this file are subject to the Mozilla Public License Version
 1.1 (the "License"); you may not use this file except in compliance with
 the License. You may obtain a copy of the License at
 http://www.mozilla.org/MPL/
 
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.

The Original Code is GeoPackage extensions

The Initial Developer of the Original Code is Sandro Furieri
 
Portions created by the Initial Developer are Copyright (C) 2014
the Initial Developer. All Rights Reserved.

Contributor(s):


Alternatively, the contents of this file may be used under the terms of
either the GNU General Public License Version 2 or later (the "GPL"), or
the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
in which case the provisions of the GPL or the LGPL are applicable instead
of those above. If you wish to allow use of your version of this file only
under the terms of either the GPL or the LGPL, and not to allow others to
use your version of this file under the terms of the MPL, indicate your
decision by deleting the provisions above and replace them with the notice
and other provisions required by the GPL or the LGPL. If you do not delete
the provisions above, a recipient may use your version of this file under
the terms of any one of the MPL, the GPL or the LGPL.
 
*/

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

#include <spatialite/gaiaconfig.h>

#ifdef ENABLE_GEOPACKAGE	/* only if GEOPACKAGE is enabled */

#include "sqlite3.h"
#include "spatialite.h"

static void
do_unlink_all ()
{
/* deleting all temporary files */
    unlink ("./copy-gpkg_test.sqlite");
    unlink ("./copy-test-legacy-3.0.1.sqlite");
    unlink ("./out1.gpkg");
    unlink ("./out2.gpkg");
    unlink ("./out1.sqlite");
    unlink ("./out2.sqlite");
}

static int
do_load_legacy (const char *path)
{
/* loading the Legacy Test DB */
    sqlite3 *db_handle;
    int ret;
    void *cache = NULL;
    const char *sql;
    char *sql_err = NULL;

    cache = spatialite_alloc_connection ();
    ret = sqlite3_open_v2 (path, &db_handle, SQLITE_OPEN_READWRITE, NULL);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "cannot open '%s': %s\n", path,
		   sqlite3_errmsg (db_handle));
	  sqlite3_close (db_handle);
	  return 0;
      }
    spatialite_init_ex (db_handle, cache, 0);

/* creating a table */
    sql = "CREATE TABLE test0 (id INTEGER NOT NULL PRIMARY KEY)";
    ret = sqlite3_exec (db_handle, sql, NULL, NULL, &sql_err);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "CREATE TABLE error: %s\n", sql_err);
	  sqlite3_free (sql_err);
	  return 0;
      }

/* adding a geometry */
    sql = "SELECT AddGeometryColumn('test0', 'geom', 4326, 'POINT', 'XY')";
    ret = sqlite3_exec (db_handle, sql, NULL, NULL, &sql_err);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "AddGeometryColumn error: %s\n", sql_err);
	  sqlite3_free (sql_err);
	  return 0;
      }

/* inserting one row */
    sql = "INSERT INTO test0 (id, geom) VALUES(1, MakePoint(1, 2, 4326))";
    ret = sqlite3_exec (db_handle, sql, NULL, NULL, &sql_err);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "INSERT INTO error: %s\n", sql_err);
	  sqlite3_free (sql_err);
	  return 0;
      }

    sqlite3_close (db_handle);
    spatialite_cleanup_ex (cache);
    return 1;
}

static sqlite3 *
connect_db (const char *path, int flags, void *cache)
{
/* attempting to connect a SQLite DB file */
    sqlite3 *db_handle;
    int ret;

    ret = sqlite3_open_v2 (path, &db_handle, flags, NULL);
    if (ret != SQLITE_OK)
      {
	  fprintf (stderr, "cannot open '%s': %s\n", path,
		   sqlite3_errmsg (db_handle));
	  sqlite3_close (db_handle);
	  return NULL;
      }
    spatialite_init_ex (db_handle, cache, 0);
    return db_handle;
}

static int
already_existing (const char *path)
{
/* check if a file already exists */
    FILE *in = fopen (path, "rb");
    if (in != NULL)
      {
	  /* already existing and accessible */
	  fclose (in);
	  return 1;
      }
    if (errno == ENOENT)
	return 0;
    return 1;
}

static int
open_connections (const char *path_origin, const char *path_destination,
		  void *cache_in, void *cache_out, sqlite3 ** xhandle_in,
		  sqlite3 ** xhandle_out)
{
/* establishing IN and OUT DB connections */
    sqlite3 *handle_in = NULL;
    sqlite3 *handle_out = NULL;

    if (already_existing (path_destination))
      {
	  fprintf (stderr, "Already existing output destination:\n\"%s\"\n",
		   path_destination);
	  return 0;
      }

/* attempting to connect the IN DB */
    handle_in = connect_db (path_origin, SQLITE_OPEN_READONLY, cache_in);
    if (handle_in == NULL)
	goto error;

/* attempting to connect the OUT DB */
    handle_out =
	connect_db (path_destination,
		    SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, cache_out);
    if (handle_out == NULL)
	goto error;
    *xhandle_in = handle_in;
    *xhandle_out = handle_out;
    return 1;

  error:
    if (handle_in != NULL)
	sqlite3_close (handle_in);
    if (handle_out != NULL)
	sqlite3_close (handle_out);
    return 0;
}

int
main (int argc, char *argv[])
{
    sqlite3 *handle_in = NULL;
    sqlite3 *handle_out = NULL;
    void *cache_in = NULL;
    void *cache_out = NULL;
    const char *path_origin;
    const char *path_destination;
    int ret;
    if (argc > 1 || argv[0] == NULL)
	argc = 1;		/* silencing stupid compiler warnings */

#ifdef _WIN32
/* this test is disabled on Windows because it takes too long */
#else
    do_unlink_all ();

/* converting from SpatiaLite v4 to GPKG */
    ret = system ("cp ./gpkg_test.sqlite copy-gpkg_test.sqlite");
    if (ret != 0)
      {
	  fprintf (stderr, "cannot copy gpkg_test.sqlite database\n");
	  return -1;
      }
    path_origin = "./copy-gpkg_test.sqlite";
    path_destination = "./out1.gpkg";
    cache_in = spatialite_alloc_connection ();
    cache_out = spatialite_alloc_connection ();
    if (!open_connections
	(path_origin, path_destination, cache_in, cache_out, &handle_in,
	 &handle_out))
      {
	  do_unlink_all ();
	  return -1;
      }
    if (!gaiaSpatialite2GPKG
	(handle_in, path_origin, handle_out, path_destination))
      {
	  do_unlink_all ();
	  return -1;
      }
    sqlite3_close (handle_in);
    sqlite3_close (handle_out);
    spatialite_cleanup_ex (cache_in);
    spatialite_cleanup_ex (cache_out);

/* converting from GPKG to SpatiaLite */
    path_origin = "./out1.gpkg";
    path_destination = "./out1.sqlite";
    cache_in = spatialite_alloc_connection ();
    cache_out = spatialite_alloc_connection ();
    if (!open_connections
	(path_origin, path_destination, cache_in, cache_out, &handle_in,
	 &handle_out))
      {
	  do_unlink_all ();
	  return -1;
      }
    if (!gaiaGPKG2Spatialite
	(handle_in, path_origin, handle_out, path_destination))
      {
	  do_unlink_all ();
	  return -1;
      }
    sqlite3_close (handle_in);
    sqlite3_close (handle_out);
    spatialite_cleanup_ex (cache_in);
    spatialite_cleanup_ex (cache_out);

/* converting from SpatiaLite v3 to GPKG */
    ret =
	system ("cp ./test-legacy-3.0.1.sqlite copy-test-legacy-3.0.1.sqlite");
    if (ret != 0)
      {
	  do_unlink_all ();
	  fprintf (stderr, "cannot copy test-legacy-3.0.1.sqlite database\n");
	  return -1;
      }
    if (!do_load_legacy ("./copy-test-legacy-3.0.1.sqlite"))
      {
	  do_unlink_all ();
	  return -1;
      }
    path_origin = "./copy-test-legacy-3.0.1.sqlite";
    path_destination = "./out2.sqlite";
    cache_in = spatialite_alloc_connection ();
    cache_out = spatialite_alloc_connection ();
    if (!open_connections
	(path_origin, path_destination, cache_in, cache_out, &handle_in,
	 &handle_out))
      {
	  do_unlink_all ();
	  return -1;
      }
    if (!gaiaSpatialite2GPKG
	(handle_in, path_origin, handle_out, path_destination))
      {
	  do_unlink_all ();
	  return -1;
      }
    sqlite3_close (handle_in);
    sqlite3_close (handle_out);
    spatialite_cleanup_ex (cache_in);
    spatialite_cleanup_ex (cache_out);

    do_unlink_all ();
#endif /* not WIN32 */
    return 0;
}

#endif /* endif GEOPACKAGE enabled */