From 8a8f2140d68367f0a8bbfb9cd8a9e0b83f079f5f Mon Sep 17 00:00:00 2001 From: Mark Cave-Ayland Date: Tue, 16 Sep 2008 22:29:54 +0000 Subject: [PATCH] Add an example of how the liblwgeom API can be used by other C programs to manipulate geometries and output the result. Currently the unparser.c program shows how a geometry can be constructed "on the fly" and then exported in both WKT and HEXWKB formats; however it gives programmers the potential to devise some very unique processing tools which can generate files that can be loaded into PostGIS. git-svn-id: http://svn.osgeo.org/postgis/trunk@2971 b70326c6-7e19-0410-871a-916f4a2858ee --- liblwgeom/examples/Makefile | 9 ++ liblwgeom/examples/README | 7 ++ liblwgeom/examples/unparser.c | 190 ++++++++++++++++++++++++++++++++++ 3 files changed, 206 insertions(+) create mode 100644 liblwgeom/examples/Makefile create mode 100644 liblwgeom/examples/README create mode 100644 liblwgeom/examples/unparser.c diff --git a/liblwgeom/examples/Makefile b/liblwgeom/examples/Makefile new file mode 100644 index 000000000..059ebb2ca --- /dev/null +++ b/liblwgeom/examples/Makefile @@ -0,0 +1,9 @@ +# +# liblwgeom examples Makefile +# + +all: + gcc -I../ -o unparser unparser.c ../liblwgeom.a -lm + +clean: + rm -f unparser diff --git a/liblwgeom/examples/README b/liblwgeom/examples/README new file mode 100644 index 000000000..efe85922d --- /dev/null +++ b/liblwgeom/examples/README @@ -0,0 +1,7 @@ +liblwgeom API examples +====================== + +This directory contains examples of how to use the liblwgeom API from other C programs. Since liblwgeom is a static library, it is currently limited to tools within the PostGIS source tree. However, it is envisaged that programmers may make use of this interface to produce other geometry processing tools that can input and output WKT and WKB (which is, of course, easily loadable into PostGIS). + + +Mark. diff --git a/liblwgeom/examples/unparser.c b/liblwgeom/examples/unparser.c new file mode 100644 index 000000000..a2dc9d639 --- /dev/null +++ b/liblwgeom/examples/unparser.c @@ -0,0 +1,190 @@ +/********************************************************************** + * $Id: unparser.c 2797 2008-05-31 09:56:44Z mcayland $ + * + * PostGIS - Spatial Types for PostgreSQL + * http://postgis.refractions.net + * Copyright 2001-2006 Refractions Research Inc. + * + * This is free software; you can redistribute and/or modify it under + * the terms of the GNU General Public Licence. See the COPYING file. + * + **********************************************************************/ + +#include +#include + +#include "liblwgeom.h" + + +void lwgeom_init_allocators() +{ + /* + * Any program linked into liblwgeom *MUST* have a function called lwgeom_init_allocators() + * defined. The first time that a memory allocation is required, liblwgeom calls this function + * to enable the user to setup their own functions for lwalloc/lwfree. Hence when being called + * from PostGIS we can ensure we use palloc/pfree for all memory requests rather than the + * system memory management calls. + * + * Since using the standard malloc/free is likely to be a common option, liblwgeom contains a + * function called lwgeom_install_default_allocators() which sets this up for you. Hence most + * people will only ever need this line within their lwgeom_init_allocations() function. + */ + + lwgeom_install_default_allocators(); +} + + +int main() +{ + /* + * An example to show how to call the WKT/WKB unparsers in liblwgeom + */ + + LWGEOM *lwgeom; + uchar *serialized_lwgeom, *wkt, *wkb; + size_t wkb_size; + + POINTARRAY *pa; + POINT2D point2d; + POINTARRAY **rings; + + LWPOINT *testpoint; + LWLINE *testline; + LWPOLY *testpoly; + + + /* + * Construct a geometry equivalent to POINT(0 51) + */ + + pa = ptarray_construct(0, 0, 0); + point2d.x = 0; + point2d.y = 51; + pa = ptarray_addPoint(pa, (uchar *)&point2d, 2, 0); + + testpoint = lwpoint_construct(-1, NULL, pa); + + /* Generate the LWGEOM from LWPOINT, then serialize it ready for the parser */ + lwgeom = lwpoint_as_lwgeom(testpoint); + serialized_lwgeom = lwgeom_serialize(lwgeom); + + /* Output the geometry in WKT and WKB */ + wkt = serialized_lwgeom_to_ewkt(serialized_lwgeom); + printf("WKT format : %s\n", wkt); + wkb = serialized_lwgeom_to_hexwkb(serialized_lwgeom, NDR, &wkb_size); + printf("HEXWKB format : %s\n\n", wkb); + + /* Free all of the allocated items */ + lwfree(wkb); + lwfree(wkt); + lwfree(serialized_lwgeom); + pfree_point(testpoint); + + + /* + * Construct a geometry equivalent to LINESTRING(0 0, 2 2, 4 1) + */ + + pa = ptarray_construct(0, 0, 0); + point2d.x = 0; + point2d.y = 0; + pa = ptarray_addPoint(pa, (uchar *)&point2d, 2, 0); + + point2d.x = 2; + point2d.y = 2; + pa = ptarray_addPoint(pa, (uchar *)&point2d, 2, 1); + + point2d.x = 4; + point2d.y = 1; + pa = ptarray_addPoint(pa, (uchar *)&point2d, 2, 2); + + testline = lwline_construct(-1, NULL, pa); + + /* Generate the LWGEOM from LWLINE, then serialize it ready for the parser */ + lwgeom = lwline_as_lwgeom(testline); + serialized_lwgeom = lwgeom_serialize(lwgeom); + + /* Output the geometry in WKT and WKB */ + wkt = serialized_lwgeom_to_ewkt(serialized_lwgeom); + printf("WKT format : %s\n", wkt); + wkb = serialized_lwgeom_to_hexwkb(serialized_lwgeom, NDR, &wkb_size); + printf("HEXWKB format : %s\n\n", wkb); + + /* Free all of the allocated items */ + lwfree(wkb); + lwfree(wkt); + lwfree(serialized_lwgeom); + pfree_line(testline); + + + + /* + * Construct a geometry equivalent to POLYGON((0 0, 0 10, 10 10, 10 0, 0 0)(3 3, 3 6, 6 6, 6 3, 3 3)) + */ + + /* Allocate memory for the rings */ + rings = lwalloc(sizeof(POINTARRAY) * 2); + + /* Construct the first ring */ + rings[0] = ptarray_construct(0, 0, 0); + point2d.x = 0; + point2d.y = 0; + rings[0] = ptarray_addPoint(rings[0], (uchar *)&point2d, 2, 0); + + point2d.x = 0; + point2d.y = 10; + rings[0] = ptarray_addPoint(rings[0], (uchar *)&point2d, 2, 1); + + point2d.x = 10; + point2d.y = 10; + rings[0] = ptarray_addPoint(rings[0], (uchar *)&point2d, 2, 2); + + point2d.x = 10; + point2d.y = 0; + rings[0] = ptarray_addPoint(rings[0], (uchar *)&point2d, 2, 3); + + point2d.x = 0; + point2d.y = 0; + rings[0] = ptarray_addPoint(rings[0], (uchar *)&point2d, 2, 4); + + + /* Construct the second ring */ + rings[1] = ptarray_construct(0, 0, 0); + point2d.x = 3; + point2d.y = 3; + rings[1] = ptarray_addPoint(rings[1], (uchar *)&point2d, 2, 0); + + point2d.x = 3; + point2d.y = 6; + rings[1] = ptarray_addPoint(rings[1], (uchar *)&point2d, 2, 1); + + point2d.x = 6; + point2d.y = 6; + rings[1] = ptarray_addPoint(rings[1], (uchar *)&point2d, 2, 2); + + point2d.x = 6; + point2d.y = 3; + rings[1] = ptarray_addPoint(rings[1], (uchar *)&point2d, 2, 3); + + point2d.x = 3; + point2d.y = 3; + rings[1] = ptarray_addPoint(rings[1], (uchar *)&point2d, 2, 4); + + testpoly = lwpoly_construct(-1, NULL, 2, rings); + + /* Generate the LWGEOM from LWPOLY, then serialize it ready for the parser */ + lwgeom = lwpoly_as_lwgeom(testpoly); + serialized_lwgeom = lwgeom_serialize(lwgeom); + + /* Output the geometry in WKT and WKB */ + wkt = serialized_lwgeom_to_ewkt(serialized_lwgeom); + printf("WKT format : %s\n", wkt); + wkb = serialized_lwgeom_to_hexwkb(serialized_lwgeom, NDR, &wkb_size); + printf("HEXWKB format : %s\n\n", wkb); + + /* Free all of the allocated items */ + lwfree(wkb); + lwfree(wkt); + lwfree(serialized_lwgeom); + pfree_polygon(testpoly); +} -- 2.50.0