Skip to content

Commit

Permalink
Merge pull request #7 from SymbolixAU/stream
Browse files Browse the repository at this point in the history
Stream
  • Loading branch information
SymbolixAU authored Apr 19, 2018
2 parents b25ee37 + 7daef51 commit e172dce
Show file tree
Hide file tree
Showing 18 changed files with 363 additions and 40 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
^codecov\.yml$
^README\.Rmd$
^cran-comments\.md$
^data-raw$
9 changes: 6 additions & 3 deletions 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.0001
Version: 0.3.0002
Authors@R: c(
person("David", "Cooley", ,"[email protected]", role = c("aut", "cre"))
)
Expand All @@ -10,9 +10,12 @@ License: GPL-3
Encoding: UTF-8
LazyData: true
LinkingTo:
Rcpp, rapidjsonr
Rcpp,
rapidjsonr
Imports:
Rcpp, rapidjsonr
Rcpp,
rapidjsonr,
curl
RoxygenNote: 6.0.1
Suggests:
covr,
Expand Down
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Generated by roxygen2: do not edit by hand

S3method(geojson_sf,character)
S3method(geojson_sf,connection)
S3method(geojson_sf,default)
S3method(geojson_sfc,character)
S3method(geojson_sfc,connection)
S3method(geojson_sfc,default)
export(geojson_sf)
export(geojson_sfc)
export(geojson_wkt)
Expand Down
12 changes: 12 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# geojsonsf 0.3

* `geojson_sf()` and `geojson_sfc()` can now read from a url or file


## v0.2.0 First release

* `geojson_sf()` convert GeoJSON to `sf` object
* `geojson_sfc()` convert GeoJSON to `sfc` object
* `geojson_wkt()` convert GeoJSON to `data.frame` with a Well-known text column


8 changes: 8 additions & 0 deletions R/RcppExports.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,11 @@ 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_sf_file <- function(file) {
.Call(`_geojsonsf_rcpp_read_sf_file`, file)
}

80 changes: 77 additions & 3 deletions R/geojson_sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#'
#' Extracts geometries from GeoJSON and returns an `sfc` object
#'
#' @param geojson string or vector of GeoJSON
#' @param geojson string or vector of GeoJSON, or a URL or file pointing to a geojson file
#'
#' @examples
#' ## load 'sf' for print methods
Expand All @@ -11,7 +11,44 @@
#' geojson_sfc(geojson)
#'
#' @export
geojson_sfc <- function(geojson) rcpp_geojson_to_sfc(geojson)
geojson_sfc <- function(geojson) UseMethod("geojson_sfc")


#' @export
geojson_sfc.character <- function(geojson) {

if(length(geojson) > 1) {
return(rcpp_geojson_to_sfc(geojson))
}

if (is_url(geojson)) {
return(geojson_sfc(curl::curl(geojson)))
} else if (file.exists(geojson) ) {
return(rcpp_read_sfc_file(normalizePath(geojson)))
}
return(rcpp_geojson_to_sfc(geojson))
}

#' @export
geojson_sfc.connection <- function(geojson) geojson_sfc(read_url(geojson))

#' @export
geojson_sfc.default <- function(geojson) rcpp_geojson_to_sfc(geojson)

is_url <- function(geojson) grepl("^https?://", geojson, useBytes=TRUE)

read_url <- function(con) {
out <- tryCatch({
paste0(readLines(con), collapse = "")
},
error = function(cond){
warning("There was an error downloading the geojson")
},
finally = {
close(con)
})
}


#' Geojson to sf
#'
Expand All @@ -26,4 +63,41 @@ geojson_sfc <- function(geojson) rcpp_geojson_to_sfc(geojson)
#'
#' @inheritParams geojson_sfc
#' @export
geojson_sf <- function(geojson) rcpp_geojson_to_sf(geojson)
geojson_sf <- function(geojson) UseMethod("geojson_sf")


#' @export
geojson_sf.character <- function(geojson) {

if(length(geojson) > 1) {
return(rcpp_geojson_to_sf(geojson))
}

if (is_url(geojson)) {
return(geojson_sf(curl::curl(geojson)))
} else if (file.exists(geojson) ) {
return(rcpp_read_sf_file(normalizePath(geojson)))
}
return(rcpp_geojson_to_sf(geojson))
}

#' @export
geojson_sf.connection <- function(geojson) geojson_sf(read_url(geojson))

#' @export
geojson_sf.default <- function(geojson) rcpp_geojson_to_sf(geojson)

is_url <- function(geojson) grepl("^https?://", geojson, useBytes=TRUE)

