From c71d532a7a421c540c4d3c8bf4e9a7391455ccb6 Mon Sep 17 00:00:00 2001 From: dcooley Date: Sun, 15 Mar 2020 01:53:30 -0700 Subject: [PATCH] R_xlen_t and stuff --- R/scratch.R | 160 ------------------ inst/include/geojsonsf/geojson/api/df_api.hpp | 2 +- .../geojsonsf/geojson/geojson_properties.hpp | 2 +- .../geojsonsf/geojson/geojson_to_sf.hpp | 2 +- inst/include/geojsonsf/geojson/parse.hpp | 16 +- .../geojsonsf/geojson/writers/writers.hpp | 28 +-- inst/include/geojsonsf/wkt/geojson_wkt.hpp | 2 +- src/geojson_wkt.cpp | 6 +- 8 files changed, 29 insertions(+), 189 deletions(-) delete mode 100644 R/scratch.R diff --git a/R/scratch.R b/R/scratch.R deleted file mode 100644 index 46dee29..0000000 --- a/R/scratch.R +++ /dev/null @@ -1,160 +0,0 @@ -# -# -# library(sf) -# -# pt1 <- sf::st_sf( geometry = sf::st_sfc( sf::st_point(c(0,0)))) -# mp1 <- sf::st_sf( geometry = sf::st_sfc( sf::st_multipoint(x = matrix(1:6, ncol = 2)))) -# mp2 <- sf::st_sf( geometry = sf::st_sfc( sf::st_multipoint(x = matrix(1:6, ncol = 3)))) -# mp3 <- sf::st_sf( geometry = sf::st_sfc( sf::st_multipoint(x = matrix(1:8, ncol = 4)))) -# pt2 <- sf::st_sf( geometry = sf::st_sfc( sf::st_point(c(1,1,3,4)))) -# ls1 <- sf::st_sf( geometry = sf::st_sfc( sf::st_linestring(x = matrix(1:6, ncol = 2)))) -# ls2 <- sf::st_sf( geometry = sf::st_sfc( sf::st_linestring(x = matrix(1:6, ncol = 3)))) -# mls1 <- sf::st_sf( geometry = sf::st_sfc( sf::st_multilinestring(x = list(matrix(1:6, ncol = 2), matrix(6:1, ncol = 2), matrix(1:6, ncol = 2))))) -# -# js <- '{"type":"MultiPolygon","coordinates":[[[[0.0,0.0],[0.0,1.0],[1.0,1.0],[1.0,0.0],[0.0,0.0]],[[0.5,0.5],[0.5,0.75],[0.75,0.75],[0.75,0.5],[0.5,0.5]]],[[[2.0,2.0],[2.0,3.0],[3.0,3.0],[3.0,2.0],[2.0,2.0]]]]}' -# mp <- geojson_sf( js ) -# sf::st_crs( mp ) <- sf::st_crs( mls1 ) -# -# sf <- rbind(pt1, mp1, mp2, mp3, pt2, ls1, ls2, mls1, mp) -# sf$id <- 1:nrow(sf) -# -# sf_geojson( sf ) -# -# -# geojsonsf:::rcpp_sf_to_geojson_downcast( sf[1, ] ) -# geojsonsf:::rcpp_sf_to_geojson_downcast( sf[2, ] ) -# -# geojsonsf:::rcpp_sf_to_geojson_downcast( sf[9, ] ) -# -# - - - - -# nc <- sf::st_read(system.file("shape/nc.shp", package="sf")) -# nc_poly <- sf::st_cast( nc, to = "POLYGON" ) -# -# nc[4, ] -# geo <- geojsonsf:::rcpp_sf_to_geojson_downcast( nc[4, ] ) -# sf <- geojsonsf::geojson_sf( geo ) -# sf - - - - - - - - - -# -# sf -# nc_poly -# -# geojsonsf:::rcpp_sf_to_geojson_downcast( nc[4, ]) -# -# -# nc <- sf::st_read(system.file("shape/nc.shp", package="sf")) -# -# geo_down <- geojsonsf:::rcpp_sf_to_geojson_downcast( nc ) -# sf_down <- geojsonsf::geojson_sf( geo_down ) -# -# library(microbenchmark) -# -# microbenchmark( -# sf = { -# nc_poly <- sf::st_cast( nc, to = "POLYGON") -# geo_poly <- geojsonsf::sf_geojson( nc_poly ) -# }, -# geojsonsf = { -# geo_sf <- geojsonsf:::rcpp_sf_to_geojson_downcast( nc ) -# }, -# times = 5 -# ) -# -# # Unit: milliseconds -# # expr min lq mean median uq max neval -# # sf 16.019968 16.952798 20.264359 17.283295 19.396397 31.669338 5 -# # geojsonsf 2.348934 2.597222 3.203101 2.847933 3.017071 5.204344 5 -# -# microbenchmark( -# sf = { -# nc_poly <- sf::st_cast( nc, to = "POLYGON") -# geo_poly <- geojsonsf::sf_geojson( nc_poly ) -# }, -# geojsonsf = { -# geo_sf <- geojsonsf:::rcpp_sf_to_geojson_downcast( nc ) -# nc2 <- geojsonsf::geojson_sf( geo_sf ) -# }, -# times = 5 -# ) -# -# # Unit: milliseconds -# # expr min lq mean median uq max neval -# # sf 13.75293 14.42003 16.96273 14.93394 15.38814 26.31861 5 -# # geojsonsf 10.78687 11.34112 11.60321 11.63283 11.75583 12.49937 5 -# -# sf$geometry -# -# types <- vapply(sf$geometry, function(x) class(x)[[2]], "") -# downcast <- gsub("MULTI","",types) -# -# types[ types != downcast ] -# -# matrix(c(types, downcast), ncol = 2) -# -# ## given the geometry type (MULTIPIONT, MULTILINESTRING, MULTIPOLYGON) -# ## calculate how many of each POINT, LINESTRING, POLYGON are within the MULTI -# -# dim( sf[2, 'geometry'][[1]][[1]] ) -# -# dim( sf[8, 'geometry'][[1]][[1]][[1]] ) -# -# -# library(Rcpp) -# -# ## -# -# cppFunction('int mls_multiplier( Rcpp::List mls ) { -# int multiplier = 1; -# return mls.size(); -# }') -# -# mls_multiplier( sf[8, ]$geometry[[1]] ) -# -# cppFunction('int mp_multiplier( Rcpp::NumericMatrix mp ){ -# return mp.nrow(); -# }') -# -# mp_multiplier( sf[2, ]$geometry[[1]] ) -# mp_multiplier( sf[3, ]$geometry[[1]] ) -# mp_multiplier( sf[4, ]$geometry[[1]] ) -# -# cppFunction('int mpoly_multiplier( Rcpp::List mp ){ -# -# Rcpp::List pl = mp[0]; -# return pl.size(); -# -# return mp.size(); -# int multiplier = 1; -# for( int i = 0; i < mp.size(); i++ ) { -# Rcpp::List poly = mp[i]; -# multiplier = multiplier + poly.size(); -# } -# return multiplier; -# }') -# -# mpoly_multiplier( sf[9, ]$geometry ) -# -# mpoly_multiplier( nc[4, ]$geometry ) -# -# sf_geojson(sf[9,]) - - - -# sf <- geojsonsf:::rcpp_read_sf_file_stream(normalizePath("~/Downloads/Texas.geojson"), FALSE) - -# writeLines(mapdeck::geojson, "~/Desktop/geo_melbourne.json") - -# sf <- geojsonsf::geojson_sf( "~/Desktop/geo_melbourne.json" ) -# sfc <- geojsonsf::geojson_sfc( "~/Desktop/geo_melbourne.json" ) diff --git a/inst/include/geojsonsf/geojson/api/df_api.hpp b/inst/include/geojsonsf/geojson/api/df_api.hpp index 7801da8..7d3d7a1 100644 --- a/inst/include/geojsonsf/geojson/api/df_api.hpp +++ b/inst/include/geojsonsf/geojson/api/df_api.hpp @@ -132,7 +132,7 @@ namespace api { for ( i = 0; i < df.length(); ++i) { Rcpp::String this_column = column_names[i]; - int idx = sfheaders::utils::where_is( this_column, geometry_columns ); + R_xlen_t idx = sfheaders::utils::where_is( this_column, geometry_columns ); if ( idx == -1 ) { // i.e. it's not in the vector diff --git a/inst/include/geojsonsf/geojson/geojson_properties.hpp b/inst/include/geojsonsf/geojson/geojson_properties.hpp index 6103f5a..f5ced15 100644 --- a/inst/include/geojsonsf/geojson/geojson_properties.hpp +++ b/inst/include/geojsonsf/geojson/geojson_properties.hpp @@ -71,7 +71,7 @@ namespace geojson_properties { std::vector< std::string > n = properties.names(); std::reverse( n.begin(), n.end() ); std::vector< std::string > sv( n.size() ); - unsigned int i; + R_xlen_t i; for( i = 0; i < n.size(); ++i ) { sv[i] = n[i]; diff --git a/inst/include/geojsonsf/geojson/geojson_to_sf.hpp b/inst/include/geojsonsf/geojson/geojson_to_sf.hpp index dc905bb..95c79f9 100644 --- a/inst/include/geojsonsf/geojson/geojson_to_sf.hpp +++ b/inst/include/geojsonsf/geojson/geojson_to_sf.hpp @@ -41,7 +41,7 @@ namespace sf { Rcpp::List sf(1); Rcpp::List sfc(1); Rcpp::List properties(1); - unsigned int doc_ele; + R_xlen_t doc_ele; if (d.IsObject()) { Rcpp::List sfg(1); diff --git a/inst/include/geojsonsf/geojson/parse.hpp b/inst/include/geojsonsf/geojson/parse.hpp index 8862c4a..eef1320 100644 --- a/inst/include/geojsonsf/geojson/parse.hpp +++ b/inst/include/geojsonsf/geojson/parse.hpp @@ -67,8 +67,8 @@ namespace parse { std::string geom_type; geojsonsf::validate::validate_geometries(val, sfg_objects); auto geometries = val["geometries"].GetArray(); - unsigned int n = geometries.Size(); - unsigned int i; + R_xlen_t n = geometries.Size(); + R_xlen_t i; Rcpp::List geom_collection(n); for (i = 0; i < n; ++i) { @@ -137,8 +137,8 @@ namespace parse { geojsonsf::geojson_properties::get_property_keys(p, property_keys); geojsonsf::geojson_properties::get_property_types(p, property_types); - unsigned int geomsize = 1; - unsigned int i; + R_xlen_t geomsize = 1; + R_xlen_t i; if (expand_geometries && type == "GeometryCollection") { geojsonsf::validate::validate_geometries( geometry, sfg_objects ); @@ -185,8 +185,8 @@ namespace parse { auto features = fc["features"].GetArray(); - unsigned int n = features.Size(); // number of features - unsigned int i; + R_xlen_t n = features.Size(); // number of features + R_xlen_t i; Rcpp::List feature_collection(n); @@ -204,7 +204,7 @@ namespace parse { const Value& v, Rcpp::List& sfc, Rcpp::List& properties, - int i, + R_xlen_t i, Rcpp::NumericVector& bbox, Rcpp::NumericVector& z_range, Rcpp::NumericVector& m_range, @@ -279,7 +279,7 @@ namespace parse { Document& d, Rcpp::List& sfc, Rcpp::List& properties, - int i, + R_xlen_t i, Rcpp::NumericVector& bbox, Rcpp::NumericVector& z_range, Rcpp::NumericVector& m_range, diff --git a/inst/include/geojsonsf/geojson/writers/writers.hpp b/inst/include/geojsonsf/geojson/writers/writers.hpp index 3bd00ce..727b29b 100644 --- a/inst/include/geojsonsf/geojson/writers/writers.hpp +++ b/inst/include/geojsonsf/geojson/writers/writers.hpp @@ -97,7 +97,7 @@ namespace writers { } template< typename Writer > - inline void polygon_separator( Writer& writer, int i, int n ) { + inline void polygon_separator( Writer& writer, R_xlen_t i, R_xlen_t n ) { if (i < ( n - 1 ) ) { writer.EndArray(); writer.EndArray(); @@ -107,7 +107,7 @@ namespace writers { } template< typename Writer > - inline void line_separator( Writer& writer, int i, int n) { + inline void line_separator( Writer& writer, R_xlen_t i, R_xlen_t n) { if ( i < ( n - 1 ) ) { writer.EndArray(); writer.StartArray(); @@ -116,8 +116,8 @@ namespace writers { template< typename Writer > inline void points_to_geojson( Writer& writer, Rcpp::IntegerVector& point, int digits ) { - int n = point.size(); - int i; + R_xlen_t n = point.size(); + R_xlen_t i; int value; writer.StartArray(); for ( i = 0; i < n; ++i ) { @@ -138,8 +138,8 @@ namespace writers { template< typename Writer > inline void points_to_geojson( Writer& writer, Rcpp::NumericVector& point, int digits ) { - int n = point.size(); - int i; + R_xlen_t n = point.size(); + R_xlen_t i; double value; writer.StartArray(); for ( i = 0; i < n; ++i ) { @@ -187,8 +187,8 @@ namespace writers { template< typename Writer > inline void linestring_to_geojson( Writer& writer, Rcpp::IntegerMatrix& line, int digits ) { - int i; - int nrow = line.nrow(); + R_xlen_t i; + R_xlen_t nrow = line.nrow(); for ( i = 0; i < nrow; ++i ) { Rcpp::IntegerVector this_row = line(i, Rcpp::_ ); points_to_geojson( writer, this_row, digits ); @@ -202,8 +202,8 @@ namespace writers { template< typename Writer > inline void linestring_to_geojson( Writer& writer, Rcpp::NumericMatrix& line, int digits ) { - int i; - int nrow = line.nrow(); + R_xlen_t i; + R_xlen_t nrow = line.nrow(); for ( i = 0; i < nrow; ++i ) { Rcpp::NumericVector this_row = line(i, Rcpp::_ ); points_to_geojson( writer, this_row, digits ); @@ -238,8 +238,8 @@ namespace writers { template< typename Writer > inline void polygon_to_geojson( Writer& writer, Rcpp::List& sfg, int digits ) { - int i; - int n = sfg.size(); + R_xlen_t i; + R_xlen_t n = sfg.size(); for ( i = 0; i < n; ++i ) { Rcpp::NumericMatrix sfgi = sfg[i]; linestring_to_geojson( writer, sfgi, digits ); @@ -254,8 +254,8 @@ namespace writers { template< typename Writer > inline void multi_polygon_to_geojson( Writer& writer, Rcpp::List& sfg, int digits ) { - int i; - int n = sfg.size(); + R_xlen_t i; + R_xlen_t n = sfg.size(); for ( i = 0; i < n; ++i ) { Rcpp::List sfgi = sfg[i]; polygon_to_geojson( writer, sfgi, digits ); diff --git a/inst/include/geojsonsf/wkt/geojson_wkt.hpp b/inst/include/geojsonsf/wkt/geojson_wkt.hpp index 18e39aa..2439b96 100644 --- a/inst/include/geojsonsf/wkt/geojson_wkt.hpp +++ b/inst/include/geojsonsf/wkt/geojson_wkt.hpp @@ -5,7 +5,7 @@ #include "rapidjson/document.h" using namespace rapidjson; -void coord_separator(std::ostringstream& os, int i, int n); +void coord_separator(std::ostringstream& os, R_xlen_t i, R_xlen_t n); void begin_wkt(std::ostringstream& os, std::string& geom_type, R_xlen_t& coord_dim ); diff --git a/src/geojson_wkt.cpp b/src/geojson_wkt.cpp index d1e793e..ad31aef 100644 --- a/src/geojson_wkt.cpp +++ b/src/geojson_wkt.cpp @@ -64,19 +64,19 @@ void object_separator(std::ostringstream& os) { os << ","; } -void coord_separator(std::ostringstream& os, int i, int n) { +void coord_separator(std::ostringstream& os, R_xlen_t i, R_xlen_t n) { if (i < (n - 1) ) { os << ","; } } -void line_separator_wkt(std::ostringstream& os, int i, int n) { +void line_separator_wkt(std::ostringstream& os, R_xlen_t i, R_xlen_t n) { if (i < (n - 1) ) { os << "),("; } } -void polygon_separate_wkt(std::ostringstream& os, int i, int n) { +void polygon_separate_wkt(std::ostringstream& os, R_xlen_t i, R_xlen_t n) { if (i < (n - 1) ) { os << ")),(("; }