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
|
/*
demo1.c
Author: Sandro Furieri a.furieri@lqt.it
This software is provided 'as-is', without any express or implied
warranty. In no event will the author be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <spatialite/gaiaconfig.h>
/*
these headers are required in order to support
SQLite/SpatiaLite
*/
#include <sqlite3.h>
#include <spatialite/gaiageo.h>
#include <spatialite.h>
int
main (int argc, char *argv[])
{
int ret;
sqlite3 *handle;
sqlite3_stmt *stmt;
gaiaGeomCollPtr geom;
char sql[256];
int i;
int ic;
char **results;
int n_rows;
int n_columns;
char *err_msg = NULL;
int len;
char *table_name;
char **p_geotables = NULL;
int n_geotables = 0;
int row_no;
const void *blob;
int blob_size;
int geom_type;
double measure;
void *cache;
if (argc != 2)
{
fprintf (stderr, "usage: %s test_db_path\n", argv[0]);
return -1;
}
/*
trying to connect the test DB:
- this demo was designed in order to connect the standard
TEST-2.3.SQLITE sample DB
- but you can try to use any SQLite/SpatiaLite DB at your will
Please notice: we'll establish a READ ONLY connection
*/
ret = sqlite3_open_v2 (argv[1], &handle, SQLITE_OPEN_READONLY, NULL);
if (ret != SQLITE_OK)
{
printf ("cannot open '%s': %s\n", argv[1], sqlite3_errmsg (handle));
sqlite3_close (handle);
return -1;
}
/*
VERY IMPORTANT:
you must initialize the SpatiaLite extension [and related]
BEFORE attempting to perform any other SQLite call
==========================================================
Please note: starting since 4.1.0 this is completely canged:
- a separate memory block (internal cache) is required by
each single connection
- allocating/freeing this block falls under the responsibility
of the program handling the connection
- in multithreaded programs a connection can never be share by
different threads; the internal-cache block must be allocated
by the same thread holding the connection
*/
cache = spatialite_alloc_connection ();
spatialite_init_ex (handle, cache, 0);
/* showing the SQLite version */
printf ("SQLite version: %s\n", sqlite3_libversion ());
/* showing the SpatiaLite version */
printf ("SpatiaLite version: %s\n", spatialite_version ());
printf ("\n\n");
/*
SQL query #1
we'll retrieve GEOMETRY tables from Spatial Metadata
we are assuming this query will return only few rows,
so this time we'll use the sqlite3_get_table() interface
this interface is very simple to use
the result set is returned as a rectangular array [rows/columns]
allocated in a temporary memory storage
so, this interface is well suited for small sized result sets,
but performs badly when accessing a large sized resul set
as a side effect, each column value is returned as text, and
isn't possible at all to retrieve true column types
(INTEGER, FLOAT ...)
*/
strcpy (sql,
"SELECT DISTINCT f_table_name FROM geometry_columns ORDER BY 1");
ret = sqlite3_get_table (handle, sql, &results, &n_rows, &n_columns,
&err_msg);
if (ret != SQLITE_OK)
{
/* some error occurred */
printf ("query#1 SQL error: %s\n", err_msg);
sqlite3_free (err_msg);
goto abort;
}
if (n_rows > 1)
{
/* first row always contains column names and is meaningless in this context */
n_geotables = n_rows;
/* allocating a dynamic pointer array to store geotable names */
p_geotables = malloc (sizeof (char *) * n_geotables);
for (i = 1; i <= n_rows; i++)
{
/*
now we'll fetch one row at each time [and we have only one column to fetch]
this one is is a simplified demo; but when writing a real application
you always must check for NULL values !!!!
*/
table_name = results[(i * n_columns) + 0];
/* and we'll store each geotable name into the dynamic pointer array */
len = strlen (table_name);
p_geotables[i - 1] = malloc (len + 1);
strcpy (p_geotables[i - 1], table_name);
}
/* we can now free the table results */
sqlite3_free_table (results);
}
for (i = 0; i < n_geotables; i++)
{
/* now we'll scan each geotable we've found in Spatial Metadata */
printf ("========= table '%s' ========================\n",
p_geotables[i]);
/*
SQL query #2
we'll retrieve any column from the current geotable
we are assuming this query will return lots of rows,
so we have to use sqlite3_prepare_v2() interface
this interface is a more complex one, but is well
suited in order to access huge sized result sets
and true value type control is supported
*/
sprintf (sql, "SELECT * FROM %s", p_geotables[i]);
ret = sqlite3_prepare_v2 (handle, sql, strlen (sql), &stmt, NULL);
if (ret != SQLITE_OK)
{
/* some error occurred */
printf ("query#2 SQL error: %s\n", sqlite3_errmsg (handle));
goto abort;
}
/*
the sqlite3_prepare_v2() call simply parses the SQL statement,
checking for syntax validity, allocating internal structs etc
but no result set row is really yet available
*/
/* we'll now save the #columns within the result set */
n_columns = sqlite3_column_count (stmt);
row_no = 0;
while (1)
{
/* this is an infinite loop, intended to fetch any row */
/* we are now trying to fetch the next available row */
ret = sqlite3_step (stmt);
if (ret == SQLITE_DONE)
{
/* there are no more rows to fetch - we can stop looping */
break;
}
if (ret == SQLITE_ROW)
{
/* ok, we've just fetched a valid row to process */
row_no++;
printf ("row #%d\n", row_no);
for (ic = 0; ic < n_columns; ic++)
{
/*
and now we'll fetch column values
for each column we'll then get:
- the column name
- a column value, that can be of type: SQLITE_NULL, SQLITE_INTEGER,
SQLITE_FLOAT, SQLITE_TEXT or SQLITE_BLOB, according to internal DB storage type
*/
printf ("\t%-10s = ",
sqlite3_column_name (stmt, ic));
switch (sqlite3_column_type (stmt, ic))
{
case SQLITE_NULL:
printf ("NULL");
break;
case SQLITE_INTEGER:
printf ("%d", sqlite3_column_int (stmt, ic));
break;
case SQLITE_FLOAT:
printf ("%1.4f",
sqlite3_column_double (stmt, ic));
break;
case SQLITE_TEXT:
printf ("'%s'",
sqlite3_column_text (stmt, ic));
break;
case SQLITE_BLOB:
blob = sqlite3_column_blob (stmt, ic);
blob_size = sqlite3_column_bytes (stmt, ic);
/* checking if this BLOB actually is a GEOMETRY */
geom =
gaiaFromSpatiaLiteBlobWkb (blob,
blob_size);
if (!geom)
{
/* for sure this one is not a GEOMETRY */
printf ("BLOB [%d bytes]", blob_size);
}
else
{
geom_type = gaiaGeometryType (geom);
if (geom_type == GAIA_UNKNOWN)
printf ("EMPTY or NULL GEOMETRY");
else
{
char *geom_name;
if (geom_type == GAIA_POINT)
geom_name = "POINT";
if (geom_type == GAIA_LINESTRING)
geom_name = "LINESTRING";
if (geom_type == GAIA_POLYGON)
geom_name = "POLYGON";
if (geom_type == GAIA_MULTIPOINT)
geom_name = "MULTIPOINT";
if (geom_type ==
GAIA_MULTILINESTRING)
geom_name = "MULTILINESTRING";
if (geom_type ==
GAIA_MULTIPOLYGON)
geom_name = "MULTIPOLYGON";
if (geom_type ==
GAIA_GEOMETRYCOLLECTION)
geom_name =
"GEOMETRYCOLLECTION";
printf ("%s SRID=%d", geom_name,
geom->Srid);
if (geom_type == GAIA_LINESTRING
|| geom_type ==
GAIA_MULTILINESTRING)
{
#ifndef OMIT_GEOS /* GEOS is required */
gaiaGeomCollLength_r (cache,
geom,
&measure);
printf (" length=%1.2f",
measure);
#else
printf
(" length=?? [no GEOS support available]");
#endif /* GEOS enabled/disabled */
}
if (geom_type == GAIA_POLYGON ||
geom_type ==
GAIA_MULTIPOLYGON)
{
#ifndef OMIT_GEOS /* GEOS is required */
gaiaGeomCollArea (geom,
&measure);
printf (" area=%1.2f",
measure);
#else
printf
("area=?? [no GEOS support available]");
#endif /* GEOS enabled/disabled */
}
}
/* we have now to free the GEOMETRY */
gaiaFreeGeomColl (geom);
}
break;
};
printf ("\n");
}
if (row_no >= 5)
{
/* we'll exit the loop after the first 5 rows - this is only a demo :-) */
break;
}
}
else
{
/* some unexpected error occurred */
printf ("sqlite3_step() error: %s\n",
sqlite3_errmsg (handle));
sqlite3_finalize (stmt);
goto abort;
}
}
/* we have now to finalize the query [memory cleanup] */
sqlite3_finalize (stmt);
printf ("\n\n");
}
/* disconnecting the test DB */
ret = sqlite3_close (handle);
if (ret != SQLITE_OK)
{
printf ("close() error: %s\n", sqlite3_errmsg (handle));
return -1;
}
/* freeing the internal-cache memory block */
spatialite_cleanup_ex (cache);
printf ("\n\nsample successfully terminated\n");
/* we have to free the dynamic pointer array used to store geotable names */
for (i = 0; i < n_geotables; i++)
{
/* freeing each tablename */
free (p_geotables[i]);
}
free (p_geotables);
spatialite_shutdown ();
return 0;
abort:
sqlite3_close (handle);
/* freeing the internal-cache memory block */
spatialite_cleanup_ex (cache);
if (p_geotables)
{
/* we have to free the dynamic pointer array used to store geotable names */
for (i = 0; i < n_geotables; i++)
{
/* freeing each tablename */
free (p_geotables[i]);
}
free (p_geotables);
}
spatialite_shutdown ();
return -1;
}
|