Created
June 13, 2016 07:21
-
-
Save javrucebo/da86e7bf0fb4f5367e2b405c3bed1e54 to your computer and use it in GitHub Desktop.
Benchmark and check the SpatialPolygons_to_geojson function based on Rcpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# install newest geojsonio | |
devtools::install_github("ropensci/geojsonio") | |
# include the new function | |
source("https://gist.github.com/javrucebo/fbbfe4b504d49893ad03d8fe0bd84f73/raw/8cf2cc10ddef665ea8f1538e011fc6484e978cef/SpatialPolygons_to_geojson.R") | |
library(magrittr) | |
library(stringi) | |
# helper function: extract all numerical values from json string to check | |
# if values are nearly equal | |
xx_js2num <- function(str) { | |
stri_match_all_regex(str, "(?<=[\\[,: {])([-+]?\\d+(?:\\.\\d+(?:[eE][-+]\\d+)?)?)") %>% extract2(1) %>% .[,1] %>% as.numeric | |
} | |
# helper function: substiture all numerical values by symbols to compare structure | |
xx_js_wo_num <- function(str) { | |
# replace numbers by {NUM}" | |
stri_replace_all_regex(str, "(?<=[\\[,: {])([-+]?\\d+(?:\\.\\d+(?:[eE][-+]\\d+)?)?)","{NUM}") %>% | |
# add newline after each comma for later diffing | |
stri_replace_all_fixed(",",",\n") %>% | |
# eliminate spaces | |
stri_replace_all_fixed(" ","") %>% | |
# remove escape in front of '/' (as writeOGR introduces) | |
stri_replace_all_fixed("\\/","/") | |
} | |
# compare to json strings whether they are "equal" | |
# a) extract all numerical values and calculate pairwise differences. | |
# maximum absolute/relative difference 1e-4 | |
# b) compare structure/strings without numerics | |
jsonstring_compare_pairwise <- function(x,y) { | |
# extract numerical value | |
x_num <- xx_js2num(x) | |
y_num <- xx_js2num(y) | |
# extract structure w/o nubers | |
x_str <- xx_js_wo_num(x) | |
y_str <- xx_js_wo_num(y) | |
cat(x,file="/tmp/kkx") | |
cat(y,file="/tmp/kky") | |
cat(x_str,file="/tmp/jjx") | |
cat(y_str,file="/tmp/jjy") | |
# check whether structure is equal and numerical value close to each other | |
check_num <- max(ifelse(y_num <1,abs(x_num-y_num),abs(x_num-y_num)/abs(y_num)))<1e-04 | |
check_str <- x_str == y_str | |
if (!check_num) { message(paste("numeric difference",max(ifelse(y_num <1,abs(x_num-y_num),abs(x_num-y_num)/abs(y_num))))) } | |
if (!check_str) { message("string difference") } | |
check_num & check_str | |
} | |
# check values if they are nearly equal | |
jsonstring_compare <- function(values) { | |
all(sapply(values[-1], function(x) jsonstring_compare_pairwise(values[[1]], x))) | |
} | |
## benchmark and check the functions | |
bench_sptogeojson <- function(sp, times=1) { | |
sp::proj4string(sp) <- "" | |
print(microbenchmark::microbenchmark( | |
gejson_json=geojsonio::geojson_json(sp), | |
SpatialPolygons_to_geojson=SpatialPolygons_to_geojson(sp), | |
times=times, | |
check=jsonstring_compare), | |
unit="s", signif=3) | |
invisible(sp) | |
} | |
bench_sptogeojson(raster::getData('GADM', country='VAT', level=0)) | |
bench_sptogeojson(raster::getData('GADM', country='UKR', level=1)) | |
bench_sptogeojson(raster::getData('GADM', country='SWE', level=2)) | |
bench_sptogeojson(raster::getData('GADM', country='CAN', level=3)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment