/* ** Is shape1 within shape2, returns MS_TRUE/MS_FALSE or -1 for an error. */ int msGEOSWithin(shapeObj *shape1, shapeObj *shape2) { #ifdef USE_GEOS GEOSGeom g1, g2; int result; if(!shape1 || !shape2) return -1; if(!shape1->geometry) /* if no geometry for shape1 then build one */ shape1->geometry = (GEOSGeom) msGEOSShape2Geometry(shape1); g1 = shape1->geometry; if(!g1) return -1; if(!shape2->geometry) /* if no geometry for shape2 then build one */ shape2->geometry = (GEOSGeom) msGEOSShape2Geometry(shape2); g2 = shape2->geometry; if(!g2) return -1; result = GEOSWithin(g1, g2); return ((result==2) ? -1 : result); #else msSetError(MS_GEOSERR, "GEOS support is not available.", "msGEOSWithin()"); return -1; #endif }
double msGEOSDistance(shapeObj *shape1, shapeObj *shape2) { #ifdef USE_GEOS GEOSGeom g1, g2; double distance; int result; if(!shape1 || !shape2) return -1; if(!shape1->geometry) /* if no geometry for shape1 then build one */ shape1->geometry = (GEOSGeom) msGEOSShape2Geometry(shape1); g1 = (GEOSGeom) shape1->geometry; if(!g1) return -1; if(!shape2->geometry) /* if no geometry for shape2 then build one */ shape2->geometry = (GEOSGeom) msGEOSShape2Geometry(shape2); g2 = (GEOSGeom) shape2->geometry; if(!g2) return -1; result = GEOSDistance(g1, g2, &distance); return ((result==0) ? -1 : distance); #else return msDistanceShapeToShape(shape1, shape2); /* fall back on brute force method (for MapScript) */ #endif }
shapeObj *msGEOSSymDifference(shapeObj *shape1, shapeObj *shape2) { #ifdef USE_GEOS GEOSGeom g1, g2, g3; if(!shape1 || !shape2) return NULL; if(!shape1->geometry) /* if no geometry for the shape then build one */ shape1->geometry = (GEOSGeom) msGEOSShape2Geometry(shape1); g1 = (GEOSGeom) shape1->geometry; if(!g1) return NULL; if(!shape2->geometry) /* if no geometry for the shape then build one */ shape2->geometry = (GEOSGeom) msGEOSShape2Geometry(shape2); g2 = (GEOSGeom) shape2->geometry; if(!g2) return NULL; g3 = GEOSSymDifference(g1, g2); return msGEOSGeometry2Shape(g3); #else msSetError(MS_GEOSERR, "GEOS support is not available.", "msGEOSSymDifference()"); return NULL; #endif }
pointObj *msGEOSGetCentroid(shapeObj *shape) { #ifdef USE_GEOS GEOSGeom g1, g2; GEOSCoordSeq coords; pointObj *point; if(!shape) return NULL; if(!shape->geometry) /* if no geometry for the shape then build one */ shape->geometry = (GEOSGeom) msGEOSShape2Geometry(shape); g1 = (GEOSGeom) shape->geometry; if(!g1) return NULL; g2 = GEOSGetCentroid(g1); point = (pointObj *) malloc(sizeof(pointObj)); coords = (GEOSCoordSeq) GEOSGeom_getCoordSeq(g2); GEOSCoordSeq_getX(coords, 0, &(point->x)); GEOSCoordSeq_getY(coords, 0, &(point->y)); /* GEOSCoordSeq_getZ(coords, 0, &(point->z)); */ GEOSCoordSeq_destroy(coords); return point; #else msSetError(MS_GEOSERR, "GEOS support is not available.", "msGEOSGetCentroid()"); return NULL; #endif }
double msGEOSLength(shapeObj *shape) { #if defined(USE_GEOS) && defined(GEOS_CAPI_VERSION_MAJOR) && defined(GEOS_CAPI_VERSION_MINOR) && (GEOS_CAPI_VERSION_MAJOR > 1 || GEOS_CAPI_VERSION_MINOR >= 1) GEOSGeom g; double length; int result; if(!shape) return -1; if(!shape->geometry) /* if no geometry for the shape then build one */ shape->geometry = (GEOSGeom) msGEOSShape2Geometry(shape); g = (GEOSGeom) shape->geometry; if(!g) return -1; result = GEOSLength(g, &length); return ((result==0) ? -1 : length); #elif defined(USE_GEOS) msSetError(MS_GEOSERR, "GEOS support enabled, but old version lacks GEOSLength().", "msGEOSLength()"); return -1; #else msSetError(MS_GEOSERR, "GEOS support is not available.", "msGEOSLength()"); return -1; #endif }
/* ** Does shape1 intersect shape2, returns MS_TRUE/MS_FALSE or -1 for an error. */ int msGEOSIntersects(shapeObj *shape1, shapeObj *shape2) { #ifdef USE_GEOS GEOSGeom g1, g2; int result; if(!shape1 || !shape2) return -1; if(!shape1->geometry) /* if no geometry for shape1 then build one */ shape1->geometry = (GEOSGeom) msGEOSShape2Geometry(shape1); g1 = (GEOSGeom) shape1->geometry; if(!g1) return -1; if(!shape2->geometry) /* if no geometry for shape2 then build one */ shape2->geometry = (GEOSGeom) msGEOSShape2Geometry(shape2); g2 = (GEOSGeom) shape2->geometry; if(!g2) return -1; result = GEOSIntersects(g1, g2); return ((result==2) ? -1 : result); #else if(!shape1 || !shape2) return -1; switch(shape1->type) { /* todo: deal with point shapes */ case(MS_SHAPE_LINE): switch(shape2->type) { case(MS_SHAPE_LINE): return msIntersectPolylines(shape1, shape2); case(MS_SHAPE_POLYGON): return msIntersectPolylinePolygon(shape1, shape2); } break; case(MS_SHAPE_POLYGON): switch(shape2->type) { case(MS_SHAPE_LINE): return msIntersectPolylinePolygon(shape2, shape1); case(MS_SHAPE_POLYGON): return msIntersectPolygons(shape1, shape2); } break; } return -1; #endif }
shapeObj *msGEOSBoundary(shapeObj *shape) { #ifdef USE_GEOS GEOSGeom g1, g2; if(!shape) return NULL; if(!shape->geometry) /* if no geometry for the shape then build one */ shape->geometry = (GEOSGeom) msGEOSShape2Geometry(shape); g1 = (GEOSGeom) shape->geometry; if(!g1) return NULL; g2 = GEOSBoundary(g1); return msGEOSGeometry2Shape(g2); #else msSetError(MS_GEOSERR, "GEOS support is not available.", "msGEOSBoundary()"); return NULL; #endif }
shapeObj *msGEOSOffsetCurve(shapeObj *p, double offset) { #if defined USE_GEOS && defined HAVE_GEOS_OFFSET_CURVE GEOSGeom g1, g2; if(!p) return NULL; if(!p->geometry) /* if no geometry for the shape then build one */ p->geometry = (GEOSGeom) msGEOSShape2Geometry(p); g1 = (GEOSGeom) p->geometry; if(!g1) return NULL; g2 = GEOSOffsetCurve(g1, offset, 4, GEOSBUF_JOIN_MITRE, fabs(offset*1.5)); return msGEOSGeometry2Shape(g2); #else msSetError(MS_GEOSERR, "GEOS support is not available.", "msGEOSingleSidedBuffer()"); return NULL; #endif }
shapeObj *msGEOSTopologyPreservingSimplify(shapeObj *shape, double tolerance) { #ifdef USE_GEOS GEOSGeom g1, g2; if(!shape) return NULL; if(!shape->geometry) /* if no geometry for the shape then build one */ shape->geometry = (GEOSGeom) msGEOSShape2Geometry(shape); g1 = (GEOSGeom) shape->geometry; if(!g1) return NULL; g2 = GEOSTopologyPreserveSimplify(g1, tolerance); return msGEOSGeometry2Shape(g2); #else msSetError(MS_GEOSERR, "GEOS Simplify support is not available.", "msGEOSTopologyPreservingSimplify()"); return NULL; #endif }
/* Return should be freed with msGEOSFreeWKT */ char *msGEOSShapeToWKT(shapeObj *shape) { #ifdef USE_GEOS GEOSGeom g; if(!shape) return NULL; /* if we have a geometry, we should update it*/ msGEOSFreeGeometry(shape); shape->geometry = (GEOSGeom) msGEOSShape2Geometry(shape); g = (GEOSGeom) shape->geometry; if(!g) return NULL; return GEOSGeomToWKT(g); #else msSetError(MS_GEOSERR, "GEOS support is not available.", "msGEOSShapeToWKT()"); return NULL; #endif }