[R-sig-Geo] Editing Shapefiles

Roger Bivand Roger.Bivand at nhh.no
Mon Dec 12 11:41:38 CET 2005


On Mon, 12 Dec 2005, Malcolm Campbell wrote:

>   
> 
> I am using the packages shapefiles, and maptools in R to visualize data
> from shapefiles, dbf files, and census data.
> 
> My question is this, is it possible once a shapefile is read into R, to
> edit the contents of the shapefile?

Yes, it is possible, using the new classes in the sp package on which 
maptools now depends. It should be possible from the object read by 
read.shapefiles() in the shapefiles package, but that route is untried.

> 
> Let me give a practical example.
> 
> I am using a shapefile of electoral wards in England and I would like to
> be able to remove some of the wards from the shapefile in order to have
> a smaller shapefile of wards, for example, London wards only. After
> reading susbstantial amounts of documentation and related articles and
> numerous web searches I have so far been unable to find an answer.
> 

Once the data from the shapefile (assuming polygons, but could be points) 
are in a SpatialPolygonsDataFrame, you just subset using the "[" operator. 
The documentation is in sp rather than maptools, although the example in 
?readShapePoly is pretty clear - it subsets the North Carolina counties to 
choose just those with less than 2 SIDS cases 1974-78. The readShapePoly() 
function will use the shapefile IDs (usually numbers from "0" upwards) so 
the sort order of the polygons and data may not be what you expect, but 
there are ways to control that too if need be.

Some of the operations may be time-consuming if you are handling many 
polygons, but we've tried tens of thousands without seeing major problems, 
just sometimes things take the time they have to.

If you also need to merge wards, that can be done in the off-CRAN package 
spgpc (on sourceforge, see the CRAN "Spatial" Task View). The sp classes 
are described in a note in the most recent R-News.

Hope this helps,

Roger

>  
> 
> Responses would be greatly appreciated.
> 
>  
> 
> Malcolm Campbell
> 
> Economic Research Institute of Northern Ireland
> 
> 2nd Floor, Floral Buildings
> 
> 2-14 East Bridge Street
> 
> BELFAST
> 
> BT1 3NQ
> 
>  
> 
> Tel: 028 9072 7370
> 
>  
> 
> M.Campbell at erini.ac.uk
> 
>  
> 
> 

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no




More information about the R-sig-Geo mailing list