将sf多边形转换为sp

huangapple go评论128阅读模式
英文:

converting sf multipolygon to sp

问题

我有一个包含多边形的sf对象,我需要将其转换为sp对象。似乎使用sf转换函数无法正常工作。

我尝试过以下方法:

  1. library(absmapsdata)
  2. library(sp)
  3. library(sf)
  4. vic_sa4 <- sa42021
  5. class(vic_sa4)
  6. vic_sa4_sp <- as_Spatial(vic_sa4)
  7. tmp <- as(vic_sa4, 'Spatial')
  8. vic_sa4_sp <- sf:::as_Spatial(st_geometry(vic_sa4))
  9. as(vic_sa4, "Spatial")

但是都给我返回了这个错误 - 但几何图形确实存在!如何解决这个问题?

  1. 在选择函数 'addAttrToGeom' 的方法时评估参数 'x' 时出错:sp类不支持空几何图形
英文:

I have sf object with multipolygons which i need to convert to sp object. It does not seem to work with sf conversion functions.

I tried

  1. library(absmapsdata)
  2. library(sp)
  3. library(sf)
  4. vic_sa4&lt;-sa42021
  5. class(vic_sa4)
  6. vic_sa4_sp &lt;- as_Spatial(vic_sa4)
  7. tmp &lt;-as(vic_sa4, &#39;Spatial&#39;)
  8. vic_sa4_sp &lt;- sf:::as_Spatial(st_geometry(vic_sa4))
  9. as(vic_sa4, &quot;Spatial&quot;)

it all gives me this error - but the geometry is indeed there! how to fix this?

  1. error in evaluating the argument &#39;x&#39; in selecting a method for function &#39;addAttrToGeom&#39;: empty geometries are not supported by sp classes

答案1

得分: 2

需要删除具有空几何形状的项目。您可以使用 st_is_empty 进行测试。这里有一些由过多的负缓冲导致的空几何形状示例:

  1. p = st_point(c(1,1))
  2. pv = st_sfc(p,p,p,p,p)
  3. bv = st_buffer(pv, c(1,1,0,-1,2))
  4. d = st_sf(bv)

测试:

  1. > st_is_empty(d)
  2. [1] FALSE FALSE TRUE TRUE FALSE

删除:

  1. dnonempty = d[!st_is_empty(d),]

转换:

  1. > library(sp) ; library(raster) # raster的漂亮sp打印方法
  2. > as(dnonempty, "Spatial")
  3. class : SpatialPolygonsDataFrame
  4. features : 3
  5. extent : -1, 3, -1, 3 (xmin, xmax, ymin, ymax)
  6. crs : NA
  7. variables : 0
英文:

You need to get rid of the items with empty geometry. You can test with st_is_empty. Here's a thing with some empty geometries caused by excessive negative buffering into nothingness:

  1. p = st_point(c(1,1))
  2. pv = st_sfc(p,p,p,p,p)
  3. bv = st_buffer(pv, c(1,1,0,-1,2))
  4. d = st_sf(bv)
  5. &gt; d
  6. Simple feature collection with 5 features and 0 fields (with 2 geometries empty)
  7. Geometry type: POLYGON
  8. Dimension: XY
  9. Bounding box: xmin: -1 ymin: -1 xmax: 3 ymax: 3
  10. CRS: NA
  11. bv
  12. 1 POLYGON ((2 1, 1.99863 0.94...
  13. 2 POLYGON ((2 1, 1.99863 0.94...
  14. 3 POLYGON EMPTY
  15. 4 POLYGON EMPTY
  16. 5 POLYGON ((3 1, 2.997259 0.8...

Test:

  1. &gt; st_is_empty(d)
  2. [1] FALSE FALSE TRUE TRUE FALSE

Remove:

  1. dnonempty = d[!st_is_empty(d),]

Convert:

  1. &gt; library(sp) ; library(raster) # raster&#39;s nice sp print method
  2. &gt; as(d, &quot;Spatial&quot;)
  3. Error in h(simpleError(msg, call)) :
  4. error in evaluating the argument &#39;x&#39; in selecting a method for function &#39;addAttrToGeom&#39;: empty geometries are not supported by sp classes: conversion failed
  5. &gt; as(dnonempty, &quot;Spatial&quot;)
  6. class : SpatialPolygonsDataFrame
  7. features : 3
  8. extent : -1, 3, -1, 3 (xmin, xmax, ymin, ymax)
  9. crs : NA
  10. variables : 0

huangapple
  • 本文由 发表于 2023年6月19日 13:42:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/76503887.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定