Skip to content

Commit

Permalink
need to correctly add the dim to the wkt class attributes for #47
Browse files Browse the repository at this point in the history
  • Loading branch information
SymbolixAU committed Sep 11, 2019
1 parent feefcd4 commit 4085eea
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 32 deletions.
14 changes: 7 additions & 7 deletions inst/include/geojsonsf/wkt/geojson_wkt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ using namespace rapidjson;

void coord_separator(std::ostringstream& os, int i, int n);

void begin_wkt(std::ostringstream& os, std::string& geom_type);
void begin_wkt(std::ostringstream& os, std::string& geom_type, int& coord_dim );

void end_wkt(std::ostringstream& os, std::string& geom_type);

void point_to_wkt(std::ostringstream& os, const Value& coord_array);
void point_to_wkt(std::ostringstream& os, const Value& coord_array, int& coord_dim);

void multi_point_to_wkt(std::ostringstream& os, const Value& coord_array);
void multi_point_to_wkt(std::ostringstream& os, const Value& coord_array, int& coord_dim);

void line_string_to_wkt(std::ostringstream& os, const Value& coord_array);
void line_string_to_wkt(std::ostringstream& os, const Value& coord_array, int& coord_dim);

void multi_line_string_to_wkt(std::ostringstream& os, const Value& coord_array);
void multi_line_string_to_wkt(std::ostringstream& os, const Value& coord_array, int& coord_dim);

void polygon_to_wkt(std::ostringstream& os, const Value& coord_array);
void polygon_to_wkt(std::ostringstream& os, const Value& coord_array, int& coord_dim);

void multi_polygon_to_wkt(std::ostringstream& os, const Value& coord_array);
void multi_polygon_to_wkt(std::ostringstream& os, const Value& coord_array, int& coord_dim);

#endif

20 changes: 12 additions & 8 deletions src/geojson_to_wkt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,33 +31,37 @@ void parse_geometry_object_wkt(

std::ostringstream os;
Rcpp::StringVector wkt;
int coord_dim = 0;
//begin_wkt(os, geom_type);

if (geom_type == "Point") {
point_to_wkt(os, coord_array);
point_to_wkt(os, coord_array, coord_dim );

} else if (geom_type == "MultiPoint") {
multi_point_to_wkt(os, coord_array);
multi_point_to_wkt(os, coord_array, coord_dim);

} else if (geom_type == "LineString") {
line_string_to_wkt(os, coord_array);
line_string_to_wkt(os, coord_array, coord_dim);

} else if (geom_type == "MultiLineString") {
multi_line_string_to_wkt(os, coord_array);
multi_line_string_to_wkt(os, coord_array, coord_dim);

} else if (geom_type == "Polygon") {
polygon_to_wkt(os, coord_array);
polygon_to_wkt(os, coord_array, coord_dim);

} else if (geom_type == "MultiPolygon") {
multi_polygon_to_wkt(os, coord_array);
multi_polygon_to_wkt(os, coord_array, coord_dim);

} else {
Rcpp::stop("unknown sfg type");
}

end_wkt(os, geom_type);
std::ostringstream os_begin; // call AFTER we know the XYZM dimension
begin_wkt( os_begin, geom_type, coord_dim );
os_begin << os.str();
end_wkt(os_begin, geom_type);

wkt = os.str();
wkt = os_begin.str();
transform(geom_type.begin(), geom_type.end(), geom_type.begin(), ::toupper);

// TODO( dimension )
Expand Down
30 changes: 16 additions & 14 deletions src/geojson_wkt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ std::string wkt_dim( int n ) {
}
}

