2013-03-19 84 views
0

我希望我的問題可以用一些geojson專業知識來解決。我遇到的問題與RhinoPython有關 - McNeel的Rhino 5中嵌入的IronPython引擎(更多信息請見:http://python.rhino3d.com/)。我不認爲有必要成爲RhinoPython的專家來回答這個問題。在RhinoPython中打開geojson文件

我想在RhinoPython中加載geojson文件。因爲你不能導入以GeoJSON模塊插入RhinoPython如同在Python我使用GeoJson2Rhino這裏提供了這個自定義模塊:https://github.com/localcode/rhinopythonscripts/blob/master/GeoJson2Rhino.py

現在我的劇本是這樣的:

`import rhinoscriptsyntax as rs 
import sys 
rp_scripts = "rhinopythonscripts" 
sys.path.append(rp_scripts) 
import rhinopythonscripts 

import GeoJson2Rhino as geojson 

layer_1 = rs.GetLayer(layer='Layer 01') 
layer_color = rs.LayerColor(layer_1) 

f = open('test_3.geojson') 
gj_data = geojson.load(f,layer_1,layer_color) 
f.close()` 

特別:

f = open('test_3.geojson') 
gj_data = geojson.load(f) 

工作正常,當我試圖從常規python 2.7中提取geojson數據。然而,在RhinoPython中,我收到以下錯誤消息:消息:參數'文本'的期望字符串,但'文件';參照gj_data = geojson.load(f)。

我一直在尋找上面鏈接的GeoJson2Rhino腳本,我想我已經正確設置了函數的參數。據我可以告訴它似乎並不認可我的geojson文件,並希望它作爲一個字符串。是否有一個替代文件打開函數,我可以使用它來讓函數將它識別爲geojson文件?

回答

1

該錯誤信息來判斷,它看起來像load方法需要作爲第一輸入,但在上面示例的文件對象被傳遞來代替。試試這個...

f = open('test_3.geojson') 
g = f.read(); # read contents of 'f' into a string 
gj_data = geojson.load(g) 

...或者,如果你不真正需要的文件對象...

g = open('test_3.geojson').read() # get the contents of the geojson file directly 
gj_data = geojson.load(g) 

更多信息請參見here關於蟒蛇讀取文件。