1. Coordinate reference systems
Let's take a closer look at the coordinate reference systems of our objects so far.
2. proj4string()
The function proj4string returns the CRS information from spatial objects. Here's the countries_spdf CRS from back in Chapter 2. The returned value is just a string, but a string that conforms to the description of a CRS used by Proj4, a C library that provides projection transformations.
Here, we see the coordinates of country polygons are in longitude latitude, or in other words unprojected, and use the WGS84 datum and WGS84 ellipsoid.
Compare this to the CRS for our water areas around New York. These coordinates are also unprojected, but use the NAD83 datum and GRS80 ellipsoid.
What may come as a surprise to you is that there isn't just one definition of latitude and longitude!
To define a longitude latitude coordinate system you need to specify an ellipsoid, an approximation to the not quite spherical earth, and a datum, a position of the ellipsoid relative to the earth.
Different choices lead to different coordinate systems that may be slightly more convenient or accurate for particular areas.
The WGS84 ellipsoid and datum are very commonly used for global datasets, whereas the NAD83 and GRS80 combination is very commonly used for datasets that cover the United States of America.
3. proj4string()
If you take a look at the tracts object, it has the same coordinate system as the water areas. But the neighborhoods data is different. These coordinates are projected, and use the Lambert Conformal Conic projection, or LCC for short. Most of this string is devoted to parameters that define this specific projection.
4. Setting CRS
If you create a spatial object from scratch, here we are creating a very simple SpatialPoints object with just one point in it, or import data that doesn't have projection information associated with it, you'll find the coordinate reference system is undefined.
It can be defined by assigning a string to the proj4string of the object. Let's assign this the WGS84 standard.
It's important to realize that this doesn't change the coordinates at all, you can see this by comparing the extent of the object before and after assigning the CRS.
If you need to move from one projection to another, reassigning the proj4string won't work,
5. Transforming CRS
you actually have to transform the coordinates. This is handled by the spTransform function in rgdal. spTransform takes a spatial object as the first argument and a new CRS as the second.
You can pass in a complete string, here the string corresponding to the neighborhoods object, or perhaps more usually, rather than writing it out, refer directly to the proj4string of an existing object.
6. Transforming CRS
Now notice the coordinates have actually been changed, these coordinates are now in the projected coordinate space.
7. Let's practice!
You'll explore the problem of using different coordinates systems in the next exercise then practice using spTransform to get the neighborhoods into the same coordinate system as the tracts.