SpatiaLite  4.2.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;
if (argc > 1 || argv[0] == NULL)
argc = 1; /* silencing stupid compiler warnings */
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;
}