R中的加拿大人口普查部门:无法开放

时间:2017-09-16 18:17:49

标签: r

我正在尝试按照第一个回答here的第一部分,复制并粘贴到下面:

library(rgeos)
library(rgdal)
library(maptools)
library(sp)
library(ggplot2)

# decent, uncluttered map theme (needs devtools package tho)
devtools::source_gist("https://gist.github.com/hrbrmstr/33baa3a79c5cfef0f6df")

# grab the file from "Statistics Canada"
download.file("http://www12.statcan.gc.ca/census-recensement/2011/geo/bound-limit/files-fichiers/gcd_000b11a_e.zip",
              destfile="gcd_000b11a_e.zip")

unzip("gcd_000b11a_e.zip")

# this simplifies the polygons so they load/plot faster
system("ogr2ogr canada.shp gcd_000b11a_e.shp -simplify 0.01")

# what layers do we have? you can use this to check
#   ogrListLayers("gcd_000b11a_e/canada.shp")
# but there are none, so the shapefile is the layer

canada <- readOGR("gcd_000b11a_e/","canada")

# do this to see what's available from an "identifier" standpoint
# "CDNAME" seems to be the census district name
# "PRNAME" seems to be the province name
# str(canada@data)

# rig up  some data
# make a data frame of census division areas
# you can assign as many value columns as you like
# they get merged in later and can be used as the fill level
# we'll use the area as the fill level
map_areas <- data.frame(id=canada@data$CDNAME,
                        area=sapply(slot(canada, "polygons"), slot, "area") )

# this takes a while, but it makes a data frame for use with
# ggplot and lets us use the census division name for doing things
# like applying colors
canada_map <- fortify(canada, region="CDNAME")

# merge in areas
canada_map <- merge(canada_map, map_areas, by="id")

gg <- ggplot()
gg <- gg + geom_map(data=canada_map, map=canada_map,
                    aes(map_id=id, x=long, y=lat, group=group, fill=log1p(area)),
                    color="white", size=0.1)
gg <- gg + coord_map() # can choose other projections
gg <- gg + theme_map()
gg

但是,我收到了几个错误。第一个是:

system("ogr2ogr canada.shp gcd_000b11a_e.shp -simplify 0.01")
/bin/sh: ogr2ogr: command not found

在搜索并阅读一些想法(例如herehere)之后,我发现这是rgdal的问题。我可以加载rgdal库没问题:

> library(rgdal)
> 

但后来我找了目录/ Macintosh HD / Library / Frameworks,并且没有GDAL_Frameworks子目录。

我正在运行Mac OSX Sierra,版本10.12.6和R版本3.4.1(单烛光)。

如何正确运行system命令?

1 个答案:

答案 0 :(得分:0)

如果这只是Mac上系统路径的问题,您可以选择提供gdal库的完整路径。

system("/Library/Frameworks/GDAL.framework/Programs/ogr2ogr canada.shp gcd_000b11a_e.shp -simplify 0.01")

上面的路径也可能是错误的,以便在Mac终端中获得正确的路径which ogr2ogr。如果此路径与/Library/Frameworks/GDAL.framework/Programs/ogr2ogr的路径不同,请与正确的路径进行交换。

如果您仍然遇到gdal库问题,请尝试从源代码安装并再次运行R中的系统命令 brew install gdal-20 --build-from-source