首页 文章

通过连接点(lon-lat)查找在R上绘制的谷 Map 上某个位置的区域

提问于
浏览
2

我试图使用ggmap和ggplot2库计算带点和线的标记位置的区域 . 我没有找到任何关于如何通过连接R中的经度和纬度点计算面积的例子 .

我正在寻找区域的位置的点和线的代码如下:

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

我的数据如下:

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

请帮助我找到计算我可以在我的代码中使用的区域的方法,这样当我在 Map 上绘制lat和lon的点时,我得到这些点覆盖的确切区域 .

任何类型的帮助将不胜感激!

2 回答

  • 3

    对于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]))
    

    或者对于SpatialPolygonsDataFrame

    library(raster)
    p <- shapefile(system.file("external/lux.shp", package="raster"))
    a <- area(p)
    
  • 3

    这是一种更通用的方法,用于查找由一组坐标描述的多边形区域 . 这使用包 sf 提供的空间数据的活动simplefeatures标准 . 步骤如下:

    • read_table2() 读取表格中的点数

    • 仅选择坐标列并使用 as.matrix() 转换为矩阵

    • list() 包裹在列表中 . 这是将坐标转换为几何对象所必需的 .

    • 使用 st_polygon 将点转换为多边形

    • 使用 st_sfcst_set_crs 使包理解多边形坐标在纬度/经度系统中(4326是对应于极为常见的WGS84坐标数据标准的代码)

    • 使用 st_area 计算多边形的面积 .

    这个代码是一个简单的管道:

    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
    

相关问题