st_intersects errors for st_crs(x) == st_crs(y) is not TRUE

Dobrowski picture Dobrowski · Mar 6, 2018 · Viewed 7.4k times · Source
library(tidyverse) 
library(tigris) 
library(sf)
santacruz <- tracts("CA", "Santa Cruz")
coords_sf <- locations %>% st_as_sf(coords = c("Longitude", "Latitude"), crs=4269) 

This should have the same CRS, but when I try

st_intersects(coords_sf, santacruz)

I get

Error: st_crs(x) == st_crs(y) is not TRUE

I then tried

st_set_crs(santacruz, 4269)
st_set_crs(coords_sf, 4269)
st_transform(santacruz, 4269)
st_transform(coords_sf, 4269)

and it doesn't work. I also tried

st_transform(santacruz, crs = "+proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs")

st_transform(coords_sf, crs = "+proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs")

No matter what I try with setting CRS and transforming it when I try

st_intersects(coords_sf, santacruz)

I get

Error: st_crs(x) == st_crs(y) is not TRUE

At this point I can't tell if it is something wrong with setting CRS or transforming or the st_intersects function. Thanks,

Answer

Edzer Pebesma picture Edzer Pebesma · Mar 7, 2018
st_set_crs(santacruz, 4269)

sets the CRS of the returned object, but does not replace santacruz. You need to save it:

santacruz <- st_set_crs(santacruz, 4269)

or alternatively do

st_crs(santacruz) <- 4269 

to replace the CRS.