SpatiaLite  5.1.0
All Data Structures Files Functions Variables Typedefs Macros Pages
demo5.c

This is a sample C source showing how to use the SpatiaLite's API gaiaGetVectorLayersList(), i.e. the one gathering statistic infos for Vector Layers. The typical output of this demo is shown below.

By simply specifying a DB-path demo5 will print the complete list of all Vector Layers found in that DB:

$ ./demo5 /home/sandro/db-4.0.sqlite 
SQLite version: 3.7.11
SpatiaLite version: 4.0.0-RC2

****** VectorLayersList (mode=FAST) *********
VectorLayer: Type=BasedOnSqlTable TableName=com2011
        GeometryName=geometry SRID=23032 GeometryType=MULTIPOLYGON Dims=XY
        RowCount=8094
        ExtentMin 313360.999831 / 3933878.175118
        ExtentMax 1312106.500031 / 5220492.095518
        ReadOnly=FALSE Hidden=FALSE
VectorLayer: Type=BasedOnSqlTable TableName=prov2011
        GeometryName=geometry SRID=23032 GeometryType=MULTIPOLYGON Dims=XY
        RowCount=110
        ExtentMin 313360.999831 / 3933878.175118
        ExtentMax 1312106.500031 / 5220491.200018
        ReadOnly=FALSE Hidden=FALSE
VectorLayer: Type=BasedOnSqlTable TableName=reg2011
        GeometryName=geometry SRID=23032 GeometryType=MULTIPOLYGON Dims=XY
        RowCount=20
        ExtentMin 313360.999831 / 3933878.175118
        ExtentMax 1312106.500031 / 5220491.200018
        ReadOnly=FALSE Hidden=FALSE
VectorLayer: Type=BasedOnSqlView TableName=com_prov
        GeometryName=geometry SRID=23032 GeometryType=MULTIPOLYGON Dims=XY
        RowCount=8094
        ExtentMin 313360.999831 / 3933878.175118
        ExtentMax 1312106.500031 / 5220492.095518
        ReadOnly=FALSE Hidden=FALSE
VectorLayer: Type=BasedOnSqlView TableName=prov_reg
        GeometryName=geometry SRID=23032 GeometryType=MULTIPOLYGON Dims=XY
        RowCount=110
        ExtentMin 313360.999831 / 3933878.175118
        ExtentMax 1312106.500031 / 5220491.200018
        ReadOnly=TRUE Hidden=FALSE
VectorLayer: Type=BasedOnVirtualShape TableName=com2011a
        GeometryName=geometry SRID=23032 GeometryType=MULTIPOLYGON Dims=XY
        RowCount=8094
        ExtentMin 313360.999831 / 3933878.175118
        ExtentMax 1312106.500031 / 5220492.095518
VectorLayer: Type=BasedOnVirtualShape TableName=prov2011a
        GeometryName=geometry SRID=23032 GeometryType=MULTIPOLYGON Dims=XY
        RowCount=110
        ExtentMin 313360.999831 / 3933878.175118
        ExtentMax 1312106.500031 / 5220491.200018
VectorLayer: Type=BasedOnVirtualShape TableName=reg2011a
        GeometryName=geometry SRID=23032 GeometryType=MULTIPOLYGON Dims=XY
        RowCount=20
        ExtentMin 313360.999831 / 3933878.175118
        ExtentMax 1312106.500031 / 5220491.200018


****** VectorLayersList (mode=PRECISE) *********
VectorLayer: Type=BasedOnSqlTable TableName=com2011
        GeometryName=geometry SRID=23032 GeometryType=MULTIPOLYGON Dims=XY
        RowCount=8094
        ExtentMin 313360.999831 / 3933878.175118
        ExtentMax 1312106.500031 / 5220492.095518
        ReadOnly=FALSE Hidden=FALSE
