Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v 0.2.0.3 map_wkt #34

Merged
merged 8 commits into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: NPSutils
Type: Package
Title: Collection of Functions to read/write information from the NPS Data Store
Version: 0.2.0.0
Version: 0.2.0.3
Authors@R: c(
person(given = "Robert", family = "Baker", email = "robert_baker@nps.gov",
role = c("aut", "cre"),
Expand Down Expand Up @@ -29,8 +29,9 @@ Imports:
readr,
magrittr,
crayon,
stringr
RoxygenNote: 7.2.1
stringr,
leaflet
RoxygenNote: 7.2.3
Suggests:
knitr,
rmarkdown
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ export(loadDataPackages)
export(load_data_package)
export(load_domains)
export(load_metadata)
export(map_wkt)
export(validate_data_package)
importFrom(magrittr,"%>%")
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# NPSutils 0.2.0.3
* added `map_wkt()` function to map points, polygons, or both from Well Known Text coordinates (WKT). WKT is used in place to GPS coordinates when sensitive species locations have been "fuzzed". In this case, providing a polygon rather than the an exact (albeit fuzzed) is preferable as it is clear that the location is not exact. WKT is an efficient way to store geographic shapes such as polygons in flat files such as .csv.

# NPSutils 0.2.0.2

* deprecated `get_data_package()` in favor of `getDataPackages()`. This is just a name change and does not change any functionality.
Expand Down
68 changes: 68 additions & 0 deletions R/map_wkt.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#' Map WKT geometry (points and polygons)
#'
#' @description `map_wkt()` takes a well-known text (WKT) geometry column and maps points and polygons onto a gray leaflet map. All NA geometry is dropped before mapping.
#'
#' @details Define your dataframe, the column that contains WKT, and an option to map specific geometry types.
#'
#' @param df - The name of the data frame that contains WKT geometry.
#' @param wellknowntext - The name of the specific column within the data frame that contains the WKT geometry. This parameter is currently not fully implemented and defaults to the Darwin Core 'footprintsWKT'.
#' @param type - Pick one from "points", "polygons", or "all" to map specific geometry types.
#'
#' @return The function returns a dynamic, zoomable leaflet map with the specific geometry plotted.
#' @importFrom magrittr %>%
#' @export
#'
#' @examples
#' \dontrun{
#' #map species observations
#' map_wkt(my_NPS_species_obs)
#'
#' #map species observations - points only
#' map_wkt(my_NPS_species_obs, wellknowntext = "footprintWKT", type = "points")
#' }
map_wkt <- function(df, wellknowntext = "footprintWKT", type = "all") {
#filter to just wellknowntext column:
wkt_grepl <- paste0('\\b', wellknowntext, '\\b')
df <- df[grepl(wkt_grepl, colnames(df))]
#omit NAs - not important for plotting anyway:
df <- na.omit(df)
#convert to geographic object:
df <- sf::st_as_sf(df, wkt = wellknowntext)
#new column in data frame for the geometry type
df$geometry_types <- sf::st_geometry_type(df)
#use the geometry_type column to filter only for POINT or POLYGON
df_pts <- df[df$geometry_types == "POINT",]
df_polys <- df[df$geometry_types == "POLYGON",]

#only map what is requested
if(type == "points") {
map <- leaflet::leaflet(df,
options = leafletOptions(preferCanvas = TRUE)) %>%
#addTiles(group = "OSM (default)"); prevent unwanted map updates:
leaflet::addProviderTiles(providers$Esri.WorldGrayCanvas,
options = providerTileOptions(
updateWhenZooming = FALSE,
updateWhenIdle = TRUE)) %>%
leaflet::addCircles(data = df_pts, color = "blue",) #odd stray ,
} else if(type == "polygons") {
map <- leaflet::leaflet(df,
options = leafletOptions(preferCanvas = TRUE)) %>%
#addTiles(group = "OSM (default)"); prevent unwanted map updates:
leaflet::addProviderTiles(providers$Esri.WorldGrayCanvas,
options = providerTileOptions(
updateWhenZooming = FALSE,
updateWhenIdle = TRUE)) %>%
leaflet::addPolygons(data = df_polys, color = "red",) #odd stray ,
} else if(type == "all") {
map <- leaflet::leaflet(df,
options = leafletOptions(preferCanvas = TRUE)) %>%
#addTiles(group = "OSM (default)") %>%; prevent unwatned map updates:
leaflet::addProviderTiles(providers$Esri.WorldGrayCanvas,
options = providerTileOptions(
updateWhenZooming = FALSE,
updateWhenIdle = TRUE)) %>%
leaflet::addCircles(data = df_pts, color = "blue",) %>%
leaflet::addPolygons(data = df, color = "red",) #odd stray ,
}
return(map)
}
2 changes: 1 addition & 1 deletion docs/404.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/LICENSE-text.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/LICENSE.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/articles/NPSutils.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/articles/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions docs/authors.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion docs/news/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions docs/pkgdown.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pandoc: 2.17.1.1
pandoc: 2.19.2
pkgdown: 2.0.6.9000
pkgdown_sha: 5bb671b1a8bac8e926479868681936f7c9ee41b3
articles:
NPSutils: NPSutils.html
last_built: 2023-02-08T18:49Z

last_built: 2023-02-28T17:14Z
4 changes: 2 additions & 2 deletions docs/reference/getDataPackages.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/get_park_code.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/get_park_taxon_citations.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/get_park_taxon_refs.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/get_park_taxon_url.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/get_ref_info.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/get_unit_code.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/get_unit_code_info.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/get_unit_info.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion docs/reference/index.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions docs/reference/loadDataPackages.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/load_data_package.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/load_domains.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion docs/reference/load_metadata.html

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading