gSimplify 不简化 R 中的 shapefile [英] gSimplify not simplifying shapefile in R
问题描述
我无法在 R 中简化 shapefile
I'm unable to simplify a shapefile in R
library(tmap)
library(maptools)
library(ggmap)
England <- readOGR(dsn = "...")
#works fine
print(qtm(England, "LAD14CD", borders = NA, fill.title = "A-Level" ))
# simplify the polygons
England<-gSimplify(England,tol=0.01, topologyPreserve=TRUE)
print(qtm(England, "LAD14CD", borders = NA, fill.title = "A-Level" ))
给出错误:
Error in process_fill(data, g$tm_fill, gborders, gt, gf, z = z + which(plot.order == :
Fill argument neither colors nor valid variable name(s)
如果您查看 UK 数据对象,您会发现它已从大空间多边形数据帧更改为大空间多边形并删除了 @data
If you look at the UK dataobject you can see that it has changed from a Large Spatial polygonDataFrame to Large Spatial Polygons and dropped the @data
相反,如果您尝试仅简化 Shapefile 中的多边形:
Instead, if you try to only simplify the polygons in the Shapefile:
England@polygons<-gSimplify(England@polygons,tol=0.01, topologyPreserve=TRUE)
它说:
Error in gSimplify(England@polygons, tol = 0.01, topologyPreserve = TRUE) :
cannot get a slot ("proj4string") from an object of type "list"
如何简化 shapefile 中的多边形?
How can I simplify the polygons from a shapefile?
推荐答案
gSimplify
返回的只是几何图形,而不是属性,所以你必须构造一个新的 SpatialPolygonsDataFrame
代码>使用简化的几何图形和原始的属性数据:
The return from gSimplify
is just the geometry, not the attributes, so you have to construct a new SpatialPolygonsDataFrame
with the simplified geometry and the attribute data from the original:
> England2 <-gSimplify(England,tol=0.01, topologyPreserve=TRUE)
> England3 = SpatialPolygonsDataFrame(England2, data=England@data)
我认为多边形的顺序是相同的,除非任何东西都被简化了.检查 length(England2)
与 England
的行数相同,或匹配 ID 上的行.
I think the polygons are guaranteed to be in the same order, unless anything has been simplified away. Check length(England2)
has the same number of rows as England
, or match up the rows on an ID.
这篇关于gSimplify 不简化 R 中的 shapefile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!