2017-09-16 72 views
0

我試圖按照第一個答案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([email protected]) 

# 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([email protected]$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 

搜索四周,閱讀一些想法(例如here,並here)我發現,它與rgdal後一個問題。我可以加載rgdal庫中沒有的問題:

> library(rgdal) 
> 

但後來我看了目錄/ Macintosh HD /資源庫/框架,並沒有GDAL_Frameworks子目錄。

我正在運行Mac OSX Sierra,版本10.12.6和R版本3.4.1(單個蠟燭)。

如何正確運行system命令?

+0

會發生什麼,當你試着去'系統(「這ogr2ogr」)'? – Stedy

+0

也許你應該單獨安裝ogr2ogr並嘗試在R之外運行它? –

+0

@Stedy我點擊回車鍵,我得到'>'符號,所以命令執行成功。 – StatsSorceress

回答

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

+0

嗨@Gonzo,我認爲這需要我安裝自制軟件,是嗎?另外,我沒有'/ Library/Frameworks/GDAL.framework' - GDAL.framework目錄不存在。 – StatsSorceress

+2

強烈建議使用Homebrew '/ usr/bin/ruby​​ -e「$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)」'。 看來你沒有安裝gdal或者安裝了錯誤的方法。安裝自制軟件,比源自gdal並在R中使用完整路徑系統命令。應該工作。請享用! – Gonzo