[英]Finding area of a location on a google map plotted in R by connecting points (lon-lat)
I am trying to calculate area of a marked location with points and lines using ggmap and ggplot2 libraries. 我正在尝试使用ggmap和ggplot2库计算带有点和线的标记位置的面积。 I am not finding any example of how to calculate areas by connecting points of longitudes and latitudes in R. 我找不到任何有关如何通过连接R中的经度和纬度点来计算面积的示例。
My code for the points and lines of the location for which I am working to find area is below: 我要查找的区域的点和线的代码如下:
library(ggmap)
library(ggplot2)
coordinates <- read.csv("U://30-Power & Water//25 Renewables//R plots//plant location plot//lat_lon.csv", header=T)
coordinates <- data.frame(coordinates)
map <- get_map(location = c(mean(coordinates[1:13,3]), mean(coordinates[1:13,2])), zoom = 13, maptype = "satellite", source = "google")
Sweihan <- ggmap(map)+
geom_point(data = coordinates, aes(x = coordinates[,3], y = coordinates[,2]))+
geom_path(data = coordinates, aes(x = coordinates[,3], y = coordinates[,2]))
+ geom_polygon(data = coordinates, aes(x = coordinates[1:13,3], y = coordinates[1:13,2]))
Sweihan
My data looks like this : 我的数据如下所示:
Point Latitute..N. Longitude..
1 P1 24.53450 55.41547
2 P2 24.52929 55.41913
3 P3 24.52929 55.43241
4 P4 24.54342 55.46566
5 P5 24.55113 55.46241
6 P6 24.55545 55.47364
7 P7 24.56041 55.47109
8 P8 24.55841 55.46529
9 P9 24.55867 55.46521
10 P10 24.54863 55.43838
11 P11 24.54712 55.43917
12 P12 24.54085 55.42715
13 P13 24.54043 55.42712
14 P1 24.53450 55.41547
please help me finding approach for calculating area that I can use within my code so that when I plot points of lat and lon on the map, I get the exact area that these points cover. 请帮助我找到可以在代码中使用的面积计算方法,以便在地图上绘制纬度和经度点时,可以得到这些点所覆盖的确切面积。
Any type of your help will be appreciated! 任何类型的帮助将不胜感激!
Here is a more generic approach to find the area of a polygon described by a set of coordinates. 这是一种更通用的方法,用于查找由一组坐标描述的多边形区域。 This uses the active simplefeatures standard for spatial data provided by the package sf
. 这将主动simplefeatures标准用于由包sf
提供的空间数据。 The steps go as follows: 步骤如下:
read_table2()
用read_table2()
将点读入表 as.matrix()
仅选择坐标列并使用as.matrix()
转换为矩阵 list()
. 用list()
包装在列表中。 This is necessary to convert the coordinates into a geometry object. 必须将坐标转换为几何对象。 st_polygon
to turn the points into a polygon 使用st_polygon
将点变成多边形 st_sfc
and st_set_crs
to give the package an understanding that the polygon coordinates are in a latitude/longitude system (4326 is a code that corresponds to the extremely common WGS84 standard for coordinate data) 使用st_sfc
和st_set_crs
使程序包了解多边形坐标在纬度/经度系统中(4326是与极其通用的WGS84坐标数据标准相对应的代码) st_area
to calculate the area of the polygon. 使用st_area
计算多边形的面积。 The code for this is a straightforward pipe: 此代码是一个简单的管道:
library(sf)
tbl <- readr::read_table2(
"Point Latitude Longitude
P1 24.53450 55.41547
P2 24.52929 55.41913
P3 24.52929 55.43241
P4 24.54342 55.46566
P5 24.55113 55.46241
P6 24.55545 55.47364
P7 24.56041 55.47109
P8 24.55841 55.46529
P9 24.55867 55.46521
P10 24.54863 55.43838
P11 24.54712 55.43917
P12 24.54085 55.42715
P13 24.54043 55.42712
P1 24.53450 55.41547"
)
tbl[, c(3,2)] %>%
as.matrix() %>%
list() %>%
st_polygon() %>%
st_sfc() %>%
st_set_crs(4326) %>%
st_area()
7919415 m^2
For a matrix of lon/lat coordinates 对于lon / lat坐标矩阵
p <- rbind(c(-180,-20), c(-140,55), c(10, 0), c(-140,-60), c(-180,-20))
library(geosphere)
areaPolygon(p)
# with your data "d"
# areaPolygon(as.matrix(d[,3:2]))
Or for a SpatialPolygonsDataFrame 或用于SpatialPolygonsDataFrame
library(raster)
p <- shapefile(system.file("external/lux.shp", package="raster"))
a <- area(p)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.