简体   繁体   English

通过连接点(lon-lat)在R中绘制的Google地图上查找位置的区域

[英]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: 步骤如下:

  1. Read in the points to a table with read_table2() read_table2()将点读入表
  2. Select only the coordinate columns and convert to a matrix with as.matrix() 仅选择坐标列并使用as.matrix()转换为矩阵
  3. Wrap inside a list with list() . list()包装在列表中。 This is necessary to convert the coordinates into a geometry object. 必须将坐标转换为几何对象。
  4. Use st_polygon to turn the points into a polygon 使用st_polygon将点变成多边形
  5. Use 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_sfcst_set_crs使程序包了解多边形坐标在纬度/经度系统中(4326是与极其通用的WGS84坐标数据标准相对应的代码)
  6. Use 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.

相关问题 将纬度/经度点映射到R中的形状文件 - Map Lat/Lon Points to a Shape File in R 使用R和Google Map API获取两点之间的距离(lat,lon) - Getting driving distance between two points (lat, lon) using R and Google Map API 使用R在OpenStreetMap上绘制纬度/经度点 - Plot lat/lon points on OpenStreetMap Using R 从具有多边形/区域和点(纬度,经度)的shapefile中,找出每个点属于哪个多边形/区域? 在R中 - From a shapefile with polygons/areas, and points (lat,lon), figure out which polygon/area each point belongs to? In R R: ggmap 如何在生成的地图上添加经度/纬度点? - R: ggmap How do I add the lon/lat points on my generated map? 如何在ggplot中的空间地图上叠加纬度/经度点? - How to overlay lat/lon points on a spatial map in ggplot? 如何在choropleth映射上绘制出现点(经/纬度)? - How do I plot occurrence points (lon/lat) on a choropleth map? 使用R从澳大利亚的lat / lon点网站提取高程 - Extracting elevation from website for lat/lon points in Australia, using R 在R中使用shapefile过滤纬度点 - Filter lat-lon points using shapefile in R 在R中的世界地图上绘制的点上添加标签 - Adding labels to points plotted on world map in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM