|
| 1 | +#' Calculate the area of a polygon defined by latitude and longitude points |
| 2 | +#' |
| 3 | +#' This function takes in latitude and longitude vectors and calculates the area of the polygon |
| 4 | +#' defined by those points. It can handle different coordinate systems such as WGS-84, GCJ-02, and BD-09. |
| 5 | +#' |
| 6 | +#' @param longitude A numeric vector of longitude points. |
| 7 | +#' @param latitude A numeric vector of latitude points. |
| 8 | +#' @param coordinate A string indicating the coordinate system of the input points, can be "WGS-84", |
| 9 | +#' "GCJ-02", or "BD-09". Default is "WGS-84". |
| 10 | +#' @param chull A logical value indicating whether to use the convex hull of the points. Default is TRUE. |
| 11 | +#' |
| 12 | +#' @return A numeric value representing the area of the polygon in square meters. |
| 13 | +#' @export |
| 14 | +#' |
| 15 | +#' @examples |
| 16 | +#' area <- areaCalculator( |
| 17 | +#' latitude = c(31.0, 31.1, 31.2), |
| 18 | +#' longitude = c(121.0, 121.1, 121.2), coordinate = "WGS-84" |
| 19 | +#' ) |
| 20 | +areaCalculator <- function(longitude, |
| 21 | + latitude, |
| 22 | + coordinate = "WGS-84", |
| 23 | + chull = TRUE) { |
| 24 | + # Check if latitude and longitude lengths match |
| 25 | + if (length(latitude) != length(longitude)) { |
| 26 | + stop("Latitude and longitude vectors must have the same length.") |
| 27 | + } |
| 28 | + if (!coordinate %in% c("WGS-84", "GCJ-02", "BD-09")) { |
| 29 | + stop("coordinate must be one of WGS-84, GCJ-02, and BD-09") |
| 30 | + } |
| 31 | + |
| 32 | + # Create a data frame with the input coordinates |
| 33 | + data <- data.frame(longitude = longitude, latitude = latitude) |
| 34 | + |
| 35 | + # Convert coordinates to WGS-84 if necessary |
| 36 | + if (coordinate == "GCJ-02") { |
| 37 | + data$latitude <- purrr::map2( |
| 38 | + .x = latitude, |
| 39 | + .y = longitude, |
| 40 | + \(x, y)convertCoordinates( |
| 41 | + latitude = x, |
| 42 | + longitude = y, |
| 43 | + from = "GCJ-02", |
| 44 | + to = "WGS-84" |
| 45 | + )[1] |
| 46 | + ) |
| 47 | + data$longitude <- purrr::map2( |
| 48 | + .x = latitude, |
| 49 | + .y = longitude, |
| 50 | + \(x, y)convertCoordinates( |
| 51 | + latitude = x, |
| 52 | + longitude = y, |
| 53 | + from = "GCJ-02", |
| 54 | + to = "WGS-84" |
| 55 | + )[2] |
| 56 | + ) |
| 57 | + } else if (coordinate == "BD-09") { |
| 58 | + data$latitude <- purrr::map2( |
| 59 | + .x = latitude, |
| 60 | + .y = longitude, |
| 61 | + \(x, y)convertCoordinates( |
| 62 | + latitude = x, |
| 63 | + longitude = y, |
| 64 | + from = "BD-09", |
| 65 | + to = "WGS-84" |
| 66 | + )[1] |
| 67 | + ) |
| 68 | + data$longitude <- purrr::map2( |
| 69 | + .x = latitude, |
| 70 | + .y = longitude, |
| 71 | + \(x, y)convertCoordinates( |
| 72 | + latitude = x, |
| 73 | + longitude = y, |
| 74 | + from = "BD-09", |
| 75 | + to = "WGS-84" |
| 76 | + )[2] |
| 77 | + ) |
| 78 | + } |
| 79 | + |
| 80 | + |
| 81 | + # Filter the data to include only the convex hull points if chull is TRUE |
| 82 | + if (chull) { |
| 83 | + data <- data[chull(data$longitude, data$latitude), ] |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + # Calculate the area of the polygon |
| 88 | + area <- geosphere::areaPolygon(data) |
| 89 | + |
| 90 | + return(area) |
| 91 | +} |
0 commit comments