Session Ready
Exercise

Extracting geometric information from your vector layers

There are several functions in sf that allow you to access geometric information like area from your vector features. For example, the functions st_area() and st_length() return the area and length of your features, respectively.

Note that the result of functions like st_area() and st_length() will not be a traditional vector. Instead the result has a class of units which means the vector result is accompanied by metadata describing the object's units. As a result, code like this won't quite work:

# This will not work
result <- st_area(parks)
result > 30000

Instead you need to either remove the units with unclass():

# This will work
val <- 30000
unclass(result) > val

or you need to convert val's class to units, for example:

# This will work
units(val) <- units(result)
result > val
Instructions
100 XP

sf and dplyr are loaded in your workspace.

  • Read in the parks shapefile (the file is "parks.shp").
  • Compute the areas of the parks.
  • Create a histogram of the areas using hist() to quickly visualize the data spread.
  • Filter the parks object with filter() and limit to parks with unclass(areas) > 30000 (areas greater than 30,000 square meters).
  • Plot the geometry of the result with plot() and st_geometry().