Simple way to subset SpatialPolygonsDataFrame (i.e. delete polygons) by attribute in R

后端 未结 5 596
星月不相逢
星月不相逢 2020-12-04 11:54

I would like simply delete some polygons from a SpatialPolygonsDataFrame object based on corresponding attribute values in the @data data frame so that I can plot a simplifi

5条回答
  •  醉梦人生
    2020-12-04 12:45

    I used the above technique to make a map of just Australia:

    australia.map < - world.map[world.map$NAME == "Australia",]
    plot(australia.map)
    

    The comma after "Australia" is important, as it turns out.

    One flaw with this method is that it appears to retain all of the attribute columns and rows for all of the other countries, and just populates them with zeros. I found that if I wrote out a .shp file, then read it back in using readOGR (rgdal package), it automatically removes the null geographic data. Then I could write another shape file with only the data I want.

    writeOGR(australia.map,".","australia",driver="ESRI Shapefile")
    australia.map < - readOGR(".","australia")
    writeOGR(australia.map,".","australia_small",driver="ESRI Shapefile")
    

    On my system, at least, it's the "read" function that removes the null data, so I have to write the file after reading it back once (and if I try to re-use the filename, I get an error). I'm sure there's a simpler way, but this seems to work good enough for my purposes anyway.

提交回复
热议问题