首页 > 解决方案 > shp的R函数

问题描述

我正在尝试在 R 中读取我的 shapefile,但仍然收到错误消息

我安装了这些库

library(sf)
library(ggplot2)
library(tmap)
library(tmaptools)
library(leaflet)
library(dplyr)

mydata<-st_read("C:/Users/User/Documents/PlotLocations_HARV.shp",stringsAsFactors = FALSE)

我收到此错误消息:

Cannot open data source C:\Users\User\Documents\PlotLocations_HARV.shp
Error in CPL_read_ogr(dsn, layer, as.character(options), quiet, type,  : 
  Open failed.
In addition: Warning message:
In CPL_read_ogr(dsn, layer, as.character(options), quiet, type,  :
  GDAL Error 4: Unable to open C:\Users\User\Documents\PlotLocations_HARV.shx or C:\Users\User\Documents\PlotLocations_HARV.SHX.Try --config SHAPE_RESTORE_SHX true to restore or create it

我也有整个文件
aoi_boundary_HARV <- st_read("C:\Users\rhusein\Documents\NEONDSSiteLayoutFiles\NEON-DS-Site-Layout-Files\HARV\HarClip_UTMZ18.shp")

并且仍然收到错误消息

CPL_read_ogr (dsn, layer, as.character(options), quiet, type, 中的错误:打开失败。 – julia 1 小时前

验证文件 PlotLocations_HARV.shx 是否存在于与 PlotLocations_HARV.shp 相同的文件夹中

是的,两者都在文件夹中

现在我很好,谢谢

标签: rfileshapes

解决方案


“shapefile”由多个具有不同扩展名的文件组成。

  • .shp - 此文件包含特征的几何图形。
  • .shx -- 空间索引
  • .dbf -- 属性值

除非这些文件都在那里,否则该文件将不会打开。ESRI 将其称为形状文件有点误导,但你有它。

您将看到其他几个,例如用于空间参考的.prj和用于元数据的.xml,但它们不是必需的。

你的错误是

Unable to open C:\Users\User\Documents\PlotLocations_HARV.shx

这意味着您由于某种原因无法打开空间索引文件。

所以解决问题,你可以确保*.shx文件和.shp文件在同一个文件夹中,或者使用选项SHAPE_RESTORE_SHX重新生成索引。

您说“现在我很好”,所以我假设您找到了文件并将其移到那里,或者重新生成了索引。


推荐阅读