Skip to content

Commit

Permalink
fix and close #26
Browse files Browse the repository at this point in the history
  • Loading branch information
SymbolixAU authored and SymbolixAU committed May 26, 2018
1 parent 4404c0a commit 3c0c503
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
5 changes: 5 additions & 0 deletions src/sf_geojson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,13 +289,18 @@ void write_geometry(SEXP sfg, Rcpp::String& geojson) {
Rcpp::String g = cls[1];
geom_type = g;

// need to keep track of GEOMETRYCOLLECTIONs so we can correctly close them
bool isGeometryCollection = (geom_type == "GEOMETRYCOLLECTION") ? true : false;

int sfglength = get_sexp_length(sfg);

if (sfglength == 0) {
geojson += "null";
} else {
begin_geojson_geometry(geojson, geom_type);
write_geojson(geojson, sfg, geom_type, cls);

geom_type = (isGeometryCollection) ? "GEOMETRYCOLLECTION" : geom_type;
end_geojson_geometry(geojson, geom_type);
}
}
Expand Down
26 changes: 19 additions & 7 deletions tests/testthat/test-sf_geojson.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,32 @@ test_that("sfc objects converted to GeoJSON", {

js <- '{"type":"MultiPolygon","coordinates":[[[[0,0],[0,1],[1,1],[1,0],[0,0]],[[0.5,0.5],[0.5,0.75],[0.75,0.75],[0.75,0.5],[0.5,0.5]]],[[[2,2],[2,3],[3,3],[3,2],[2,2]]]]}'
expect_true(sfc_geojson(geojson_sfc(js)) == gsub(" |\\r|\\n|\\t","",js))
})

test_that("GeometryCollections correctly closed", {
js <- '{"type":"GeometryCollection","geometries":[{"type":"Point","coordinates":[100,0]},{"type":"LineString","coordinates":[[101,0],[102,1]]},{"type":"MultiPoint","coordinates":[[0,0],[1,1],[2,2]]}]}'
sf <- geojson_sfc(js)
j <- sfc_geojson(sf)
expect_true(gsub(" |\\n|\\r|\\t","",js) == j)

})
js <- '{"type":"GeometryCollection","geometries":[{"type":"MultiLineString","coordinates":[[[0,0],[0,1],[1,1],[1,0],[0,0]]]}]}'
sf <- geojson_sf(js)
j <- sf_geojson(sf)
sf2 <- geojson_sf(j)
expect_equal(sf, sf2)

js <- '{"type":"GeometryCollection","geometries":[{"type":"Polygon","coordinates":[[[0,0],[0,1],[1,1],[1,0],[0,0]]]}]}'
sf <- geojson_sf(js)
j <- sf_geojson(sf)
sf2 <- geojson_sf(j)
expect_equal(sf, sf2)

js <- '{"type":"GeometryCollection","geometries":[{"type":"MultiPolygon","coordinates":[[[[0,0],[0,1],[1,1],[1,0],[0,0]]]]}]}'
sf <- geojson_sf(js)
j <- sf_geojson(sf)
sf2 <- geojson_sf(j)
expect_equal(sf, sf2)
})

test_that("single sf objects converted to GeoJSON", {

Expand Down Expand Up @@ -78,20 +96,16 @@ test_that("single sf objects converted to GeoJSON", {
sf <- geojson_sf(js)
j <- sf_geojson(sf)
expect_true(gsub(" |\\n|\\r|\\t","",js) == j)

})


test_that("sf without properties not converted to FeatureCollections", {

js <- '[{"type":"Polygon","coordinates":[[[0,0],[1,1]]]},{"type":"MultiLineString","coordinates":[[[0,0],[1,1]],[[3,3],[4,4]]]}]'
sf <- geojson_sf(js)
expect_true(all(sf_geojson(sf, atomise = F) == sf_geojson(sf, atomise = T)))

js <- '[{"type":"Polygon","coordinates":[[[0,0],[1,1]]]},{"type":"MultiLineString","coordinates":[[[0,0],[1,1]]]},{"type":"GeometryCollection","geometries":[{"type":"Point", "coordinates":[100.0, 0.0]},{"type":"LineString","coordinates":[[101,0],[102,1]]},{"type":"MultiPoint","coordinates":[[0,0],[1,1],[2,2]]}]}]'
sf <- geojson_sf(js)
expect_true(all(sf_geojson(sf, atomise = T) == sf_geojson(sf, atomise = F)))

})

test_that("sf with properties converted to FeatureCollection", {
Expand Down Expand Up @@ -121,9 +135,7 @@ test_that("sf with properties converted to FeatureCollection", {
expect_true(length(sf_geojson(sf, atomise = T)) == 2)
})


test_that("sf object with properties converted to sfc", {

fgc <- '{"type":"Feature","geometry":{"type":"GeometryCollection","geometries":[{"type":"Point","coordinates":[100,0]},{"type":"LineString","coordinates":[[101,0],[102,1]]}]},"properties": {"prop0": "value0","prop1": "value1"}}'
sf <- geojson_sf(fgc)
expect_false(grepl("properties",sfc_geojson(sf$geometry)))
Expand Down

0 comments on commit 3c0c503

Please sign in to comment.