Making your own vector data

  • Some data that is spatial doesn’t come prepackaged as a spatial object
  • One common instance of this is spatial location data collected using a GPS
  • The sf package let’s us load data with spatial coordinates, like latitude and longitude, as spatial data
library(sf)
  • This allows us to combine it with other spatial data for analyses

  • For example, what if our plot data for Harvard Forest as originally provided as a table with latitude and longitude columns instead of a shape file?
  • We have a version of the plots data that is stored like this in harv_plots.csv

  • To read in data like this as a spatial object we use the read_sf function
  • The first argument is still the name of the file we are going to read
harv_plots <- read_sf("data/harv/harv_plots.csv")
  • But now we also need to tell it which columns the spatial data is located in
  • We do this using the options argument, which is a vector containing two strings
  • "X_POSSIBLE_NAMES=colnameforx" and "Y_POSSIBLE_NAMES=colnamefory"
  • In our case those column names are longitude and latitude
harv_plots <- read_sf("data/harv/harv_plots.csv",
                      options = c("X_POSSIBLE_NAMES=longitude", "Y_POSSIBLE_NAMES=latitude"))
  • NO spaces in options arguments

  • The data is assumed to be in latitude and longitude
  • If the data is stored in a different CRS that can be specified using the crs argument
  • Since our data is lat/long data use 4326
harv_plots <- read_sf("data/harv/harv_plots.csv",
                      options = c("X_POSSIBLE_NAMES=longitude", "Y_POSSIBLE_NAMES=latitude"),
                      crs = 4326)
  • If the data is stored in UTM coordinates then enter the appropriate code for that zone

  • If we look at harv_plots we can see that it looks like all of our other vector data
  • We can plot, reproject, and extract values from rasters using this data just like we can from shape files
harv_plots_utm <- st_transform(harv_plots, st_crs(harv_dtm))
harv_plots_utm
  • We can even save it as a shape file if we want, which we’ll see how to do in the lesson on save spatial data

Do Tasks 1 and 2 of Cropping NEON Data.