【问题标题】:Geographical CRS given to non-conformant data in R为 R 中的不合格数据提供地理 CRS
【发布时间】:2014-02-21 20:58:28
【问题描述】:

很抱歉为此打扰您,但我可能要花 5 个小时才能解决这个问题,但我无法解决。

我有一个包含大约 37,000 个条目的数据集。它们中的每一个都有自己的 Lon 和 Lat 坐标值。 检查整体值,它们的范围分别如下:纬度(-54.4871,70.66344)和经度(-177.375,178.4419)。这是绝对合理的。

我使用 ArcGIS 创建了一个包含这 37000 个点的 shapefile:一切正常。

然后我需要使用 R 处理这些数据,我用于代码的命令是(maptools 包):

cells <- readShapeSpatial('RES',IDvar="id_obj", 
                          proj4string=CRS("+proj=longlat +datum=WGS84"))

但是 R 给出了一个错误:

validityMethod(as(object, superClass)) 中的错误:地理 CRS 给予不合格数据:2.76663393422e+145

(我不知道这个数字来自哪里,它不是我数据集的一部分......)

阅读此博客上的其他帖子似乎原因应该是 lon 或 lat 的数据无效,但正如我上面提到的,我的数据集不是这种情况。

我尝试创建不同的shapefile,第一个没有投影,使用了几个投影(WGS84 Mercator,web mercator...),但错误总是一样...

感谢您的帮助。

【问题讨论】:

标签: r maptools


【解决方案1】:

底线是您的 shapefile 似乎已损坏。

points shapefile 有两个主要部分,一个包含点坐标的coords 部分,以及一个包含“属性”数据(关于点的信息,如您的情况下的区域和国家/地区)的数据部分。您的 shapefile 在数据部分也有 Lon 和 Lat,但它们不匹配

library(rgdal)
setwd("<directory with shapefile...>")
map <- readOGR(dsn=".", layer="test")

range(map@data$Lat)
# [1] -54.48708  70.66344

range(map@coords[,2])
# [1]  -5.448708e+01  2.766634e+145

重投影涉及转换 coords 部分中的信息,这就是它失败的原因。

这是一种解决方法,但破解 SpatialPointsDataFrame 不是一个好主意:

map@coords <- as.matrix(map@data[c("Lon","Lat")])
map@bbox   <- rbind(range(map@coords[,1]),range(map@coords[,2]))
wgs.84 <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
proj4string(map) <- CRS(wgs.84)

library(ggplot2)
gg <- data.frame(map@coords)
ggplot(gg) + 
  geom_point(aes(x=Lon,y=Lat), size=1, alpha=0.5, colour="blue") + 
  coord_fixed()

mercator <- "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs"
map.mercator <- spTransform(map,CRS=CRS(mercator))
gg <- data.frame(map.mercator@coords)
ggplot(gg) + 
  geom_point(aes(x=Lon,y=Lat), size=1, alpha=0.5, colour="green") + 
  coord_fixed()

我建议您重新创建 shapefile 并重试。

【讨论】:

  • 非常感谢。我真的很感激。
猜你喜欢
  • 2015-10-05
  • 2021-09-06
  • 1970-01-01
  • 2010-10-05
  • 2021-08-07
  • 1970-01-01
  • 1970-01-01
  • 2020-09-11
  • 2021-12-11
相关资源
最近更新 更多