Get startedGet started for free

More sp classes and methods

1. More sp classes and methods

You just went a long way down through the structure of that SpatialPolygonsDataFrame to find something that looked like coordinates. Let's review the steps.

2. Hierarchy of SpatialPolygonsDataFrame

A SpatialPolygonsDataFrame object, in addition to data and other slots, has a slot called polygons spelled with a lower case p.

3. Hierarchy of SpatialPolygonsDataFrame

Inside this slot is a list.

4. Hierarchy of SpatialPolygonsDataFrame

The list has one element for every spatial entity. In our case, countries_sp_df held polygons for 177 countries, so this list has 177 elements. Each of these elements was another S4 object class defined by sp, a Polygons object.

5. Hierarchy of SpatialPolygonsDataFrame

You looked at the 169th element, but had you looked at any other it would have looked the same.

6. Hierarchy of SpatialPolygonsDataFrame

There is a Polygons slot, this time with an uppercase P, and some other slots. This Polygons object defines a country, in this 169th case, the United States.

7. Hierarchy of SpatialPolygonsDataFrame

Inside the Polygons slot is another list.

8. Hierarchy of SpatialPolygonsDataFrame

Each element in this list is yet another S4 object defined by sp called a Polygon.

9. Hierarchy of SpatialPolygonsDataFrame

Each Polygon has

10. Hierarchy of SpatialPolygonsDataFrame

a coords slot that finally contains some spatial coordinates describing a single polygon. Why do we need a such a complicated hierarchy? For the United States, we can describe a set of points that form the boundary of the contiguous US, that might be a Polygon object. But that isn't enough, the USA includes Alaska and Hawaii, we need another Polygon object for every island. This list of Polygon objects gets put together into a Polygons object describing the USA. We need one of these Polygons objects for every country in our data.

11. Other sp classes

To summarize, a Polygon object describes a single polygon,

12. Other sp classes

many are put together in a Polygons object,

13. Other sp classes

and many of those along with coordinate info make up a SpatialPolygons object.

14. Other sp classes

With the addition of a data frame it becomes a SpatialPolygonsDataFrame.

15. Other sp classes

In a similar way sp also provides classes for line data. A single line is stored in a Line object,

16. Other sp classes

many lines can be combined into a Lines object,

17. Other sp classes

and when combined with coordinate info

18. Other sp classes

and a data frame becomes a SpatialLinesDataFrame object.

19. Other sp classes

A SpatialPointsDataFrame class also exists, but since there is no need for a hierarchy with points, coordinates can go directly into a SpatialPoints object without the need for a Point and Points object. While it's useful to be able to dig into an object to pull out things, in practice you are more likely to want to subset the set of spatial elements, keeping the result as a spatial object. That way you keep all the information about the coordinate system, and still have access to the useful methods, while only keeping the polygons of interest.

20. Subsetting sp classes

The subsetting methods for sp objects are designed to work like subsetting a data frame. You pass two arguments in through square brackets. The first subsets the spatial elements. For example, passing a 1 returns a SpatialPolygonsDatFrame, where the data slot only gives the first row, and the polygons slot now only contains the polygon data for the first country. Just like data frames, you can instead pass in a logical to select elements that correspond to TRUE.

21. Let's practice!

You'll try out these two approaches over the next few exercises.

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.