From c556d9c348097156b2f868a3422f2ca29fe3ede2 Mon Sep 17 00:00:00 2001 From: Damonsoul <244085168@qq.com> Date: Mon, 4 Nov 2024 16:44:35 +0800 Subject: [PATCH] add fun areaCaculator --- DESCRIPTION | 2 + NAMESPACE | 1 + R/areaCalculator.R | 91 +++++++++++++++++++++++++++++++++++++++++++ man/areaCalculator.Rd | 29 ++++++++++++++ 4 files changed, 123 insertions(+) create mode 100644 R/areaCalculator.R create mode 100644 man/areaCalculator.Rd diff --git a/DESCRIPTION b/DESCRIPTION index 4e7cc66..595e353 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -15,10 +15,12 @@ Encoding: UTF-8 LazyData: true Imports: geojsonsf, + geosphere, htmltools, htmlwidgets, leaflet, leaflet.extras, + purrr, Rcpp, sf, stringr diff --git a/NAMESPACE b/NAMESPACE index 6b404fa..684e184 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -3,6 +3,7 @@ export(addCityShape) export(addProvinceShape) export(addTilesAmap) +export(areaCalculator) export(convertCoordinates) import(sf) importFrom(Rcpp,sourceCpp) diff --git a/R/areaCalculator.R b/R/areaCalculator.R new file mode 100644 index 0000000..9739892 --- /dev/null +++ b/R/areaCalculator.R @@ -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) +} diff --git a/man/areaCalculator.Rd b/man/areaCalculator.Rd new file mode 100644 index 0000000..725cbd6 --- /dev/null +++ b/man/areaCalculator.Rd @@ -0,0 +1,29 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/areaCalculator.R +\name{areaCalculator} +\alias{areaCalculator} +\title{Calculate the area of a polygon defined by latitude and longitude points} +\usage{ +areaCalculator(longitude, latitude, coordinate = "WGS-84", chull = TRUE) +} +\arguments{ +\item{longitude}{A numeric vector of longitude points.} + +\item{latitude}{A numeric vector of latitude points.} + +\item{coordinate}{A string indicating the coordinate system of the input points, can be "WGS-84", +"GCJ-02", or "BD-09". Default is "WGS-84".} + +\item{chull}{A logical value indicating whether to use the convex hull of the points. Default is TRUE.} +} +\value{ +A numeric value representing the area of the polygon in square meters. +} +\description{ +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. +} +\examples{ +area <- areaCalculator(latitude = c(31.0, 31.1, 31.2), +longitude = c(121.0, 121.1, 121.2), coordinate = "WGS-84") +}