Skip to content

Commit

Permalink
add fun areaCaculator
Browse files Browse the repository at this point in the history
  • Loading branch information
Damonsoul committed Nov 4, 2024
1 parent af12133 commit c556d9c
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ Encoding: UTF-8
LazyData: true
Imports:
geojsonsf,
geosphere,
htmltools,
htmlwidgets,
leaflet,
leaflet.extras,
purrr,
Rcpp,
sf,
stringr
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export(addCityShape)
export(addProvinceShape)
export(addTilesAmap)
export(areaCalculator)
export(convertCoordinates)
import(sf)
importFrom(Rcpp,sourceCpp)
Expand Down
91 changes: 91 additions & 0 deletions R/areaCalculator.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#' Calculate the area of a polygon defined by latitude and longitude points
#'
#' This function takes in latitude and longitude vectors and calculates the area of the polygon
#' defined by those points. It can handle different coordinate systems such as WGS-84, GCJ-02, and BD-09.
#'
#' @param longitude A numeric vector of longitude points.
#' @param latitude A numeric vector of latitude points.
#' @param coordinate A string indicating the coordinate system of the input points, can be "WGS-84",
#' "GCJ-02", or "BD-09". Default is "WGS-84".
#' @param chull A logical value indicating whether to use the convex hull of the points. Default is TRUE.
#'
#' @return A numeric value representing the area of the polygon in square meters.
#' @export
#'
#' @examples
#' area <- areaCalculator(
#' latitude = c(31.0, 31.1, 31.2),
#' longitude = c(121.0, 121.1, 121.2), coordinate = "WGS-84"
#' )
areaCalculator <- function(longitude,
latitude,
coordinate = "WGS-84",
chull = TRUE) {
# Check if latitude and longitude lengths match
if (length(latitude) != length(longitude)) {
stop("Latitude and longitude vectors must have the same length.")
}
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$latitude <- purrr::map2(
.x = latitude,
.y = longitude,
\(x, y)convertCoordinates(
latitude = x,
longitude = y,
from = "GCJ-02",
to = "WGS-84"
)[1]
)
data$longitude <- purrr::map2(
.x = latitude,
.y = longitude,
\(x, y)convertCoordinates(
latitude = x,
longitude = y,
from = "GCJ-02",
to = "WGS-84"
)[2]
)
} else if (coordinate == "BD-09") {
data$latitude <- purrr::map2(
.x = latitude,
.y = longitude,
\(x, y)convertCoordinates(
latitude = x,
longitude = y,
from = "BD-09",
to = "WGS-84"
)[1]
)
data$longitude <- purrr::map2(
.x = latitude,
.y = longitude,
\(x, y)convertCoordinates(
latitude = x,
longitude = y,
from = "BD-09",
to = "WGS-84"
)[2]
)
}


# Filter the data to include only the convex hull points if chull is TRUE
if (chull) {
data <- data[chull(data$longitude, data$latitude), ]
}


# Calculate the area of the polygon
area <- geosphere::areaPolygon(data)

return(area)
}
29 changes: 29 additions & 0 deletions man/areaCalculator.Rd

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

0 comments on commit c556d9c

Please sign in to comment.