VectorLayer: Type=BasedOnSqlTable TableName=prov2011
        GeometryName=geometry SRID=23032 GeometryType=MULTIPOLYGON Dims=XY
        RowCount=110
        ExtentMin 313360.999831 / 3933878.175118
        ExtentMax 1312106.500031 / 5220491.200018
        ReadOnly=FALSE Hidden=FALSE
VectorLayer: Type=BasedOnSqlTable TableName=reg2011
        GeometryName=geometry SRID=23032 GeometryType=MULTIPOLYGON Dims=XY
        RowCount=20
        ExtentMin 313360.999831 / 3933878.175118
        ExtentMax 1312106.500031 / 5220491.200018
        ReadOnly=FALSE Hidden=FALSE
VectorLayer: Type=BasedOnSqlView TableName=com_prov
        GeometryName=geometry SRID=23032 GeometryType=MULTIPOLYGON Dims=XY
        RowCount=8094
        ExtentMin 313360.999831 / 3933878.175118
        ExtentMax 1312106.500031 / 5220492.095518
        ReadOnly=FALSE Hidden=FALSE
VectorLayer: Type=BasedOnSqlView TableName=prov_reg
        GeometryName=geometry SRID=23032 GeometryType=MULTIPOLYGON Dims=XY
        RowCount=110
        ExtentMin 313360.999831 / 3933878.175118
        ExtentMax 1312106.500031 / 5220491.200018
        ReadOnly=TRUE Hidden=FALSE
VectorLayer: Type=BasedOnVirtualShape TableName=com2011a
        GeometryName=geometry SRID=23032 GeometryType=MULTIPOLYGON Dims=XY
        RowCount=8094
        ExtentMin 313360.999831 / 3933878.175118
        ExtentMax 1312106.500031 / 5220492.095518
VectorLayer: Type=BasedOnVirtualShape TableName=prov2011a
        GeometryName=geometry SRID=23032 GeometryType=MULTIPOLYGON Dims=XY
        RowCount=110
        ExtentMin 313360.999831 / 3933878.175118
        ExtentMax 1312106.500031 / 5220491.200018
VectorLayer: Type=BasedOnVirtualShape TableName=reg2011a
        GeometryName=geometry SRID=23032 GeometryType=MULTIPOLYGON Dims=XY
        RowCount=20
        ExtentMin 313360.999831 / 3933878.175118
        ExtentMax 1312106.500031 / 5220491.200018

sample successfully terminated

By optionally specifying a Layer name demo5 will print a more detailed list for that single Layer:

$ ./demo5 /home/sandro/db-4.0.sqlite com2011
SQLite version: 3.7.11
SpatiaLite version: 4.0.0-RC2

****** VectorLayersList (mode=FAST) *********
VectorLayer: Type=BasedOnSqlTable TableName=com2011
        GeometryName=geometry SRID=23032 GeometryType=MULTIPOLYGON Dims=XY
        RowCount=8094
        ExtentMin 313360.999831 / 3933878.175118
        ExtentMax 1312106.500031 / 5220492.095518
        ReadOnly=FALSE Hidden=FALSE
                Field #0) FieldName=PRO_COM
                        IntegerValues=8094 
                        IntRange 1001 / 110010
                Field #1) FieldName=COD_REG
                        IntegerValues=8094 
                        IntRange 1 / 20
                Field #2) FieldName=COD_PRO
                        IntegerValues=8094 
                        IntRange 1 / 110
                Field #3) FieldName=NOME_COM
                        TextValues=8094 
                        MaxSize/Length=35
                Field #4) FieldName=NOME_TED
                        TextValues=8094 
                        MaxSize/Length=36
                Field #5) FieldName=SHAPE_Leng
                        DoubleValues=8094 
                        DoubleRange 1566.303618 / 327044.574999
                Field #6) FieldName=SHAPE_Area
                        DoubleValues=8094 
                        DoubleRange 120613.967719 / 1287358944.600000
                Field #7) FieldName=Geometry
                        BlobValues=8094 
                        MaxSize/Length=222151


