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.
Diese Übung ist Teil des Kurses
Visualizing Geospatial Data in R
Anleitung zur Übung
- Call
proj4string()onneighborhoods, then again onnyc_tracts. Verify the two strings are different. - Take a look at the
head()of thecoordinates()ofneighborhoodsand repeat fornyc_tracts. Can you see the problem?nyc_tractshas x coordinates around -70, butneighborhoodsis around 1,000,000! - Plot
neighborhoods, then plotnyc_tractswithcol = "red"andadd = TRUEto add them on top.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
library(sp)
# proj4string() on nyc_tracts and neighborhoods
# coordinates() on nyc_tracts and neighborhoods
# plot() neighborhoods and nyc_tracts