You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

st_intersects报错:st_crs(x) == st_crs(y) is not TRUE问题咨询

Troubleshooting st_crs(x) == st_crs(y) is not TRUE Error with st_intersects

I’ve run into this exact head-scratcher before, so let’s break down the possible causes and fixes step by step—this error almost always traces back to subtle CRS mismatches you might miss at first glance, or small oversights in how you’re setting/transforming coordinates.

1. First, Verify the Exact CRS Details (Don’t Just Trust EPSG Codes)

Sometimes two objects share the same EPSG code, but their underlying proj4 strings have tiny differences (like older vs. newer PROJ library definitions) that make st_crs() return FALSE when comparing them. To get the full picture:

# Print full CRS info for both objects
cat("CRS of x:\n")
print(st_crs(x))
cat("\nCRS of y:\n")
print(st_crs(y))

# Compare the raw proj4 strings and EPSG codes directly
st_crs(x)$proj4string == st_crs(y)$proj4string
st_crs(x)$epsg == st_crs(y)$epsg

If the EPSG codes match but the proj4 strings don’t, this is likely a PROJ version mismatch. Fix it by explicitly transforming one object to the CRS of the other:

# Force y to use x's exact CRS
y <- st_transform(y, st_crs(x))

# Re-check the match
st_crs(x) == st_crs(y)

2. Make Sure Both Objects Are Valid SF/SFC Objects

It’s easy to accidentally strip sf attributes (e.g., using dplyr::select() without keeping the geometry column, or converting to a plain data frame). If either x or y isn’t an sf/sfc object, st_crs() might return NULL, causing the comparison to fail.

Check their classes first:

class(x)
class(y)

If one is a plain data frame, reattach the geometry column to restore its sf status:

# Replace "geometry" with your actual geometry column name
y <- st_sf(y, sf_column_name = "geometry")

3. Don’t Confuse st_set_crs() with st_transform()

A super common mistake is mixing up these two functions:

  • st_set_crs() assigns a CRS to an object that doesn’t have one (it doesn’t change coordinate values).
  • st_transform() actually converts coordinate values to match a new CRS.

If an object has an incorrect CRS assigned, first set it correctly, then transform:

# Example: If y's CRS was misassigned to EPSG:4326 but it's actually EPSG:3857
y <- st_set_crs(y, 3857) %>% st_transform(st_crs(x))

4. Check for Mixed Coordinate Types (Geographic vs. Projected)

Even if EPSG codes look similar, one object might be in a geographic CRS (lat/long, like EPSG:4326) and the other in a projected CRS (meters/feet, like EPSG:3857). These are fundamentally different, so st_crs() will never consider them equal.

Use these functions to confirm:

st_is_longlat(x)
st_is_longlat(y)

If they differ, transform one to match the other’s coordinate type.

5. Update Your sf Package

Older versions of the sf package had bugs in how CRS objects were compared. Updating might resolve the issue outright:

# Update from CRAN
update.packages("sf")

# Or install the latest development version if needed
# devtools::install_github("r-spatial/sf")

Final Check

After trying these steps, re-run st_crs(x) == st_crs(y) to confirm they match, then test st_intersects() again. If you’re still stuck, sharing the full output of st_crs(x) and st_crs(y) will help narrow things down further.

内容的提问来源于stack exchange,提问作者Dobrowski

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:11:42