Skip to content

Commit

Permalink
Handle Vega geometry types in data (#1359)
Browse files Browse the repository at this point in the history
* Handle Vega geometry types in data

* Add test
  • Loading branch information
philippjfr committed May 19, 2020
1 parent d71eb30 commit 715d1cc
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
4 changes: 4 additions & 0 deletions panel/pane/vega.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ def _get_sources(self, json, sources):
if name in sources or isinstance(datasets[name], dict):
continue
data = datasets.pop(name)
if isinstance(data, list) and any(isinstance(d, dict) and 'geometry' in d for d in data):
# Handle geometry records types
datasets[name] = data
continue
columns = set(data[0]) if data else []
if self.is_altair(self.object):
import altair as alt
Expand Down
67 changes: 67 additions & 0 deletions panel/tests/pane/test_vega.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,62 @@
}
}

gdf_example = {
'config': {'view': {'continuousWidth': 400, 'continuousHeight': 300}},
'data': {'name': 'data-778223ce4ff5da49611148b060c0cd3d'},
'mark': {'type': 'geoshape', 'fill': 'lightgray', 'stroke': 'white'},
'height': 600,
'projection': {'reflectY': True, 'type': 'identity'},
'width': 800,
'$schema': 'https://vega.github.io/schema/vega-lite/v4.0.0.json',
'datasets': {
'data-778223ce4ff5da49611148b060c0cd3d': [
{
'bid': 'SR01-01',
'type': 'Feature',
'geometry': {
'type': 'Polygon',
'coordinates': [
[[120.0, 855.0],
[120.0, 925.0],
[20.0, 925.0],
[20.0, 855.0],
[120.0, 855.0]]
]
}
},
{
'bid': 'SR02-02',
'type': 'Feature',
'geometry': {
'type': 'Polygon',
'coordinates': [
[[120.0, 940.0],
[120.0, 1010.0],
[20.0, 1010.0],
[20.0, 940.0],
[120.0, 940.0]]
]
}
},
{
'bid': 'SR03-03',
'type': 'Feature',
'geometry': {
'type': 'Polygon',
'coordinates': [
[[240.0, 940.0],
[240.0, 1010.0],
[140.0, 1010.0],
[140.0, 940.0],
[240.0, 940.0]]
]
}
}
]
}
}

def test_get_vega_pane_type_from_dict():
assert PaneBase.get_pane_type(vega_example) is Vega

Expand Down Expand Up @@ -109,6 +165,17 @@ def test_vega_pane(document, comm):
assert pane._models == {}


def test_vega_geometry_data(document, comm):
pane = Pane(gdf_example)

# Create pane
model = pane.get_root(document, comm=comm)
assert isinstance(model, VegaPlot)

# Ensure geometries are not packed into CDS
assert model.data_sources == {}


def test_vega_pane_inline(document, comm):
pane = Pane(vega_inline_example)

Expand Down

0 comments on commit 715d1cc

Please sign in to comment.