[geos-devel] Examples using GEOS from C?

David Blasby dblasby at refractions.net
Wed Jun 16 12:23:26 EDT 2004

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 C
> 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 *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 error!");
				PG_RETURN_NULL(); //never get here


More information about the geos-devel mailing list