【问题标题】:R sf st_write error: feature not successfully writtenR sf st_write 错误:功能未成功写入
【发布时间】:2022-08-05 05:52:20
【问题描述】:

我正在保存一个如下所示的 shapefile:

Simple feature collection with 337152 features and 35 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: -137.9625 ymin: 5.495833 xmax: -52.61605 ymax: 62.74232
Geodetic CRS:  WGS 84

st_write(sf_dat, \'temp.shp\'))

我收到 50 条警告,上面写着:

Warning messages:
1: In CPL_write_ogr(obj, dsn, layer, driver, as.character(dataset_options),  ... :
  GDAL Message 1: Value 7120014530 of field hybas_id of feature 0 not successfully written. 
Possibly due to too larger number with respect to field width

我无法理解错误。谁能解释为什么会这样? 谢谢

  • 我不是 100% 确定,但对于以 shapefile 格式实现的字段类型而言,您的值似乎太大了(c.f. ESRI: ArcGIS field data types):长整数似乎提供了 32 位分辨率,范围介于 -2,147,483,648至 +2,147,483,647。
  • 除了 falk-env 的输入,我鼓励你检查保存的数据集并找到这一行。通常,当我收到此警告时,该值仍然存在。请参阅解决方案以了解两种可能的解决方法

标签: r sf


【解决方案1】:

如果你想摆脱这个问题,我建议尝试[我没有运行代码,所以谨慎行事]:

  • 以不同的格式保存,例如geojson 使用st_write(sf_dat, 'temp.geojson'))
  • 在保存之前将字段重新格式化为字符串,然后在加载数据集时将其格式化回整数:
sf_dat$hybas_id <- as.character(sf_dat$hybas_id)
st_write(sf_dat, 'temp.shp'))

## then to re-load data (also using the dplyr library):
sf_dat <- st_read('temp.shp') %>% mutate(hybas_id  = as.integer(hybas_id)))

【讨论】:

  • 或者,这在使用 shapefile 时很常见,您还可以考虑降低一些准确性并将值保存为数千,例如newval_1000 &lt;- 7120015 而不是 val &lt;- 7120014530。但这当然取决于您的用例。
  • 好的,非常感谢。我会努力解决这个问题。
  • @falk-env 建议的选项很好,但要注意 - 如果 hybas_id 字段应该是唯一标识符,那么你会丢失它。在这种情况下,保存为字符串可能会更好,或者保存为带有所有小数的浮点数,然后在重新读取时乘以 1000。
  • 是的,当hybas_id 是您的唯一标识符时,请确保将您的值保存为字符。如果我没记错的话,Shapefile 最多允许每个字段存储 254 个字符。
【解决方案2】:

尝试将其保存到地理包 temp.pgkg,将你的背离 shapefile 并且永远不要回头。

【讨论】:

    猜你喜欢
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-07
    • 2021-02-27
    • 1970-01-01
    相关资源
    最近更新 更多