Skip to content

Commit c556d9c

Browse files
committed
add fun areaCaculator
1 parent af12133 commit c556d9c

File tree

4 files changed

+123
-0
lines changed

4 files changed

+123
-0
lines changed

DESCRIPTION

+2
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@ Encoding: UTF-8
1515
LazyData: true
1616
Imports:
1717
geojsonsf,
18+
geosphere,
1819
htmltools,
1920
htmlwidgets,
2021
leaflet,
2122
leaflet.extras,
23+
purrr,
2224
Rcpp,
2325
sf,
2426
stringr

NAMESPACE

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
export(addCityShape)
44
export(addProvinceShape)
55
export(addTilesAmap)
6+
export(areaCalculator)
67
export(convertCoordinates)
78
import(sf)
89
importFrom(Rcpp,sourceCpp)

R/areaCalculator.R

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
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+
}

man/areaCalculator.Rd

+29
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)