GEOSGeometry* rgeo_convert_to_detached_geos_geometry(VALUE obj, VALUE factory, VALUE type, VALUE* klasses) { VALUE object; GEOSGeometry* geom; RGeo_GeometryData* object_data; const GEOSPreparedGeometry* prep; RGeo_Globals* globals; if (klasses) { *klasses = Qnil; } globals = RGEO_FACTORY_DATA_PTR(factory)->globals; object = rb_funcall(globals->feature_module, globals->id_cast, 5, obj, factory, type, globals->sym_force_new, globals->sym_keep_subtype); geom = NULL; if (!NIL_P(object)) { object_data = RGEO_GEOMETRY_DATA_PTR(object); geom = object_data->geom; if (klasses) { *klasses = object_data->klasses; if (NIL_P(*klasses)) { *klasses = CLASS_OF(object); } } prep = object_data->prep; if (prep && prep != (GEOSPreparedGeometry*)1 && prep != (GEOSPreparedGeometry*)2) { GEOSPreparedGeom_destroy_r(object_data->geos_context, prep); } object_data->geos_context = NULL; object_data->geom = NULL; object_data->prep = NULL; object_data->factory = Qnil; object_data->klasses = Qnil; } return geom; }
void QgsZonalStatistics::statisticsFromMiddlePointTest( 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; float* scanLine = ( float * ) CPLMalloc( sizeof( float ) * nCellsX ); cellCenterY = rasterBBox.yMaximum() - pixelOffsetY * cellSizeY - cellSizeY / 2; count = 0; sum = 0; const GEOSGeometry* polyGeos = poly->asGeos(); if ( !polyGeos ) { return; } GEOSContextHandle_t geosctxt = QgsGeometry::getGEOSHandler(); const GEOSPreparedGeometry* polyGeosPrepared = GEOSPrepare_r( geosctxt, poly->asGeos() ); if ( !polyGeosPrepared ) { return; } GEOSCoordSequence* cellCenterCoords = 0; GEOSGeometry* currentCellCenter = 0; for ( int i = 0; i < nCellsY; ++i ) { if ( GDALRasterIO( band, GF_Read, pixelOffsetX, pixelOffsetY + i, nCellsX, 1, scanLine, nCellsX, 1, GDT_Float32, 0, 0 ) != CPLE_None ) { continue; } cellCenterX = rasterBBox.xMinimum() + pixelOffsetX * cellSizeX + cellSizeX / 2; for ( int j = 0; j < nCellsX; ++j ) { GEOSGeom_destroy_r( geosctxt, currentCellCenter ); cellCenterCoords = GEOSCoordSeq_create_r( geosctxt, 1, 2 ); GEOSCoordSeq_setX_r( geosctxt, cellCenterCoords, 0, cellCenterX ); GEOSCoordSeq_setY_r( geosctxt, cellCenterCoords, 0, cellCenterY ); currentCellCenter = GEOSGeom_createPoint_r( geosctxt, cellCenterCoords ); if ( scanLine[j] != mInputNodataValue ) //don't consider nodata values { if ( GEOSPreparedContains_r( geosctxt, polyGeosPrepared, currentCellCenter ) ) { if ( !qIsNaN( scanLine[j] ) ) { sum += scanLine[j]; } ++count; } } cellCenterX += cellSizeX; } cellCenterY -= cellSizeY; } CPLFree( scanLine ); GEOSPreparedGeom_destroy_r( geosctxt, polyGeosPrepared ); }
GEOSGeometry *_project_line_string(GEOSContextHandle_t handle, GEOSGeometry *g_line_string, Interpolator *interpolator, GEOSGeometry *g_domain, double threshold) { const GEOSCoordSequence *src_coords = GEOSGeom_getCoordSeq_r(handle, g_line_string); unsigned int src_size, src_idx; const GEOSPreparedGeometry *gp_domain = GEOSPrepare_r(handle, g_domain); GEOSCoordSeq_getSize_r(handle, src_coords, &src_size); // check exceptions LineAccumulator lines; for(src_idx = 1; src_idx < src_size; src_idx++) { _project_segment(handle, src_coords, src_idx - 1, src_idx, interpolator, gp_domain, threshold, lines); } GEOSPreparedGeom_destroy_r(handle, gp_domain); return lines.as_geom(handle); }
void QgsZonalStatistics::statisticsFromMiddlePointTest( void* band, const QgsGeometry& poly, int pixelOffsetX, int pixelOffsetY, int nCellsX, int nCellsY, double cellSizeX, double cellSizeY, const QgsRectangle& rasterBBox, FeatureStats &stats ) { double cellCenterX, cellCenterY; float* scanLine = ( float * ) CPLMalloc( sizeof( float ) * nCellsX ); cellCenterY = rasterBBox.yMaximum() - pixelOffsetY * cellSizeY - cellSizeY / 2; stats.reset(); GEOSGeometry* polyGeos = poly.exportToGeos(); if ( !polyGeos ) { return; } GEOSContextHandle_t geosctxt = QgsGeometry::getGEOSHandler(); const GEOSPreparedGeometry* polyGeosPrepared = GEOSPrepare_r( geosctxt, polyGeos ); if ( !polyGeosPrepared ) { GEOSGeom_destroy_r( geosctxt, polyGeos ); return; } GEOSCoordSequence* cellCenterCoords = nullptr; GEOSGeometry* currentCellCenter = nullptr; for ( int i = 0; i < nCellsY; ++i ) { if ( GDALRasterIO( band, GF_Read, pixelOffsetX, pixelOffsetY + i, nCellsX, 1, scanLine, nCellsX, 1, GDT_Float32, 0, 0 ) != CPLE_None ) { continue; } cellCenterX = rasterBBox.xMinimum() + pixelOffsetX * cellSizeX + cellSizeX / 2; for ( int j = 0; j < nCellsX; ++j ) { if ( validPixel( scanLine[j] ) ) { GEOSGeom_destroy_r( geosctxt, currentCellCenter ); cellCenterCoords = GEOSCoordSeq_create_r( geosctxt, 1, 2 ); GEOSCoordSeq_setX_r( geosctxt, cellCenterCoords, 0, cellCenterX ); GEOSCoordSeq_setY_r( geosctxt, cellCenterCoords, 0, cellCenterY ); currentCellCenter = GEOSGeom_createPoint_r( geosctxt, cellCenterCoords ); if ( GEOSPreparedContains_r( geosctxt, polyGeosPrepared, currentCellCenter ) ) { stats.addValue( scanLine[j] ); } } cellCenterX += cellSizeX; } cellCenterY -= cellSizeY; } GEOSGeom_destroy_r( geosctxt, currentCellCenter ); CPLFree( scanLine ); GEOSPreparedGeom_destroy_r( geosctxt, polyGeosPrepared ); GEOSGeom_destroy_r( geosctxt, polyGeos ); }
Polygon::~Polygon() { if (m_geom) GEOSGeom_destroy_r(m_ctx, m_geom); if (m_prepGeom) GEOSPreparedGeom_destroy_r(m_ctx, m_prepGeom); m_geom = 0; m_prepGeom = 0; }
void QgsZonalStatistics::statisticsFromMiddlePointTest( const QgsGeometry &poly, int pixelOffsetX, int pixelOffsetY, int nCellsX, int nCellsY, double cellSizeX, double cellSizeY, const QgsRectangle &rasterBBox, FeatureStats &stats ) { double cellCenterX, cellCenterY; cellCenterY = rasterBBox.yMaximum() - pixelOffsetY * cellSizeY - cellSizeY / 2; stats.reset(); GEOSGeometry *polyGeos = poly.exportToGeos(); if ( !polyGeos ) { return; } GEOSContextHandle_t geosctxt = QgsGeometry::getGEOSHandler(); const GEOSPreparedGeometry *polyGeosPrepared = GEOSPrepare_r( geosctxt, polyGeos ); if ( !polyGeosPrepared ) { GEOSGeom_destroy_r( geosctxt, polyGeos ); return; } GEOSCoordSequence *cellCenterCoords = nullptr; GEOSGeometry *currentCellCenter = nullptr; QgsRectangle featureBBox = poly.boundingBox().intersect( &rasterBBox ); QgsRectangle intersectBBox = rasterBBox.intersect( &featureBBox ); QgsRasterBlock *block = mRasterProvider->block( mRasterBand, intersectBBox, nCellsX, nCellsY ); for ( int i = 0; i < nCellsY; ++i ) { cellCenterX = rasterBBox.xMinimum() + pixelOffsetX * cellSizeX + cellSizeX / 2; for ( int j = 0; j < nCellsX; ++j ) { if ( validPixel( block->value( i, j ) ) ) { GEOSGeom_destroy_r( geosctxt, currentCellCenter ); cellCenterCoords = GEOSCoordSeq_create_r( geosctxt, 1, 2 ); GEOSCoordSeq_setX_r( geosctxt, cellCenterCoords, 0, cellCenterX ); GEOSCoordSeq_setY_r( geosctxt, cellCenterCoords, 0, cellCenterY ); currentCellCenter = GEOSGeom_createPoint_r( geosctxt, cellCenterCoords ); if ( GEOSPreparedContains_r( geosctxt, polyGeosPrepared, currentCellCenter ) ) { stats.addValue( block->value( i, j ) ); } } cellCenterX += cellSizeX; } cellCenterY -= cellSizeY; } GEOSGeom_destroy_r( geosctxt, currentCellCenter ); GEOSPreparedGeom_destroy_r( geosctxt, polyGeosPrepared ); GEOSGeom_destroy_r( geosctxt, polyGeos ); delete block; }
void Crop::done(PointContext ctx) { #ifdef PDAL_HAVE_GEOS if (m_geosPreparedGeometry) GEOSPreparedGeom_destroy_r(m_geosEnvironment, m_geosPreparedGeometry); if (m_geosGeometry) GEOSGeom_destroy_r(m_geosEnvironment, m_geosGeometry); if (m_geosEnvironment) finishGEOS_r(m_geosEnvironment); #endif }
static void free_geos_prepared_geom(void* data) { if (data == NULL) { return; } geos_prepared_geometry_t* geom = (geos_prepared_geometry_t*)data; GEOSPreparedGeom_destroy_r(geom->context, geom->geometry); geom->context = NULL; geom->geometry = NULL; sqlite3_free(data); }
static void destroy_geometry_func(RGeo_GeometryData* data) { const GEOSPreparedGeometry* prep; if (data->geom) { GEOSGeom_destroy_r(data->geos_context, data->geom); } prep = data->prep; if (prep && prep != (const GEOSPreparedGeometry*)1 && prep != (const GEOSPreparedGeometry*)2 && prep != (const GEOSPreparedGeometry*)3) { GEOSPreparedGeom_destroy_r(data->geos_context, prep); } free(data); }
static VALUE method_geometry_initialize_copy(VALUE self, VALUE orig) { RGeo_GeometryData* self_data; const GEOSPreparedGeometry* prep; const GEOSGeometry* geom; RGeo_GeometryData* orig_data; GEOSContextHandle_t orig_context; GEOSGeometry* clone_geom; RGeo_FactoryData* factory_data; // Clear out any existing value self_data = RGEO_GEOMETRY_DATA_PTR(self); if (self_data->geom) { GEOSGeom_destroy_r(self_data->geos_context, self_data->geom); self_data->geom = NULL; } prep = self_data->prep; if (prep && prep != (GEOSPreparedGeometry*)1 && prep != (GEOSPreparedGeometry*)2) { GEOSPreparedGeom_destroy_r(self_data->geos_context, prep); } self_data->prep = NULL; self_data->geos_context = NULL; self_data->factory = Qnil; self_data->klasses = Qnil; // Copy value from orig geom = rgeo_get_geos_geometry_safe(orig); if (geom) { orig_data = RGEO_GEOMETRY_DATA_PTR(orig); orig_context = orig_data->geos_context; clone_geom = GEOSGeom_clone_r(orig_context, geom); if (clone_geom) { factory_data = RGEO_FACTORY_DATA_PTR(orig_data->factory); GEOSSetSRID_r(orig_context, clone_geom, GEOSGetSRID_r(orig_context, geom)); self_data->geom = clone_geom; self_data->geos_context = orig_context; self_data->prep = factory_data && (factory_data->flags & RGEO_FACTORYFLAGS_PREPARE_HEURISTIC != 0) ? (GEOSPreparedGeometry*)1 : NULL; self_data->factory = orig_data->factory; self_data->klasses = orig_data->klasses; } } return self; }
static geos_prepared_geometry_t *get_geos_prepared_geom(sqlite3_context *context, const geos_context_t *geos_context, sqlite3_value *value, errorstream_t *error) { geom_blob_header_t header; uint8_t *blob = (uint8_t *)sqlite3_value_blob(value); size_t blob_length = (size_t) sqlite3_value_bytes(value); if (blob == NULL) { return NULL; } binstream_t stream; binstream_init(&stream, blob, blob_length); geos_writer_t writer; geos_writer_init_srid(&writer, geos_context->geos_handle, header.srid); geos_context->spatialdb->read_blob_header(&stream, &header, error); geos_context->spatialdb->read_geometry(&stream, geos_writer_geom_consumer(&writer), error); GEOSGeometry *g = geos_writer_getgeometry(&writer); geos_writer_destroy(&writer, g == NULL); if (g == NULL) { return NULL; } struct GEOSPrepGeom_t const *prepared_g = GEOSPrepare_r(geos_context->geos_handle, g); if (prepared_g == NULL) { return NULL; } geos_prepared_geometry_t *result = sqlite3_malloc(sizeof(geos_prepared_geometry_t)); if (result == NULL) { GEOSPreparedGeom_destroy_r(geos_context->geos_handle, prepared_g); return NULL; } result->context = geos_context->geos_handle; result->geometry = prepared_g; result->srid = header.srid; return result; }
static VALUE method_geometry_steal(VALUE self, VALUE orig) { RGeo_GeometryData* self_data; const GEOSPreparedGeometry* prep; const GEOSGeometry* geom; RGeo_GeometryData* orig_data; geom = rgeo_get_geos_geometry_safe(orig); if (geom) { // Clear out any existing value self_data = RGEO_GEOMETRY_DATA_PTR(self); if (self_data->geom) { GEOSGeom_destroy_r(self_data->geos_context, self_data->geom); } prep = self_data->prep; if (prep && prep != (GEOSPreparedGeometry*)1 && prep != (GEOSPreparedGeometry*)2) { GEOSPreparedGeom_destroy_r(self_data->geos_context, prep); } // Steal value from orig orig_data = RGEO_GEOMETRY_DATA_PTR(orig); self_data->geom = orig_data->geom; self_data->prep = orig_data->prep; self_data->geos_context = orig_data->geos_context; self_data->factory = orig_data->factory; self_data->klasses = orig_data->klasses; // Clear out orig orig_data->geom = NULL; orig_data->prep = NULL; orig_data->geos_context = NULL; orig_data->factory = Qnil; orig_data->klasses = Qnil; } return self; }