[A couple of other questions deal with this sort of problem (see Comparing projections of two datasets when SRID not available and How to test if two GDAL datasets are in the same projection?) but not from an R perspective.]

I'm trying to figure out the best way to compare projections in R because I have a list of shapefiles and I want to reproject them to WGS84 if they are not currently in WGS84.

I thought of several ways to do this, but none seem to be bulletproof:

  1. The simplest way I can think of to do this is to use the equality operator to directly compare two proj4 strings but this will fail, for example, if there are spurious spaces in the strings or differences in decimal places.
  2. I had also thought about trying to determine the EPSG code of a proj4 string and comparing it to 4326 but (I think) not all proj4 strings have corresponding EPSG codes.
  3. Finally, I thought about just using spTransform() on all my layers (~10,000 of them) regardless of whether they're in WGS84 already or not but I figure this will eat a lot of unnecessary compute-time.

I've probably missed something, but I don't seem to be able to find any functions in packages like sp or rgdal that would do a comparison for me.

So, what have I missed? Are there best practices for performing projection comparisons in R?


If you have a correctly projected file, Raster library has a simple function to compare CRS.

compareCRS(shpA, shpB)
[1] TRUE

Using it would save you the time of writing your own function to compare the strings. If nothing else this would let you create a vector of which shapes are correctly projected. Reprojecting the shapes however might be faster to do in python (both probably pulling on gdal however. Just a matter of which environment).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.