From feefcd4bc363767447de9f2cac5eec131dca0e2c Mon Sep 17 00:00:00 2001 From: SymbolixAU Date: Wed, 11 Sep 2019 15:46:05 +1000 Subject: [PATCH] need to begin_wkt some how for #47 --- .../geojsonsf/geojson/write_geometry.hpp | 2 +- src/geojson_to_wkt.cpp | 2 +- src/geojson_wkt.cpp | 51 ++++++++++++++----- 3 files changed, 41 insertions(+), 14 deletions(-) diff --git a/inst/include/geojsonsf/geojson/write_geometry.hpp b/inst/include/geojsonsf/geojson/write_geometry.hpp index 2eb7a2a..dd3b7ec 100644 --- a/inst/include/geojsonsf/geojson/write_geometry.hpp +++ b/inst/include/geojsonsf/geojson/write_geometry.hpp @@ -59,7 +59,7 @@ namespace write_geometry { SEXP sfg = sfc[ sfg_index ]; std::string geom_type; - Rcpp::CharacterVector cls = sfheaders::getSfClass(sfg); + Rcpp::CharacterVector cls = sfheaders::sfc::getSfClass(sfg); cls_check( cls ); geom_type = cls[1]; diff --git a/src/geojson_to_wkt.cpp b/src/geojson_to_wkt.cpp index 0dc7ed6..3432a95 100644 --- a/src/geojson_to_wkt.cpp +++ b/src/geojson_to_wkt.cpp @@ -31,7 +31,7 @@ void parse_geometry_object_wkt( std::ostringstream os; Rcpp::StringVector wkt; - begin_wkt(os, geom_type); + //begin_wkt(os, geom_type); if (geom_type == "Point") { point_to_wkt(os, coord_array); diff --git a/src/geojson_wkt.cpp b/src/geojson_wkt.cpp index 756f952..069a468 100644 --- a/src/geojson_wkt.cpp +++ b/src/geojson_wkt.cpp @@ -7,22 +7,38 @@ using namespace rapidjson; +std::string wkt_dim( int n ) { + switch( n ) { + case 3: { + return " Z"; + } + case 4: { + return " ZM"; + } + default: { + return ""; + } + } +} + void begin_wkt(std::ostringstream& os, std::string& geom_type) { + //std::string dim = wkt_dim( n_coords ); + std::string dim = wkt_dim(0); if (geom_type == "Point") { - os << "POINT ("; + os << "POINT" << dim << " ("; } else if (geom_type == "MultiPoint") { - os << "MULTIPOINT (("; + os << "MULTIPOINT" << dim << " (("; } else if (geom_type == "LineString") { - os << "LINESTRING ("; + os << "LINESTRING" << dim << " ("; } else if (geom_type == "MultiLineString") { - os << "MULTILINESTRING (("; + os << "MULTILINESTRING" << dim << " (("; } else if (geom_type == "Polygon") { - os << "POLYGON (("; + os << "POLYGON" << dim << " (("; } else if (geom_type == "MultiPolygon") { - os << "MULTIPOLYGON ((("; + os << "MULTIPOLYGON" << dim << " ((("; } else if (geom_type == "GeometryCollection") { - os << "GEOMETRYCOLLECTION ("; + os << "GEOMETRYCOLLECTION" << dim << " ("; } } @@ -67,16 +83,27 @@ void polygon_separate_wkt(std::ostringstream& os, int i, int n) { } } +void add_coordinate_to_wkt_stream(std::ostringstream& os, double coord ) { + os << coord; +} -void add_lonlat_to_wkt_stream(std::ostringstream& os, float lon, float lat ) { +void add_lonlat_to_wkt_stream(std::ostringstream& os, double lon, double lat ) { os << lon << " " << lat; } void point_to_wkt(std::ostringstream& os, const Value& coord_array) { - Rcpp::NumericVector point(2); - point[0] = geojsonsf::sfg::get_lon(coord_array); - point[1] = geojsonsf::sfg::get_lat(coord_array); - add_lonlat_to_wkt_stream(os, point[0], point[1]); + int n = coord_array.Size(); + int i; + for( i = 0; i < n; i++ ) { + if( i > 0 ) { + os << " "; + } + add_coordinate_to_wkt_stream( os, coord_array[i].GetDouble() ); + } + // Rcpp::NumericVector point(2); + // point[0] = geojsonsf::sfg::get_lon(coord_array); + // point[1] = geojsonsf::sfg::get_lat(coord_array); + // add_lonlat_to_wkt_stream(os, point[0], point[1]); }