Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

gis

tigris

r-sf

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,

like image 832
Dobrowski Avatar asked Oct 30 '25 11:10

Dobrowski


2 Answers

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.

like image 68
Edzer Pebesma Avatar answered Nov 01 '25 00:11

Edzer Pebesma


I don't have your location data, but if I try with sf's nc dataset, this works for me:

library(tidyverse) 
library(tigris) 
library(sf)
santacruz <- tracts("CA", "Santa Cruz")
santacruz <- st_as_sf(santacruz) %>% st_set_crs(4269)
nc <- st_read(system.file('shape/nc.shp', package = 'sf')) %>%
  st_transform(4269)
st_intersects(nc, santacruz)

#> Sparse geometry binary predicate list of length 100, where the predicate was `intersects'
#> first 10 elements:
#>  1: (empty)
#>  2: (empty)
 #> 3: (empty)
 #> 4: (empty)
 #> 5: (empty)
 #> 6: (empty)
 #> 7: (empty)
 #> 8: (empty)
 #> 9: (empty)
 #> 10: (empty)

Note this does st_as_sf before st_set_crs on the santacruz object.

like image 32
sebdalgarno Avatar answered Nov 01 '25 01:11

sebdalgarno