From 4dfd8d94edfe5378e4b53c36003b63c04ee5a439 Mon Sep 17 00:00:00 2001 From: tim-salabim Date: Mon, 15 May 2017 23:05:53 +0200 Subject: [PATCH] add selectFeatures to select features from sf --- DESCRIPTION | 2 +- NAMESPACE | 2 + R/select.R | 32 +++++++++++++++ man/edit_map.Rd | 1 - man/selectFeatures.Rd | 91 +++++++++++++++++++++++++++++++++++++++++++ man/select_map.Rd | 1 - 6 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 R/select.R create mode 100644 man/selectFeatures.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 3ceddb9..e2085f1 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -25,4 +25,4 @@ Suggests: mapview Encoding: UTF-8 LazyData: true -RoxygenNote: 5.0.1 +RoxygenNote: 6.0.1 diff --git a/NAMESPACE b/NAMESPACE index 40391f0..c9e7922 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,8 @@ # Generated by roxygen2: do not edit by hand S3method(edit_map,leaflet) +S3method(selectFeatures,sf) S3method(select_map,leaflet) export(edit_map) +export(selectFeatures) export(select_map) diff --git a/R/select.R b/R/select.R new file mode 100644 index 0000000..1cd14f9 --- /dev/null +++ b/R/select.R @@ -0,0 +1,32 @@ +#' Interactively Select Map Features +#' +#' @param x map to use +#' +#' @param ... other arguments +#' +#' @example ./inst/examples/examples_select.R +#' @export +selectFeatures <- function(x, ...) { + UseMethod("selectFeatures") +} + +#' @export +selectFeatures.sf <- function(x, ...) { + x$edit_group = as.character(1:nrow(x)) + + addfun = switch(as.character(sf::st_dimension(sf::st_geometry(x)))[1], + "0" = leaflet::addCircleMarkers, + "1" = leaflet::addPolylines, + "2" = leaflet::addPolygons) + + m = leaflet::leaflet() %>% + leaflet::addTiles() %>% + addfun(data = x, weight = 1, group = ~edit_group) + + ind = select_map(m, ...) + + indx = ind$group[as.logical(ind$selected)] + todrop = "edit_group" + return(x[as.numeric(indx), !names(x) %in% todrop]) +} + diff --git a/man/edit_map.Rd b/man/edit_map.Rd index b24f8c4..9b04cef 100644 --- a/man/edit_map.Rd +++ b/man/edit_map.Rd @@ -49,4 +49,3 @@ edit_map(leaflet() \%>\% addTiles()) edit_map(lf) } } - diff --git a/man/selectFeatures.Rd b/man/selectFeatures.Rd new file mode 100644 index 0000000..91bc1f4 --- /dev/null +++ b/man/selectFeatures.Rd @@ -0,0 +1,91 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/select.R +\name{selectFeatures} +\alias{selectFeatures} +\title{Interactively Select Map Features} +\usage{ +selectFeatures(x, ...) +} +\arguments{ +\item{x}{map to use} + +\item{...}{other arguments} +} +\description{ +Interactively Select Map Features +} +\examples{ +library(mapedit) + +lf <- leaflet() \%>\% + addTiles() + +# draw some polygons that we will select later +drawing <- lf \%>\% + edit_map() + +# ugly way to add our drawings to a leaflet map +local({ + i <- 0 + Reduce( + function(x,y){ + i <<- i+1 + x \%>\% addGeoJSON(y, group = as.character(i)) + }, + drawing$finished, + init = lf + ) +}) \%>\% + select_map() + +\dontrun{ +# use @bhaskarvk USA Albers with leaflet code +# https://bhaskarvk.github.io/leaflet/examples/proj4Leaflet.html +#devtools::install_github("hrbrmstr/albersusa") +library(albersusa) +library(sf) +library(leaflet) +library(mapedit) + +spdf <- usa_composite() \%>\% st_as_sf() +pal <- colorNumeric( + palette = "Blues", + domain = spdf$pop_2014 +) + +bounds <- c(-125, 24 ,-75, 45) + +(lf <- leaflet( + options= + leafletOptions( + worldCopyJump = FALSE, + crs=leafletCRS( + crsClass="L.Proj.CRS", + code='EPSG:2163', + proj4def='+proj=laea +lat_0=45 +lon_0=-100 +x_0=0 +y_0=0 +a=6370997 +b=6370997 +units=m +no_defs', + resolutions = c(65536, 32768, 16384, 8192, 4096, 2048,1024, 512, 256, 128) + ))) \%>\% + fitBounds(bounds[1], bounds[2], bounds[3], bounds[4]) \%>\% + setMaxBounds(bounds[1], bounds[2], bounds[3], bounds[4]) \%>\% + addPolygons( + data=spdf, weight = 1, color = "#000000", + # adding group necessary for identification + group = ~iso_3166_2, + fillColor=~pal(pop_2014), + fillOpacity=0.7, + label=~stringr::str_c(name,' ', format(pop_2014, big.mark=",")), + labelOptions= labelOptions(direction = 'auto')#, + #highlightOptions = highlightOptions( + # color='#00ff00', bringToFront = TRUE, sendToBack = TRUE) + ) +) + + +# test out select_map with albers example +select_map( + lf, + style_false = list(weight = 1), + style_true = list(weight = 4) +) +} +} diff --git a/man/select_map.Rd b/man/select_map.Rd index c37fbb7..31e2d5d 100644 --- a/man/select_map.Rd +++ b/man/select_map.Rd @@ -89,4 +89,3 @@ select_map( ) } } -