2015-11-20 54 views
1

我創建了下面的腳本來獲得丹麥R稱重shape文件:「在plot.window錯誤(...):需要有限‘ylim’值」

# Get Shapefiles for Coastline 
shpurl <- "http://download.geofabrik.de/europe/denmark-latest.shp.zip" 
tmp <- tempfile(fileext=".zip") 
download.file(shpurl, destfile = tmp) 
files <- unzip(tmp, exdir=getwd()) 

# Load & plot shapefile 
library(maptools) 
shp <- readShapePoly(files[grep(".shp$", shpurl)]) 
plot(shp) 

的海岸線這應該給我丹麥的輪廓,但是,我不斷收到以下錯誤:

Error in plot.window(...) : need finite 'ylim' values 
In addition: Warning messages: 
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion 
2: In min(x) : no non-missing arguments to min; returning Inf 
3: In max(x) : no non-missing arguments to max; returning -Inf 

任何幫助或方向表示讚賞。

+1

你有沒有嘗試閱讀shapefile的任何[其他方式](https://www.nceas.ucsb.edu/scicomp/usecases/ReadWriteESRIShapeFiles)? – Konrad

回答

2

看一看你files值:

> length(files) 
[1] 41 

您下載有各種形狀的地域文件數量的文件。例如代碼:

require(rgdal) 
shp <- readOGR(dsn = "whereIsavedyourStuff/Stacks", layer = "roads") 

會正常執行。但是您需要指定要讀取哪些源文件。


作爲一個方面來看,相對於從網絡讀取文件,我建議你看一下這個代碼:

# Download an read US state shapefiles 
tmp_shps <- tempfile(); tmp_dir <- tempdir() 
download.file("http://www2.census.gov/geo/tiger/GENZ2014/shp/cb_2014_us_state_20m.zip", 
       tmp_shps) 
unzip(tmp_shps, exdir = tmp_dir) 

# Libs 
require(rgdal) 

# Read 
us_shps <- readOGR(dsn = tmp_dir, layer = "cb_2014_us_state_20m") 

無關,有什麼方法來讀取形狀文件,你決定使用你必須指定路徑和文件名稱。就readOGR中提供的代碼而言,這通過0​​和layer選項來實現。在您的代碼中使用files[grep(".shp$", shpurl)],您的195MB存檔中的文件名不對應於URL。你這裏有幾個選項:

  1. 您可以下載這個文件並解壓像你一樣,都認爲是*.shp的文件列表名稱,取文件名,並在循環將它們傳遞到一個列表,您會讀所有組合(實際上,你需要大量的文件,以閱讀每一層)

  2. 更好,指定要讀類似於上面提供的代碼層。

相關問題