shapeObj *msGEOSIntersection(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 = GEOSIntersection(g1, g2); return msGEOSGeometry2Shape(g3); #else msSetError(MS_GEOSERR, "GEOS support is not available.", "msGEOSIntersection()"); return NULL; #endif }
char *msudf_intersection(UDF_INIT *initid,UDF_ARGS *args, char *buf, unsigned long *length, char *is_null, char *error) { char *result; GEOSGeom geomFirst,geomSecond,geomResult; DEBUG("msudf_intersection"); geomFirst = msudf_getGeometry((unsigned char *)args->args[0],args->lengths[0]); if (geomFirst == NULL) { strcpy(error,"Invalid geometry."); *is_null = 1; return 0; } geomSecond = msudf_getGeometry((unsigned char *)args->args[1],args->lengths[1]); if (geomSecond == NULL) { GEOSGeom_destroy(geomFirst); strcpy(error,"Invalid geometry."); *is_null = 1; return 0; } geomResult = GEOSIntersection(geomFirst,geomSecond); if (geomResult!= NULL) { GEOSSetSRID(geomResult,GEOSGetSRID(geomFirst)); result = msudf_returnGeometry(initid,length,geomResult); GEOSGeom_destroy(geomFirst); GEOSGeom_destroy(geomSecond); GEOSGeom_destroy(geomResult); return result; } else { GEOSGeom_destroy(geomFirst); GEOSGeom_destroy(geomSecond); *is_null = 1; return NULL; } }
void QgsZonalStatistics::statisticsFromMiddlePointTest_improved( void* band, QgsGeometry* poly, int pixelOffsetX, int pixelOffsetY, int nCellsX, int nCellsY, double cellSizeX, double cellSizeY, const QgsRectangle& rasterBBox, double& sum, double& count ) { double cellCenterX, cellCenterY; QgsPoint currentCellCenter; float* scanLine = ( float * ) CPLMalloc( sizeof( float ) * nCellsX ); cellCenterY = rasterBBox.yMaximum() - pixelOffsetY * cellSizeY - cellSizeY / 2; count = 0; sum = 0; for ( int i = 0; i < nCellsY; ++i ) { GDALRasterIO( band, GF_Read, pixelOffsetX, pixelOffsetY + i, nCellsX, 1, scanLine, nCellsX, 1, GDT_Float32, 0, 0 ); cellCenterX = rasterBBox.xMinimum() + pixelOffsetX * cellSizeX + cellSizeX / 2; //do intersection of scanline with geometry GEOSCoordSequence* scanLineSequence = GEOSCoordSeq_create( 2, 2 ); GEOSCoordSeq_setX( scanLineSequence, 0, cellCenterX ); GEOSCoordSeq_setY( scanLineSequence, 0, cellCenterY ); GEOSCoordSeq_setX( scanLineSequence, 1, cellCenterX + nCellsX * cellSizeX ); GEOSCoordSeq_setY( scanLineSequence, 1, cellCenterY ); GEOSGeometry* scanLineGeos = GEOSGeom_createLineString( scanLineSequence ); //todo: delete GEOSGeometry* polyGeos = poly->asGeos(); GEOSGeometry* scanLineIntersection = GEOSIntersection( scanLineGeos, polyGeos ); GEOSGeom_destroy( scanLineGeos ); if ( !scanLineIntersection ) { cellCenterY -= cellSizeY; continue; } //debug //char* scanLineIntersectionType = GEOSGeomType( scanLineIntersection ); int numGeoms = GEOSGetNumGeometries( scanLineIntersection ); if ( numGeoms < 1 ) { GEOSGeom_destroy( scanLineIntersection ); cellCenterY -= cellSizeY; continue; } QList<double> scanLineList; double currentValue; GEOSGeometry* currentGeom = 0; for ( int z = 0; z < numGeoms; ++z ) { if ( numGeoms == 1 ) { currentGeom = scanLineIntersection; } else { currentGeom = GEOSGeom_clone( GEOSGetGeometryN( scanLineIntersection, z ) ); } const GEOSCoordSequence* scanLineCoordSequence = GEOSGeom_getCoordSeq( currentGeom ); if ( !scanLineCoordSequence ) { //error } unsigned int scanLineIntersectionSize; GEOSCoordSeq_getSize( scanLineCoordSequence, &scanLineIntersectionSize ); if ( !scanLineCoordSequence || scanLineIntersectionSize < 2 || ( scanLineIntersectionSize & 1 ) ) { //error } for ( unsigned int k = 0; k < scanLineIntersectionSize; ++k ) { GEOSCoordSeq_getX( scanLineCoordSequence, k, ¤tValue ); scanLineList.push_back( currentValue ); } if ( numGeoms != 1 ) { GEOSGeom_destroy( currentGeom ); } } GEOSGeom_destroy( scanLineIntersection ); qSort( scanLineList ); if ( scanLineList.size() < 1 ) { cellCenterY -= cellSizeY; continue; } int listPlace = -1; for ( int j = 0; j < nCellsX; ++j ) { //currentCellCenter = QgsPoint( cellCenterX, cellCenterY ); //instead of doing a contained test every time, find the place of scanLineList and check if even / odd if ( listPlace >= scanLineList.size() - 1 ) { break; } if ( cellCenterX >= scanLineList.at( listPlace + 1 ) ) { ++listPlace; if ( listPlace >= scanLineList.size() ) { break; } } if ( listPlace >= 0 && listPlace < ( scanLineList.size() - 1 ) && !( listPlace & 1 ) ) { if ( scanLine[j] != mInputNodataValue ) //don't consider nodata values { sum += scanLine[j]; ++count; } } cellCenterX += cellSizeX; } cellCenterY -= cellSizeY; } CPLFree( scanLine ); }
LWGEOM * lwgeom_intersection(const LWGEOM *geom1, const LWGEOM *geom2) { LWGEOM *result ; GEOSGeometry *g1, *g2, *g3 ; int is3d ; int srid ; /* A.Intersection(Empty) == Empty */ if ( lwgeom_is_empty(geom2) ) return lwgeom_clone(geom2); /* Empty.Intersection(A) == Empty */ if ( lwgeom_is_empty(geom1) ) return lwgeom_clone(geom1); /* ensure srids are identical */ srid = (int)(geom1->srid); error_if_srid_mismatch(srid, (int)(geom2->srid)); is3d = (FLAGS_GET_Z(geom1->flags) || FLAGS_GET_Z(geom2->flags)) ; initGEOS(lwnotice, lwgeom_geos_error); LWDEBUG(3, "intersection() START"); g1 = LWGEOM2GEOS(geom1); if ( 0 == g1 ) /* exception thrown at construction */ { lwerror("First argument geometry could not be converted to GEOS."); return NULL ; } g2 = LWGEOM2GEOS(geom2); if ( 0 == g2 ) /* exception thrown at construction */ { lwerror("Second argument geometry could not be converted to GEOS."); GEOSGeom_destroy(g1); return NULL ; } LWDEBUG(3, " constructed geometrys - calling geos"); LWDEBUGF(3, " g1 = %s", GEOSGeomToWKT(g1)); LWDEBUGF(3, " g2 = %s", GEOSGeomToWKT(g2)); /*LWDEBUGF(3, "g2 is valid = %i",GEOSisvalid(g2)); */ /*LWDEBUGF(3, "g1 is valid = %i",GEOSisvalid(g1)); */ g3 = GEOSIntersection(g1,g2); LWDEBUG(3, " intersection finished"); if (g3 == NULL) { GEOSGeom_destroy(g1); GEOSGeom_destroy(g2); lwerror("Error performing intersection: %s", lwgeom_geos_errmsg); return NULL; /* never get here */ } LWDEBUGF(3, "result: %s", GEOSGeomToWKT(g3) ) ; GEOSSetSRID(g3, srid); result = GEOS2LWGEOM(g3, is3d); if (result == NULL) { GEOSGeom_destroy(g1); GEOSGeom_destroy(g2); GEOSGeom_destroy(g3); lwerror("Error performing intersection: GEOS2LWGEOM: %s", lwgeom_geos_errmsg); return NULL ; /* never get here */ } GEOSGeom_destroy(g1); GEOSGeom_destroy(g2); GEOSGeom_destroy(g3); return result ; }