-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
101 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,7 @@ LazyData: true | |
Imports: | ||
geojsonsf, | ||
geosphere, | ||
grDevices, | ||
htmltools, | ||
htmlwidgets, | ||
leaflet, | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,14 @@ | ||
# Generated by roxygen2: do not edit by hand | ||
|
||
export(addAreaPolygons) | ||
export(addCityShape) | ||
export(addProvinceShape) | ||
export(addTilesAmap) | ||
export(areaCalculator) | ||
export(convertCoordinates) | ||
import(sf) | ||
importFrom(Rcpp,sourceCpp) | ||
importFrom(grDevices,chull) | ||
importFrom(htmltools,tags) | ||
importFrom(htmlwidgets,prependContent) | ||
useDynLib(leafletZH, .registration = TRUE) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#' Add Area Polygons to a Map | ||
#' | ||
#' This function adds a polygon area to a given map using the specified latitude and longitude coordinates. | ||
#' | ||
#' @param map The map object to which the polygons will be added. | ||
#' @param longitude A vector of longitudes. | ||
#' @param latitude A vector of latitudes. | ||
#' @param coordinate A string indicating the coordinate system of the input data. Options are "WGS-84", "GCJ-02", "BD-09". | ||
#' | ||
#' @return The updated map object with added polygons. | ||
#' @export | ||
#' | ||
#' @examples | ||
#' library(leaflet) | ||
#' m <- leaflet() %>% addTilesAmap() | ||
#' m <- addAreaPolygons(m, | ||
#' longitude = c(116.404, 116.407, 116.409), | ||
#' latitude = c(39.916, 39.919, 39.917), coordinate = "WGS-84" | ||
#' ) | ||
#' m | ||
addAreaPolygons <- function(map, longitude, latitude, coordinate = "WGS-84") { | ||
# Check if latitude and longitude lengths match | ||
if (length(latitude) != length(longitude)) { | ||
stop("Latitude and longitude vectors must have the same length.") | ||
} | ||
|
||
# Validate the coordinate system | ||
if (!coordinate %in% c("WGS-84", "GCJ-02", "BD-09")) { | ||
stop("coordinate must be one of 'WGS-84', 'GCJ-02', and 'BD-09'") | ||
} | ||
|
||
# Create a data frame with the input coordinates | ||
data <- data.frame(longitude = longitude, latitude = latitude) | ||
|
||
# Convert coordinates to WGS-84 if necessary | ||
if (coordinate == "GCJ-02") { | ||
data <- purrr::map2_dfr(latitude, longitude, function(lat, lon) { | ||
coords <- convertCoordinates(lat, lon, from = "GCJ-02", to = "WGS-84") | ||
data.frame(longitude = coords[2], latitude = coords[1]) | ||
}) | ||
} else if (coordinate == "BD-09") { | ||
data <- purrr::map2_dfr(latitude, longitude, function(lat, lon) { | ||
coords <- convertCoordinates(lat, lon, from = "BD-09", to = "WGS-84") | ||
data.frame(longitude = coords[2], latitude = coords[1]) | ||
}) | ||
} | ||
|
||
# Remove duplicate points and create a convex hull to define the boundary | ||
data <- data[!duplicated(data), ] | ||
if (nrow(data) < 3) { | ||
stop("At least three points are required to form a polygon.") | ||
} | ||
|
||
# Compute the convex hull and get the order of points | ||
hull_index <- grDevices::chull(data$longitude, data$latitude) | ||
hull_data <- data[hull_index, ] | ||
|
||
# Add polygons to the map | ||
map <- leaflet::addPolygons(map, | ||
lng = hull_data$longitude, | ||
lat = hull_data$latitude, | ||
fillOpacity = 0.5, | ||
color = "blue" | ||
) | ||
|
||
return(map) | ||
} |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.