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)
How to do it...

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

writeSpatialShape(sfdata,"xxpoly")

How it works...

We used the readShapeSpatial() function of the maptools package to read in a shapefile. This function takes a shapefile name as an argument and reads the data into a SpatialPolygonsDataFrame object. The first argument in the example is the path to the sids.shp example shapefile, which is provided as part of the maptools package installation. The second argument proj4string specifies the projection type as longlat so that the spatial coordinates are interpreted correctly as longitudes and latitudes.

We saved the object returned by readShapeSpatial() as sfdata (of data class SpatialPolygonsDataFrame), which we then passed to the plot() function to create a map from the shapefile data.

Once we've read the data into the appropriate format, we can perform any transformations on the data. To save the transformed dataset back into a shapefile, we use the writeSpatialShape() function, which takes the data object as the first argument and the name of the output shapefile (without any file type extension) as the second argument.

There's more

There is another package called shapefiles that can be used to read and write shapefiles. To use it, we must first install and load it:

install.packages("shapefiles")
library(shapefiles)

To read a shapefile using this package, we can use the read.shapefile() function:

sf<-system.file("shapes/sids.shp", package="maptools")[1]
sf<-substr(sf,1,nchar(sf)-4)
sfdata <- read.shapefile(sf)

We first saved the path of the sids.shp example file in a variable called sf. We had to trim the path string to remove the extension .shp because the read.shapefile() function takes just the name of the shapefile as its argument. The shapefile data is saved in a list called sfdata.

To write out a shapefile using this package, we need to use the write.shapefile() function:

write.shapefile(sfdata, "newsf")

The write.shapefile() takes two key arguments: the first is the data object (sfdata in the example) and the second is the name of the new shapefile without any file extension.

There's more
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.156.236