3
check_point_to_tile.c - Test case for GeoPackage Extensions
5
Author: Brad Hards <bradh@frogmouth.net>
7
------------------------------------------------------------------------------
9
Version: MPL 1.1/GPL 2.0/LGPL 2.1
11
The contents of this file are subject to the Mozilla Public License Version
12
1.1 (the "License"); you may not use this file except in compliance with
13
the License. You may obtain a copy of the License at
14
http://www.mozilla.org/MPL/
16
Software distributed under the License is distributed on an "AS IS" basis,
17
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
18
for the specific language governing rights and limitations under the
21
The Original Code is GeoPackage extensions
23
The Initial Developer of the Original Code is Brad Hards
25
Portions created by the Initial Developer are Copyright (C) 2011
26
the Initial Developer. All Rights Reserved.
31
Alternatively, the contents of this file may be used under the terms of
32
either the GNU General Public License Version 2 or later (the "GPL"), or
33
the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
34
in which case the provisions of the GPL or the LGPL are applicable instead
35
of those above. If you wish to allow use of your version of this file only
36
under the terms of either the GPL or the LGPL, and not to allow others to
37
use your version of this file under the terms of the MPL, indicate your
38
decision by deleting the provisions above and replace them with the notice
39
and other provisions required by the GPL or the LGPL. If you do not delete
40
the provisions above, a recipient may use your version of this file under
41
the terms of any one of the MPL, the GPL or the LGPL.
51
#include <spatialite.h>
53
#include "test_helpers.h"
55
int main (int argc UNUSED, char *argv[] UNUSED)
57
sqlite3 *db_handle = NULL;
65
void *cache = spatialite_alloc_connection();
66
char *old_SPATIALITE_SECURITY_ENV = NULL;
69
#endif /* not WIN32 */
71
old_SPATIALITE_SECURITY_ENV = getenv("SPATIALITE_SECURITY");
73
putenv("SPATIALITE_SECURITY=relaxed");
75
setenv("SPATIALITE_SECURITY", "relaxed", 1);
78
ret = sqlite3_open_v2 (":memory:", &db_handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
79
// For debugging / testing if required
80
// ret = sqlite3_open_v2 ("check_point_to_tile.sqlite", &db_handle, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
81
spatialite_init_ex(db_handle, cache, 0);
82
if (old_SPATIALITE_SECURITY_ENV)
85
env = sqlite3_mprintf("SPATIALITE_SECURITY=%s", old_SPATIALITE_SECURITY_ENV);
89
setenv("SPATIALITE_SECURITY", old_SPATIALITE_SECURITY_ENV, 1);
95
putenv("SPATIALITE_SECURITY=");
97
unsetenv("SPATIALITE_SECURITY");
100
if (ret != SQLITE_OK) {
101
fprintf (stderr, "cannot open in-memory db: %s\n", sqlite3_errmsg (db_handle));
102
sqlite3_close (db_handle);
107
/* Create the base tables */
108
ret = sqlite3_exec (db_handle, "SELECT InitSpatialMetadata(1)", NULL, NULL, &err_msg);
109
if (ret != SQLITE_OK) {
110
fprintf(stderr, "Unexpected InitSpatialMetadata result: %i, (%s)\n", ret, err_msg);
111
sqlite3_free (err_msg);
115
ret = sqlite3_exec (db_handle, "SELECT gpkgCreateBaseTables()", NULL, NULL, &err_msg);
116
if (ret != SQLITE_OK) {
117
fprintf(stderr, "Unexpected gpkgCreateBaseTables() result: %i, (%s)\n", ret, err_msg);
118
sqlite3_free (err_msg);
122
/* create a target table */
123
ret = sqlite3_exec (db_handle, "DROP TABLE IF EXISTS test1_tiles", NULL, NULL, &err_msg);
124
if (ret != SQLITE_OK) {
125
fprintf (stderr, "DROP test1_tiles error: %s\n", err_msg);
126
sqlite3_free (err_msg);
129
ret = sqlite3_exec (db_handle, "CREATE TABLE test1_tiles (id INTEGER PRIMARY KEY AUTOINCREMENT, zoom_level INTEGER NOT NULL DEFAULT 0, tile_column INTEGER NOT NULL DEFAULT 0, tile_row INTEGER NOT NULL DEFAULT 0, tile_data BLOB NOT NULL DEFAULT (zeroblob(4)))", NULL, NULL, &err_msg);
130
if (ret != SQLITE_OK) {
131
fprintf (stderr, "CREATE mytable_tiles error: %s\n", err_msg);
132
sqlite3_free (err_msg);
135
/* add in some test entries */
136
ret = sqlite3_exec (db_handle, "INSERT INTO test1_tiles (id, zoom_level, tile_column, tile_row, tile_data) VALUES (1, 0, 0, 0, BlobFromFile('tile000.jpeg'))", NULL, NULL, &err_msg);
137
if (ret != SQLITE_OK) {
138
fprintf (stderr, "INSERT level0 error: %s\n", err_msg);
139
sqlite3_free (err_msg);
142
ret = sqlite3_exec (db_handle, "INSERT INTO test1_tiles (id, zoom_level, tile_column, tile_row, tile_data) VALUES (2, 1, 0, 0, BlobFromFile('tile100.jpeg'))", NULL, NULL, &err_msg);
143
if (ret != SQLITE_OK) {
144
fprintf (stderr, "INSERT level1 error: %s\n", err_msg);
145
sqlite3_free (err_msg);
148
ret = sqlite3_exec (db_handle, "INSERT INTO test1_tiles (id, zoom_level, tile_column, tile_row, tile_data) VALUES (3, 1, 0, 1, BlobFromFile('tile101.jpeg'))", NULL, NULL, &err_msg);
149
if (ret != SQLITE_OK) {
150
fprintf (stderr, "INSERT level1 error: %s\n", err_msg);
151
sqlite3_free (err_msg);
154
ret = sqlite3_exec (db_handle, "INSERT INTO test1_tiles (id, zoom_level, tile_column, tile_row, tile_data) VALUES (4, 1, 1, 1, BlobFromFile('tile111.jpeg'))", NULL, NULL, &err_msg);
155
if (ret != SQLITE_OK) {
156
fprintf (stderr, "INSERT level1 error: %s\n", err_msg);
157
sqlite3_free (err_msg);
160
ret = sqlite3_exec (db_handle, "DROP TABLE IF EXISTS test1_tiles_rt_metadata", NULL, NULL, &err_msg);
161
if (ret != SQLITE_OK) {
162
fprintf (stderr, "DROP test1_tiles_rt_metadata error: %s\n", err_msg);
163
sqlite3_free (err_msg);
166
ret = sqlite3_exec (db_handle, "CREATE TABLE test1_tiles_rt_metadata (id INTEGER PRIMARY KEY AUTOINCREMENT, min_x DOUBLE NOT NULL DEFAULT -180.0, min_y DOUBLE NOT NULL DEFAULT -90.0, max_x DOUBLE NOT NULL DEFAULT 180.0, max_y DOUBLE NOT NULL DEFAULT 90.0)", NULL, NULL, &err_msg);
167
if (ret != SQLITE_OK) {
168
fprintf (stderr, "CREATE test1_tiles_rt_metadata error: %s\n", err_msg);
169
sqlite3_free (err_msg);
172
ret = sqlite3_exec (db_handle, "INSERT INTO test1_tiles_rt_metadata (id, min_x, min_y, max_x, max_y) VALUES (1, -180.0, -90.0, 180.0, 90.0)", NULL, NULL, &err_msg);
173
if (ret != SQLITE_OK) {
174
fprintf (stderr, "INSERT level0 _rt_metadata error: %s\n", err_msg);
175
sqlite3_free (err_msg);
178
ret = sqlite3_exec (db_handle, "INSERT INTO test1_tiles_rt_metadata (id, min_x, min_y, max_x, max_y) VALUES (2, -180.0, 0.0, 0.0, 90.0)", NULL, NULL, &err_msg);
179
if (ret != SQLITE_OK) {
180
fprintf (stderr, "INSERT level1 (0,0) _rt_metadata error: %s\n", err_msg);
181
sqlite3_free (err_msg);
184
/* Deliberately skip 3 */
185
ret = sqlite3_exec (db_handle, "INSERT INTO test1_tiles_rt_metadata (id, min_x, min_y, max_x, max_y) VALUES (4, 0.0, -90.0, 180.0, 0.0)", NULL, NULL, &err_msg);
186
if (ret != SQLITE_OK) {
187
fprintf (stderr, "INSERT level1 (1,1) _rt_metadata error: %s\n", err_msg);
188
sqlite3_free (err_msg);
192
ret = sqlite3_exec (db_handle, "INSERT INTO raster_columns VALUES (\"test1_tiles\", \"tile_data\", 100, 0, 4326)", NULL, NULL, &err_msg);
193
if (ret != SQLITE_OK) {
194
fprintf (stderr, "INSERT raster_columns row error: %s\n", err_msg);
195
sqlite3_free (err_msg);
199
/* try gpkgPointToTile normal operation, using AGD94 */
200
sql_statement = "SELECT gpkgPointToTile(\"test1_tiles\", 3577, 20.0, -30.0, 1), BlobFromFile(\"tile111.jpeg\")";
201
ret = sqlite3_prepare_v2 (db_handle, sql_statement, strlen(sql_statement), &stmt, NULL);
202
if (ret != SQLITE_OK)
204
fprintf(stderr, "failed to prepare SQL statement: %i (%s)\n", ret, sqlite3_errmsg(db_handle));
207
ret = sqlite3_step (stmt);
208
if (ret != SQLITE_ROW)
210
fprintf(stderr, "unexpected return value for first step: %i (%s)\n", ret, sqlite3_errmsg(db_handle));
213
if (sqlite3_column_type (stmt, 0) != SQLITE_BLOB)
215
fprintf(stderr, "bad type for column 0: %i\n", sqlite3_column_type (stmt, 0));
218
if (sqlite3_column_type (stmt, 1) != SQLITE_BLOB)
220
fprintf(stderr, "bad type for column 1: %i\n", sqlite3_column_type (stmt, 1));
223
blob0 = sqlite3_column_blob(stmt, 0);
224
blob1 = sqlite3_column_blob(stmt, 1);
225
if (sqlite3_column_bytes(stmt, 0) != sqlite3_column_bytes(stmt, 1))
227
fprintf(stderr, "mismatch in blob sizes: %i vs %i\n", sqlite3_column_bytes(stmt, 0), sqlite3_column_bytes(stmt, 1));
230
for (i = 0; i < sqlite3_column_bytes(stmt, 0); ++i)
232
if (blob0[i] != blob1[i])
234
fprintf(stderr, "mismatch in blob content at offset: %i\n", i);
239
ret = sqlite3_step(stmt);
240
if (ret != SQLITE_DONE)
242
fprintf(stderr, "unexpected return value for second step: %i\n", ret);
245
ret = sqlite3_finalize (stmt);
247
ret = sqlite3_close (db_handle);
248
if (ret != SQLITE_OK) {
249
fprintf (stderr, "sqlite3_close() error: %s\n", sqlite3_errmsg (db_handle));
253
spatialite_cleanup_ex(cache);