****** VectorLayersList (mode=PRECISE) *********
VectorLayer: Type=BasedOnSqlTable TableName=com2011
        GeometryName=geometry SRID=23032 GeometryType=MULTIPOLYGON Dims=XY
        RowCount=8094
        ExtentMin 313360.999831 / 3933878.175118
        ExtentMax 1312106.500031 / 5220492.095518
        ReadOnly=FALSE Hidden=FALSE
                Field #0) FieldName=PRO_COM
                        IntegerValues=8094 
                        IntRange 1001 / 110010
                Field #1) FieldName=COD_REG
                        IntegerValues=8094 
                        IntRange 1 / 20
                Field #2) FieldName=COD_PRO
                        IntegerValues=8094 
                        IntRange 1 / 110
                Field #3) FieldName=NOME_COM
                        TextValues=8094 
                        MaxSize/Length=35
                Field #4) FieldName=NOME_TED
                        TextValues=8094 
                        MaxSize/Length=36
                Field #5) FieldName=SHAPE_Leng
                        DoubleValues=8094 
                        DoubleRange 1566.303618 / 327044.574999
                Field #6) FieldName=SHAPE_Area
                        DoubleValues=8094 
                        DoubleRange 120613.967719 / 1287358944.600000
                Field #7) FieldName=Geometry
                        BlobValues=8094 
                        MaxSize/Length=222151

