[geos-devel] Examples using GEOS from C?

Steve Lime steve.lime at dnr.state.mn.us
Wed Jun 16 23:54:29 EDT 2004

Thanks Dave (and Norman)! Just the help I needed to get started. Let's
see if we can get buffering working...


>>> dblasby at refractions.net 06/16/04 11:23 AM >>>
Steve Lime wrote:
> Hi Folks: I'm starting to tinker with using GEOS with MapServer. I'm
> wondering if folks have any examples of using it from within straight
> code. I'd also be interested in learning more about the best ways to
> operate on non-GEOS geometries. I've heard folks mention that there
> should be more efficient ways than converting between representations.
> Thanks!

PostGIS is a C program, so if you look in there you'll see an example.

Basically, I wrote a very very simple C++ library 
(postgis_geos_wrapper.cpp) that interfaced with GEOS.   If you tag your 
C++ functions with 'extern "C"', you'll be able to call them from a C 
library (postgis_geos.c).

You'll have to write your own constructors that take the mapserv 
ShapeObjs and convert to GEOS geometries.  You can either construct 
brand-new real-live GEOS Coordinates, or you can wrap the ShapeObj 
coordinates with a CoordinateList class.

For example:

extern "C" char GEOSisSimple(Geometry *g1);
char GEOSisSimple(Geometry *g1)
		return g1->isSimple();
	catch (GEOSException *ge)
			// give error message as a NOTICE
		NOTICE_MESSAGE((char *)ge->toString().c_str());
		delete ge;
		return 2;  // error occured

	catch (...)
		return 2; //error occured


Datum issimple(PG_FUNCTION_ARGS)
	GEOMETRY		*geom = (GEOMETRY *) 

	Geometry *g1;
	int result;

	if (geom->nobjs == 0)


	//elog(NOTICE,"GEOS init()");

		g1 = 	POSTGIS2GEOS(geom ); // construct GEOS geometry

		result = GEOSisSimple(g1);

			if (result == 2)
				elog(ERROR,"GEOS issimple() threw an
				PG_RETURN_NULL(); //never get here


geos-devel mailing list
geos-devel at geos.refractions.net

More information about the geos-devel mailing list