Skip to content

Commit

Permalink
geojson_feature()
Browse files Browse the repository at this point in the history
  • Loading branch information
iliatimofeev committed May 9, 2018
1 parent 9f91c00 commit 76a2af8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
18 changes: 18 additions & 0 deletions altair/utils/tests/test_geojson.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,21 @@ def test_geo_pandas():
assert dct['data']['format'] == {'type':'json','property':'features'}
assert (gpd.GeoDataFrame.from_features(dct['data']['values']) == data).all().all()
assert dct['encoding'] == {'fill': {'field': 'properties.prop', 'type': 'quantitative'}}

def test_geojson_feature():
Chart = lambda data,**arg: alt.Chart(alt.geojson_feature(data,'test_prop')
).mark_geoshape().project().encode(**arg)

# Fake GeoInterface
data = _create_fake_geo_interface()
dct = Chart(data).to_dict()

assert dct['data']['format'] == {'type':'json','property':'test_prop'}
assert dct['data']['values'] == data.__geo_interface__

# url
data = "url.json"
dct = Chart(data).to_dict()

assert dct['data']['format'] == {'type':'json','property':'test_prop'}
assert dct['data']['url'] == data
28 changes: 28 additions & 0 deletions altair/vegalite/v2/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -1295,3 +1295,31 @@ def topo_feature(url, feature, **kwargs):
"""
return core.UrlData(url=url, format=core.TopoDataFormat(type='topojson',
feature=feature, **kwargs))
def geojson_feature(data, feature, **kwargs):
"""A convenience function for extracting features from a geojson object or url
Parameters
----------
data : anyOf(string, geojson.GeoJSON)
string is interpreted as URL from which to load the data set.
geojson.GeoJSON is interpreted as data set itself.
feature : string
The JSON property containing the GeoJSON object set to convert to
a GeoJSON feature collection. For example ``features[0].geometry``.
**kwargs :
additional keywords passed to JsonDataFormat
"""
if isinstance(data, six.string_types):
return core.UrlData(url=data, format=core.JsonDataFormat(type='json',
property=feature, **kwargs))
elif hasattr(data,'__geo_interface__'):
return core.InlineData(values=data.__geo_interface__ , format=core.JsonDataFormat(type='json',
property=feature, **kwargs))
else:
warnings.warn("data of type {0} not recognized".format(type(data)))
return data


0 comments on commit 76a2af8

Please sign in to comment.