gSimplify not simplifying shapefile in R

删除回忆录丶 提交于 2020-01-02 08:53:48

问题


I'm unable to simplify a shapefile in R

Shapefile from here: https://geoportal.statistics.gov.uk/Docs/Boundaries/Local_authority_district_(GB)_2014_Boundaries_(Generalised_Clipped).zip

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" )) 

Gives an error of:

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)

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

Instead, if you try to only simplify the polygons in the Shapefile:

England@polygons<-gSimplify(England@polygons,tol=0.01, topologyPreserve=TRUE)

It says that:

Error in gSimplify(England@polygons, tol = 0.01, topologyPreserve = TRUE) : 
cannot get a slot ("proj4string") from an object of type "list"

How can I simplify the polygons from a shapefile?


回答1:


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)

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.



来源:https://stackoverflow.com/questions/34827043/gsimplify-not-simplifying-shapefile-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!