Chapter 4 Projections

Now that we have our data in the R program, we need to make sure all the data have the same projection. For a refresher on projections, please watch this primer video. To output the coordinate reference system of each dataset, we can use the crs() function and provide the variable name of our imported geospatial dataset.

crs(superfund)
## CRS arguments:
##  +proj=utm +zone=19 +datum=NAD83 +units=m +no_defs
crs(svi)
## CRS arguments:
##  +proj=utm +zone=19 +datum=NAD83 +units=m +no_defs
crs(elevation)
## CRS arguments:
##  +proj=utm +zone=19 +datum=NAD83 +units=m +no_defs

As you can tell, the superfund sites, social vulnerability index, and elevation data have the exact same output. This means the projections are the same. Let's go ahead and change the projections for all these data to a geographic coordinate system. Since leaflet prefers spatial data that are in angular units (degrees), we should make sure to project our data to a geographic coordinate system. Leaflet prefers data in epsg code 4326 or 3857, so let's convert all the data to this projection. Use the function spTransform() to change the projection in vector data types and the function projectRaster() to change the projection in raster data.
When we use these projection functions we first specify the name of the variable that contains our data (superfund, svi, elevation), then we specify the new coordinate reference system (new_crs). Here, I gave our projected datasets new variable names (superfund_projected, svi_projected, and elevation_projected).

new_crs <- '+init=epsg:4326' # the code for our new projection
superfund_projected <- spTransform(superfund, CRS(new_crs))
svi_projected <- spTransform(svi, crs(new_crs))
elevation_projected <- projectRaster(elevation, crs=crs(new_crs))

Great! Now, let's double-check that our spatial data are in the same projection:

crs(superfund_projected)
## CRS arguments: +proj=longlat +datum=WGS84 +no_defs
crs(svi_projected)
## CRS arguments: +proj=longlat +datum=WGS84 +no_defs
crs(elevation_projected)
## CRS arguments: +proj=longlat +datum=WGS84 +no_defs