read_url <- function(con) {
out <- tryCatch({
paste0(readLines(con), collapse = "")
},
error = function(cond){
warning("There was an error downloading the geojson")
},
finally = {
close(con)
})
}

67 changes: 67 additions & 0 deletions R/scratch.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,76 @@

## are they only attached when there are mixxed geometries?

## test
## url string
## connection
## file (local)
## file from package
##

# url <- "http://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_outline_500k.json"
# sf <- geojson_sf(url)

# url <- "http://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_050_00_500k.json"
# sf <- geojson_sf(url)
#
# con <- curl::curl(url)
# sf <- geojson_sf(con)
#
# f <- system.file("examples", "california.geojson", package = "geojsonio")
# sf <- geojson_sf(f)

# geojson_sf(("~/Desktop/myjson.json"))


# url <- "http://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_050_00_500k.json"
# sf <- geojson_sfc(url)
#
# con <- curl::curl(url)
# sf <- geojson_sfc(con)
#
# f <- system.file("examples", "california.geojson", package = "geojsonio")
# sf <- geojson_sfc(f)
#
# geojson_sfc(("~/Desktop/myjson.json"))


# g <- gsf$geometry
#
# g
#
# sfg <- sf::st_sf(g)
# attributes(sfg$g)
#
# library(Rcpp)
#
# con <- curl::curl(url = "http://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_050_00_500k.json")
# geo <- readLines(con)
#
#
# cppFunction('Rcpp::StringVector cppPaste(Rcpp::StringVector sv) {
#
# std::ostringstream os;
# for (int i = 0; i < sv.size(); i++) {
# os << sv[i];
# }
#
# return os.str();
#
# }')
#
# geo1 <- cppPaste(geo)
#
# geojsonsf::geojson_sf(geo1)
#
# library(microbenchmark)
#
# microbenchmark(
# rcpp = {
# cppPaste(geo)
# },
# base = {
# paste0(geo)
# },
# times = 5
# )
24 changes: 21 additions & 3 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ geojson_sf(js)

### Speed

This benchmark shows a comparison with `library(sf)` for converting GeoJSON of 3,221 counties in the US in to an `sf` object
This benchmark shows a comparison with `library(sf)` for converting a string of GeoJSON of 3,221 counties in the US in to an `sf` object

```{r, warning=FALSE}
myurl <- "http://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_050_00_500k.json"
Expand All @@ -122,12 +122,30 @@ geo <- paste0(geo, collapse = "")
library(microbenchmark)
microbenchmark(
geojsonsf = {
geojson_sf(geo)
},
sf = {
sf::st_read(geo, quiet = T)
},
times = 2
)
```

Reading directly from a URL is comparable between the two

```{r, warning=FALSE}
myurl <- "http://eric.clst.org/assets/wiki/uploads/Stuff/gz_2010_us_050_00_500k.json"
library(microbenchmark)
microbenchmark(
geojsonsf = {
geojson_sf(geo)
geojson_sf(myurl)
},
sf = {
sf::st_read(geo, quiet = T)
sf::st_read(myurl, quiet = T)
},
times = 2
)
Expand Down
4 changes: 4 additions & 0 deletions data-raw/generate_geojson.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@


geo_melbourne <- paste0(googleway::geo_melbourne, collapse = "")
usethis::use_data(geo_melbourne)
Binary file added data/geo_melbourne.rda
Binary file not shown.
10 changes: 10 additions & 0 deletions inst/include/geojson_to_sf.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,14 @@ void fill_property_vectors(Document& doc_properties,
Rcpp::List& properties,
int& row_index);

Rcpp::List construct_sf(Rcpp::List& lst, std::set< std::string >& property_keys,
std::map< std::string, std::string>& property_types,
Document& doc_properties,
int& sfg_objects,
int& row_index);

Rcpp::List generic_geojson_to_sf(Rcpp::StringVector geojson);

Rcpp::List create_sfc(Rcpp::StringVector geojson);

#endif
2 changes: 1 addition & 1 deletion man/geojson_sf.Rd

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

2 changes: 1 addition & 1 deletion man/geojson_sfc.Rd

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

2 changes: 1 addition & 1 deletion man/geojson_wkt.Rd

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

24 changes: 24 additions & 0 deletions src/RcppExports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,35 @@ BEGIN_RCPP
return rcpp_result_gen;
END_RCPP
}
// rcpp_read_sfc_file
Rcpp::List rcpp_read_sfc_file(std::string file);
RcppExport SEXP _geojsonsf_rcpp_read_sfc_file(SEXP fileSEXP) {
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));
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) {
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));
return rcpp_result_gen;
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_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},
{NULL, NULL, 0}
};

Expand Down
Loading

0 comments on commit e172dce

Please sign in to comment.