Skip to content

Commit

Permalink
add fun addAreaPolygons
Browse files Browse the repository at this point in the history
  • Loading branch information
Damonsoul committed Nov 4, 2024
1 parent c556d9c commit 48f62c8
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ LazyData: true
Imports:
geojsonsf,
geosphere,
grDevices,
htmltools,
htmlwidgets,
leaflet,
Expand Down
2 changes: 2 additions & 0 deletions NAMESPACE
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)
67 changes: 67 additions & 0 deletions R/addAreaPolygons.R
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)
}
1 change: 1 addition & 0 deletions R/leafletZH-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"_PACKAGE"

## usethis namespace: start
#' @importFrom grDevices chull
#' @importFrom Rcpp sourceCpp
#' @useDynLib leafletZH, .registration = TRUE
## usethis namespace: end
Expand Down
30 changes: 30 additions & 0 deletions man/addAreaPolygons.Rd

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

0 comments on commit 48f62c8

Please sign in to comment.