【问题标题】:How to add location points onto a shapefile imported into R from QGIS using ggplot2如何使用ggplot2将位置点添加到从QGIS导入R的shapefile上
【发布时间】:2021-07-05 15:22:28
【问题描述】:

我正在开展一个涉及离岸位置 GPS 坐标的项目。我正在寻找测量每个点与海岸的距离。我已经在 QGIS 中创建了相关海岸线的 shapefile,并且我已经使用 st_read() 函数(在本例中名为“biminishore”)成功地将其导入到 R 中。 使用以下代码,我可以在 ggplot2 中绘制我的 shapefile:

bplot = ggplot() + 
  geom_sf(data = biminishore, size = 0.1, color = "black", fill = "green1") + 
  ggtitle("Bimini, The Bahamas")  +
  coord_sf() +
  theme_classic() 
plot(bplot)

现在,我想在导入的 shapefile 上添加位置坐标(以 .csv 格式导入到 R 中,Lat 和 Lon 具有单独的列)作为图层。谁能建议如何以一种允许我计算每个点与最近海岸线点之间的距离的方式进行此操作? 我目前的尝试给出了错误:Error in st_transform.sfc(st_geometry(x), crs, ...) : cannot transform sfc object with missing crs

我认为这意味着我的坐标系不兼容,但尚未找到解决方法。到目前为止,我已经尝试使用SpatialPoints() 组合我的点列。我也尝试过使用多种形式的st_set_crs()st_transform(),但我还没有运气。任何帮助是极大的赞赏!谢谢!

【问题讨论】:

    标签: r ggplot2 qgis sf


    【解决方案1】:

    以 csv 格式读取您的积分文件,然后将其转换为 sf 对象:

    library(tidyverse)
    library(sf)
    
    points <- read_csv('path_to_points.csv')
    
    #make it an sf object, change Long and Lat to the correct column name
    points_sf <- st_as_sf(points, coords = c("Long", "Lat"))
    
    # set crs of points_sf to same as biminishore object
    points_sf <- st_set_crs(points_sf, st_crs(biminishore))
    
    

    然后你应该能够通过添加将它们绘制在一起:

    + geom_sf(data = points_sf)
    

    到您的 ggplot2 通话。

    使用sf::st_nearest_feature(points_sf, biminishore) 可以找到两者之间最近的特征。

    关于最近特征和距离的好帖子:https://gis.stackexchange.com/questions/349955/getting-a-new-column-with-distance-to-the-nearest-feature-in-r

    【讨论】:

    • 您好,非常感谢@mrhellmann。不幸的是,当我开始设置 crs 时,我遇到了这个错误:Error in st_set_crs(points_sf, crs = st_crs(biminishore)) : unused argument (crs = st_crs(biminishore))
    • 试试 st_set_crs(points_sf, value = st_crs(biminishore)) ,我可能使用了错误的参数名称。
    • 没关系,我最终使用 st_crs(points_sf) = st_crs(biminishore) 让它工作了,谢谢你的帮助!
    猜你喜欢
    • 2022-01-02
    • 1970-01-01
    • 2018-07-24
    • 2014-10-10
    • 2022-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-08
    相关资源
    最近更新 更多