如何修复 over() 中的错误:identicalCRS(x, y) 不是 TRUE?
How to fix Error in over() : identicalCRS(x, y) is not TRUE?
我正在使用以下代码,最初由@Josh O'Brien 编写并由@Neil L 改编。该代码旨在使用给定位置的纬度和经度坐标为该位置生成正确的县名行。
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)
当我 运行 该代码时,我收到一条错误消息:
Error in over(pointsSP, counties_sp) : identicalCRS(x, y) is not TRUE
我一辈子都弄不明白问题出在哪里。我已经尝试过针对堆栈溢出的相同错误的解决方案,但没有任何运气——我将不胜感激任何帮助。这是数据的前十行。
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")
如返回的错误所示,问题是由于 pointsSP
和 counties_sp
的投影 (CRS) 不相同引起的。在您的情况下,定义 pointsSP
空间对象时有一个小错字:您需要在投影参数之间插入一个 space,"+proj=longlat+datum=WGS84"
-> "+proj=longlat +datum=WGS84"
.
确保第二个对象 (pointsSP) 使用与第一个对象 (counties_sp) 相同的投影的一种方法是使用 proj4string
函数,如下所示:
pointsSP <- SpatialPoints(pointsDF, proj4string = CRS(proj4string(counties_sp)))
注:
我收到的错误与 OP 详述的错误不同。就我而言,代码在以下行中失败:
pointsSP <- SpatialPoints(pointsDF, proj4string=CRS("+proj=longlat+datum=WGS84"))
#> Error in CRS("+proj=longlat+datum=WGS84") : unknown projection id
我的猜测是 OP 使用的是旧版本的 sp
包,它不会验证提供的 CRS 是否有效。然后,由于解释的原因,在行 indices <- over(pointsSP, counties_sp)
中执行失败。
我正在使用以下代码,最初由@Josh O'Brien 编写并由@Neil L 改编。该代码旨在使用给定位置的纬度和经度坐标为该位置生成正确的县名行。
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)
当我 运行 该代码时,我收到一条错误消息:
Error in over(pointsSP, counties_sp) : identicalCRS(x, y) is not TRUE
我一辈子都弄不明白问题出在哪里。我已经尝试过针对堆栈溢出的相同错误的解决方案,但没有任何运气——我将不胜感激任何帮助。这是数据的前十行。
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")
如返回的错误所示,问题是由于 pointsSP
和 counties_sp
的投影 (CRS) 不相同引起的。在您的情况下,定义 pointsSP
空间对象时有一个小错字:您需要在投影参数之间插入一个 space,"+proj=longlat+datum=WGS84"
-> "+proj=longlat +datum=WGS84"
.
确保第二个对象 (pointsSP) 使用与第一个对象 (counties_sp) 相同的投影的一种方法是使用 proj4string
函数,如下所示:
pointsSP <- SpatialPoints(pointsDF, proj4string = CRS(proj4string(counties_sp)))
注:
我收到的错误与 OP 详述的错误不同。就我而言,代码在以下行中失败:
pointsSP <- SpatialPoints(pointsDF, proj4string=CRS("+proj=longlat+datum=WGS84"))
#> Error in CRS("+proj=longlat+datum=WGS84") : unknown projection id
我的猜测是 OP 使用的是旧版本的 sp
包,它不会验证提供的 CRS 是否有效。然后,由于解释的原因,在行 indices <- over(pointsSP, counties_sp)
中执行失败。