3
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,

thelatemail
  • 91,185
  • 12
  • 128
  • 188
Dobrowski
  • 48
  • 1
  • 1
  • 6
  • 1
    What is `locations`? – thelatemail Mar 06 '18 at 22:47
  • 1
    Can you provide a reproducible example with sample points from `locations`? One thing is that `tigris` doesn't provide `sf` class objects by default unless you set a global option, otherwise it will be a SpatialPolygonsDataFrame. Try `santacruz = tigris::tracts("CA", "Santa Cruz", class = "sf")`? In particular, what is the result of `st_crs(santacruz)`? – Calum You Mar 06 '18 at 22:53
  • I forgot to list it in my write up, but I had `options(tigris_class = "sf")` However, when I add `santacruz <- st_as_sf(santacruz) %>% st_set_crs(4269)` It works. Before when I was getting errors I was seeing `> st_crs(santacruz) Coordinate Reference System: EPSG: 4269 proj4string: "+proj=longlat +datum=NAD83 +no_defs" > st_crs(coords_sf) Coordinate Reference System: EPSG: 4269 proj4string: "+proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs"` – Dobrowski Mar 07 '18 at 17:50

2 Answers2

7
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.

Edzer Pebesma
  • 3,814
  • 16
  • 26
1

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.

sebdalgarno
  • 2,929
  • 12
  • 28