// [[Rcpp::export]] Rcpp::List CPL_curve_to_linestring(Rcpp::List sfc) { // need to pass more parameters? #nocov start std::vector<OGRGeometry *> g = ogr_from_sfc(sfc, NULL); std::vector<OGRGeometry *> out(g.size()); for (size_t i = 0; i < g.size(); i++) { OGRCurve *cs = (OGRCurve *) g[i]; out[i] = cs->CastToLineString(cs); } return sfc_from_ogr(out, true); // destroys out; } // #nocov end
// [[Rcpp::export]] Rcpp::List CPL_compoundcurve_to_linear(Rcpp::List sfc) { // need to pass more parameters? std::vector<OGRGeometry *> g = ogr_from_sfc(sfc, NULL); std::vector<OGRGeometry *> out(g.size()); for (size_t i = 0; i < g.size(); i++) { OGRCompoundCurve *cs = (OGRCompoundCurve *) g[i]; out[i] = cs->getLinearGeometry(); OGRGeometryFactory::destroyGeometry(g[i]); } return sfc_from_ogr(out, true); // destroys out; }
// [[Rcpp::export]] Rcpp::List CPL_circularstring_to_linestring(Rcpp::List sfc) { // need to pass more parameters? std::vector<OGRGeometry *> g = ogr_from_sfc(sfc, NULL); std::vector<OGRGeometry *> out(g.size()); for (size_t i = 0; i < g.size(); i++) { OGRCircularString *cs = (OGRCircularString *) g[i]; out[i] = cs->CurveToLine(); OGRGeometryFactory::destroyGeometry(g[i]); } return sfc_from_ogr(out, true); // destroys out; }
// [[Rcpp::export]] Rcpp::List CPL_roundtrip(Rcpp::List sfc) { // for debug purposes std::vector<OGRGeometry *> g = ogr_from_sfc(sfc, NULL); for (size_t i = 0; i < g.size(); i++) { char *out; g[i]->exportToWkt(&out); Rcpp::Rcout << out << std::endl; CPLFree(out); } return sfc_from_ogr(g, true); // destroys g; }
// [[Rcpp::export]] Rcpp::List CPL_wrap_dateline(Rcpp::List sfc, Rcpp::CharacterVector opt, bool quiet = true) { std::vector <char *> options = create_options(opt, quiet); std::vector<OGRGeometry *> g = ogr_from_sfc(sfc, NULL); std::vector<OGRGeometry *> ret(g.size()); for (size_t i = 0; i < g.size(); i++) { ret[i] = OGRGeometryFactory::transformWithOptions(g[i], NULL, options.data()); OGRGeometryFactory::destroyGeometry(g[i]); } return sfc_from_ogr(ret, true); // destroys ret; }
// [[Rcpp::export]] Rcpp::List CPL_gdal_segmentize(Rcpp::List sfc, double dfMaxLength = 0.0) { if (dfMaxLength <= 0.0) throw std::invalid_argument("argument dfMaxLength should be positive\n"); std::vector<OGRGeometry *> g = ogr_from_sfc(sfc, NULL); for (size_t i = 0; i < g.size(); i++) g[i]->segmentize(dfMaxLength); Rcpp::List ret = sfc_from_ogr(g, true); ret.attr("crs") = sfc.attr("crs"); return ret; }
// [[Rcpp::export]] Rcpp::List CPL_sfc_from_wkt(Rcpp::CharacterVector wkt) { std::vector<OGRGeometry *> g(wkt.size()); OGRGeometryFactory f; for (int i = 0; i < wkt.size(); i++) { char *wkt_str = wkt(i); #if GDAL_VERSION_MAJOR <= 2 && GDAL_VERSION_MINOR <= 2 handle_error(f.createFromWkt(&wkt_str, NULL, &(g[i]))); #else handle_error(f.createFromWkt( (const char*) wkt_str, NULL, &(g[i]))); #endif } return sfc_from_ogr(g, true); }
// [[Rcpp::export]] Rcpp::List CPL_multisurface_to_multipolygon(Rcpp::List sfc) { // need to pass more parameters? std::vector<OGRGeometry *> g = ogr_from_sfc(sfc, NULL); std::vector<OGRGeometry *> out(g.size()); for (size_t i = 0; i < g.size(); i++) { OGRMultiSurface *cs = (OGRMultiSurface *) g[i]; if (cs->hasCurveGeometry(true)) { out[i] = cs->getLinearGeometry(); OGRGeometryFactory::destroyGeometry(g[i]); } else out[i] = cs->CastToMultiPolygon(cs); // consumes cs #nocov if (out[i] == NULL) Rcpp::stop("CPL_multisurface_to_multipolygon: NULL returned - non-polygonal surface?"); // #nocov } return sfc_from_ogr(out, true); // destroys out; }
// [[Rcpp::export]] Rcpp::List CPL_gdal_linestring_sample(Rcpp::List sfc, Rcpp::List distLst) { if (sfc.size() != distLst.size()) throw std::invalid_argument("sfc and dist should have equal length"); std::vector<OGRGeometry *> g = ogr_from_sfc(sfc, NULL); std::vector<OGRGeometry *> out(g.size()); for (size_t i = 0; i < g.size(); i++) { OGRGeometryCollection *gc = new OGRGeometryCollection; Rcpp::NumericVector dists = distLst[i]; for (size_t j = 0; j < dists.size(); j++) { OGRPoint *poPoint = new OGRPoint; ((OGRLineString *) g[i])->Value(dists[j], poPoint); gc->addGeometry(poPoint); } out[i] = OGRGeometryFactory::forceToMultiPoint(gc); } Rcpp::List ret = sfc_from_ogr(out, true); ret.attr("crs") = sfc.attr("crs"); return ret; }
// [[Rcpp::export]] Rcpp::List CPL_transform(Rcpp::List sfc, Rcpp::CharacterVector proj4) { // import proj4string: OGRSpatialReference *dest = new OGRSpatialReference; handle_error(dest->importFromProj4((const char *) (proj4[0]))); // transform geometries: std::vector<OGRGeometry *> g = ogr_from_sfc(sfc, NULL); if (g.size() == 0) { dest->Release(); // #nocov Rcpp::stop("CPL_transform: zero length geometry list"); // #nocov } OGRCoordinateTransformation *ct = OGRCreateCoordinateTransformation(g[0]->getSpatialReference(), dest); if (ct == NULL) { dest->Release(); // #nocov Rcpp::stop("OGRCreateCoordinateTransformation() returned NULL: PROJ.4 available?"); // #nocov } for (size_t i = 0; i < g.size(); i++) { CPLPushErrorHandler(CPLQuietErrorHandler); OGRErr err = 0; if (! g[i]->IsEmpty()) err = g[i]->transform(ct); CPLPopErrorHandler(); if (err == 1 || err == 6) { OGRwkbGeometryType geomType = g[i]->getGeometryType(); OGRGeometryFactory f; f.destroyGeometry(g[i]); g[i] = f.createGeometry(geomType); } else handle_error(err); } Rcpp::List ret = sfc_from_ogr(g, true); // destroys g; ct->DestroyCT(ct); dest->Release(); return ret; }