saeranv
saeranv

Reputation: 165

Opening a geojson file in RhinoPython

I'm hoping my problem can be solved with some geojson expertise. The problem I'm having has to do with RhinoPython - the embedded IronPython engine in McNeel's Rhino 5 (more info here: http://python.rhino3d.com/). I don't think its necessary to be an expert on RhinoPython to answer this question.

I'm trying to load a geojson file in RhinoPython. Because you can't import the geojson module into RhinoPython like in Python I'm using this custom module GeoJson2Rhino provided here: https://github.com/localcode/rhinopythonscripts/blob/master/GeoJson2Rhino.py

Right now my script looks like this:

`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()`

In particular:

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

works fine when I'm trying to extract geojson data from regular python 2.7. However in RhinoPython I'm getting the following error message: Message: expected string for parameter 'text' but got 'file'; in reference to gj_data = geojson.load(f).

I've been looking at the GeoJson2Rhino script linked above and I think I've set the parameters for the function correctly. As far as I can tell it doesn't seem to recognize my geojson file, and wants it as a string. Is there an alternative file open function I can use to get the function to recognize it as a geojson file?

Upvotes: 0

Views: 1124

Answers (1)

pearswj
pearswj

Reputation: 66

Judging by the error message, it looks like the load method requires a string as the first input but in the above example a file object is being passed instead. Try this...

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

...or, if you don't actually need the file object...

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

See here for more information about reading files in python.

Upvotes: 1

Related Questions