~ubuntu-branches/ubuntu/wily/spatialite/wily-proposed

« back to all changes in this revision

Viewing changes to test/check_point_to_tile_different_srid.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-07-14 11:57:46 UTC
  • mfrom: (16.1.1 sid)
  • Revision ID: package-import@ubuntu.com-20150714115746-e2iljfmb5sq7o5hh
Tags: 4.3.0-1
Move from experimental to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 
3
 
 check_point_to_tile.c - Test case for GeoPackage Extensions
4
 
 
5
 
 Author: Brad Hards <bradh@frogmouth.net>
6
 
 
7
 
 ------------------------------------------------------------------------------
8
 
 
9
 
 Version: MPL 1.1/GPL 2.0/LGPL 2.1
10
 
 
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/
15
 
 
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
19
 
License.
20
 
 
21
 
The Original Code is GeoPackage extensions
22
 
 
23
 
The Initial Developer of the Original Code is Brad Hards
24
 
 
25
 
Portions created by the Initial Developer are Copyright (C) 2011
26
 
the Initial Developer. All Rights Reserved.
27
 
 
28
 
Contributor(s):
29
 
 
30
 
 
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.
42
 
 
43
 
*/
44
 
 
45
 
#include <inttypes.h>
46
 
#include <stdlib.h>
47
 
#include <stdio.h>
48
 
#include <string.h>
49
 
 
50
 
#include <sqlite3.h>
51
 
#include <spatialite.h>
52
 
 
53
 
#include "test_helpers.h"
54
 
 
55
 
int main (int argc UNUSED, char *argv[] UNUSED)
56
 
{
57
 
    sqlite3 *db_handle = NULL;
58
 
    char *sql_statement;
59
 
    sqlite3_stmt *stmt;
60
 
    int ret;
61
 
    char *err_msg = NULL;
62
 
    int i; 
63
 
    const uint8_t *blob0;
64
 
    const uint8_t *blob1;
65
 
    void *cache = spatialite_alloc_connection();
66
 
    char *old_SPATIALITE_SECURITY_ENV = NULL;
67
 
#ifdef _WIN32
68
 
        char *env;
69
 
#endif /* not WIN32 */
70
 
 
71
 
    old_SPATIALITE_SECURITY_ENV = getenv("SPATIALITE_SECURITY");
72
 
#ifdef _WIN32
73
 
        putenv("SPATIALITE_SECURITY=relaxed");
74
 
#else /* not WIN32 */
75
 
    setenv("SPATIALITE_SECURITY", "relaxed", 1);
76
 
#endif
77
 
    
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)
83
 
    {
84
 
#ifdef _WIN32 
85
 
          env = sqlite3_mprintf("SPATIALITE_SECURITY=%s", old_SPATIALITE_SECURITY_ENV);
86
 
          putenv(env);
87
 
          sqlite3_free(env);
88
 
#else /* not WIN32 */
89
 
      setenv("SPATIALITE_SECURITY", old_SPATIALITE_SECURITY_ENV, 1);
90
 
#endif
91
 
    }
92
 
    else
93
 
    {
94
 
#ifdef _WIN32
95
 
          putenv("SPATIALITE_SECURITY=");
96
 
#else /* not WIN32 */
97
 
      unsetenv("SPATIALITE_SECURITY");
98
 
#endif
99
 
    }
100
 
    if (ret != SQLITE_OK) {
101
 
      fprintf (stderr, "cannot open in-memory db: %s\n", sqlite3_errmsg (db_handle));
102
 
      sqlite3_close (db_handle);
103
 
      db_handle = NULL;
104
 
      return -1;
105
 
    }
106
 
 
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);
112
 
        return -2;
113
 
    }
114
 
    
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);
119
 
        return -3;
120
 
    }
121
 
 
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);
127
 
      return -10;
128
 
    }
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);
133
 
      return -11;
134
 
    }
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);
140
 
      return -12;
141
 
    }
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);
146
 
      return -13;
147
 
    }
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);
152
 
      return -14;
153
 
    }
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);
158
 
      return -15;
159
 
    }
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);
164
 
      return -16;
165
 
    }
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);
170
 
      return -17;
171
 
    }
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);
176
 
      return -18;
177
 
    }
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);
182
 
      return -19;
183
 
    }
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);
189
 
      return -20;
190
 
    }
191
 
    
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);
196
 
      return -21;
197
 
    }
198
 
    
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)
203
 
    {
204
 
        fprintf(stderr, "failed to prepare SQL statement: %i (%s)\n", ret, sqlite3_errmsg(db_handle));
205
 
        return -30;
206
 
    }
207
 
    ret = sqlite3_step (stmt);
208
 
    if (ret != SQLITE_ROW)
209
 
    {
210
 
        fprintf(stderr, "unexpected return value for first step: %i (%s)\n", ret, sqlite3_errmsg(db_handle));
211
 
        return -31;
212
 
    }
213
 
    if (sqlite3_column_type (stmt, 0) != SQLITE_BLOB)
214
 
    {
215
 
        fprintf(stderr, "bad type for column 0: %i\n", sqlite3_column_type (stmt, 0));
216
 
        return -32;
217
 
    }
218
 
    if (sqlite3_column_type (stmt, 1) != SQLITE_BLOB)
219
 
    {
220
 
        fprintf(stderr, "bad type for column 1: %i\n", sqlite3_column_type (stmt, 1));
221
 
        return -33;
222
 
    }
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))
226
 
    {
227
 
        fprintf(stderr, "mismatch in blob sizes: %i vs %i\n", sqlite3_column_bytes(stmt, 0), sqlite3_column_bytes(stmt, 1));
228
 
        return -34;
229
 
    }
230
 
    for (i = 0; i < sqlite3_column_bytes(stmt, 0); ++i)
231
 
    {
232
 
        if (blob0[i] != blob1[i])
233
 
        {
234
 
            fprintf(stderr, "mismatch in blob content at offset: %i\n", i);
235
 
            return -35;
236
 
        }
237
 
    }
238
 
 
239
 
    ret = sqlite3_step(stmt);
240
 
    if (ret != SQLITE_DONE)
241
 
    {
242
 
        fprintf(stderr, "unexpected return value for second step: %i\n", ret);
243
 
        return -36;
244
 
    }
245
 
    ret = sqlite3_finalize (stmt);
246
 
    
247
 
    ret = sqlite3_close (db_handle);
248
 
    if (ret != SQLITE_OK) {
249
 
        fprintf (stderr, "sqlite3_close() error: %s\n", sqlite3_errmsg (db_handle));
250
 
        return -100;
251
 
    }
252
 
    
253
 
    spatialite_cleanup_ex(cache);
254
 
 
255
 
    return 0;
256
 
}