使用gSimplify简化了带有空间多边形的writeOGR


12

我正在使用gSimplify(rgeos包)来简化shapefile的几何形状。该功能运行良好,但现在我无法在新的shapefile中写入输出。我尝试了一些方法:

writeOGR(simplyshape, file, driver="ESRI Shapefile", layer='test')

我懂了

obj必须是SpatialPointsDataFrame,SpatialLinesDataFrame或SpatialPolygonsDataFrame

与:

writePolyShape(simplyshape, file)

我得到:

错误:is(x,“ SpatialPolygonsDataFrame”)不是TRUE

Answers:


8

将您的对象强制为适当的Spatial*DataFrame-class(点/线/多边形),例如,SpatialPolygons使用as(x, "SpatialPolygonsDataFrame" )

R> l <- readWKT("LINESTRING(0 7,1 6,2 1,3 4,4 1,5 7,6 6,7 4,8 6,9 4)")
R> x1 <- gSimplify(p, tol=10)
R> class(x1)
[1] "SpatialPolygons"
attr(,"package")
[1] "sp"
R> x2 <- as(x, "SpatialPolygonsDataFrame")
R> class(x2)
[1] "SpatialPolygonsDataFrame"
attr(,"package")
[1] "sp"

5

您需要将您的SpatialPolygons课程转换为SpatialPolygonsDataFrame课程。例如:

require(rgdal)
require(rgeos)

# Read shapefile
shp = 'C:/temp/myshp.shp'
myshp = readOGR(shp, layer = basename(strsplit(shp, "\\.")[[1]])[1])

# Read shapefile attributes
df = data.frame(myshp)

# Simplify geometry using rgeos
simplified = gSimplify(myshp, tol = 1000, topologyPreserve=FALSE)

# Create a spatial polygon data frame (includes shp attributes)
spdf = SpatialPolygonsDataFrame(simplified, df)

# Write to shapefile
writeOGR(spdf, layer = 'myshp_simplified', 'C:/temp', driver="ESRI Shapefile")
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.