【发布时间】:2017-09-17 15:06:00
【问题描述】:
我正在尝试关注第一个答案的第一部分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
搜索并阅读了一些想法(例如here 和here)后,我发现rgdal 有问题。我可以加载rgdal库没问题:
> library(rgdal)
>
但后来我查找目录/Macintosh HD/Library/Frameworks,并没有GDAL_Frameworks子目录。
我正在运行 Mac OSX Sierra,版本 10.12.6 和 R 版本 3.4.1(单蜡烛)。
如何正确运行system 命令?
【问题讨论】:
-
当您尝试
system("which ogr2ogr")时会发生什么? -
也许你应该单独安装 ogr2ogr 并尝试在 R 之外运行它?
-
@Stedy 我按回车键,我得到了 '>' 符号,所以命令执行成功。
-
@Hack-R,不知道该怎么做,因为 ogr2ogr 是 rgdal 库的一部分,据我所知,个别命令不能单独安装...
-
@StatsSorceress 不,不是。它是 GDAL 的一部分,与 R 无关。 rgdal 库只是试图包含它。它并不总是有效。所以你应该尝试在 R 之外安装它。trac.osgeo.org/gdal/wiki/DownloadingGdalBinaries 然后你可以从良好的安装运行 org2ogr 并解决问题或将 R 包装器重新映射到良好的安装。
标签: r