void Polygon::initializeFromBounds(const BOX3D& box) { GEOSCoordSequence* coords = GEOSCoordSeq_create_r(m_ctx, 5, 3); auto set_coordinate = [coords, this](int pt_num, const double&x, const double& y, const double& z) { if (!GEOSCoordSeq_setX_r(m_ctx, coords, pt_num, x)) throw pdal_error("unable to set x for coordinate sequence"); if (!GEOSCoordSeq_setY_r(m_ctx, coords, pt_num, y)) throw pdal_error("unable to set y for coordinate sequence"); if (!GEOSCoordSeq_setZ_r(m_ctx, coords, pt_num, z)) throw pdal_error("unable to set z for coordinate sequence"); }; set_coordinate(0, box.minx, box.miny, box.minz); set_coordinate(1, box.minx, box.maxy, box.minz); set_coordinate(2, box.maxx, box.maxy, box.maxz); set_coordinate(3, box.maxx, box.miny, box.maxz); set_coordinate(4, box.minx, box.miny, box.minz); GEOSGeometry* ring = GEOSGeom_createLinearRing_r(m_ctx, coords); if (!ring) throw pdal_error("unable to create linear ring from BOX2D"); m_geom = GEOSGeom_createPolygon_r(m_ctx, ring, 0, 0); if (!m_geom) throw pdal_error("unable to create polygon from linear ring in " "BOX2D constructor"); prepare(); }
bool Polygon::covers(PointRef& ref) const { GEOSCoordSequence* coords = GEOSCoordSeq_create_r(m_ctx, 1, 3); if (!coords) throw pdal_error("Unable to allocate coordinate sequence"); const double x = ref.getFieldAs<double>(Dimension::Id::X); const double y = ref.getFieldAs<double>(Dimension::Id::Y); const double z = ref.getFieldAs<double>(Dimension::Id::Z); if (!GEOSCoordSeq_setX_r(m_ctx, coords, 0, x)) throw pdal_error("unable to set x for coordinate sequence"); if (!GEOSCoordSeq_setY_r(m_ctx, coords, 0, y)) throw pdal_error("unable to set y for coordinate sequence"); if (!GEOSCoordSeq_setZ_r(m_ctx, coords, 0, z)) throw pdal_error("unable to set z for coordinate sequence"); GEOSGeometry* p = GEOSGeom_createPoint_r(m_ctx, coords); if (!p) throw pdal_error("unable to allocate candidate test point"); bool covers = (bool)(GEOSPreparedCovers_r(m_ctx, m_prepGeom, p)); GEOSGeom_destroy_r(m_ctx, p); return covers; }
void Crop::crop(PointBuffer& input, PointBuffer& output) { bool logOutput = (log()->getLevel() > LogLevel::Debug4); if (logOutput) log()->floatPrecision(8); for (PointId idx = 0; idx < input.size(); ++idx) { double x = input.getFieldAs<double>(Dimension::Id::X, idx); double y = input.getFieldAs<double>(Dimension::Id::Y, idx); double z = input.getFieldAs<double>(Dimension::Id::Z, idx); if (logOutput) { log()->floatPrecision(10); log()->get(LogLevel::Debug5) << "input: " << x << " y: " << y << " z: " << z << std::endl; } if (m_poly.empty()) { // We don't have a polygon, just a bounds. Filter on that // by itself. if (!m_cropOutside && m_bounds.contains(x, y, z)) output.appendPoint(input, idx); } #ifdef PDAL_HAVE_GEOS else { int ret(0); // precise filtering based on the geometry GEOSCoordSequence* coords = GEOSCoordSeq_create_r(m_geosEnvironment, 1, 3); if (!coords) throw pdal_error("unable to allocate coordinate sequence"); ret = GEOSCoordSeq_setX_r(m_geosEnvironment, coords, 0, x); if (!ret) throw pdal_error("unable to set x for coordinate sequence"); ret = GEOSCoordSeq_setY_r(m_geosEnvironment, coords, 0, y); if (!ret) throw pdal_error("unable to set y for coordinate sequence"); ret = GEOSCoordSeq_setZ_r(m_geosEnvironment, coords, 0, z); if (!ret) throw pdal_error("unable to set z for coordinate sequence"); GEOSGeometry* p = GEOSGeom_createPoint_r(m_geosEnvironment, coords); if (!p) throw pdal_error("unable to allocate candidate test point"); if (static_cast<bool>(GEOSPreparedContains_r(m_geosEnvironment, m_geosPreparedGeometry, p)) != m_cropOutside) output.appendPoint(input, idx); GEOSGeom_destroy_r(m_geosEnvironment, p); } #endif } }
void terms_to_coordseq(GEOSCommand *command) { char *buffer = command->param_bytes; int *index = &command->index; int size; GEOSCoordSequence *coordSeq = NULL; GEOSContextHandle_t handle = command->driver_data->handle; if(ei_decode_list_header(buffer, index, &size)) { erl_send_error(command, "notalist"); return; } unsigned int i; for(i = 0; i < size; i++) { //for each element int digitc; double x,y,z; if(ei_decode_tuple_header(buffer, index, &digitc)) { erl_send_error(command, "invalidlist"); if(coordSeq!=NULL) GEOSCoordSeq_destroy_r(handle, coordSeq); return; } if(coordSeq == NULL) { coordSeq = GEOSCoordSeq_create_r(handle, size, digitc); if(coordSeq == NULL) { erl_send_error(command, "create"); return; } } if(next_number(command, &x) || GEOSCoordSeq_setX_r(handle, coordSeq, i, x)==0) { erl_send_error(command, "setx"); GEOSCoordSeq_destroy_r(handle, coordSeq); return; } if(next_number(command, &y) || GEOSCoordSeq_setY_r(handle, coordSeq, i, y)==0) { erl_send_error(command, "sety"); GEOSCoordSeq_destroy_r(handle, coordSeq); return; } if(digitc == 3) { if(next_number(command, &z) || GEOSCoordSeq_setZ_r(handle, coordSeq, i, z)==0) { erl_send_error(command, "setz"); GEOSCoordSeq_destroy_r(handle, coordSeq); return; } } } ERL_WRITE_PTR_GEOSCOORDSEQUENCE(command, coordSeq); }
static void populate_geom_into_coord_seq(GEOSContextHandle_t context, const GEOSGeometry* geom, GEOSCoordSequence* coord_seq, unsigned int i, char has_z) { const GEOSCoordSequence* cs = GEOSGeom_getCoordSeq_r(context, geom); double x = 0; if (cs) { GEOSCoordSeq_getX_r(context, cs, 0, &x); } GEOSCoordSeq_setX_r(context, coord_seq, i, x); x = 0; if (cs) { GEOSCoordSeq_getY_r(context, cs, 0, &x); } GEOSCoordSeq_setY_r(context, coord_seq, i, x); x = 0; if (has_z && cs) { GEOSCoordSeq_getZ_r(context, cs, 0, &x); } GEOSCoordSeq_setZ_r(context, coord_seq, i, x); }
VALUE rgeo_create_geos_point(VALUE factory, double x, double y, double z) { VALUE result = Qnil; RGeo_FactoryData* factory_data = RGEO_FACTORY_DATA_PTR(factory); GEOSContextHandle_t context = factory_data->geos_context; GEOSCoordSequence* coord_seq = GEOSCoordSeq_create_r(context, 1, 3); if (coord_seq) { if (GEOSCoordSeq_setX_r(context, coord_seq, 0, x)) { if (GEOSCoordSeq_setY_r(context, coord_seq, 0, y)) { if (GEOSCoordSeq_setZ_r(context, coord_seq, 0, z)) { GEOSGeometry* geom = GEOSGeom_createPoint_r(context, coord_seq); if (geom) { result = rgeo_wrap_geos_geometry(factory, geom, factory_data->globals->geos_point); } } } } } return result; }
GEOSGeometry* createGEOSPoint(GEOSContextHandle_t ctx, double x, double y, double z) { int ret(0); // precise filtering based on the geometry GEOSCoordSequence* coords = GEOSCoordSeq_create_r(ctx, 1, 3); if (!coords) throw pdal_error("unable to allocate coordinate sequence"); ret = GEOSCoordSeq_setX_r(ctx, coords, 0, x); if (!ret) throw pdal_error("unable to set x for coordinate sequence"); ret = GEOSCoordSeq_setY_r(ctx, coords, 0, y); if (!ret) throw pdal_error("unable to set y for coordinate sequence"); ret = GEOSCoordSeq_setZ_r(ctx, coords, 0, z); if (!ret) throw pdal_error("unable to set z for coordinate sequence"); GEOSGeometry* p = GEOSGeom_createPoint_r(ctx, coords); if (!p) throw pdal_error("unable to allocate candidate test point"); return p; }
Q_NOWARN_UNREACHABLE_POP // Return Nth vertex in GEOSGeometry as a POINT. // May return NULL if the geometry has NO vertexex. static GEOSGeometry *LWGEOM_GEOS_getPointN( const GEOSGeometry *g_in, uint32_t n ) { GEOSContextHandle_t handle = QgsGeos::getGEOSHandler(); uint32_t dims; const GEOSCoordSequence *seq_in = nullptr; GEOSCoordSeq seq_out; double val; uint32_t sz; int gn; GEOSGeometry *ret = nullptr; switch ( GEOSGeomTypeId_r( handle, g_in ) ) { case GEOS_MULTIPOINT: case GEOS_MULTILINESTRING: case GEOS_MULTIPOLYGON: case GEOS_GEOMETRYCOLLECTION: { for ( gn = 0; gn < GEOSGetNumGeometries_r( handle, g_in ); ++gn ) { const GEOSGeometry *g = GEOSGetGeometryN_r( handle, g_in, gn ); ret = LWGEOM_GEOS_getPointN( g, n ); if ( ret ) return ret; } break; } case GEOS_POLYGON: { ret = LWGEOM_GEOS_getPointN( GEOSGetExteriorRing_r( handle, g_in ), n ); if ( ret ) return ret; for ( gn = 0; gn < GEOSGetNumInteriorRings_r( handle, g_in ); ++gn ) { const GEOSGeometry *g = GEOSGetInteriorRingN_r( handle, g_in, gn ); ret = LWGEOM_GEOS_getPointN( g, n ); if ( ret ) return ret; } break; } case GEOS_POINT: case GEOS_LINESTRING: case GEOS_LINEARRING: break; } seq_in = GEOSGeom_getCoordSeq_r( handle, g_in ); if ( ! seq_in ) return nullptr; if ( ! GEOSCoordSeq_getSize_r( handle, seq_in, &sz ) ) return nullptr; if ( ! sz ) return nullptr; if ( ! GEOSCoordSeq_getDimensions_r( handle, seq_in, &dims ) ) return nullptr; seq_out = GEOSCoordSeq_create_r( handle, 1, dims ); if ( ! seq_out ) return nullptr; if ( ! GEOSCoordSeq_getX_r( handle, seq_in, n, &val ) ) return nullptr; if ( ! GEOSCoordSeq_setX_r( handle, seq_out, n, val ) ) return nullptr; if ( ! GEOSCoordSeq_getY_r( handle, seq_in, n, &val ) ) return nullptr; if ( ! GEOSCoordSeq_setY_r( handle, seq_out, n, val ) ) return nullptr; if ( dims > 2 ) { if ( ! GEOSCoordSeq_getZ_r( handle, seq_in, n, &val ) ) return nullptr; if ( ! GEOSCoordSeq_setZ_r( handle, seq_out, n, val ) ) return nullptr; } return GEOSGeom_createPoint_r( handle, seq_out ); }
static GEOSCoordSequence* coord_seq_from_array(VALUE factory, VALUE array, char close) { Check_Type(array, T_ARRAY); RGeo_FactoryData* factory_data = RGEO_FACTORY_DATA_PTR(factory); VALUE point_type = factory_data->globals->feature_point; unsigned int len = (unsigned int)RARRAY_LEN(array); char has_z = (char)(RGEO_FACTORY_DATA_PTR(factory)->flags & RGEO_FACTORYFLAGS_SUPPORTS_Z_OR_M); unsigned int dims = has_z ? 3 : 2; double* coords = ALLOC_N(double, len == 0 ? 1 : len * dims); if (!coords) { return NULL; } GEOSContextHandle_t context = factory_data->geos_context; unsigned int i; for (i=0; i<len; ++i) { char good = 0; const GEOSGeometry* entry_geom = rgeo_convert_to_geos_geometry(factory, rb_ary_entry(array, i), point_type); if (entry_geom) { const GEOSCoordSequence* entry_cs = GEOSGeom_getCoordSeq_r(context, entry_geom); if (entry_cs) { double x; if (GEOSCoordSeq_getX_r(context, entry_cs, 0, &x)) { coords[i*dims] = x; if (GEOSCoordSeq_getY_r(context, entry_cs, 0, &x)) { coords[i*dims+1] = x; good = 1; if (has_z) { if (GEOSCoordSeq_getZ_r(context, entry_cs, 0, &x)) { coords[i*dims+2] = x; } else { good = 0; } } } } } } if (!good) { free(coords); return NULL; } } if (len > 0 && close) { if (coords[0] == coords[(len-1)*dims] && coords[1] == coords[(len-1)*dims+1]) { close = 0; } } else { close = 0; } GEOSCoordSequence* coord_seq = GEOSCoordSeq_create_r(context, len + close, 3); if (coord_seq) { for (i=0; i<len; ++i) { GEOSCoordSeq_setX_r(context, coord_seq, i, coords[i*dims]); GEOSCoordSeq_setY_r(context, coord_seq, i, coords[i*dims+1]); GEOSCoordSeq_setZ_r(context, coord_seq, i, has_z ? coords[i*dims+2] : 0); } if (close) { GEOSCoordSeq_setX_r(context, coord_seq, len, coords[0]); GEOSCoordSeq_setY_r(context, coord_seq, len, coords[1]); GEOSCoordSeq_setZ_r(context, coord_seq, len, has_z ? coords[2] : 0); } } free(coords); return coord_seq; }