Geographical CRS given to non-conformant data in R

筅森魡賤 提交于 2019-12-06 04:06:42

The bottom line is that your shapefile seems to be corrupt.

The points shapefile has two main sections, a coords section that has the coordinates of the points, and a data section that has "attribute" data (information about the points, like region and country in your case). Your shapefile has Lon and Lat in the data section as well, but they don't match:

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

Reprojection involves transforming the information in the coords section, which is why it failed.

Here is a workaround, but hacking the SpatialPointsDataFrame is not a good idea:

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()

I'd recommend you re-create the shapefile and try again.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!