-
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
4 changed files
with
123 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
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
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,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) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.