Skip to content

Commit

Permalink
Merge pull request #18 from SymbolixAU/unnest
Browse files Browse the repository at this point in the history
Unnest
  • Loading branch information
SymbolixAU authored Apr 26, 2018
2 parents 62c3ec6 + bae020e commit abea816
Show file tree
Hide file tree
Showing 17 changed files with 317 additions and 305 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# R for travis: see documentation at https://docs.travis-ci.com/user/languages/r

language: r
r: devel
sudo: false
cache: packages

Expand Down
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: geojsonsf
Type: Package
Title: GeoJSON to Simple Feature Converter
Version: 0.3.0009
Version: 0.3.0010
Authors@R: c(
person("David", "Cooley", ,"[email protected]", role = c("aut", "cre"))
)
Expand Down
16 changes: 8 additions & 8 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

rcpp_geojson_to_sfc <- function(geojson) {
.Call(`_geojsonsf_rcpp_geojson_to_sfc`, geojson)
rcpp_geojson_to_sfc <- function(geojson, flatten_geometries) {
.Call(`_geojsonsf_rcpp_geojson_to_sfc`, geojson, flatten_geometries)
}

rcpp_geojson_to_sf <- function(geojson) {
.Call(`_geojsonsf_rcpp_geojson_to_sf`, geojson)
rcpp_geojson_to_sf <- function(geojson, flatten_geometries) {
.Call(`_geojsonsf_rcpp_geojson_to_sf`, geojson, flatten_geometries)
}

rcpp_geojson_to_wkt <- function(geojson) {
.Call(`_geojsonsf_rcpp_geojson_to_wkt`, geojson)
}

rcpp_read_sfc_file <- function(file) {
.Call(`_geojsonsf_rcpp_read_sfc_file`, file)
rcpp_read_sfc_file <- function(file, flatten_geometries) {
.Call(`_geojsonsf_rcpp_read_sfc_file`, file, flatten_geometries)
}

rcpp_read_sf_file <- function(file) {
.Call(`_geojsonsf_rcpp_read_sf_file`, file)
rcpp_read_sf_file <- function(file, flatten_geometries) {
.Call(`_geojsonsf_rcpp_read_sf_file`, file, flatten_geometries)
}

rcpp_sfc_to_geojson <- function(sfc) {
Expand Down
43 changes: 26 additions & 17 deletions R/geojson_sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@
#' Extracts geometries from GeoJSON and returns an `sfc` object
#'
#' @param geojson string or vector of GeoJSON, or a URL or file pointing to a geojson file
#' @param flatten_geometries logical indicating whether to unnest GEOMETRYCOLLECTION rows. see details
#'
#' @details
#' specifying \code{flatten_geometries = TRUE} will expand individual \code{GEOMETRYCOLLECTION}
#' geometries to their own row in the resulting `sf` object. If the geometries are part
#' of a \code{Feature} (i.e., with properties), the properties will be repeated on each row.
#'
#' The \code{GEOMETRYCOLLECTION} information is not kept when using \code{flatten_geometries = TRUE}. Therefore,
#' it is not possible to reconstruct the \code{GEOMETRYCOLLECTION} after unnesting it.
#'
#' @examples
#'
Expand All @@ -20,28 +29,28 @@
#'}
#'
#' @export
geojson_sfc <- function(geojson) UseMethod("geojson_sfc")
geojson_sfc <- function(geojson, flatten_geometries = FALSE) UseMethod("geojson_sfc")


#' @export
geojson_sfc.character <- function(geojson) {
geojson_sfc.character <- function(geojson, flatten_geometries = FALSE) {

if(length(geojson) > 1) {
return(rcpp_geojson_to_sfc(geojson))
return(rcpp_geojson_to_sfc(geojson, flatten_geometries))
}
if (is_url(geojson)) {
return(geojson_sfc(curl::curl(geojson)))
return(geojson_sfc(curl::curl(geojson), flatten_geometries))
} else if (file.exists(geojson) ) {
return(rcpp_read_sfc_file(normalizePath(geojson)))
return(rcpp_read_sfc_file(normalizePath(geojson), flatten_geometries))
}
return(rcpp_geojson_to_sfc(geojson))
return(rcpp_geojson_to_sfc(geojson, flatten_geometries))
}

#' @export
geojson_sfc.connection <- function(geojson) geojson_sfc(read_url(geojson))
geojson_sfc.connection <- function(geojson, flatten_geometries = FALSE) geojson_sfc(read_url(geojson), flatten_geometries)

#' @export
geojson_sfc.default <- function(geojson) rcpp_geojson_to_sfc(geojson)
geojson_sfc.default <- function(geojson, flatten_geometries = FALSE) rcpp_geojson_to_sfc(geojson, flatten_geometries)

#' Geojson to sf
#'
Expand All @@ -63,30 +72,30 @@ geojson_sfc.default <- function(geojson) rcpp_geojson_to_sfc(geojson)
#' sf <- geojson_sf(myurl)
#'}
#'
#' @inheritParams geojson_sfc
#' @inherit geojson_sfc params details
#' @export
geojson_sf <- function(geojson) UseMethod("geojson_sf")
geojson_sf <- function(geojson, flatten_geometries = FALSE) UseMethod("geojson_sf")


#' @export
geojson_sf.character <- function(geojson) {
geojson_sf.character <- function(geojson, flatten_geometries = FALSE) {

if(length(geojson) > 1) {
return(rcpp_geojson_to_sf(geojson))
return(rcpp_geojson_to_sf(geojson, flatten_geometries))
}
if (is_url(geojson)) {
return(geojson_sf(curl::curl(geojson)))
return(geojson_sf(curl::curl(geojson), flatten_geometries))
} else if (file.exists(geojson) ) {
return(rcpp_read_sf_file(normalizePath(geojson)))
return(rcpp_read_sf_file(normalizePath(geojson), flatten_geometries))
}
return(rcpp_geojson_to_sf(geojson))
return(rcpp_geojson_to_sf(geojson, flatten_geometries))
}

#' @export
geojson_sf.connection <- function(geojson) geojson_sf(read_url(geojson))
geojson_sf.connection <- function(geojson, flatten_geometries = F) geojson_sf(read_url(geojson), flatten_geometries)

#' @export
geojson_sf.default <- function(geojson) rcpp_geojson_to_sf(geojson)
geojson_sf.default <- function(geojson, flatten_geometries = F) rcpp_geojson_to_sf(geojson, flatten_geometries)


## TODO:
Expand Down
2 changes: 1 addition & 1 deletion R/scratch.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
# url <- "https://data.seattle.gov/resource/pdbw-sw7q.geojson"
# sf <- sf::st_read(url, quiet = T)
# sf2 <- geojson_sf(url)

#

48 changes: 28 additions & 20 deletions inst/include/geojson_to_sf.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,41 @@ void parse_geometry_object(Rcpp::List& sfc, int i, const Value& geometry,
std::set< std::string >& geometry_types,
int& sfg_objects);

Rcpp::List parse_geometry_collection_object(const Value& val, Rcpp::NumericVector& bbox,
Rcpp::List parse_geometry_collection_object(const Value& val,
Rcpp::NumericVector& bbox,
std::set< std::string >& geometry_types,
int& sfg_objects);
int& sfg_objects,
bool& flatten_geometries);

Rcpp::List parse_feature_object(const Value& feature,
Rcpp::NumericVector& bbox,
std::set< std::string >& geometry_types,
int& sfg_objects,
std::set< std::string >& property_keys,
Document& doc_properties,
std::map< std::string, std::string>& property_types);
std::map< std::string, std::string>& property_types,
bool& flatten_geometries);

Rcpp::List parse_feature_collection_object(const Value& fc,
Rcpp::NumericVector& bbox,
std::set< std::string >& geometry_types,
int& sfg_objects,
std::set< std::string >& property_keys,
Document& doc_properties,
std::map< std::string, std::string>& property_types);
std::map< std::string, std::string>& property_types,
bool& flatten_geometries);

void parse_geojson(const Value& v,
Rcpp::List& sfc,
Rcpp::List& properties,
int i,
Rcpp::NumericVector& bbox,
std::set< std::string >& geometry_types,
int& sfg_objects,
std::set< std::string >& property_keys,
Document& doc_properties,
std::map< std::string, std::string>& property_types,
bool& flatten_geometries);

void parse_geojson_array(Document& d,
Rcpp::List& sfc,
Expand All @@ -43,7 +59,8 @@ void parse_geojson_array(Document& d,
int& sfg_objects,
std::set< std::string >& property_keys,
Document& doc_properties,
std::map< std::string, std::string>& property_types);
std::map< std::string, std::string>& property_types,
bool& flatten_geometries);

void parse_geojson_object(Document& d,
Rcpp::List& sfc,
Expand All @@ -53,25 +70,16 @@ void parse_geojson_object(Document& d,
int& sfg_objects,
std::set< std::string >& property_keys,
Document& doc_properties,
std::map< std::string, std::string>& property_types);

void parse_geojson(const Value& v,
Rcpp::List& sfc,
Rcpp::List& properties,
int i,
Rcpp::NumericVector& bbox,
std::set< std::string >& geometry_types,
int& sfg_objects,
std::set< std::string >& property_keys,
Document& doc_properties,
std::map< std::string, std::string>& property_types);
std::map< std::string, std::string>& property_types,
bool& flatten_geometries);

Rcpp::List geojson_to_sf(const char* geojson, Rcpp::NumericVector& bbox,
std::set< std::string >& geometry_types,
int& sfg_objects,
std::set< std::string >& property_keys,
Document& doc_properties,
std::map< std::string, std::string>& property_types);
std::map< std::string, std::string>& property_types,
bool& flatten_geometries);

void setup_property_vectors(std::map< std::string, std::string>& property_types,
Rcpp::List& properties, int& sfg_objects);
Expand All @@ -87,8 +95,8 @@ Rcpp::List construct_sf(Rcpp::List& lst, std::set< std::string >& property_keys,
int& sfg_objects,
int& row_index);

Rcpp::List generic_geojson_to_sf(Rcpp::StringVector geojson);
Rcpp::List generic_geojson_to_sf(Rcpp::StringVector geojson, bool& flatten_geometries);

Rcpp::List create_sfc(Rcpp::StringVector geojson);
Rcpp::List create_sfc(Rcpp::StringVector geojson, bool& flatten_geometries);

#endif
12 changes: 11 additions & 1 deletion man/geojson_sf.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion man/geojson_sfc.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 20 additions & 16 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,26 @@
using namespace Rcpp;

// rcpp_geojson_to_sfc
Rcpp::List rcpp_geojson_to_sfc(Rcpp::StringVector geojson);
RcppExport SEXP _geojsonsf_rcpp_geojson_to_sfc(SEXP geojsonSEXP) {
Rcpp::List rcpp_geojson_to_sfc(Rcpp::StringVector geojson, bool& flatten_geometries);
RcppExport SEXP _geojsonsf_rcpp_geojson_to_sfc(SEXP geojsonSEXP, SEXP flatten_geometriesSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< Rcpp::StringVector >::type geojson(geojsonSEXP);
rcpp_result_gen = Rcpp::wrap(rcpp_geojson_to_sfc(geojson));
Rcpp::traits::input_parameter< bool& >::type flatten_geometries(flatten_geometriesSEXP);
rcpp_result_gen = Rcpp::wrap(rcpp_geojson_to_sfc(geojson, flatten_geometries));
return rcpp_result_gen;
END_RCPP
}
// rcpp_geojson_to_sf
Rcpp::List rcpp_geojson_to_sf(Rcpp::StringVector geojson);
RcppExport SEXP _geojsonsf_rcpp_geojson_to_sf(SEXP geojsonSEXP) {
Rcpp::List rcpp_geojson_to_sf(Rcpp::StringVector geojson, bool flatten_geometries);
RcppExport SEXP _geojsonsf_rcpp_geojson_to_sf(SEXP geojsonSEXP, SEXP flatten_geometriesSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< Rcpp::StringVector >::type geojson(geojsonSEXP);
rcpp_result_gen = Rcpp::wrap(rcpp_geojson_to_sf(geojson));
Rcpp::traits::input_parameter< bool >::type flatten_geometries(flatten_geometriesSEXP);
rcpp_result_gen = Rcpp::wrap(rcpp_geojson_to_sf(geojson, flatten_geometries));
return rcpp_result_gen;
END_RCPP
}
Expand All @@ -40,24 +42,26 @@ BEGIN_RCPP
END_RCPP
}
// rcpp_read_sfc_file
Rcpp::List rcpp_read_sfc_file(std::string file);
RcppExport SEXP _geojsonsf_rcpp_read_sfc_file(SEXP fileSEXP) {
Rcpp::List rcpp_read_sfc_file(std::string file, bool flatten_geometries);
RcppExport SEXP _geojsonsf_rcpp_read_sfc_file(SEXP fileSEXP, SEXP flatten_geometriesSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< std::string >::type file(fileSEXP);
rcpp_result_gen = Rcpp::wrap(rcpp_read_sfc_file(file));
Rcpp::traits::input_parameter< bool >::type flatten_geometries(flatten_geometriesSEXP);
rcpp_result_gen = Rcpp::wrap(rcpp_read_sfc_file(file, flatten_geometries));
return rcpp_result_gen;
END_RCPP
}
// rcpp_read_sf_file
Rcpp::List rcpp_read_sf_file(std::string file);
RcppExport SEXP _geojsonsf_rcpp_read_sf_file(SEXP fileSEXP) {
Rcpp::List rcpp_read_sf_file(std::string file, bool flatten_geometries);
RcppExport SEXP _geojsonsf_rcpp_read_sf_file(SEXP fileSEXP, SEXP flatten_geometriesSEXP) {
BEGIN_RCPP
Rcpp::RObject rcpp_result_gen;
Rcpp::RNGScope rcpp_rngScope_gen;
Rcpp::traits::input_parameter< std::string >::type file(fileSEXP);
rcpp_result_gen = Rcpp::wrap(rcpp_read_sf_file(file));
Rcpp::traits::input_parameter< bool >::type flatten_geometries(flatten_geometriesSEXP);
rcpp_result_gen = Rcpp::wrap(rcpp_read_sf_file(file, flatten_geometries));
return rcpp_result_gen;
END_RCPP
}
Expand Down Expand Up @@ -86,11 +90,11 @@ END_RCPP
}

static const R_CallMethodDef CallEntries[] = {
{"_geojsonsf_rcpp_geojson_to_sfc", (DL_FUNC) &_geojsonsf_rcpp_geojson_to_sfc, 1},
{"_geojsonsf_rcpp_geojson_to_sf", (DL_FUNC) &_geojsonsf_rcpp_geojson_to_sf, 1},
{"_geojsonsf_rcpp_geojson_to_sfc", (DL_FUNC) &_geojsonsf_rcpp_geojson_to_sfc, 2},
{"_geojsonsf_rcpp_geojson_to_sf", (DL_FUNC) &_geojsonsf_rcpp_geojson_to_sf, 2},
{"_geojsonsf_rcpp_geojson_to_wkt", (DL_FUNC) &_geojsonsf_rcpp_geojson_to_wkt, 1},
{"_geojsonsf_rcpp_read_sfc_file", (DL_FUNC) &_geojsonsf_rcpp_read_sfc_file, 1},
{"_geojsonsf_rcpp_read_sf_file", (DL_FUNC) &_geojsonsf_rcpp_read_sf_file, 1},
{"_geojsonsf_rcpp_read_sfc_file", (DL_FUNC) &_geojsonsf_rcpp_read_sfc_file, 2},
{"_geojsonsf_rcpp_read_sf_file", (DL_FUNC) &_geojsonsf_rcpp_read_sf_file, 2},
{"_geojsonsf_rcpp_sfc_to_geojson", (DL_FUNC) &_geojsonsf_rcpp_sfc_to_geojson, 1},
{"_geojsonsf_rcpp_sf_to_geojson", (DL_FUNC) &_geojsonsf_rcpp_sf_to_geojson, 2},
{NULL, NULL, 0}
Expand Down
Loading

0 comments on commit abea816

Please sign in to comment.