This is a sample C source showing how to manipulate GEOMETRY within Spatialite. It essentially follows on from the functionality shown in the demo1.c example, and covers:
- creating geometries
- exploring geometries
- querying the basic properties of a geometry
Note that this does not require a database command line argument. Here is a typical run:
$ ./demo2
step#1: POINT Dimension=0 IsValid=1
POINT 0/1 x=1.5000 y=2.7500
step#2: LINESTRING Dimension=1 IsValid=1
LINESTRING 0/1 has 5 vertices
vertex 0/5 x=1.0000 y=1.0000
vertex 1/5 x=2.0000 y=1.0000
vertex 2/5 x=2.0000 y=2.0000
vertex 3/5 x=100.0000 y=2.0000
vertex 4/5 x=100.0000 y=100.0000
step#3: POLYGON Dimension=2 IsValid=1
POLYGON 0/1 has 2 holes
ExteriorRing has 5 vertices
vertex 0/5 x=0.0000 y=0.0000
vertex 1/5 x=50.0000 y=0.0000
vertex 2/5 x=50.0000 y=50.0000
vertex 3/5 x=0.0000 y=50.0000
vertex 4/5 x=0.0000 y=0.0000
InteriorRing 0/2 has 5 vertices
vertex 0/5 x=40.0000 y=40.0000
vertex 1/5 x=41.0000 y=40.0000
vertex 2/5 x=41.0000 y=41.0000
vertex 3/5 x=40.0000 y=41.0000
vertex 4/5 x=40.0000 y=40.0000
InteriorRing 1/2 has 5 vertices
vertex 0/5 x=30.0000 y=30.0000
vertex 1/5 x=31.0000 y=30.0000
vertex 2/5 x=31.0000 y=31.0000
vertex 3/5 x=30.0000 y=31.0000
vertex 4/5 x=30.0000 y=30.0000
step#4: MULTIPOINT Dimension=0 IsValid=1
POINT 0/5 x=5.0000 y=5.0000
POINT 1/5 x=15.0000 y=5.0000
POINT 2/5 x=5.0000 y=15.0000
POINT 3/5 x=25.0000 y=5.0000
POINT 4/5 x=5.0000 y=25.0000
step#5: MULTILINESTRING Dimension=1 IsValid=1
LINESTRING 0/2 has 2 vertices
vertex 0/2 x=30.0000 y=10.0000
vertex 1/2 x=10.0000 y=30.0000
LINESTRING 1/2 has 2 vertices
vertex 0/2 x=40.0000 y=50.0000
vertex 1/2 x=50.0000 y=40.0000
step#6: MULTIPOLYGON Dimension=2 IsValid=1
POLYGON 0/2 has 0 holes
ExteriorRing has 5 vertices
vertex 0/5 x=60.0000 y=60.0000
vertex 1/5 x=70.0000 y=60.0000
vertex 2/5 x=70.0000 y=70.0000
vertex 3/5 x=60.0000 y=70.0000
vertex 4/5 x=60.0000 y=60.0000
POLYGON 1/2 has 0 holes
ExteriorRing has 5 vertices
vertex 0/5 x=80.0000 y=80.0000
vertex 1/5 x=90.0000 y=80.0000
vertex 2/5 x=90.0000 y=90.0000
vertex 3/5 x=80.0000 y=90.0000
vertex 4/5 x=80.0000 y=80.0000
step#7: GEOMETRYCOLLECTION Dimension=2 IsValid=1
POINT 0/2 x=100.0000 y=100.0000
POINT 1/2 x=100.0000 y=0.0000
LINESTRING 0/2 has 2 vertices
vertex 0/2 x=130.0000 y=110.0000
vertex 1/2 x=110.0000 y=130.0000
LINESTRING 1/2 has 2 vertices
vertex 0/2 x=140.0000 y=150.0000
vertex 1/2 x=150.0000 y=140.0000
POLYGON 0/2 has 0 holes
ExteriorRing has 5 vertices
vertex 0/5 x=160.0000 y=160.0000
vertex 1/5 x=170.0000 y=160.0000
vertex 2/5 x=170.0000 y=170.0000
vertex 3/5 x=160.0000 y=170.0000
vertex 4/5 x=160.0000 y=160.0000
POLYGON 1/2 has 0 holes
ExteriorRing has 5 vertices
vertex 0/5 x=180.0000 y=180.0000
vertex 1/5 x=190.0000 y=180.0000
vertex 2/5 x=190.0000 y=190.0000
vertex 3/5 x=180.0000 y=190.0000
vertex 4/5 x=180.0000 y=180.0000
step#8: checking WKT representations
GEOMETRYCOLLECTION(POINT(1.5 2.75))
GEOMETRYCOLLECTION(LINESTRING(1 1, 2 1, 2 2, 100 2, 100 100))
GEOMETRYCOLLECTION(POLYGON((0 0, 50 0, 50 50, 0 50, 0 0), (40 40, 41 40, 41 41, 40 41, 40 40), (30 30, 31 30, 31 31, 30 31, 30 30)))
GEOMETRYCOLLECTION(POINT(5 5), POINT(15 5), POINT(5 15), POINT(25 5), POINT(5 25))
GEOMETRYCOLLECTION(LINESTRING(30 10, 10 30), LINESTRING(40 50, 50 40))
GEOMETRYCOLLECTION(POLYGON((60 60, 70 60, 70 70, 60 70, 60 60)), POLYGON((80 80, 90 80, 90 90, 80 90, 80 80)))
GEOMETRYCOLLECTION(POINT(100 100), POINT(100 0), LINESTRING(130 110, 110 130), LINESTRING(140 150, 150 140), POLYGON((160 160, 170 160, 170 170, 160 170, 160 160)), POLYGON((180 180, 190 180, 190 190, 180 190, 180 180)))
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <spatialite/gaiaconfig.h>
#include <geos_c.h>
#include <sqlite3.h>
static char *
geom_type (int type)
{
static char *name = "EMPTY / NULL GEOMETRY";
name = "POINT";
name = "LINESTRING";
name = "POLYGON";
name = "MULTIPOINT";
name = "MULTILINESTRING";
name = "MULTIPOLYGON";
name = "GEOMETRYCOLLECTION";
return name;
}
static void
{
int n_pts = 0;
int n_lns = 0;
int n_pgs = 0;
int cnt;
int iv;
int ir;
double x;
double y;
while (pt)
{
n_pts++;
}
while (ln)
{
n_lns++;
}
while (pg)
{
n_pgs++;
}
if (n_pts)
{
cnt = 0;
while (pt)
{
printf ("\t\t\tPOINT %d/%d x=%1.4f y=%1.4f\n",
cnt, n_pts, pt->
X, pt->
Y);
cnt++;
}
}
if (n_lns)
{
cnt = 0;
while (ln)
{
printf ("\t\t\tLINESTRING %d/%d has %d vertices\n",
for (iv = 0; iv < ln->
Points; iv++)
{
printf ("\t\t\t\tvertex %d/%d x=%1.4f y=%1.4f\n",
}
cnt++;
}
}
if (n_pgs)
{
cnt = 0;
while (pg)
{
printf ("\t\t\tPOLYGON %d/%d has %d hole%c\n",
printf (
"\t\t\t\tExteriorRing has %d vertices\n", rng->
Points);
for (iv = 0; iv < rng->
Points; iv++)
{
printf ("\t\t\t\t\tvertex %d/%d x=%1.4f y=%1.4f\n",
}
{
printf ("\t\t\t\tInteriorRing %d/%d has %d vertices\n",
for (iv = 0; iv < rng->
Points; iv++)
{
printf
("\t\t\t\t\tvertex %d/%d x=%1.4f y=%1.4f\n",
}
}
cnt++;
}
}
}
int
main (int argc, char *argv[])
{
int ret;
sqlite3 *handle;
void *cache;
if (argc > 1 || argv[0] == NULL)
argc = 1;
ret = sqlite3_open_v2 (":memory:", &handle, SQLITE_OPEN_READONLY, NULL);
if (ret != SQLITE_OK)
{
printf ("cannot open '%s': %s\n", ":memory:",
sqlite3_errmsg (handle));
sqlite3_close (handle);
return -1;
}
#ifndef OMIT_GEOS
printf ("step#1: %s\t\tDimension=%d IsValid=%d\n",
geometry_printout (geo_pt);
printf ("\nstep#2: %s\tDimension=%d IsValid=%d\n",
geometry_printout (geo_ln);
printf ("\nstep#3: %s\tDimension=%d IsValid=%d\n",
geometry_printout (geo_pg);
printf ("\nstep#4: %s\tDimension=%d IsValid=%d\n",
geometry_printout (geo_mpt);
printf ("\nstep#5: %s\tDimension=%d IsValid=%d\n",
geometry_printout (geo_mln);
printf ("\nstep#6: %s\tDimension=%d IsValid=%d\n",
geometry_printout (geo_mpg);
printf ("\nstep#7: %s\tDimension=%d IsValid=%d\n",
geometry_printout (geo_coll);
printf ("\nstep#8: checking WKT representations\n");
{
printf (
"\n%s\n", wkt.
Buffer);
}
{
printf (
"\n%s\n", wkt.
Buffer);
}
{
printf (
"\n%s\n", wkt.
Buffer);
}
{
printf (
"\n%s\n", wkt.
Buffer);
}
{
printf (
"\n%s\n", wkt.
Buffer);
}
{
printf (
"\n%s\n", wkt.
Buffer);
}
{
printf (
"\n%s\n", wkt.
Buffer);
}
#else
printf ("no GEOS support available: skipping any test\n");
#endif
if (geo_pt)
if (geo_ln)
if (geo_pg)
if (geo_mpt)
if (geo_mln)
if (geo_mpg)
if (geo_coll)
sqlite3_close (handle);
return 0;
}
#define GAIA_GEOMETRYCOLLECTION
BLOB-Geometry CLASS: GEOMETRYCOLLECTION.
Definition: gg_const.h:160
SPATIALITE_DECLARE void * spatialite_alloc_connection(void)
Initializes the internal memory block supporting each connection.
int Points
number of points [aka vertices]
Definition: gg_structs.h:132
gaiaRingPtr Interiors
array of interior rings
Definition: gg_structs.h:200
GAIAGEO_DECLARE void gaiaFreeGeomColl(gaiaGeomCollPtr geom)
Destroys a Geometry object.
Container for OGC POLYGON Geometry.
Definition: gg_structs.h:193
GAIAGEO_DECLARE int gaiaGeometryType(gaiaGeomCollPtr geom)
Determines the corresponding Type for a Geometry object.
double * Coords
COORDs mem-array.
Definition: gg_structs.h:164
double Y
Y coordinate.
Definition: gg_structs.h:84
GAIAGEO_DECLARE gaiaGeomCollPtr gaiaAllocGeomColl(void)
Allocates a 2D Geometry [XY].
Container for dynamically growing output buffer.
Definition: gg_structs.h:497
gaiaLinestringPtr FirstLinestring
pointer to first LINESTRING [linked list]; may be NULL
Definition: gg_structs.h:246
SPATIALITE_DECLARE void spatialite_init_ex(sqlite3 *db_handle, const void *ptr, int verbose)
Initializes a SpatiaLite connection.
GAIAGEO_DECLARE void gaiaAddPointToGeomColl(gaiaGeomCollPtr p, double x, double y)
Creates a new 2D Point [XY] object into a Geometry object.
int Points
number of points [aka vertices]
Definition: gg_structs.h:162
GAIAGEO_DECLARE gaiaPolygonPtr gaiaAddPolygonToGeomColl(gaiaGeomCollPtr p, int vert, int interiors)
Creates a new Polygon object into a Geometry object.
double X
X coordinate.
Definition: gg_structs.h:82
struct gaiaPointStruct * Next
pointer to next item [double linked list]
Definition: gg_structs.h:92
#define gaiaGetPoint(xy, v, x, y)
macro extracting XY coordinates
Definition: gg_const.h:462
#define GAIA_MULTIPOINT
BLOB-Geometry CLASS: MULTIPOINT.
Definition: gg_const.h:154
#define GAIA_MULTILINESTRING
BLOB-Geometry CLASS: MULTILINESTRING.
Definition: gg_const.h:156
SPATIALITE_DECLARE void spatialite_shutdown(void)
Finalizes the library.
GAIAGEO_DECLARE int gaiaIsValid_r(const void *p_cache, gaiaGeomCollPtr geom)
Checks if a Geometry object represents an OGC Valid Geometry.
Container for OGC POINT Geometry.
Definition: gg_structs.h:79
#define GAIA_MULTIPOLYGON
BLOB-Geometry CLASS: MULTIPOLYGON.
Definition: gg_const.h:158
GAIAGEO_DECLARE int gaiaDimension(gaiaGeomCollPtr geom)
Determines OGC dimensions for a Geometry object.
Container for OGC RING Geometry.
Definition: gg_structs.h:159
Container for OGC LINESTRING Geometry.
Definition: gg_structs.h:129
int NumInteriors
number of interior rings (may be, none)
Definition: gg_structs.h:198
GAIAGEO_DECLARE gaiaRingPtr gaiaAddInteriorRing(gaiaPolygonPtr p, int pos, int vert)
Creates a new Interior Ring object into a Polygon object.
SPATIALITE_DECLARE void spatialite_cleanup_ex(const void *ptr)
Cleanup a SpatiaLite connection.
gaiaPolygonPtr FirstPolygon
pointer to first POLYGON [linked list]; may be NULL
Definition: gg_structs.h:250
#define GAIA_LINESTRING
BLOB-Geometry CLASS: LINESTRING.
Definition: gg_const.h:150
Geometry handling functions and constants.
#define GAIA_POLYGON
BLOB-Geometry CLASS: POLYGON.
Definition: gg_const.h:152
struct gaiaLinestringStruct * Next
pointer to next item [linked list]
Definition: gg_structs.h:146
gaiaPointPtr FirstPoint
pointer to first POINT [linked list]; may be NULL
Definition: gg_structs.h:242
#define GAIA_POINT
BLOB-Geometry CLASS: POINT.
Definition: gg_const.h:148
gaiaRingPtr Exterior
the exterior ring (mandatory)
Definition: gg_structs.h:196
int Error
validity flag
Definition: gg_structs.h:506
Container for OGC GEOMETRYCOLLECTION Geometry.
Definition: gg_structs.h:227
double * Coords
COORDs mem-array.
Definition: gg_structs.h:134
char * Buffer
current buffer
Definition: gg_structs.h:500
Main SpatiaLite header file.
#define gaiaSetPoint(xy, v, x, y)
macro setting XY coordinates
Definition: gg_const.h:479
struct gaiaPolygonStruct * Next
pointer to next item [linked list]
Definition: gg_structs.h:214
GAIAGEO_DECLARE gaiaLinestringPtr gaiaAddLinestringToGeomColl(gaiaGeomCollPtr p, int vert)
Creates a new Linestring object into a Geometry object.