Skip to content

Commit

Permalink
tests for #91
Browse files Browse the repository at this point in the history
  • Loading branch information
dcooley committed Dec 3, 2021
1 parent bc68bfb commit 81d85b6
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 45 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

## v2.1.0

* empty arrays now allowed [issue 91](https://github.com/SymbolixAU/geojsonsf/issues/91)

## v2.0.1

* links to geometries library
Expand Down
32 changes: 0 additions & 32 deletions inst/include/geojsonsf/sf/sfg/geojson_sfg.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,8 @@ namespace sfg {
Rcpp::NumericVector& z_range,
Rcpp::NumericVector& m_range
) {
// Rcpp::Rcout << "get points 1" << std::endl;
R_xlen_t i;

// if( n == 0 ) {
// nv = empty_point();
// Rcpp::Rcout << "empty point " << nv << std::endl;
// } else {

bool isEmpty = point_array.Size() == 0;

if( isEmpty ) {
Expand Down Expand Up @@ -68,32 +62,12 @@ namespace sfg {
std::string attribute
) {

// Rcpp::Rcout << "get points 2" << std::endl;

// if( point_array[i].Empty() ) {
// nv[i] = Rcpp::NumericVector::get_na();
// } else {

// if( point_array.Empty() ) {
// Rcpp::NumericVector empty(2, Rcpp::NumericVector::get_na());
// sfc[i] = empty;
// //return;
// }

//Rcpp::min(point_array.Size(), 2);

R_xlen_t n = point_array.Size() == 0 ? 2 : point_array.Size();
// TODO: if n == 0, make it 2 and fill with NA
// This will mean it will pass the sfheaders dimension check

// Rcpp::Rcout << "n: " << n << std::endl;

Rcpp::NumericVector nv( n );

get_points( point_array, n, nv, bbox, z_range, m_range );

//Rcpp::stop("stop");

if ( requires_attribute ) {
std::string dim = sfheaders::sfg::sfg_dimension( n );

Expand All @@ -105,7 +79,6 @@ namespace sfg {

}

// Rcpp::Rcout << "nv " << nv << std::endl;
sfc[i] = nv;
}

Expand All @@ -126,8 +99,6 @@ namespace sfg {

Rcpp::NumericMatrix nm( n, 4 );

// Rcpp::Rcout << "linestring n : " << n << std::endl;

for( row = 0; row < n; row++ ) {
const Value& coord_array = line_array[ row ];
R_xlen_t n_points = coord_array.Size();
Expand All @@ -145,12 +116,9 @@ namespace sfg {
nm( row, Rcpp::_ ) = nv;
}

// Rcpp::Rcout << "max_cols : " << max_cols << std::endl;
// Rcpp::Rcout << "ncol: " << nm.ncol() << std::endl;
if( nm.nrow() > 0 ) {
nm = nm( Rcpp::_, Rcpp::Range(0, ( max_cols - 1 ) ) );
}
// Rcpp::Rcout << "nm : " << nm << std::endl;

if ( requires_attribute ) {
std::string dim = sfheaders::sfg::sfg_dimension( max_cols );
Expand Down
36 changes: 29 additions & 7 deletions inst/tinytest/test-empty_geometries.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,33 @@
##"empty features are allowed", {

geo <- '{"type":"FeatureCollection","features":[]}'
sf <- geojsonsf::geojson_sf( geo )
expect_true( nrow(sf) == 0)
expect_true( "sfc_GEOMETRY" %in% attr( sf$geometry , "class" ) )
geo <- '{"type":"FeatureCollection","features":[]}'
sf <- geojsonsf::geojson_sf( geo )
expect_true( nrow(sf) == 0)
expect_true( "sfc_GEOMETRY" %in% attr( sf$geometry , "class" ) )

## round-trip
geo2 <- geojsonsf::sf_geojson( sf, simplify = FALSE )
expect_equal( geo, as.character( geo2 ) )
## round-trip
geo2 <- geojsonsf::sf_geojson( sf, simplify = FALSE )
expect_equal( geo, as.character( geo2 ) )


geo <- '{
"type": "FeatureCollection",
"features": [
{ "type": "Feature", "properties": null, "geometry": {"type":"Point","coordinates":[]} },
{ "type": "Feature", "properties": null, "geometry": {"type":"MultiPoint","coordinates":[]} },
{ "type": "Feature", "properties": null, "geometry": {"type":"LineString","coordinates":[]} },
{ "type": "Feature", "properties": null, "geometry": {"type":"MultiLineString","coordinates":[]} },
{ "type": "Feature", "properties": null, "geometry": {"type":"Polygon","coordinates":[]} },
{ "type": "Feature", "properties": null, "geometry": {"type":"MultiPolygon","coordinates":[]} }
]}'


sf <- geojsonsf::geojson_sf(geo)
geo <- geojsonsf::sf_geojson(sf)

geojson_sf('{ "type": "Feature", "properties": null, "geometry": {"type":"Point","coordinates":[]} }')
geojson_sf('{ "type": "Feature", "properties": null, "geometry": {"type":"MultiPoint","coordinates":[]}}')
geojson_sf('{ "type": "Feature", "properties": null, "geometry": {"type":"LineString","coordinates":[]}}')
geojson_sf('{ "type": "Feature", "properties": null, "geometry": {"type":"MultiLineString","coordinates":[]}}')
geojson_sf('{ "type": "Feature", "properties": null, "geometry": {"type":"Polygon","coordinates":[]}}')
geojson_sf('{ "type": "Feature", "properties": null, "geometry": {"type":"MultiPolygon","coordinates":[]}}')
5 changes: 3 additions & 2 deletions inst/tinytest/test-geojson_properties.R
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@
js <- '{"type":"Point","coordinates":[,]}'
expect_error(geojson_sf(js), "Invalid JSON")

js <- '{"type":"Point","coordinates":[]}'
expect_error(geojson_sf(js), "Invalid lon/lat object")
## Issue 91 - allowing []
#js <- '{"type":"Point","coordinates":[]}'
#expect_error(geojson_sf(js), "Invalid lon/lat object")

js <- '{"type":"Point","coordinates":{}}'
expect_error(geojson_sf(js), "No 'array' member at object index 0 - invalid GeoJSON")
Expand Down
5 changes: 3 additions & 2 deletions inst/tinytest/test-geojson_validate.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
js <- '{"type":"Point","coordinates":null}'
expect_error(geojson_sf(js), "No 'array' member at object index 0 - invalid GeoJSON")

js <- '{"type":"Point","coordinates":[]}'
expect_error(geojson_sf(js), "Invalid lon/lat object")
## Issue 91 - allowing []
# js <- '{"type":"Point","coordinates":[]}'
# expect_error(geojson_sf(js), "Invalid lon/lat object")

## "Feature Object has correct members", {

Expand Down
2 changes: 0 additions & 2 deletions src/geojson_to_wkt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@
#include "geojsonsf/geojson/geojson_properties.hpp"
#include "geojsonsf/wkt/geojson_wkt.hpp"

//#include "sfheaders/sfg/sfg_attributes.hpp"

using namespace rapidjson;

void parse_geometry_object_wkt(
Expand Down

0 comments on commit 81d85b6

Please sign in to comment.