Working with ESRI shapefiles

In this recipe, we will learn how to read and write geographic data in the form of shapefiles (.shp), using Geographical Information Systems (GIS) software created by ESRI and some other similar software.

Getting ready

We are going to use the maptools package for this recipe. So, let's install and load it first:

install.packages("maptools")
library(maptools)

How to do it...

We are going to read an example shapefile provided with the maptools package and plot it:

sfdata <- readShapeSpatial(system.file("shapes/sids.shp", 
package="maptools")[1], proj4string=CRS("+proj=longlat"))

plot(sfdata, col="orange", border="white", axes=TRUE)

To write out the data as another shapefile, we can do:

writeSpatialShape(sfdata,"xxpoly")

How it ...

Get R: Data Analysis and Visualization now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.