3

Let's suppose that we've got a leaflet map with some markers and another layer, a raster image let's say. Is there a tool, or a way to create one, that allows one to select with an area, a subset of those markers? And then, how could one calculate a summary of the selected group's corresponding variables? I am looking for a selection tool simmilar to the measuring tool that leaflet already supports, and then a way to calculate the summary and output it to a panel in the map. So far I have looked at the Crosstalk package in R, but I have not seen a way to use the selected values to calculate an aggregate statistic. Also, googling didn't clarify much the situation for me, and there doesn't seem to be a clear answer on the git hub threads I've read.

3

I think what you are after can be achieved with a combination of packages sf and mapedit. Make sure to check out the sf package vignettes in case you are not yet familiar with the package. It really is tremendously helpful for all sorts of spatial data (manipulation) tasks. mapedit is a young package dedicated to interactively manipulate spatial data in R. At the time of this writing, to achieve what you are after, you will need to install the development version from github (see commented out first line below). mapedit has a function called selectFeatures which has two modes - click and draw. mode = draw will allow the user to select features on the map by drawing polygons/rectangles/lines/points (click enables selection via clicking on features as the names suggests).

Here is a "reproducible" example:

# 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 the base map with a raster layer
m = mapview(poppendorf[[5]])

# 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, map = m)

# 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, map = m, mode = "draw")

# check the selection (selected will be diplayed in blue)
mapview(dframe_sf, map = m, 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, map = m, mode = "draw", op = st_disjoint)

# check the selection (selected will be diplayed in blue)
mapview(dframe_sf, map = m, col.regions = "red") + diff_selected

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.