Merging data from different CRS/projections
Every spatial object has a coordinate reference system (CRS) associated with it. Generally, this is set when the data are imported and will be read directly from the spatial files. This is how the neighborhoods
and nyc_tracts
obtained their coordinate system information.
Both the sp
and raster
packages have a proj4string()
function that returns the CRS of the object it's called on.
Trying to work with spatial data using different CRSs is a bit like trying to work with a dataset in miles and another in kilometers. They are measuring the same thing, but the numbers aren't directly comparable.
Let's take a look at our two polygon objects.
Cet exercice fait partie du cours
Visualizing Geospatial Data in R
Instructions
- Call
proj4string()
onneighborhoods
, then again onnyc_tracts
. Verify the two strings are different. - Take a look at the
head()
of thecoordinates()
ofneighborhoods
and repeat fornyc_tracts
. Can you see the problem?nyc_tracts
has x coordinates around -70, butneighborhoods
is around 1,000,000! - Plot
neighborhoods
, then plotnyc_tracts
withcol = "red"
andadd = TRUE
to add them on top.
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
library(sp)
# proj4string() on nyc_tracts and neighborhoods
# coordinates() on nyc_tracts and neighborhoods
# plot() neighborhoods and nyc_tracts