sf is for birds


1 Spatial analysis with R

A major powertool in R for spatial analysis and geograpy data processing is the sf package.

Reading in data

The main type of data you will be reading in are shapefiles. Shapefiles are not one file, but are actually a set of files that include the data, the attributes of the coordinates, the projection, the coordinates, and information that tells the software the type of data it is.

Let’s pull in some point tree data from San Francisco and play with it a bit.

Now, let’s look at the data

You can see that there are latitude and longitude columns, but R does not yet know that this is a geographic dataset.

We get an error for missing coordinates, so let’s get rid of the missing coordinates.

We can check that the data set is a geographic dataset by looking at a plot of the data.

Looks like there are some trees in the wrong spot. So, let’s look at which trees intersect with the City of San Francisco shapefile. We will read in the shapefile and then run an intersection.

Buffers Geom_sf

library(tigris)

city_shape <- st_read("https://data.sfgov.org/api/geospatial/pdvd-w2q4?method=export&format=Shapefile")

cities <- metro_divisions()

san_fran <- filter(cities, grepl("San Francisco", NAME))

st_crs(san_fran)

trees_in_san_fran <- st_intersection(sf_trees_geo, san_fran)

Now let’s draw a buffer around each tree that remains inside the boundary of San Francisco proper. To draw a buffer we need to make sure the coordinates are correct. Remember when we talked about coordinate reference systems? Universal tranverse mercator, or UTMs have coordinates in meters. San Francisco is in UTM zone 10.

trees_in_san_fran <- st_transform(trees_in_san_fran, crs = 32610)
trees_in_san_fran <- st_buffer(trees_in_san_fran, dist = 1000)

Finally, let’s see which tree is the farthest distance from a loon in Leech Lake, Minnesota.

leech_lake_center_lat <- 47.162382 
leech_lake_center_long <- -94.428769

coords <- c(leech_lake_center_long, leech_lake_center_lat)

leech_lake_mn <- st_sfc(st_point(coords))

leech_lake_mn <- st_as_sf(leech_lake_mn, crs = 9822)

trees_in_san_fran <- st_transform(trees_in_san_fran, crs = 9822)
trees_in_san_fran_center <- st_centroid(trees_in_san_fran)

distance_to_loons <- st_distance(leech_lake_mn, trees_in_san_fran_center)