簡體   English   中英

我試圖根據ZevRoss博客使用leafletR包制作交互式地圖。但是代碼中存在錯誤

[英]I am trying to make an interactive map using leafletR package according to a ZevRoss blog. But there is an error in the code

該ZevRoss博客是如下: http://zevross.com/blog/2014/04/11/using-r-to-quickly-create-an-interactive-online-map-using-the-leafletr-package/

有錯誤的代碼是:

# ----- Write data to GeoJSON
leafdat<-paste(downloaddir, "/", filename, ".geojson", sep="") 
writeOGR(subdat, leafdat, layer="", driver="GeoJSON")

錯誤是:

writeOGR出錯(subdat,leafdat,layer =“”,driver =“GeoJSON”):
GDAL錯誤3:無法打開文件'd:/Leaflet/County_2010Census_DP1.geojson'

因為我是R的新生,所以我經常搜索這個問題而沒有得到任何好的答案。

我在Windows 7 32bit上使用Rstudio R 3.1.1版(2014-07-10)。 我的rgdal版本是0.9-1。

博客中的其他代碼運行成功,這句話似乎是唯一的難點。

您可以使用leafletR包創建GeoJSON:

library('leafletR')
Your_GeoJSON <- toGeoJSON(data=YourData, dest=getwd())

我試圖在一段時間內找到解決這個神秘錯誤的方法。

最終我在Gdal包錯誤的門票網站上發現了這篇文章,澄清了問題並給出了解決方案。

基本上問題出在rgdal和Gdal之間的界面上(Gdal改變了他們的工作方式,最新版本的rgdal尚未觀看):

writeOGR() calls ogrCheckExists("foo.geojson") to check first if the file exists before creating a new dataset.
In the 1.11 version the OGR GeoJSON driver will emit an error message that this file doesn't exists, whereas previous version didn't emit an error message. 
rgdal catches this error as a fatal one and doesn't go to the writing step. This should be fixed in rgdal.
Meanwhile you have an easy workaround : add check_exists = FALSE as a parameter to writeOGR()

因此,以下代碼將起作用:

writeOGR(spDf,'foo.geojson','spDf', driver='GeoJSON',check_exists = FALSE)

當然,如果在該位置已經存在具有所選名稱的geojson文件,則writeOGR仍然失敗。

即使您的計算機上已有“d:”驅動器,並且您有權寫入該驅動器,請嘗試以下操作:

--------------------------------

leafdat<-paste(downloaddir, "/",  ".geojson", sep="") 

> leafdat

> "d:/Leaflet/.geojson"

writeOGR(subdat, leafdat, layer="", driver="GeoJSON")

--------------------------------

然后你可以在“d:/ Leaflet”上獲得“.geojson”文件。 將文件名“.geojson”更改為“County_2010Census_DP1.geojson”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM