How to subset a shapefile

 ̄綄美尐妖づ 提交于 2019-11-29 15:32:33

subset with names should work. You don't need the shapefiles package if you are reading in with readOGR:

> require(rgdal)
Loading required package: rgdal
Loading required package: sp
rgdal: version: 0.8-14, (SVN revision 496)
Geospatial Data Abstraction Library extensions to R successfully loaded
Loaded GDAL runtime: GDAL 1.9.0, released 2011/12/29
Path to GDAL shared files: /usr/share/gdal/1.9
Loaded PROJ.4 runtime: Rel. 4.8.0, 6 March 2012, [PJ_VERSION: 470]
Path to PROJ.4 shared files: (autodetected)

Get the 169 polygons of Indian states into a spatial object:

> India = readOGR(".","india_state")
OGR data source with driver: ESRI Shapefile 
Source: ".", layer: "india_state"
with 169 features and 3 fields
Feature type: wkbPolygon with 2 dimensions

Now subset by name:

> Gujarat = subset(India, NAME=="Gujarat")
> dim(Gujarat)
[1] 12  3
> dim(India)
[1] 169   3

I can plot these objects, they map nicely:

> plot(Gujarat)

Without your data or knowing what version of R and the packages you have, there is no way of knowing why this fails for you.

Package: rgdal Version: 0.8-14

Package: sp Version: 1.0-14

R version 3.0.2....

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