2014-09-18 52 views
0

我正在嘗試讀取壓縮後的地圖文件以用作輪廓上的地圖疊加層,但我似乎無法解壓該文件。以下是我的命令;如何打開解壓縮後的地圖文件

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

我想在我的情節中使用zmb2作爲底圖。壓縮文件和腳本位於相同的目錄中。 下面是我得到:

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

無法打開層 但我確實看到unzip命令的作品,因爲我看到DBF,PRJ,SHP和SHX擴展4個新的文件

任何幫助將不勝感激。

回答

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參數將保持整潔shape文件在自己的目錄中。

的原因ZMB_ADM0是,有多種形狀文件:

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

[email protected]$id <- rownames([email protected]) 
[email protected]$id <- rownames([email protected]) 
[email protected]$id <- rownames([email protected]) 

adm_labs0 <- data.frame(id=rownames([email protected]), [email protected]$ADM0) 
adm_labs1 <- data.frame(id=rownames([email protected]), [email protected]$ADM1) 
adm_labs2 <- data.frame(id=rownames([email protected]), [email protected]$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") 

[email protected]$id <- rownames([email protected]) 
[email protected]$id <- rownames([email protected]) 
[email protected]$id <- rownames([email protected]) 

adm_labs0 <- data.frame(id=rownames([email protected]), [email protected]$ADM0) 
adm_labs1 <- data.frame(id=rownames([email protected]), [email protected]$ADM1) 
adm_labs2 <- data.frame(id=rownames([email protected]), [email protected]$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

+0

你真的做得比我想的要多。非常感謝,雖然我有我自己的另一個問題,那是我有多個版本的R,所以我得到的錯誤:「錯誤:ggplot2不知道如何處理類函數的數據」 – 2014-09-19 06:47:18

+0

只是一個請求@hrbrmstr,當我上面運行你的代碼時,我得到:「$ < - 。data.frame'('* tmp *',」id「,value = c(」0「,」1「 ,「2」,「3」,::替換有72行,數據有1個。我不知道你是否可以花你的寶貴的建議我在哪裏可能會出現問題,否則創建和填充文件夾os shapefile。 – 2014-09-19 11:06:15