2013-03-19 82 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关于蟒蛇读取文件。