Zilore Mumba
Zilore Mumba

Reputation: 1500

How can I open an unzipped map file

I am trying to read a zipped map file to use as a map overlay on contours, but I do not seem to be able to unzip the file. Below are the commands I have;

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

I want to use zmb2 as a base map in my plot. The zip file and the script are in the same directory. Below is what I get:

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

Cannot open layer But I do see the unzip command works because I see 4 new files with dbf, prj, shp and shx extensions

Any help will be appreciated.

Upvotes: 0

Views: 843

Answers (1)

hrbrmstr
hrbrmstr

Reputation: 78792

Try:

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

The extdir parameter will keep the shapefiles tidy in their own directory.

The reason for the ZMB_ADM0 is that there are multiple shapefiles:

ZMB_ADM0.SHP
ZMB_ADM1.SHP
ZMB_ADM2.SHP

each with their own layers:

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)

Which one you use will depend on what level of administrative boundaries you need:

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

Upvotes: 3

Related Questions