Last active
July 4, 2019 03:48
-
-
Save timelyportfolio/bf0ec45f0642a1a0f0ae3bed2485201e to your computer and use it in GitHub Desktop.
R sf simple features with quakes data, st_make_grid, dplyr, mapview zcol
This file contains hidden or 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
library(sf) | |
library(dplyr) | |
library(mapview) | |
library(mapedit) | |
qk_mx <- data.matrix(quakes[,2:1]) | |
qk_mp <- st_multipoint(qk_mx) | |
qk_sf <- st_sf(st_cast(st_sfc(qk_mp), "POINT"), quakes, crs=4326) | |
grd <- st_sf(geom=st_make_grid(qk_sf), crs=4326) | |
grd_mut <- grd %>% | |
# add id | |
mutate(id = 1:n()) %>% | |
# calculate | |
# index of quakes contained in grid rect | |
# number of quakes in grid rect | |
# mean mag of quakes in grid rect | |
mutate( | |
qk_contained = lapply(st_contains(st_sf(geom), qk_sf), identity), | |
n_quakes = sapply(qk_contained,length), | |
mean_mag = sapply( | |
qk_contained, | |
function(x) { | |
mean(qk_sf[x,]$mag) | |
} | |
) | |
) #%>% | |
# make n_quakes NA if 0 | |
#mutate( | |
# n_quakes = ifelse(n_quakes==0, NA, n_quakes), | |
# mean_mag = ifelse(is.nan(mean_mag), 0, mean_mag) | |
#) | |
mapview(grd_mut, zcol="n_quakes", na.color="gray", legend=TRUE) | |
# as edzer highlights there is only na.color and not na.alpha | |
mapview(grd_mut, zcol="mean_mag", na.color="white", legend=TRUE) |
very nice @mdsumner!!! I knew raster
would be faster, but did not know how to implement. Thanks!
The mapview addLargeFeatures
mode does not work properly in this case as the png state is cropped at the dateline. A long standing issue with leaflet/mapview. In fact, the very first reported issue on mapview is still open :-( r-spatial/mapview#6
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@timelyportfolio this is great to see the dplyr flow applied to such a complex task with sf! I can see a pretty specific optimization though, if we leverage raster directly. (Whether that makes sense will always depend on the component parts of the workflow - this point-in-cell task is something we do a lot - your approach would already work for arbitrary polygon regions, and mine would not).
Taking things a little closer to the metal, we can directly index between the raster cells and the features. I use spex as well for some convenience with rounded alignment when creating the raster, and a quadmesh method for creating the feature-per-pixel sf object which is a bit faster than other methods I think.
A rep of 5000 gives us Tim's desired 5e6 points without data.table. I just shamelessly repeat the points with a jitter to get a lot of variability.
Notes: