[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...

Steve

>>> 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
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:

(C++:)
extern "C" char GEOSisSimple(Geometry *g1);
char GEOSisSimple(Geometry *g1)
{
	try
	{
		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
	}
}



(C:)

Datum issimple(PG_FUNCTION_ARGS)
{
	GEOMETRY		*geom = (GEOMETRY *) 
PG_DETOAST_DATUM(PG_GETARG_DATUM(0));

	Geometry *g1;
	int result;

	if (geom->nobjs == 0)
		PG_RETURN_BOOL(true);

		initGEOS(MAXIMUM_ALIGNOF);

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

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

		result = GEOSisSimple(g1);
		GEOSdeleteGeometry(g1);

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

	PG_RETURN_BOOL(result);
}

_______________________________________________
geos-devel mailing list
geos-devel at geos.refractions.net
http://geos.refractions.net/mailman/listinfo/geos-devel




More information about the geos-devel mailing list