887
887

Reputation: 619

How to fix Error in over() : identicalCRS(x, y) is not TRUE?

I am working with the following code, originally written by @Josh O'Brien and adapted by @Neil L. The code is intended to use latitude and longitude coordinates of a given location to generate the correct county name for that row.

library(maps)
library(maptools)

latlong2county <- function(pointsDF) { 
        # Prepare SpatialPolygons object with one SpatialPolygon 
        # per county 
        counties <- map('county', fill=TRUE, col="transparent", plot=FALSE) 
        IDs <- sapply(strsplit(counties$names, ":"), function(x) x[1]) 
        counties_sp <- map2SpatialPolygons(counties, IDs=IDs, proj4string=CRS("+proj=longlat 
               +datum=WGS84")) 
        # Convert pointsDF to a SpatialPoints object 
        pointsSP <- SpatialPoints(pointsDF, proj4string=CRS("+proj=longlat+datum=WGS84")) 
        # Use 'over' to get _indices_ of the Polygons object containing each point 
        indices <- over(pointsSP, counties_sp) 
        # Return the county names of the Polygons object containing each point 
        countyNames <- sapply(counties_sp@polygons, function(x) x@ID) 
        countyNames[indices] 
} 

# Test the function using points in Wisconsin and Oregon. 
testPoints <- data.frame(x = c(-90, -120), y = c(44, 44)) 

latlong2county(testPoints) 

When I run that code, I get an error that reads:

 Error in over(pointsSP, counties_sp) : identicalCRS(x, y) is not TRUE 

I cannot for the life of me figure out what the issue is. I have tried solutions for the same error on stack overflow and have not had any luck--I would appreciate any help. Here are the first ten rows for data.

structure(list(lon = c(-95.91241468, -90.11001628, -98.30641348, 
-80.72761498, -94.51613158, -72.84020128, -117.1440706, -73.75580388, 
-88.12470008, -71.13771368), lat = c(36.10732598, 32.44879598, 
29.50962608, 35.00136258, 38.88771948, 41.77643578, 33.14457978, 
41.00829888, 43.05468238, 42.37087558), state = c("Oklahoma", 
"Mississippi", "Texas", "North Carolina", "Missouri", "Connecticut", 
"California", "New York", "Wisconsin", "Massachusetts")), row.names = c(NA, 
10L), class = "data.frame")

Upvotes: 0

Views: 1896

Answers (1)

Gorka
Gorka

Reputation: 2079

As the returned error suggests, the issue is caused because the projections (CRS) of pointsSP and counties_sp are not identical. In your case, there is a small typo when defining the pointsSP spatial object: you need to insert a space between the projection arguments, "+proj=longlat+datum=WGS84" -> "+proj=longlat +datum=WGS84".

A way to ensure that the second object (pointsSP) uses the same projection as the first object (counties_sp) is to use the proj4string function as follows:

pointsSP <- SpatialPoints(pointsDF, proj4string = CRS(proj4string(counties_sp)))

Note:

I was getting a different error to the one detailed by OP. In my case, the code was failing in the following line:

pointsSP <- SpatialPoints(pointsDF, proj4string=CRS("+proj=longlat+datum=WGS84")) 
#> Error in CRS("+proj=longlat+datum=WGS84") : unknown projection id

My guess is that OP is using an older version of the sp package, which does not verify if the provided CRS is valid. Then, the execution fails in the line indices <- over(pointsSP, counties_sp) as consequence of the explained reason.

Upvotes: 2

Related Questions