Repository: altair-viz/altair
Pull request #1714 (2019-10-01 21:10:09)

ENH: add datatype geojson
This PR adds the `:G` option for `geojson` data type. As was found out in https://github.com/altair-viz/altair/issues/1711 this was missing in Altair.

With this PR the following example (part of this PR) will work:

```python
import altair as alt
from vega_datasets import data

states = alt.topo_feature(data.us_10m.url, 'states')
source = data.income.url

alt.Chart(source).mark_geoshape().encode(
    shape='geo:G',
    color='pct:Q',
    tooltip=['name:N', 'pct:Q'],
    facet='group:N'
).transform_lookup(
    lookup='id',
    from_=alt.LookupData(data=states, key='id'),
    as_='geo'
).properties(
    width=300,
    height=175,
    columns=2
).project(
    type='albersUsa'
)
```

<img width="694" alt="image" src="https://user-images.githubusercontent.com/5186265/65999828-7dea8d00-e49e-11e9-8dff-97f6a2d16584.png">



Patch:
diff --git a/altair/examples/beckers_barley_trellis_plot.py b/altair/examples/beckers_barley_trellis_plot.py
index f154c886..51486827 100644
--- a/altair/examples/beckers_barley_trellis_plot.py
+++ b/altair/examples/beckers_barley_trellis_plot.py
@@ -1,7 +1,7 @@
 """
 Becker's Barley Trellis Plot
 ----------------------------
-The example demonstrates the trellis charts created by Richard Becker, William Cleveland and others in the 1990s. Using the visualization technique below they identified an anomoly in a widely used agriculatural dataset, which they termed `"The Morris Mistake." <http://ml.stat.purdue.edu/stat695t/writings/Trellis.User.pdf>`_. It became their favored way of showcasing the power of this pioneering plot.
+The example demonstrates the trellis charts created by Richard Becker, William Cleveland and others in the 1990s. Using the visualization technique below they identified an anomoly in a widely used agriculatural dataset, which they termed `"The Morris Mistake." <https://www.albany.edu/acc/courses/acc522fall2007/lecturenotes/trellis.usermanual.pdf>`_. It became their favored way of showcasing the power of this pioneering plot.
 """
 # category: case studies
 import altair as alt
diff --git a/altair/examples/us_incomebrackets_by_state_facet.py b/altair/examples/us_incomebrackets_by_state_facet.py
new file mode 100644
index 00000000..6923a1d8
--- /dev/null
+++ b/altair/examples/us_incomebrackets_by_state_facet.py
@@ -0,0 +1,30 @@
+"""
+US Income by State: Wrapped Facet
+---------------------------------
+This example shows how to create a map of income in the US by state,
+faceted over income brackets 
+"""
+# category: maps
+
+import altair as alt
+from vega_datasets import data
+
+states = alt.topo_feature(data.us_10m.url, 'states')
+source = data.income.url
+
+alt.Chart(source).mark_geoshape().encode(
+    shape='geo:G',
+    color='pct:Q',
+    tooltip=['name:N', 'pct:Q'],
+    facet='group:N'
+).transform_lookup(
+    lookup='id',
+    from_=alt.LookupData(data=states, key='id'),
+    as_='geo'
+).properties(
+    width=300,
+    height=175,
+    columns=2
+).project(
+    type='albersUsa'
+)
\ No newline at end of file
diff --git a/altair/utils/core.py b/altair/utils/core.py
index ffe8c088..b4181825 100644
--- a/altair/utils/core.py
+++ b/altair/utils/core.py
@@ -47,7 +47,8 @@ def infer_dtype(value):
 TYPECODE_MAP = {'ordinal': 'O',
                 'nominal': 'N',
                 'quantitative': 'Q',
-                'temporal': 'T'}
+                'temporal': 'T',
+                'geojson': 'G'}
 
 INV_TYPECODE_MAP = {v: k for k, v in TYPECODE_MAP.items()}
 
diff --git a/doc/user_guide/encoding.rst b/doc/user_guide/encoding.rst
index cdd24a3d..d5854b2e 100644
--- a/doc/user_guide/encoding.rst
+++ b/doc/user_guide/encoding.rst
@@ -132,6 +132,7 @@ quantitative  ``Q``           a continuous real-valued quantity
 ordinal       ``O``           a discrete ordered quantity
 nominal       ``N``           a discrete unordered category
 temporal      ``T``           a time or date value
+geojson       ``G``           a geographic shape
 ============  ==============  ================================================
 
 If types are not specified for data input as a DataFrame, Altair defaults to


Test patch:
diff --git a/altair/utils/tests/test_core.py b/altair/utils/tests/test_core.py
index bbd0aaab..c19e0c40 100644
--- a/altair/utils/tests/test_core.py
+++ b/altair/utils/tests/test_core.py
@@ -82,11 +82,13 @@ def test_parse_shorthand():
     check('foobar:nominal', type='nominal', field='foobar')
     check('foobar:ordinal', type='ordinal', field='foobar')
     check('foobar:temporal', type='temporal', field='foobar')
+    check('foobar:geojson', type='geojson', field='foobar')    
 
     check('foobar:Q', type='quantitative', field='foobar')
     check('foobar:N', type='nominal', field='foobar')
     check('foobar:O', type='ordinal', field='foobar')
     check('foobar:T', type='temporal', field='foobar')
+    check('foobar:G', type='geojson', field='foobar')    
 
     # Fields with aggregate and/or type
     check('average(foobar)', field='foobar', aggregate='average')
diff --git a/altair/vegalite/v3/tests/test_api.py b/altair/vegalite/v3/tests/test_api.py
index c8d7995c..bf8aa7f3 100644
--- a/altair/vegalite/v3/tests/test_api.py
+++ b/altair/vegalite/v3/tests/test_api.py
@@ -476,7 +476,6 @@ def test_filter_transform_selection_predicates():
     assert chart.to_dict()['transform'] == [{'filter': {'selection': {'or': ['s1', 's2']}}}]
 
 
-
 def test_resolve_methods():
     chart = alt.LayerChart().resolve_axis(x='shared', y='independent')
     assert chart.resolve == alt.Resolve(axis=alt.AxisResolveMap(x='shared', y='independent'))