Search code examples
rleafletrgdal

How to simplify spatial polygon using rmapshaper?


I am trying to simplify a spatial polygons dataframe with the following code:

library(rmapshaper)
library(rgdal)
countyshapes <- readOGR("https://raw.githubusercontent.com/kjhealy/us-county/master/data/geojson/gz_2010_us_050_00_500k.json")
countyshapes <- ms_simplify(countyshapes, keep = 0.1)

But this returns the following error:

Error in sp::SpatialPolygonsDataFrame(polys, data = input@data) : 
  row.names of data and Polygons IDs do not match

Is there a way I can work around this error to simplify my SP object?


Solution

  • I ran the code and if it worked, preferably update the rmapshaper package:

    packageVersion("rmapshaper") 
    #[1] ‘0.4.4’
    
    countyshapes[1:5,]
    #An object of class "SpatialPolygonsDataFrame"
    #Slot "data":
    #  GEO_ID STATE COUNTY      NAME   LSAD CENSUSAREA
    #1 0500000US01029    01    029  Cleburne County    560.100
    #2 0500000US01031    01    031    Coffee County    678.972
    #3 0500000US01037    01    037     Coosa County    650.926
    #4 0500000US01039    01    039 Covington County   1030.456
    #5 0500000US01041    01    041  Crenshaw County    608.840
    

    Another alternative is that you use the sf::st_transform function