Get startedGet started for free

Introducing sp objects

1. Introducing sp objects

So far, the spatial data you've seen has been in data frames. Data frames are nice because you already know how to manipulate and visualize them, but data frames aren't ideal for spatial data.

2. Data frames aren't great for storing spatial data

Data frames are fine for storing coordinates, but there is no natural way to store information about the coordinate reference system. This may not seem like much of a problem when you are only working with one coordinate system and you know which one. But as soon as you have data in more than one coordinate system, or you are sharing data with others, you need to have a system for storing and manipulating the coordinate reference system. We saw in our polygon example, of sales summarized to wards, that the ward level data was repeated as many times as there were points needed to describe the polygon. This is a bit inefficient, really we should only need to store the polygon level data once per polygon. And finally, we saw to store polygons, which may have many pieces, and may have polygons inside polygons, we had to store a lot of extra information about the order to draw points, and which points belong to which polygons. We had to force something that is naturally hierarchical into a rectangular format. For these reasons it makes sense to have special objects for storing spatial data.

3. The sp package

In this chapter we'll focus on the objects that the sp package defines. The package provides special classes for storing spatial objects and methods for manipulating these objects. While sp does provide classes for raster data, we'll see a better set of objects for raster data in the next chapter. Here we'll focus on point, line and polygon data. There's always a bit of overhead to master a new type of object, but the benefit is that the sp classes have become a standard in R for storing spatial data. Many packages that work with spatial data will expect the data in an sp object. Putting in the time to understand them now will pay off whenever you run into one of these packages.

4. Let's practice!

Rather than tell you about the objects sp defines right now, we're going to give you some to explore on your own.