@@ -190,7 +190,6 @@ def render(self, **kwargs):
190190 vg.parse.spec(spec, function(chart) { chart({el:div}).update(); });}""" ), # noqa
191191 name = 'vega_parse' )
192192
193-
194193class GeoJson (MacroElement ):
195194 def __init__ (self , data , style_function = None ):
196195 """
@@ -211,8 +210,10 @@ def __init__(self, data, style_function=None):
211210
212211 Examples
213212 --------
214- >>> # Providing file.
213+ >>> # Providing file that shall be embeded .
215214 >>> GeoJson(open('foo.json'))
215+ >>> # Providing filename that shall not be embeded.
216+ >>> GeoJson('foo.json')
216217 >>> # Providing dict.
217218 >>> GeoJson(json.load(open('foo.json')))
218219 >>> # Providing string.
@@ -224,33 +225,43 @@ def __init__(self, data, style_function=None):
224225 """
225226 super (GeoJson , self ).__init__ ()
226227 self ._name = 'GeoJson'
227- if 'read' in dir (data ):
228+ if hasattr (data ,'read' ):
229+ self .embed = True
228230 self .data = json .load (data )
229- elif type (data ) is dict :
231+ elif isinstance (data ,dict ):
232+ self .embed = True
230233 self .data = data
234+ elif isinstance (data , text_type ) or isinstance (data , binary_type ):
235+ if data .lstrip ()[0 ] in '[{' : # This is a GeoJSON inline string
236+ self .embed = True
237+ self .data = json .loads (data )
238+ else : # This is a filename
239+ self .embed = False
240+ self .data = data
231241 else :
232- self .data = json .loads (data )
233-
234- if 'features' not in self .data .keys ():
235- # Catch case when GeoJSON is just a single Feature or a geometry.
236- if not (isinstance (self .data , dict ) and 'geometry' in self .data .keys ()):
237- # Catch case when GeoJSON is just a geometry.
238- self .data = {'type' : 'Feature' , 'geometry' : self .data }
239- self .data = {'type' : 'FeatureCollection' , 'features' : [self .data ]}
242+ raise ValueError ('Unhandled data type.' )
240243
241244 if style_function is None :
242245 style_function = lambda x : {}
243246 self .style_function = style_function
244247
245248 self ._template = Template (u"""
246249 {% macro script(this, kwargs) %}
247- var {{this.get_name()}} = L.geoJson({{this.style_data()}})
250+ var {{this.get_name()}} = L.geoJson(
251+ {% if this.embed %}{{this.style_data()}}{% else %}"{{this.data}}"{% endif %})
248252 .addTo({{this._parent.get_name()}})
249253 .setStyle(function(feature) {return feature.properties.style;});
250254 {% endmacro %}
251255 """ ) # noqa
252256
253257 def style_data (self ):
258+ if 'features' not in self .data .keys ():
259+ # Catch case when GeoJSON is just a single Feature or a geometry.
260+ if not (isinstance (self .data , dict ) and 'geometry' in self .data .keys ()):
261+ # Catch case when GeoJSON is just a geometry.
262+ self .data = {'type' : 'Feature' , 'geometry' : self .data }
263+ self .data = {'type' : 'FeatureCollection' , 'features' : [self .data ]}
264+
254265 for feature in self .data ['features' ]:
255266 feature .setdefault ('properties' ,{}).setdefault ('style' ,{}).update (
256267 self .style_function (feature ))
0 commit comments