-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Performance with sp objects #28
Comments
Profiling shows major bottleneck is in |
Is there a way to get around writing to disk by writing to
|
The main bottleneck seems actually to be due to a - seemingly unnecessary - transformation loop in the As you have already stated: The critical command in the # rmapshaper/R/simplify.R function ms_simplify_sp line 157
geojson <- sp_to_GeoJSON(input) And inside # rmapshaper/R/zzz.R function sp_to_GeoJSON line 53-55
proj <- sp::proj4string(sp)
js <- geojsonio::geojson_json(sp)
structure(js, proj4 = proj) So let's see what # geojsonio/R/geosjson_json.R function geojson.SpatialPolygonsDataFrame line 193
to_json(geojson_rw(input), ...) it is transforming the output from # geojsonio/R/zzz.R function geojson_rw 306-308
tmp <- tempfile(fileext = ".geojson")
suppressMessages(geojson_write(input, file = tmp))
jsonlite::fromJSON(tmp, simplifyDataFrame = FALSE, simplifyMatrix = FALSE, ...) So what we are basically doing is to transform the So let's write a faster version of ms_simplify_fast <- function(sp, ...) {
# this is mimicking sp_to_GeoJSON without the additional loop JSON -> R -> JSON
proj4 <- proj4string(sp)
tmp <- tempfile(fileext = ".geojson")
suppressMessages(geojsonio::geojson_write(sp, file = tmp))
gj <- structure(readChar(tmp, file.info(tmp)$size), class = c("json", "geo_json"))
file.remove(tmp)
# here we run the simplification function
gj_simp <- rmapshaper::ms_simplify(gj, ...)
# this is same as GeoJSON_to_sp
sp <- suppressWarnings(
suppressMessages(
rgdal::readOGR(gj_simp, "OGRGeoJSON", verbose = FALSE,
disambiguateFIDs = TRUE, p4s = proj4)
))
# this is same as curly_brace_na(sp)
sp@data[sp@data == "{ }"] <- NA
sp
} I do not know if the transforming back and forth adds anything additional (or leaves out something ...), but in any case for the Benchmark the fast function with a couple of different maps: bench_simplify <- function(country) {
map <- raster::getData('GADM', level='1', country=country)
print(microbenchmark::microbenchmark(original=simp_original <- ms_simplify(map),
fast=simp_fast <- ms_simplify_fast(map),
times=5) ,
unit="s", signif=3)
} and look at the result:
which is a nice speedup ;-) |
Thanks @javrucebo! That is a very good point - we shouldn't need to go through the intermediate json object. I will double-check that There is also a major bottleneck with writing a very large sp object to disk as geojson |
Actually funny enough I just double-checked and the writing as geojson isn't so bad... so either my memory is faulty or rgdal had a recent update that fixed it... |
Largely fixed in geojsonio by @javrucebo suggestions |
Seems really slow compared to eg:
Might be related to writing really long geojson strings??
The text was updated successfully, but these errors were encountered: