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? 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. 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 -------------- next part -------------- An HTML attachment was scrubbed... URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20051212/c15c018e/attachment.html> -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: image/gif Size: 530 bytes Desc: image001.gif URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20051212/c15c018e/attachment.gif>
Editing Shapefiles
3 messages · Malcolm Campbell, Roger Bivand, Susumu Tanimura
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
Dear Malcolm and R-sig-Geo members Map object from shapefiles with read.shape() can be merged or clipped. Here are examples written in Japanese but the code is universal. For clipping http://www.okada.jp.org/RWiki/index.php?ShapeFile%A5%E9%A5%A4%A5%D6%A5%E9%A5%EA For Union and Dissolve http://www.okada.jp.org/RWiki/index.php?cmd=read&page=Rmap%A4%F2%BB%C8%A4%C3%A4%BF%C3%CF%BF%DE%C9%BD%BC%A8&word=Rmap#content_1_35 In addition, You can use shapefiles from R to GoogleEarth. http://www.okada.jp.org/RWiki/index.php?GoogleEarth%A4%C8R