void begin_wkt(std::ostringstream& os, std::string& geom_type) {
//std::string dim = wkt_dim( n_coords );
std::string dim = wkt_dim(0);
void begin_wkt(std::ostringstream& os, std::string& geom_type, int& coord_dim ) {
std::string dim = wkt_dim( coord_dim );
//std::string dim = wkt_dim(0);

if (geom_type == "Point") {
os << "POINT" << dim << " (";
Expand Down Expand Up @@ -91,8 +91,10 @@ 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) {
void point_to_wkt(std::ostringstream& os, const Value& coord_array, int& coord_dim ) {
int n = coord_array.Size();
coord_dim = std::max( coord_dim, n );
//Rcpp::Rcout << "max coord dim: " << coord_dim << std::endl;
int i;
for( i = 0; i < n; i++ ) {
if( i > 0 ) {
Expand All @@ -107,52 +109,52 @@ void point_to_wkt(std::ostringstream& os, const Value& coord_array) {
}


void multi_point_to_wkt(std::ostringstream& os, const Value& coord_array) {
void multi_point_to_wkt(std::ostringstream& os, const Value& coord_array, int& coord_dim ) {
size_t n = coord_array.Size();
unsigned int i;
for (i = 0; i < n; i++) {
geojsonsf::validate::validate_array(coord_array[i]);
point_to_wkt(os, coord_array[i]);
point_to_wkt(os, coord_array[i], coord_dim );
coord_separator(os, i, n);
}
}

void line_string_to_wkt(std::ostringstream& os, const Value& coord_array) {
void line_string_to_wkt(std::ostringstream& os, const Value& coord_array, int& coord_dim) {
size_t n = coord_array.Size();
unsigned int i;
for (i = 0; i < n; i++) {
geojsonsf::validate::validate_array(coord_array[i]);
point_to_wkt(os, coord_array[i]);
point_to_wkt(os, coord_array[i], coord_dim );
coord_separator(os, i, n);
}
}

void multi_line_string_to_wkt(std::ostringstream& os, const Value& coord_array) {
void multi_line_string_to_wkt(std::ostringstream& os, const Value& coord_array, int& coord_dim) {
size_t n = coord_array.Size();
unsigned int i;
for (i = 0; i < n; i++) {
geojsonsf::validate::validate_array(coord_array[i]);
line_string_to_wkt(os, coord_array[i]);
line_string_to_wkt(os, coord_array[i], coord_dim );
line_separator_wkt(os, i, n);
}
}

void polygon_to_wkt(std::ostringstream& os, const Value& coord_array) {
void polygon_to_wkt(std::ostringstream& os, const Value& coord_array, int& coord_dim) {
size_t n = coord_array.Size();
unsigned int i;
for (i = 0; i < n; i++) {
geojsonsf::validate::validate_array(coord_array[i]);
line_string_to_wkt(os, coord_array[i]);
line_string_to_wkt(os, coord_array[i], coord_dim );
line_separator_wkt(os, i, n);
}
}

void multi_polygon_to_wkt(std::ostringstream& os, const Value& coord_array) {
void multi_polygon_to_wkt(std::ostringstream& os, const Value& coord_array, int& coord_dim) {
size_t n = coord_array.Size();
unsigned int i;
for (i = 0; i < n; i++) {
geojsonsf::validate::validate_array(coord_array[i]);
polygon_to_wkt(os, coord_array[i]);
polygon_to_wkt(os, coord_array[i], coord_dim);
polygon_separate_wkt(os, i, n);
}
}
Expand Down
20 changes: 17 additions & 3 deletions tests/testthat/test-geojson_wkt.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,26 @@ test_that("wkt created correctly", {
test_that("WKT with NULL objects", {

js <- '{"type":"Feature","properties":{"id":1.0},"geometry":null}'
#expect_true( geojson_wkt( js )$geometry[[1]] == "POINT EMPTY" )
expect_true( geojson_wkt( js )$geometry[[1]] == "POINT EMPTY" )

js <- '{"type":"FeatureCollection","features":[
{"type":"Feature","properties":{"id":1.0},"geometry":{"type":"Point","coordinates":[0.0,0.0]}},
{"type":"Feature","properties":{"id":2.0},"geometry":null}]}'
{"type":"Feature","properties":{"id":1.0, "val":"a"},"geometry":{"type":"Point","coordinates":[0.0,0.0]}},
{"type":"Feature","properties":{"id":2.0, "val":"b"},"geometry":null}]}'

#geojson_wkt( js )

})

test_that("WKT with Z and M dimensions handled",{

p <- '{"type":"Point", "coordinates":[0,0,0]}'
mp <- '{"type":"MultiPoint", "coordinates":[[0,0,0],[2.324,2,1,1]]}'
ls <- '{"type":"LineString", "coordinates":[[0,0],[1,1,1,1]]}'

p <- geojson_wkt( p )
mp <- geojson_wkt( mp )
ls <- geojson_wkt( ls )

expect_equal( attr(p$geometry[[1]], "class")[[1]], "XYZ" )

})

0 comments on commit 4085eea

Please sign in to comment.