Session Ready
Exercise

Measuring distance between features

Of course, measuring distance between feature sets is a component of spatial analysis 101 -- a core skill for any analyst. There are several functions in base R as well as in the packages rgeos and geosphere to compute distances, but the st_distance() function from sf provides a useful feature-to-feature distance matrix as output and can be used for most distance calculation needs.

In this exercise you'll measure the distance from the Empire State Building to all the parks and identify the closest one.

Instructions
100 XP
  • Read in the parks shapefile ("parks.shp").
  • Test whether the CRS of parks matches the CRS of the preloaded object empire_state with st_crs().
  • Project/transform the parks object to match the empire_state object. You'll need st_transform() and st_crs().
  • Use st_distance() to compute the distance between the Empire State Building (empire_state) and the parks.
  • Use head() to take a quick look at the result.
  • Identify the index of the smallest distance using which.min() and assign it to nearest.
  • Use nearest to pull out the row from parks of the nearest park (see the signname variable, it should be Greeley Square Park).