long long msudf_intersects(UDF_INIT *initid,UDF_ARGS *args,char *is_null, char *error) { GEOSGeom geom1,geom2; long long result; DEBUG("msudf_intersects"); geom1 = msudf_getGeometry((unsigned char *)args->args[0],args->lengths[0]); if (geom1 == NULL) { strcpy(error,"Invalid geometry."); *is_null = 1; return 0; } geom2 = msudf_getGeometry((unsigned char *)args->args[1],args->lengths[1]); if (geom2 == NULL) { strcpy(error,"Invalid geometry."); *is_null = 1; return 0; } result = GEOSIntersects(geom1,geom2); if (geom1 != NULL) GEOSGeom_destroy(geom1); if (geom2 != NULL) GEOSGeom_destroy(geom2); if (result >1) { *is_null = 1; return 0; } else { return result; } }
void object::test<2>() { geom1_ = GEOSGeomFromWKT("POLYGON((1 1,1 5,5 5,5 1,1 1))"); geom2_ = GEOSGeomFromWKT("POINT(2 2)"); ensure( 0 != geom1_ ); ensure( 0 != geom2_ ); char const r1 = GEOSIntersects(geom1_, geom2_); ensure_equals(int(r1), 1); char const r2 = GEOSIntersects(geom2_, geom1_); ensure_equals(int(r2), 1); }
void object::test<1>() { geom1_ = GEOSGeomFromWKT("POLYGON EMPTY"); geom2_ = GEOSGeomFromWKT("POLYGON EMPTY"); ensure( 0 != geom1_ ); ensure( 0 != geom2_ ); char const r1 = GEOSIntersects(geom1_, geom2_); ensure_equals(r1, 0); char const r2 = GEOSIntersects(geom2_, geom1_); ensure_equals(r2, 0); }
void object::test<3>() { geom1_ = GEOSGeomFromWKT("MULTIPOLYGON(((0 0,0 10,10 10,10 0,0 0)))"); geom2_ = GEOSGeomFromWKT("POLYGON((1 1,1 2,2 2,2 1,1 1))"); ensure( 0 != geom1_ ); ensure( 0 != geom2_ ); char const r1 = GEOSIntersects(geom1_, geom2_); ensure_equals(int(r1), 1); char const r2 = GEOSIntersects(geom2_, geom1_); ensure_equals(int(r2), 1); }
void object::test<5>() { const char *hex = "0103000020E61000000100000005000000737979F3DDCC2CC0F92154F9E7534540000000000000F07F000000000000F07F8F806E993F7E55C0304B29FFEA8554400634E8D1DD424540B5FEE6A37FCD4540737979F3DDCC2CC0F92154F9E7534540"; geom1_ = GEOSGeomFromHEX_buf((unsigned char*)hex, std::strlen(hex)); ensure( 0 != geom1_ ); char const r1 = GEOSIntersects(geom1_, geom1_); ensure_equals(int(r1), 2); }
/* ** 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 }
void object::test<4>() { GEOSCoordSequence* cs = GEOSCoordSeq_create(5, 2); double nan = std::numeric_limits<double>::quiet_NaN(); GEOSCoordSeq_setX(cs, 0, 1); GEOSCoordSeq_setY(cs, 0, 1); for (unsigned int i=1; i<4; ++i) { GEOSCoordSeq_setX(cs, i, nan); GEOSCoordSeq_setY(cs, i, nan); } GEOSCoordSeq_setX(cs, 4, 1); GEOSCoordSeq_setY(cs, 4, 1); geom1_ = GEOSGeom_createPolygon(GEOSGeom_createLinearRing(cs), NULL, 0); char const r1 = GEOSIntersects(geom1_, geom1_); ensure_equals(int(r1), 2); }
bool PolygonReader::gridPointsInPolygon( std::vector<GridPointData> & pointsInPolygon, const GEOSGeom polygon ) { /* WdbProjectionPtr getWdbProjection(const std::string & def) { typedef std::map<std::string, WdbProjectionPtr> ProjectionMap; static ProjectionMap projections; ProjectionMap::iterator ret = projections.find(def); if ( ret == projections.end() ) // not found { // ensure that cache does not grow to ridiculous size if ( projections.size() > 512 ) projections.clear(); std::pair<ProjectionMap::iterator, bool> result = projections.insert(std::make_pair(def, WdbProjectionPtr(new WdbProjection(def)))); ret = result.first; } return ret->second; } */ BoundingBox bounds = getBounds( polygon ); //elog(DEBUG1, GEOSGeomToWKT(polygon) ); int startI = (bounds.left_ - reader_.placeSpecification().startX_) / reader_.placeSpecification().xIncrement_; if (startI < 0) startI = 0; int endI = ((bounds.right_ - reader_.placeSpecification().startX_) / reader_.placeSpecification().xIncrement_) + 1; if (endI > reader_.placeSpecification().xNumber_) endI = reader_.placeSpecification().xNumber_; int startJ = (bounds.bottom_ - reader_.placeSpecification().startY_) / reader_.placeSpecification().yIncrement_; if (startJ < 0) startJ = 0; int endJ = ((bounds.top_ - reader_.placeSpecification().startY_) / reader_.placeSpecification().yIncrement_) + 1; if (endJ > reader_.placeSpecification().yNumber_) endJ = reader_.placeSpecification().yNumber_; char res = 0; GEOSCoordSequence * seq; GEOSGeom point; double x; double y; for (int j = startJ; j < endJ; j++ ) { for (int i = startI; i < endI; i++) { x = reader_.placeSpecification().startX_ + (i * reader_.placeSpecification().xIncrement_); y = reader_.placeSpecification().startY_ + (j * reader_.placeSpecification().yIncrement_); WdbProjection prj( reader_.placeSpecification().projDefinition_ ); if ( ! isMetric( reader_.placeSpecification().projDefinition_ ) ) { x *= DEG_TO_RAD; y *= DEG_TO_RAD; } prj.transformToDefault( 1, &x, &y ); if ( ! isMetric( DEFAULT_PROJECTION ) ) { x *= RAD_TO_DEG; y *= RAD_TO_DEG; } // Intersects seq = GEOSCoordSeq_create(1, 2); GEOSCoordSeq_setX(seq, 0, x); GEOSCoordSeq_setY(seq, 0, y); point = GEOSGeom_createPoint(seq); //elog(DEBUG1, GEOSGeomToWKT(point) ); res = GEOS_DLL GEOSIntersects(polygon, point); if (res == 1) { GridPointData posPt; posPt.x = i; posPt.y = j; pointsInPolygon.push_back(posPt); } GEOSGeom_destroy(point); } } // Return return ( pointsInPolygon.size() > 0 ); }
Datum intersectsPrepared(PG_FUNCTION_ARGS) { #ifndef PREPARED_GEOM elog(ERROR,"Not implemented in this version!"); PG_RETURN_NULL(); /* never get here */ #else PG_LWGEOM * geom1; PG_LWGEOM * geom2; bool result; BOX2DFLOAT4 box1, box2; PrepGeomCache * prep_cache; int32 key1, key2; geom1 = (PG_LWGEOM *) PG_DETOAST_DATUM(PG_GETARG_DATUM(0)); geom2 = (PG_LWGEOM *) PG_DETOAST_DATUM(PG_GETARG_DATUM(1)); key1 = PG_GETARG_INT32(2); key2 = PG_GETARG_INT32(3); errorIfGeometryCollection(geom1,geom2); errorIfSRIDMismatch(pglwgeom_getSRID(geom1), pglwgeom_getSRID(geom2)); /* * short-circuit 1: if geom2 bounding box does not overlap * geom1 bounding box we can prematurely return FALSE. * Do the test IFF BOUNDING BOX AVAILABLE. */ if ( getbox2d_p(SERIALIZED_FORM(geom1), &box1) && getbox2d_p(SERIALIZED_FORM(geom2), &box2) ) { if (( box2.xmax < box1.xmin ) || ( box2.xmin > box1.xmax ) || ( box2.ymax < box1.ymin ) || ( box2.ymin > box1.ymax )) PG_RETURN_BOOL(FALSE); } prep_cache = GetPrepGeomCache( fcinfo, geom1, geom2 ); initGEOS(lwnotice, lwnotice); if ( prep_cache && prep_cache->prepared_geom ) { if ( prep_cache->argnum == 1 ) { GEOSGeom g = POSTGIS2GEOS(geom2); result = GEOSPreparedIntersects( prep_cache->prepared_geom, g); GEOSGeom_destroy(g); } else { GEOSGeom g = POSTGIS2GEOS(geom1); result = GEOSPreparedIntersects( prep_cache->prepared_geom, g); GEOSGeom_destroy(g); } } else { GEOSGeom g1 = POSTGIS2GEOS(geom1); GEOSGeom g2 = POSTGIS2GEOS(geom2); result = GEOSIntersects( g1, g2); GEOSGeom_destroy(g1); GEOSGeom_destroy(g2); } if (result == 2) { elog(ERROR,"GEOS contains() threw an error!"); PG_RETURN_NULL(); /* never get here */ } PG_FREE_IF_COPY(geom1, 0); PG_FREE_IF_COPY(geom2, 1); PG_RETURN_BOOL(result); #endif /* PREPARED_GEOM */ }
int main(int argc, char** argv) { GEOSGeometry* box; GEOSWKTReader* reader; clusterGIS_dataset* dataset; clusterGIS_record* record; clusterGIS_record** head; GEOSGeometry* record_geometry; int rank; double startprocessing; if(argc != 3) { fprintf(stderr, "Usage %s input output", argv[0]); exit(1); } clusterGIS_Init(&argc, &argv); MPI_Comm_rank(MPI_COMM_WORLD, &rank); reader = GEOSWKTReader_create(); box = GEOSWKTReader_read(reader, "POLYGON((-112.0859375 33.4349975585938,-112.0859375 33.4675445556641,-112.059799194336 33.4675445556641,-112.059799194336 33.4349975585938,-112.0859375 33.4349975585938))"); dataset = clusterGIS_Load_csv_distributed(MPI_COMM_WORLD, argv[1]); clusterGIS_Create_wkt_geometries(dataset, 1); startprocessing = MPI_Wtime(); record = dataset->data; head = &(dataset->data); /* keep records that match the criteria, otherwise delete them */ while(record != NULL) { char intersects; /*record_geometry = GEOSWKTReader_read(reader, record->data[1]); if(record_geometry == NULL) { fprintf(stderr, "%d: parse error\n", rank); MPI_Abort(MPI_COMM_WORLD, 2); }*/ intersects = GEOSIntersects(record->geometry, box); if(intersects == 2) { fprintf(stderr, "%d: error with overlap function\n", rank); MPI_Abort(MPI_COMM_WORLD, 2); } if(intersects == 1) { /* record overlaps with box */ head = &(record->next); record = record->next; } else if(intersects == 0) { /* no overlap */ *head = record->next; clusterGIS_Free_record(record); record = *head; } else { /* should never get here */ MPI_Abort(MPI_COMM_WORLD, 2); } } printf("%d: processing time %5.2fs\n", rank, MPI_Wtime() - startprocessing); clusterGIS_Write_csv_distributed(MPI_COMM_WORLD, argv[2], dataset); clusterGIS_Finalize(); return 0; }