sample successfully terminated
/*
demo5.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 <time.h>
/*
these headers are required in order to support
SQLite/SpatiaLite
*/
#include <sqlite3.h>
#include <spatialite.h>
#define ARG_NONE 0
#define ARG_DB_PATH 1
#define ARG_TABLE 2
#define ARG_GEOMETRY 3
static void
do_print_list (gaiaVectorLayersListPtr list, int n_mode)
{
/* prints the layers list */
const char *mode = "FAST";
mode = "OPTIMISTIC";
mode = "PESSIMISTIC";
printf ("\n****** VectorLayersList (mode=%s) *********\n", mode);
if (list == NULL)
{
printf ("The VectorLayersList is empty !!!\n\n");
return;
}
lyr = list->First;
while (lyr)
{
/* printing the Layer Header */
const char *lyr_type = "UnknownType";
const char *geom_type = "UnknownType";
const char *dims = "UnknownDims";
switch (lyr->LayerType)
{
lyr_type = "BasedOnSqlTable";
break;
lyr_type = "BasedOnSqlView";
break;
lyr_type = "BasedOnVirtualShape";
break;
};
switch (lyr->GeometryType)
{
geom_type = "GEOMETRY";
break;
geom_type = "POINT";
break;
geom_type = "LINESTRING";
break;
geom_type = "POLYGON";
break;
geom_type = "MULTIPOINT";
break;
geom_type = "MULTILINESTRING";
break;
geom_type = "MULTIPOLYGON";
break;
geom_type = "GEOMETRYCOLLECTION";
break;
};
switch (lyr->Dimensions)
{
case GAIA_XY:
dims = "XY";
break;
case GAIA_XY_Z:
dims = "XYZ";
break;
case GAIA_XY_M:
dims = "XYM";
break;
dims = "XYXM";
break;
};
printf ("VectorLayer: Type=%s TableName=%s\n", lyr_type,
lyr->TableName);
printf ("\tGeometryName=%s SRID=%d GeometryType=%s Dims=%s\n",
lyr->GeometryName, lyr->Srid, geom_type, dims);
if (lyr->ExtentInfos)
{
printf ("\tRowCount=%d\n", lyr->ExtentInfos->Count);
printf ("\tExtentMin %f / %f\n\tExtentMax %f / %f\n",
lyr->ExtentInfos->MaxY);
}
if (lyr->AuthInfos)
printf ("\tReadOnly=%s Hidden=%s\n",
(lyr->AuthInfos->IsReadOnly == 0) ? "FALSE" : "TRUE",
(lyr->AuthInfos->IsHidden == 0) ? "FALSE" : "TRUE");
fld = lyr->First;
while (fld)
{
/* printing AttributeFields infos */
printf ("\t\tField #%d) FieldName=%s\n", fld->Ordinal,
printf ("\t\t\t");
if (fld->NullValuesCount)
printf ("NullValues=%d ", fld->NullValuesCount);
printf ("IntegerValues=%d ", fld->IntegerValuesCount);
printf ("DoubleValues=%d ", fld->DoubleValuesCount);
if (fld->TextValuesCount)
printf ("TextValues=%d ", fld->TextValuesCount);
if (fld->BlobValuesCount)
printf ("BlobValues=%d ", fld->BlobValuesCount);
printf ("\n");
if (fld->MaxSize)
printf ("\t\t\tMaxSize/Length=%d\n", fld->MaxSize->MaxSize);
if (fld->IntRange)
#if defined(_WIN32) || defined(__MINGW32__)
/* CAVEAT: M$ runtime doesn't supports %lld for 64 bits */
printf ("\t\t\tIntRange %I64d / %I64d\n",
#else
printf ("\t\t\tIntRange %lld / %lld\n",
#endif
if (fld->DoubleRange)
printf ("\t\t\tDoubleRange %f / %f\n",
fld = fld->Next;
}
lyr = lyr->Next;
}
printf ("\n");
}
static void
do_help ()
{
/* printing the argument list */
fprintf (stderr, "\n\nusage: demo5 ARGLIST\n");
fprintf (stderr,
"==============================================================\n");
fprintf (stderr, "-d or --db-path pathname the SpatiaLite DB path\n");
fprintf (stderr,
"-t or --table table-name the table to be checked\n");
fprintf (stderr,
"-g or --geometry column_name geometry column [optional]\n\n");
fprintf (stderr, "you can specify one of the following modes:\n");
fprintf (stderr, "-o or --optimistic OPTIMISTIC mode\n");
fprintf (stderr, "-p or --pessimistic PESSIMISTIC mode\n");
}
int
main (int argc, char *argv[])
{
int ret;
sqlite3 *handle;
int i;
int next_arg = ARG_NONE;
int error = 0;
const char *db_path = NULL;
const char *table = NULL;
const char *geometry = NULL;
void *cache;
for (i = 1; i < argc; i++)
{
/* parsing the invocation arguments */
if (next_arg != ARG_NONE)
{
switch (next_arg)
{
case ARG_DB_PATH:
db_path = argv[i];
break;
case ARG_TABLE:
table = argv[i];
break;
case ARG_GEOMETRY:
geometry = argv[i];
break;
};
next_arg = ARG_NONE;
continue;
}
if (strcasecmp (argv[i], "--help") == 0
|| strcmp (argv[i], "-h") == 0)
{
do_help ();
return -1;
}
if (strcasecmp (argv[i], "-d") == 0
|| strcasecmp (argv[i], "--db-path") == 0)
{
next_arg = ARG_DB_PATH;
continue;
}
if (strcasecmp (argv[i], "-t") == 0
|| strcmp (argv[i], "--table") == 0)
{
next_arg = ARG_TABLE;
continue;
}
if (strcasecmp (argv[i], "-g") == 0
|| strcmp (argv[i], "--geometry") == 0)
{
next_arg = ARG_GEOMETRY;
continue;
}
if (strcasecmp (argv[i], "-p") == 0
|| strcmp (argv[i], "--pessimistic") == 0)
{
next_arg = ARG_NONE;
continue;
}
if (strcasecmp (argv[i], "-o") == 0
|| strcmp (argv[i], "--optimistic") == 0)
{
next_arg = ARG_NONE;
continue;
}
fprintf (stderr, "unknown argument: %s\n", argv[i]);
error = 1;
}
if (error)
{
do_help ();
return -1;
}
/* checking the arguments */
if (!db_path)
{
fprintf (stderr, "did you forget setting the --db-path argument ?\n");
error = 1;
}
if (error)
{
do_help ();
return -1;
}
/*
trying to connect the test DB:
- this demo is intended to create an existing, already populated database
*/
ret = sqlite3_open_v2 (db_path, &handle,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL);
if (ret != SQLITE_OK)
{
printf ("cannot open '%s': %s\n", argv[1], sqlite3_errmsg (handle));
sqlite3_close (handle);
return -1;
}
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");
/* listing the requested layer(s) */
list = gaiaGetVectorLayersList (handle, table, geometry, mode);
do_print_list (list, mode);
/* disconnecting the test DB */
ret = sqlite3_close (handle);
if (ret != SQLITE_OK)
{
printf ("close() error: %s\n", sqlite3_errmsg (handle));
return -1;
}
printf ("\n\nsample successfully terminated\n");
return 0;
}
Geometry handling functions and constants.
#define GAIA_VECTOR_MULTIPOLYGON
Vector Layer Geometry: MultiPolygon.
Definition: gg_const.h:100
#define GAIA_VECTOR_GEOMETRY
Vector Layer Geometry: Geometry.
Definition: gg_const.h:88
#define GAIA_VECTOR_VIEW
Vector Layer: Spatial View.
Definition: gg_const.h:81
#define GAIA_VECTOR_GEOMETRYCOLLECTION
Vector Layer Geometry: GeometryCollection.
Definition: gg_const.h:102
#define GAIA_VECTORS_LIST_PESSIMISTIC
mode: PESSIMISTIC
Definition: gg_const.h:72
#define GAIA_VECTOR_MULTIPOINT
Vector Layer Geometry: MultiPoint.
Definition: gg_const.h:96
#define GAIA_XY
Coordinate Dimensions: XY.
Definition: gg_const.h:336
#define GAIA_VECTOR_VIRTUAL
Vector Layer: Virtual Shape.
Definition: gg_const.h:83
#define GAIA_XY_Z_M
Coordinate Dimensions: XYZM.
Definition: gg_const.h:342
#define GAIA_VECTOR_POLYGON
Vector Layer Geometry: Polygon.
Definition: gg_const.h:94
#define GAIA_VECTORS_LIST_OPTIMISTIC
mode: OPTIMISTIC
Definition: gg_const.h:69
#define GAIA_VECTOR_MULTILINESTRING
Vector Layer Geometry: MultiLinestring.
Definition: gg_const.h:98
#define GAIA_XY_Z
Coordinate Dimensions: XYZ.
Definition: gg_const.h:338
#define GAIA_VECTOR_TABLE
Vector Layer: Spatial Table.
Definition: gg_const.h:79
#define GAIA_VECTOR_POINT
Vector Layer Geometry: Point.
Definition: gg_const.h:90
#define GAIA_XY_M
Coordinate Dimensions: XYM.
Definition: gg_const.h:340
#define GAIA_VECTOR_LINESTRING
Vector Layer Geometry: Linestring.
Definition: gg_const.h:92
Main SpatiaLite header file.
SPATIALITE_DECLARE void gaiaFreeVectorLayersList(gaiaVectorLayersListPtr ptr)
Destroys a VectorLayersList object.
SPATIALITE_DECLARE void spatialite_cleanup_ex(const void *ptr)
Cleanup a SpatiaLite connection.
SPATIALITE_DECLARE gaiaVectorLayersListPtr gaiaGetVectorLayersList(sqlite3 *handle, const char *table, const char *geometry, int mode)
Queries the Metadata tables supporting Vector Layers.
SPATIALITE_DECLARE void * spatialite_alloc_connection(void)
Initializes the internal memory block supporting each connection.
SPATIALITE_DECLARE void spatialite_init_ex(sqlite3 *db_handle, const void *ptr, int verbose)
Initializes a SpatiaLite connection.
SPATIALITE_DECLARE const char * spatialite_version(void)
Return the current library version.
SPATIALITE_DECLARE void spatialite_shutdown(void)
Finalizes the library.
double MinValue
Minimum value.
Definition: gg_structs.h:746
double MaxValue
Maximum value.
Definition: gg_structs.h:748
sqlite3_int64 MinValue
Minimum value.
Definition: gg_structs.h:728
sqlite3_int64 MaxValue
Maximum value.
Definition: gg_structs.h:730
int MaxSize
MaxSize / MaxLength.
Definition: gg_structs.h:712
LayerAttributeField infos.
Definition: gg_structs.h:762
gaiaAttributeFieldDoubleRangePtr DoubleRange
pointer to range of Double values infos (may be NULL)
Definition: gg_structs.h:782
gaiaAttributeFieldIntRangePtr IntRange
pointer to range of Integer values infos (may be NULL)
Definition: gg_structs.h:780
int Ordinal
ordinal position
Definition: gg_structs.h:764
int BlobValuesCount
total count of BLOB values
Definition: gg_structs.h:776
struct gaiaLayerAttributeFieldInfos * Next
pointer to next item (linked list)
Definition: gg_structs.h:784
int DoubleValuesCount
total count of DOUBLE values
Definition: gg_structs.h:772
char * AttributeFieldName
SQL name of the corresponding column.
Definition: gg_structs.h:766
gaiaAttributeFieldMaxSizePtr MaxSize
pointer to MaxSize/Length infos (may be NULL)
Definition: gg_structs.h:778
int TextValuesCount
total count of TEXT values
Definition: gg_structs.h:774
int IntegerValuesCount
total count of INTEGER values
Definition: gg_structs.h:770
int NullValuesCount
total count of NULL values
Definition: gg_structs.h:768
int IsHidden
Hidden layer: TRUE or FALSE.
Definition: gg_structs.h:690
int IsReadOnly
Read-Only layer: TRUE or FALSE.
Definition: gg_structs.h:688
double MinX
Extent: min X.
Definition: gg_structs.h:666
double MaxX
Extent: max X.
Definition: gg_structs.h:670
int Count
row count (aka feature count)
Definition: gg_structs.h:664
double MaxY
Extent: max Y.
Definition: gg_structs.h:672
double MinY
Extent: min Y.
Definition: gg_structs.h:668
Vector Layer item.
Definition: gg_structs.h:798
int Srid
SRID value.
Definition: gg_structs.h:807
int LayerType
one of GAIA_VECTOR_UNKNOWN, GAIA_VECTOR_TABLE, GAIA_VECTOR_VIEW, GAIA_VECTOR_VIRTUAL
Definition: gg_structs.h:801
gaiaLayerExtentPtr ExtentInfos
pointer to Extent infos (may be NULL)
Definition: gg_structs.h:820
int GeometryType
one of GAIA_VECTOR_UNKNOWN, GAIA_VECTOR_POINT, GAIA_VECTOR_LINESTRING, GAIA_VECTOR_POLYGON,...
Definition: gg_structs.h:812
struct gaiaVectorLayerItem * Next
pointer to next item (linked list)
Definition: gg_structs.h:828
char * GeometryName
SQL name of the corresponding Geometry column.
Definition: gg_structs.h:805
gaiaLayerAttributeFieldPtr First
pointer to first Field/Attribute (linked list)
Definition: gg_structs.h:824
gaiaLayerAuthPtr AuthInfos
pointer to Auth infos (may be NULL)
Definition: gg_structs.h:822
char * TableName
SQL name of the corresponding table.
Definition: gg_structs.h:803
int Dimensions
one of GAIA_VECTOR_UNKNOWN, GAIA_XY, GAIA_XY_Z, GAIA_XY_M, GAIA_XY_ZM
Definition: gg_structs.h:814
Container for Vector Layers List.
Definition: gg_structs.h:842
gaiaVectorLayerPtr First
pointer to first vector layer (linked list)
Definition: gg_structs.h:844