Giter VIP home page Giter VIP logo

Comments (8)

jhagenauer avatar jhagenauer commented on May 22, 2024 1

Thanks for \4... this seems still to improve performance significantly.

from geocompr.

Nowosad avatar Nowosad commented on May 22, 2024

raster tips: set options(rasterMaxMemory = nbytes) to change limit for redirect to file (by default not set, hard coded to 100Mb)

https://twitter.com/mdsumner/status/868655402224762880
https://gist.github.com/mdsumner/7cb21ce030ca5e9acae7d14e0d929162

from geocompr.

Robinlovelace avatar Robinlovelace commented on May 22, 2024

@mdsumner thanks for providing this list. Are they still your top tips or would you make any alterations to them based on recent developments? Are any not futureproof?

from geocompr.

mdsumner avatar mdsumner commented on May 22, 2024

4 and 5 are bugs, so they could be fixed - other than that I think they're pretty good :)

I'll add "use spex::polygonize(raster) for fastest conversion to a polygon-per-pixel sf layer /14"

for 2 I'd add use tabularaster::cellnumbers instead of juggling lists of cells

raster does now have some sf support (for extract, by conversion to Spatial)

also watch rhijmans/geospat, r-spatial/stars, and ecohealthalliance/fasterize

from geocompr.

Robinlovelace avatar Robinlovelace commented on May 22, 2024

Great - I think we've got most of those covered now:

One question: is the distinction between spatial and geometric operations clear? And have we missed anything? Thanks @mdsumner for all these tips, hope we've done them justice!

from geocompr.

mdsumner avatar mdsumner commented on May 22, 2024

LGTM! The book is awesome btw, it's great to see this all laid out in detail.

5/ is now fixed, it's in the queue on CRAN

There's no need to as(x, "Spatial") for extract now for sf, since Nov 2017 (support for sf is patchy but more is coming)

One new "tip" is that extract can be massively sped up by using fasterize, but it's a bit abstract - and presumably raster will apply this improvement some time soon. I also didn't realize extract had that df argument (which is embarassing).

library(raster)
library(sf)

#install.packages("spDataLarge", repos = "https://nowosad.github.io/drat/",
#                 type = "source")
library(spDataLarge)
srtm = raster(system.file("raster/srtm.tif", package = "spDataLarge"))
zion = read_sf(system.file("vector/zion.gpkg", package = "spDataLarge"))
zion = st_transform(zion, projection(srtm))

fast_extract <- function(x, y) {
  y[["ID"]] <- seq_len(nrow(y))
  rast_poly <- fasterize::fasterize(y, x, field = "ID")
  vals <- values(rast_poly)
  out <- raster::extract(x, seq_len(ncell(x))[!is.na(vals)], df = TRUE)
  out[["ID"]] <- vals[!is.na(vals)]
  out
}
library(rbenchmark)
benchmark(ex_values = raster::extract(x = srtm, y = zion, df = TRUE),
fex_values = fast_extract(srtm, zion), 
replications = 2)
#test replications elapsed relative user.self sys.self user.child sys.child
#1  ex_values            2   9.773   12.402     9.776        0          0         0
#2 fex_values            2   0.788    1.000     0.788        0          0         0


ex_values = raster::extract(x = srtm, y = zion, df = TRUE)
fex_values = fast_extract(srtm, zion)
identical(ex_values, fex_values)
#[1] TRUE

from geocompr.

Nowosad avatar Nowosad commented on May 22, 2024

Hey @mdsumner! Thanks for you kind words and sharing your knowledge.
I like the idea for fast_extract(), but it is probably too abstract for the book. It is also (I assume) more limited that raster::extract(), e.g. extracting by points or lines are not supported.

Yep, I am waiting for more consistent sf support in raster. Right now, crop expects sp object, while mask can also work on sf ones...

from geocompr.

mdsumner avatar mdsumner commented on May 22, 2024

fwiw this suggests it shouldn't matter any more: rspatial/raster#5

image

from geocompr.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.