无法更改R中SpatialPolygonsDataFrame的坐标

时间:2013-10-02 08:33:28

标签: r spatial

这是我的计划:

library(sp)
library(RColorBrewer)

#get spatial data for Spain on region level
con <- url("http://gadm.org/data/rda/ESP_adm1.RData")
print(load(con))
close(con)
# plot Spain with colors
col = c("blue","blue","blue","blue","blue","blue","blue","blue","blue",
        "blue","blue","blue","blue","blue","blue","blue","red","red")
spplot(gadm, "NAME_1", col.regions=col, main="Regiones de España",
       colorkey=F, lwd=.4, col="black")

我跑步时得到的是我正在寻找的东西,但我想改变西南部的岛屿(Islas Canarias)更接近西班牙国家。我不知道如何改变岛屿的坐标。我只知道他们在这里:

gadm[14,]

我尝试在此处进行更改:

gadm[14,]@polygons[[1]]@Polygons[[1]]@coords

但我收到了消息:no method for coercing this S4 class to a vector

1 个答案:

答案 0 :(得分:0)

还可以尝试使用sf包 - CRAN链接here

GADM数据网址现在似乎已弃用,但可以从http://gadm.org/手动下载包含西班牙SpatialPolygonsDataFrame的RDS文件。

library(sf)
spain <- readRDS(file = "ESP_adm1.rds") # downloaded from http://gadm.org/

# convert from sp to sf object
spain_sf <- st_as_sf(spain)

# Change the coordinates of Canary Islands - the 14th geometry of spain_sf,
# itself with 20 polygons, check str(st_geometry(spain_sf)[[14]])
# Will shift them closer to mainland Spain.
# The added values are degrees because the CRS is unprojected
# (e.g. add 10 to longitudes and 6 to latitudes)
for (i in 1:length(st_geometry(spain_sf)[[14]])) {
  st_geometry(spain_sf)[[14]][[i]][[1]][, 1] <- st_geometry(spain_sf)[[14]][[i]][[1]][, 1] + 10
  st_geometry(spain_sf)[[14]][[i]][[1]][, 2] <- st_geometry(spain_sf)[[14]][[i]][[1]][, 2] + 6
}

# plot to check
par(omi=c(0,0,0,2)) # make some space to the right for the legend
plot(spain_sf["NAME_1"], 
     axes = TRUE, 
     graticule = st_crs(spain_sf))
# dev.off() # resets par()

enter image description here