Last active
April 27, 2020 13:23
-
-
Save mar-wir/516bd0fd0e3f84d6262ab8b840e9d289 to your computer and use it in GitHub Desktop.
Interactive maps {mapview, mapedit} without shiny
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
#https://gis.stackexchange.com/questions/253483/leaflet-tool-for-multiple-marker-selection-and-computation-of-summary | |
# devtools::install_github("r-spatial/mapedit") | |
library(sf) # for spatial data type representation | |
library(mapview) # for the raster data and quick viewing | |
library(mapedit) # for the interaction (selection of the data) | |
# create some mock data points in the vicinity of the raster layer | |
set.seed(42) # to be reproducible | |
dframe = data.frame(a = 1:50, | |
b = rnorm(50, 2, 1), | |
x = runif(50, 11.15, 11.25), | |
y = runif(50, 49.7, 49.75)) | |
# convert data.frame to sf object as we need | |
# geo-spatial data type for this kind of objective | |
# epsg 4326 is geographic longlat 'projection' | |
dframe_sf = st_as_sf(dframe, coords = c("x", "y"), crs = 4326) | |
# inspect data on base map | |
mapview(dframe_sf) | |
# select features via polygon/rectangle/line/point or the like | |
# by using the draw tools of the draw toolbar on the left | |
# and press "Done" when finished. | |
# multiple selections are also possible. | |
# (line/point selection will not work as points have no dimension!) | |
# mode = "click" will enable selection via clicking on features. | |
selected = selectFeatures(dframe_sf, mode = "draw") | |
# check the selection (selected will be diplayed in blue) | |
mapview(dframe_sf, col.regions = "red") + selected | |
# given that selected is a sf object (and hence a data.frame) | |
# claculating summaries works just as expected with a normal data.frame | |
summary(selected) | |
mean(selected$a) | |
mean(selected$b) | |
sd(selected$b) | |
# we can also set other selection criteria. e.g. invert selection via st_disjoint | |
diff_selected = selectFeatures(dframe_sf, mode = "draw", op = st_disjoint) | |
# check the selection (selected will be diplayed in blue) | |
mapview(dframe_sf, col.regions = "red") + diff_selected |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment