Skip to content

Commit

Permalink
CRAN ready
Browse files Browse the repository at this point in the history
  • Loading branch information
SymbolixAU committed Jan 10, 2019
1 parent 48101a7 commit e78f44c
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 135 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
## v1.3

* restructured C++ src code for easier navigation and linking
* added dependency on jsonify v0.2
* added dependency on jsonify v0.2 and fixed tests
* Factors now default to strings

## v1.2.2
Expand Down
3 changes: 1 addition & 2 deletions cran-comments.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
## Release Summary

* Updates v1.2.1 to v1.3
* added dependency on jsonify v0.2 (accepted on CRAN 10th Jan 2019)
* restructured C++ API for easier navigation and linking
* added dependency on jsonify v0.2 (accepted on CRAN 10th Jan 2019) and fixed tests
* Build Note - Fedora clang : installed size is 6.3mb
- The builds says the `libs` file is 4.3mb. I can't reduce this file size, all the compiled c++ headers and files are required.

Expand Down
18 changes: 9 additions & 9 deletions inst/include/geojsonsf/geojson/api/df_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,19 @@ namespace api {
rapidjson::StringBuffer sb;
rapidjson::Writer < rapidjson::StringBuffer > writer( sb );

size_t n_cols = df.ncol();
size_t n_rows = df.nrows();
int n_cols = df.ncol();
int n_rows = df.nrows();
int i, j;
Rcpp::StringVector column_names = df.names();

// the sfc_POINT
size_t n_geometry_columns = geometry_columns.size();
int n_geometry_columns = geometry_columns.size();
Rcpp::List geometry_vectors( n_geometry_columns );

size_t n_properties = n_cols - n_geometry_columns;
int n_properties = n_cols - n_geometry_columns;
Rcpp::StringVector property_names( n_properties );

for ( i = 0; i < n_geometry_columns; i++ ) {
for( i = 0; i < n_geometry_columns; i++ ) {
Rcpp::String this_geometry = geometry_columns[i];
geometry_vectors[i] = df[ this_geometry ];
}
Expand Down Expand Up @@ -103,18 +103,18 @@ namespace api {
int& digits,
bool& factors_as_string ) {

size_t n_cols = df.ncol();
size_t n_rows = df.nrows();
int n_cols = df.ncol();
int n_rows = df.nrows();
int i, j;
Rcpp::StringVector column_names = df.names();

Rcpp::StringVector geojson( n_rows );


size_t n_geometry_columns = geometry_columns.size();
int n_geometry_columns = geometry_columns.size();
Rcpp::List geometry_vectors( n_geometry_columns );

size_t n_properties = n_cols - n_geometry_columns;
int n_properties = n_cols - n_geometry_columns;
Rcpp::StringVector property_names( n_properties );

for ( i = 0; i < n_geometry_columns; i++ ) {
Expand Down
14 changes: 7 additions & 7 deletions inst/include/geojsonsf/geojson/api/sf_api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace api {
inline Rcpp::StringVector sfc_to_geojson( Rcpp::List& sfc, int& digits ) {
// atomise - each row is a separate GeoJSON string

size_t n_rows = sfc.size();
int n_rows = sfc.size();
int i;

Rcpp::StringVector geojson( n_rows );
Expand Down Expand Up @@ -41,9 +41,9 @@ namespace api {

std::string geom_column = sf.attr("sf_column");

size_t n_cols = sf.ncol();
size_t n_properties = n_cols - 1;
size_t n_rows = sf.nrows();
int n_cols = sf.ncol();
int n_properties = n_cols - 1;
int n_rows = sf.nrows();
int i, j;
Rcpp::StringVector column_names = sf.names();
Rcpp::StringVector property_names(sf.size() - 1);
Expand Down Expand Up @@ -106,9 +106,9 @@ namespace api {

std::string geom_column = sf.attr("sf_column");

size_t n_cols = sf.ncol();
size_t n_properties = n_cols - 1;
size_t n_rows = sf.nrows();
int n_cols = sf.ncol();
int n_properties = n_cols - 1;
int n_rows = sf.nrows();
int i, j;
Rcpp::StringVector column_names = sf.names();
Rcpp::StringVector property_names(sf.size() - 1);
Expand Down
4 changes: 0 additions & 4 deletions inst/include/geojsonsf/geojson/parse.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ namespace parse {
geometry_types.insert( "GEOMETRYCOLLECTION" );

if ( !expand_geometries ) {
// TODO( dimension )
std::string dim = "XY";
std::string attribute = "GEOMETRYCOLLECTION";
geom_collection.attr("class") = geojsonsf::sfg::sfg_attributes( dim, attribute );
Expand All @@ -94,9 +93,6 @@ namespace parse {

geojsonsf::validate::validate_geometry(feature, sfg_objects);

// TODO( null property ==> NULL geometry)
//validate_properties(feature, sfg_objects);

Rcpp::List sfc(1);

const Value& geometry = feature["geometry"];
Expand Down
6 changes: 2 additions & 4 deletions inst/include/geojsonsf/geojson/writers/write_geojson.hpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
#ifndef GEOJSONSF_GEOJSON_WRITE_GEOJSON_H
#define GEOJSONSF_GEOJSON_WRITE_GEOJSON_H
#ifndef GEOJSONSF_GEOJSON_WRITERS_WRITE_GEOJSON_H
#define GEOJSONSF_GEOJSON_WRITERS_WRITE_GEOJSON_H

#include <Rcpp.h>
#include "geojsonsf/geojson/writers/writers.hpp"
#include "geojsonsf/geometrycollection/geometrycollection.hpp"

namespace geojsonsf {
namespace geojson {
namespace write_geojson {

template< typename Writer >
Expand Down Expand Up @@ -105,7 +104,6 @@ namespace write_geojson {
}

} // namespace geojsonsf
} // namespace geojson
} // namespace write_geojson

#endif
106 changes: 4 additions & 102 deletions inst/include/geojsonsf/write_geojson.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,110 +2,12 @@
#define GEOJSONSF_WRITE_GEOJSON_H

#include <Rcpp.h>
#include "geojsonsf/geojson/writers/writers.hpp"
#include "geojsonsf/geometrycollection/geometrycollection.hpp"
// #include "geojsonsf/geojson/writers/writers.hpp"
// #include "geojsonsf/geometrycollection/geometrycollection.hpp"

// TODO( remove once spatialwidget v0.2 is on CRAN)
// TODO( deprecate this in favour of geojsonsf/geojson/writers/write_geojson)

namespace geojsonsf {
namespace write_geojson {

template< typename Writer >
inline void write_geojson(Writer& writer, SEXP sfg, std::string& geom_type, Rcpp::CharacterVector& cls, int& digits ) {

if (geom_type == "POINT") {
geojsonsf::writers::points_to_geojson( writer, sfg, digits );

} else if (geom_type == "MULTIPOINT") {
geojsonsf::writers::linestring_to_geojson( writer, sfg, digits );

} else if (geom_type == "LINESTRING") {
geojsonsf::writers::linestring_to_geojson( writer, sfg, digits );

} else if (geom_type == "MULTILINESTRING") {
Rcpp::List multiline = Rcpp::as< Rcpp::List >( sfg );
geojsonsf::writers::polygon_to_geojson( writer, multiline, digits );

} else if (geom_type == "POLYGON") {
Rcpp::List polygon = Rcpp::as< Rcpp::List >(sfg);
geojsonsf::writers::polygon_to_geojson( writer, polygon, digits );

} else if (geom_type == "MULTIPOLYGON") {
Rcpp::List multipolygon = Rcpp::as< Rcpp::List >( sfg );
geojsonsf::writers::multi_polygon_to_geojson( writer, multipolygon, digits );

} else if (geom_type == "GEOMETRYCOLLECTION") {
Rcpp::List gc = Rcpp::as< Rcpp::List >( sfg );
Rcpp::List sfgi(1);
for (int i = 0; i < gc.size(); i++) {
sfgi[0] = gc[i];
std::string gc_geom_type;
bool isnull = false;
geojsonsf::geometrycollection::gc_type( sfgi, gc_geom_type, isnull, cls );
if( !isnull ) {
SEXP sfg_gc = gc[i];
geojsonsf::writers::begin_geojson_geometry(writer, gc_geom_type);
write_geojson( writer, sfg_gc, gc_geom_type, cls, digits );
geojsonsf::writers::end_geojson_geometry(writer, gc_geom_type);
}
}
}
}


/*
* used for down-casting MULTI objects
*/
template< typename Writer >
inline void write_geojson(Writer& writer, SEXP sfg, std::string& geom_type,
Rcpp::CharacterVector& cls, int geometry_index, int& digits ) {

if (geom_type == "POINT") {
geojsonsf::writers::points_to_geojson( writer, sfg, digits );

} else if (geom_type == "MULTIPOINT") {
Rcpp::NumericMatrix mls = Rcpp::as< Rcpp::NumericMatrix >( sfg );
Rcpp::NumericVector pts = mls(geometry_index, Rcpp::_ );
geojsonsf::writers::points_to_geojson( writer, pts, digits );

} else if (geom_type == "LINESTRING") {
geojsonsf::writers::linestring_to_geojson( writer, sfg, digits );

} else if (geom_type == "MULTILINESTRING") {
Rcpp::List multiline = Rcpp::as< Rcpp::List >( sfg );
SEXP ml = multiline[ geometry_index ];
geojsonsf::writers::linestring_to_geojson( writer, ml, digits );

} else if (geom_type == "POLYGON") {
Rcpp::List polygon = Rcpp::as< Rcpp::List >(sfg);
geojsonsf::writers::polygon_to_geojson( writer, polygon, digits );

} else if (geom_type == "MULTIPOLYGON") {
Rcpp::List multipolygon = Rcpp::as< Rcpp::List >( sfg );
Rcpp::List mlp = multipolygon[ geometry_index ];
geojsonsf::writers::polygon_to_geojson( writer, mlp, digits );

} else if (geom_type == "GEOMETRYCOLLECTION") {
Rcpp::List gc = Rcpp::as< Rcpp::List >( sfg );
Rcpp::List sfgi(1);
for (int i = 0; i < gc.size(); i++) {
sfgi[0] = gc[i];

std::string gc_geom_type;
bool isnull = false;
geojsonsf::geometrycollection::gc_type( sfgi, gc_geom_type, isnull, cls );
if( !isnull ) {
SEXP sfg_gc = gc[i];
geojsonsf::writers::begin_geojson_geometry(writer, gc_geom_type);
write_geojson( writer, sfg_gc, gc_geom_type, cls, digits );
geojsonsf::writers::end_geojson_geometry(writer, gc_geom_type);
}

}
}
}

} // namespace geojsonsf
} // namespace write_geojson
#include "geojsonsf/geojson/writers/write_geojson.hpp"

#endif
11 changes: 7 additions & 4 deletions tests/testthat/test-geojson_sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ context("geojsonsf")

test_that("ints vs numerics read correctly", {

## TODO Integers
# geo <- '{"type":"Point","coordinates":[0,0]}'
# expect_equal(geojson_sf( geo )[[1]], geojson_sfc( geo ) )
# expect_true(is.integer( geojson_sfc( geo )[[1]] ) )
Expand All @@ -11,6 +12,7 @@ test_that("ints vs numerics read correctly", {
expect_equal(geojson_sf( pt )[[1]], geojson_sfc( pt ) )
expect_false(is.integer( geojson_sfc( pt )[[1]] ) )

## TODO Integers
# geo <- '{"type":"MultiPoint","coordinates":[[0,0],[1,1]]}'
# expect_equal( geojson_sf( geo )[[1]], geojson_sfc( geo ) )
# expect_true( is.integer( geojson_sfc( geo )[[1]] ) )
Expand All @@ -19,6 +21,7 @@ test_that("ints vs numerics read correctly", {
expect_equal( geojson_sf( mpt )[[1]], geojson_sfc( mpt ) )
expect_false( is.integer( geojson_sfc( mpt )[[1]] ) )

## TODO Integers
# geo <- '{"type":"LineString","coordinates":[[0,0],[1,1]]}'
# expect_equal( geojson_sf( geo )[[1]], geojson_sfc( geo ) )
# expect_true( is.integer( geojson_sfc( geo )[[1]] ) )
Expand All @@ -27,11 +30,11 @@ test_that("ints vs numerics read correctly", {
expect_equal( geojson_sf( ls )[[1]], geojson_sfc( ls ) )
expect_false( is.integer( geojson_sfc( ls )[[1]] ) )

# ## TODO( if the 2nd element is a double and the first isn't? )
# geo <- '{"type":"LineString","coordinates":[[0,0.1],[1,1]]}'
# expect_equal( geojson_sf( geo )[[1]], geojson_sfc( geo ) )
# expect_false( is.integer( geojson_sfc( geo )[[1]] ) )
geo <- '{"type":"LineString","coordinates":[[0,0.1],[1,1]]}'
expect_equal( geojson_sf( geo )[[1]], geojson_sfc( geo ) )
expect_false( is.integer( geojson_sfc( geo )[[1]] ) )

## TODO integers
# geo <- '{"type":"MultiLineString","coordinates":[[[0,0],[1,1]]]}'
# expect_equal( geojson_sf( geo )[[1]], geojson_sfc( geo ) )
# expect_true( is.integer( geojson_sfc( geo )[[1]] ) )
Expand Down
2 changes: 0 additions & 2 deletions tests/testthat/test-sf_geojson.R
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,6 @@ test_that("errors are handled", {

test_that("factors are numeric", {

## TODO( this should fail - factors_as_string )

fgc <- '{"type":"Feature","geometry":{"type":"GeometryCollection","geometries":[{"type":"Point","coordinates":[100.0,0.0]},{"type":"LineString","coordinates":[[101.0,0.0],[102.0,1.0]]}]},"properties":{"prop0":"value0","prop1":"value1"}}'
sf <- geojson_sf(fgc)
sf$prop0 <- as.factor(sf$prop0)
Expand Down

0 comments on commit e78f44c

Please sign in to comment.