-
Notifications
You must be signed in to change notification settings - Fork 794
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Integration with geopandas #588 #818
Closed
Closed
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
d317c98
Merge remote-tracking branch 'altair-viz/master'
iliatimofeev e297f20
Merge remote-tracking branch 'altair-viz/master'
iliatimofeev ea65ca1
Merge remote-tracking branch 'altair-viz/master'
iliatimofeev f7d66b7
__geo_interface__ in to_geojson_values
iliatimofeev e9d28a5
GeoDataFrame support without dependency of GeoPandas
iliatimofeev 985d3f6
Merge remote-tracking branch 'altair-viz/master' into it-#588-geopandas
iliatimofeev b627c6c
Unused test file
iliatimofeev 2cd9fde
Full __geo_interface__ support, test_geojson
iliatimofeev 7db9ff8
test update
iliatimofeev 950eb72
Mistakenly added .vscode files removed
iliatimofeev 9f91c00
limit_rows two returns, to_* one if "geo" statement, four spaces for…
iliatimofeev 76a2af8
geojson_feature()
iliatimofeev 094a2e7
test_geopandas_examples (hacker version)
iliatimofeev df84294
travis-ci: move finalized locals outside try
iliatimofeev 27a3df8
remove python 3 code
iliatimofeev 301ea23
flat version
iliatimofeev c114acc
flat version
iliatimofeev 143ad04
Merge remote-tracking branch 'altair-viz/master' into it-#588-geopandas
iliatimofeev 661447d
Merge remote-tracking branch 'altair-viz/master' into it-#588-geopandas
iliatimofeev d2b46e0
GeoPandas ref
iliatimofeev 649fa21
Merge remote-tracking branch 'altair-viz/master' into it-#588-geopandas
iliatimofeev 89a999e
Merge remote-tracking branch 'altair-viz/master' into it-#588-geopandas
iliatimofeev 80c56d6
merge
iliatimofeev 1bb8192
flake8 fix
iliatimofeev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,6 +62,7 @@ target/ | |
|
||
.ipynb_checkpoints | ||
.idea/* | ||
.vscode/* | ||
tools/_build | ||
Untitled*.ipynb | ||
.mypy* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,194 @@ | ||
import pytest | ||
import pandas as pd | ||
import altair.vegalite.v2 as alt | ||
|
||
from ..data import pipe, to_values, to_csv | ||
from .. import parse_shorthand | ||
|
||
|
||
def _create_geojson(): | ||
return { | ||
"type": "FeatureCollection", | ||
"bbox": [ | ||
-161.30174569731454, | ||
-60.39157788643298, | ||
172.67580002536624, | ||
42.438347020953984 | ||
], | ||
"features": [ | ||
{ | ||
"type": "Feature", | ||
"properties": {"prop": 1}, | ||
"geometry": { | ||
"type": "LineString", | ||
"coordinates": [ | ||
[-69.2980008004234, 23.18780298146116], | ||
[-161.30174569731454, -60.39157788643298], | ||
[172.67580002536624, 24.151450472748962] | ||
] | ||
}, | ||
"id": "0", | ||
"bbox": [ | ||
-161.30174569731454, | ||
-60.39157788643298, | ||
172.67580002536624, | ||
24.151450472748962 | ||
] | ||
}, | ||
{ | ||
"type": "Feature", | ||
"properties": {"prop": 2}, | ||
"geometry": { | ||
"type": "LineString", | ||
"coordinates": [ | ||
[156.03047546751765, 42.438347020953984], | ||
[35.46296546950265, -18.185542212943375], | ||
[152.53211600051463, 23.471406463455793] | ||
] | ||
}, | ||
"id": "1", | ||
"bbox": [ | ||
35.46296546950265, | ||
-18.185542212943375, | ||
156.03047546751765, | ||
42.438347020953984 | ||
] | ||
}, | ||
{ | ||
"type": "Feature", | ||
"properties": {"prop": 3}, | ||
"geometry": { | ||
"type": "LineString", | ||
"coordinates": [ | ||
[-133.98414913936503, 25.39468871174894], | ||
[145.04376601680605, 13.058626381790845], | ||
[170.30576801294046, 38.67128737163435] | ||
] | ||
}, | ||
"id": "2", | ||
"bbox": [ | ||
-133.98414913936503, | ||
13.058626381790845, | ||
170.30576801294046, | ||
38.67128737163435 | ||
] | ||
} | ||
] | ||
} | ||
|
||
def _create_fake_geo_interface(): | ||
class FakeGeoJSON: | ||
__geo_interface__=_create_geojson() | ||
return FakeGeoJSON() | ||
|
||
def _create_fake_geodataframe(): | ||
class FakeGeoSeries: | ||
__geo_interface__=_create_geojson() | ||
def __init__(self, geometry_name = 'geometry'): | ||
self.name = geometry_name | ||
|
||
class FakeGeoDataFrame(pd.DataFrame): | ||
__geo_interface__ = _create_geojson() | ||
geometry = FakeGeoSeries() | ||
def copy(self, deep=True): | ||
data = self._data | ||
if deep: | ||
data = data.copy() | ||
return FakeGeoDataFrame(data).__finalize__(self) | ||
def drop(self, labels=None, axis=0,**kwargs): | ||
if (axis == 1) and (self.geometry.name == labels): | ||
return self.copy() | ||
return super(FakeGeoDataFrame,self).drop(labels, axis,**kwargs) | ||
|
||
return FakeGeoDataFrame({'prop':[1,2,3]}) | ||
|
||
def test_to_values_geo(): | ||
"""Test the to_values data transformer.""" | ||
|
||
data = _create_fake_geodataframe() | ||
result = pipe(data, to_values) | ||
assert result['format'] == {'type':'json'} | ||
assert result['values'][1]['geometry']==data.__geo_interface__['features'][1]['geometry'] | ||
assert result['values'][1]['type']==data.__geo_interface__['features'][1]['type'] | ||
|
||
data = _create_fake_geo_interface() | ||
result = pipe(data, to_values) | ||
assert result['format'] == {'type':'json'} | ||
assert result['values']==data.__geo_interface__ | ||
|
||
def test_chart_data_geotypes(): | ||
Chart = lambda data,**arg: alt.Chart(data).mark_geoshape().project().encode(**arg) | ||
|
||
# Fake GeoPandas | ||
data = _create_fake_geodataframe() | ||
dct = Chart(data,fill='prop').to_dict() | ||
assert dct['data']['values'][1]['geometry']==data.__geo_interface__['features'][1]['geometry'] | ||
assert dct['data']['values'][1]['type']==data.__geo_interface__['features'][1]['type'] | ||
|
||
# Fake GeoInterface | ||
data = _create_fake_geo_interface() | ||
dct = Chart(data).to_dict() | ||
assert dct['data']['format'] == {'type':'json'} | ||
assert dct['data']['values'] == data.__geo_interface__ | ||
|
||
def test_parse_shorthand_with_geodata(): | ||
def check(s, data, **kwargs): | ||
assert parse_shorthand(s, data) == kwargs | ||
|
||
data = _create_fake_geodataframe() | ||
|
||
check('prop', data, field='prop', type='quantitative') | ||
check('prop:N', data, field='prop', type='nominal') | ||
check('count(prop)', data, field='prop', aggregate='count', type='quantitative') | ||
|
||
data = _create_fake_geo_interface() | ||
|
||
check('properties.prop:Q', data, field='properties.prop', type='quantitative') | ||
check('prop', data, field='prop') | ||
|
||
def test_to_csv_geo(): | ||
"""Test the to_csv raise error with geopandas.""" | ||
|
||
data = _create_fake_geodataframe() | ||
with pytest.raises(NotImplementedError): | ||
pipe(data, to_csv) | ||
|
||
def test_geo_pandas(): | ||
gpd = pytest.importorskip('geopandas') | ||
|
||
data = gpd.GeoDataFrame.from_features(_create_geojson()) | ||
dct = alt.Chart(data).mark_geoshape().project().encode(fill='prop').to_dict() | ||
|
||
assert dct['data']['format'] == {'type':'json'} | ||
assert dct['encoding'] == {'fill': {'field': 'prop', 'type': 'quantitative'}} | ||
data2 = gpd.GeoDataFrame.from_features({ | ||
'type':'FeatureCollection', | ||
'features':[{'type':item['type'], | ||
'geometry':item['geometry'], | ||
'id':item['id'], | ||
'properties':{ k: item[k] | ||
for k in item.keys() | ||
if k not in ('type','geometry') | ||
} | ||
} for item in dct['data']['values']] | ||
}) | ||
|
||
assert (data2[data.columns] == data).all().all() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should work :) and looks more readable |
||
|
||
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As currently written the function will never progress beyond this line, and the max_rows check will never happen
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if so, how could it pass
test_limit_rows()
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or do you mean that it bypass unknown data types, then yes like #887