-
Notifications
You must be signed in to change notification settings - Fork 2.1k
geom_sf is appallingly slow in some cases #2655
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
Comments
devtools::install_github("hadley/ggplot2")
#> Using GitHub PAT from envvar GITHUB_PAT
#> Warning in strptime(x, fmt, tz = "GMT"): unknown timezone 'zone/tz/2018c.
#> 1.0/zoneinfo/America/Chicago'
#> Downloading GitHub repo hadley/ggplot2@master
#> from URL https://api.github.com/repos/hadley/ggplot2/zipball/master
#> Installing ggplot2
#> '/Library/Frameworks/R.framework/Resources/bin/R' --no-site-file \
#> --no-environ --no-save --no-restore --quiet CMD INSTALL \
#> '/private/var/folders/j5/977488_x28x3hcjmb6p5nmgw0000gn/T/RtmphU41Uj/devtools4c3513daba4c/tidyverse-ggplot2-69dfc4b' \
#> --library='/Library/Frameworks/R.framework/Versions/3.4/Resources/library' \
#> --install-tests
#>
library(ggplot2)
library(sf)
#> Warning: package 'sf' was built under R version 3.4.4
#> Linking to GEOS 3.6.1, GDAL 2.1.3, proj.4 4.9.3
con <- url("https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_PAK_3_sf.rds")
pakistan.gadm <- readRDS(con) %>% st_transform("+proj=laea +lat_0=31 +lon_0=69 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs")
ggplot() + geom_sf(data = st_simplify(pakistan.gadm, 10000), aes(fill = NAME_3)) +
guides(fill = FALSE) |
Hmm – I just took the elapsed time from before and after actually calling ggplot and it was < 4 seconds… library(ggplot2)
library(sf)
#> Linking to GEOS 3.5.0, GDAL 2.1.0, proj.4 4.8.0
con <- url("https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_PAK_3_sf.rds")
pakistan.gadm <- readRDS(con) %>% st_transform("+proj=laea +lat_0=31 +lon_0=69 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs")
x <- Sys.time()
ggplot() + geom_sf(data = st_simplify(pakistan.gadm, 10000), aes(fill = NAME_3)) +
guides(fill = FALSE) (Sys.time() - x)
#> Time difference of 3.330275 secs |
It takes about 90 seconds to render this on my system. library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.1.3, proj.4 4.9.3
con <- url("https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_PAK_3_sf.rds")
pakistan.gadm <- readRDS(con) %>%
st_transform("+proj=laea +lat_0=31 +lon_0=69 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs")
pakistan.simple <- st_simplify(pakistan.gadm, 1e4)
library(ggplot2)
library(microbenchmark)
p1 <- ggplot() +
geom_sf(data = pakistan.simple, aes(fill = NAME_3)) +
guides(fill = FALSE)
p2 <- ggplot() +
geom_sf(data = pakistan.simple)
microbenchmark(print(p1), print(p2), times = 1)
Created on 2018-05-24 by the reprex package (v0.2.0). |
While your issue may be valid, may I suggest you don’t word your next issue in ways that seem to insult the developers |
I don't know of a good way to figure out the total number of points, but I think there are a lot |
@edzer is there a simple way to total number of vertices in a multipolygon geometry? |
I tried @clauswilke 's code and it took ~ 2s on my Win 10 laptop (core i5, 16GB RAM). So problem might be OS dependent?
|
I guess we can blame the OS X Quartz device. Unfortunately it's used by R Studio and hence what people experience in interactive work. Rendering to pdf and opening in Preview takes ~2s total on the same machine. library(sf)
#> Linking to GEOS 3.6.1, GDAL 2.1.3, proj.4 4.9.3
con <- url("https://biogeo.ucdavis.edu/data/gadm3.6/Rsf/gadm36_PAK_3_sf.rds")
pakistan.gadm <- readRDS(con) %>%
st_transform("+proj=laea +lat_0=31 +lon_0=69 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs")
pakistan.simple <- st_simplify(pakistan.gadm, 1e4)
library(ggplot2)
library(microbenchmark)
p1 <- ggplot() +
geom_sf(data = pakistan.simple, aes(fill = NAME_3)) +
guides(fill = FALSE)
microbenchmark(ggsave("test1.pdf", p1), ggsave("test1.png", p1), times = 1)
#> Saving 7 x 5 in image
#> Saving 7 x 5 in image
#> Unit: seconds
#> expr min lq mean median
#> ggsave("test1.pdf", p1) 1.357542 1.357542 1.357542 1.357542
#> ggsave("test1.png", p1) 119.305828 119.305828 119.305828 119.305828
#> uq max neval
#> 1.357542 1.357542 1
#> 119.305828 119.305828 1 Created on 2018-05-25 by the reprex package (v0.2.0). |
The upside is there's nothing fundamentally wrong with ggplot's approach to rendering maps. |
That's good to know. I wonder what's up with Quartz?
THK
…On Fri, May 25, 2018 at 6:56 PM, Claus Wilke ***@***.***> wrote:
The upside is there's nothing fundamentally wrong with ggplot's approach
to rendering maps.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#2655 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAkLyXhdyjqU1kK6qPiSvvh8GD5FqyCKks5t2JoigaJpZM4UMtZF>
.
|
These are great insights! @hadley to compute # vertices, the simplest I can think of is sum(rapply(st_geometry(pakistan.gadm), nrow)) |
The general strategy used by many GIS is to not plot features smaller than
a single pixel and simplify on-the-fly to screen resolution when necessary
to avoid repeatedly painting the same pixel. I don't know if there is an
easy way to do this with ggplot et al. but it would certainly make things
fast for large coverages. Perhaps the QGIS codebase has some examples?
…On Sun, May 27, 2018 at 3:41 AM, Edzer Pebesma ***@***.***> wrote:
These are great insights!
@hadley <https://github.com/hadley> to compute # vertices, the simplest I
can think of is
sum(rapply(st_geometry(pakistan.gadm), nrow))
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#2655 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAkLyZFhjIO9v8yS_eKEJ01B1ei17XtEks5t2ma_gaJpZM4UMtZF>
.
|
This example has 127,680 points, so it's not surprising that it's slow to render. I think the solutions are outside the scope of ggplot2 — either someone needs to improve the performance of the quartz device, or sf needs to perform that optimisation suggested by @thk686 |
@hadley I agree it’s out of scope for ggplot2. Do you know how to best report a bug for the quartz device? I think the performance is seriously off, if I can render to pdf and then pdf to screen in ~2s total but rendering to png via quartz takes ~100s. |
Probably best to email the maintainer directly (I'm pretty sure it's Simon Urbanek) |
Just FYI, this was also discussed in the rstudio community forum |
I have contacted the R Core team, which is listed as official maintainer of the quartz device. |
XQuartz is a mess, put this in your onload to default to cairo instead: if(!identical(getOption("bitmapType"), "cairo") && isTRUE(capabilities()[["cairo"]])){
options(bitmapType = "cairo")
} |
Thanks!
…On Tue, May 29, 2018 at 11:37 AM, Jeroen Ooms ***@***.***> wrote:
XQuartz is a mess, put this in your onload to default to cairo instead:
if(!identical(getOption("bitmapType"), "cairo") && isTRUE(capabilities()[["cairo"]])){
options(bitmapType = "cairo")
}
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#2655 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAkLySub9Mg_zmmYIUuXJxnUS4aO9dIxks5t3XkugaJpZM4UMtZF>
.
|
Unfortunately this doesn't fix the interactive use in R Studio, though, as far as I can tell. |
@clauswilke I think it does? Rstudio uses the default png device. |
This is the behavior I see in RStudio:
|
This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/ |
Literally getting questions like "why should I use this software" from students when it takes 5 minutes to draw a map. Here's a specific example from a lesson. While it is always a bit slow, some plots take forever. Could it be because of the 'fill' argument?
The text was updated successfully, but these errors were encountered: