如何打开解压缩的地图文件

时间:2014-09-18 22:13:07

标签: r gdal ogr rgdal

我正在尝试读取压缩的地图文件以用作轮廓上的地图叠加层,但我似乎无法解压缩该文件。以下是我的命令;

library(rgdal)
setwd("C:/R_Progs")
unzip("ZMB_adm.zip")
zmb <- readOGR(dsn = "C:/R_Progs", layer = "ZMB_adm")
zmb2 <- fortify(zmb)

我想在我的情节中使用zmb2作为基本地图。 zip文件和脚本位于同一目录中。 以下是我得到的:

Error in ogrInfo(dsn = dsn, layer = layer, encoding = encoding, use_iconv = use_iconv) : 

无法打开图层 但我确实看到unzip命令有效,因为我看到4个带dbf,prj,shp和shx扩展名的新文件

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:3)

尝试:

unzip("ZMB_adm.zip", exdir="ZBM_adm")
zmb <- readOGR(dsn = "C:/R_Progs/ZBM_adm", layer="ZMB_ADM0")

## OGR data source with driver: ESRI Shapefile 
## Source: "C:/R_Progs/ZBM_adm", layer: "ZMB_ADM0"
## with 1 features and 2 fields
## Feature type: wkbPolygon with 2 dimensions

extdir参数将使shapefile保持整洁在自己的目录中。

ZMB_ADM0的原因是有多个shapefile:

ZMB_ADM0.SHP
ZMB_ADM1.SHP
ZMB_ADM2.SHP

每个都有自己的图层:

INFO: Open of `ZMB_ADM0.SHP'
      using driver `ESRI Shapefile' successful.
1: ZMB_ADM0 (Polygon)

INFO: Open of `ZMB_ADM1.SHP'
      using driver `ESRI Shapefile' successful.
1: ZMB_ADM1 (Polygon)

INFO: Open of `ZMB_ADM2.SHP'
      using driver `ESRI Shapefile' successful.
1: ZMB_ADM2 (Polygon)

您使用哪一个取决于您需要的管理边界级别:

library(ggplot2)
library(grid)

map0 <- readOGR(dsn = "C:/R_Progs/ZBM_adm", layer="ZMB_ADM0")
map1 <- readOGR(dsn = "C:/R_Progs/ZBM_adm", layer="ZMB_ADM1")
map2 <- readOGR(dsn = "C:/R_Progs/ZBM_adm", layer="ZMB_ADM2")

map0@data$id <- rownames(map0@data)
map1@data$id <- rownames(map1@data)
map2@data$id <- rownames(map2@data)

adm_labs0 <- data.frame(id=rownames(map0@data), adm0=map0@data$ADM0)
adm_labs1 <- data.frame(id=rownames(map1@data), adm1=map1@data$ADM1)
adm_labs2 <- data.frame(id=rownames(map2@data), adm2=map2@data$ADM2)

map_0 <- fortify(map0)
map_1 <- fortify(map1)
map_2 <- fortify(map2)

map_0 <- merge(map_0, adm_labs0)
map_1 <- merge(map_1, adm_labs1)
map_2 <- merge(map_2, adm_labs2)

map0 <- readOGR(dsn = "C:/R_Progs/ZBM_adm", layer="ZMB_ADM0")
map1 <- readOGR(dsn = "C:/R_Progs/ZBM_adm", layer="ZMB_ADM1")
map2 <- readOGR(dsn = "C:/R_Progs/ZBM_adm", layer="ZMB_ADM2")

map0@data$id <- rownames(map2@data)
map1@data$id <- rownames(map1@data)
map2@data$id <- rownames(map2@data)

adm_labs0 <- data.frame(id=rownames(map0@data), adm0=map0@data$ADM0)
adm_labs1 <- data.frame(id=rownames(map1@data), adm1=map1@data$ADM1)
adm_labs2 <- data.frame(id=rownames(map2@data), adm2=map2@data$ADM2)

map_0 <- fortify(map0)
map_1 <- fortify(map1)
map_2 <- fortify(map2)

map_0 <- merge(map_0, adm_labs0)
map_1 <- merge(map_1, adm_labs1)
map_2 <- merge(map_2, adm_labs2)

g0 <- ggplot(map_0, aes(x=long, y=lat, group=group)) +
  geom_polygon(color = "grey50", aes(fill=adm0), size=0.125)+
  labs(x="",y="")+ theme_bw() + labs(title="ADM0") +
  coord_fixed() + theme(legend.position="none")

g1 <- ggplot(map_1, aes(x=long, y=lat, group=group)) +
  geom_polygon(color = "grey50", aes(fill=adm1), size=0.125)+
  labs(x="",y="")+ theme_bw() + labs(title="ADM1") +
  coord_fixed() + theme(legend.position="none")

g2 <- ggplot(map_2, aes(x=long, y=lat, group=group)) +
  geom_polygon(color = "grey50", aes(fill=adm2), size=0.125)+
  labs(x="",y="")+ theme_bw() + labs(title="ADM2") +
  coord_fixed() + theme(legend.position="none")

grid.arrange(g0, g1, g2, ncol=3)

d