GeoJSONDataSource not working for Polygon

Hi All,

I’m trying to get a GeoJSONDataSource working for a simple test Polygon shape and not having any luck. Please see code below: the point appears but the square doesn’t - am I missing something obvious or is this a bug?

I’m using Bokeh 0.12.6 and Firefox 54.0.

Thanks,
Marcus.

from bokeh.models import GeoJSONDataSource
from bokeh.plotting import figure
from bokeh.io import show

geojsondata_point = ‘’’
{
“type”: “FeatureCollection”,
“features”: [
{ “type”: “Feature”,
“geometry”: {
“type”: “Point”,
“coordinates”: [0, 0]
},
“properties”: {
“name”: “Point”
}
}
]
}
‘’’

geojsondata_square = ‘’’
{
“type”: “FeatureCollection”,
“features”: [
{ “type”: “Feature”,
“geometry”: {
“type”: “Polygon”,
“coordinates”: [[0, 0], [1, 0], [1, 1], [0, 1]]
},
“properties”: {
“name”: “Square”
}
}
]
}
‘’’

geo_source_point = GeoJSONDataSource(geojson=geojsondata_point)
geo_source_square = GeoJSONDataSource(geojson=geojsondata_square)

p = figure()
p.circle(x=‘x’, y=‘y’, source=geo_source_point, size=10, color=‘blue’)
p.line(x=‘xs’, y=‘ys’, source=geo_source_square, color=‘red’)

show(p)

``

Hey Marcus,

There are two issues. First your geojson is invalid, you need to close the ring (first pair == last pair) and add another level of nesting, since a Polygon can consist of multiple rings. There is always 1 outer ring, but there can be more then 1 inner rings (holes).

So:
“coordinates”: [[[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]]]

``

Secondly, you should call the “patches” method for Polygons, instead of “line”. Like:

p.patches(xs=‘xs’, ys=‘ys’, source=geo_source_square, color=‘red’)

``

That solves it for me. Let me know if it works.

Regards,
Rutger

···

On Wednesday, July 12, 2017 at 11:19:16 AM UTC+2, Marcus Donnelly wrote:

Hi All,

I’m trying to get a GeoJSONDataSource working for a simple test Polygon shape and not having any luck. Please see code below: the point appears but the square doesn’t - am I missing something obvious or is this a bug?

I’m using Bokeh 0.12.6 and Firefox 54.0.

Thanks,
Marcus.

from bokeh.models import GeoJSONDataSource
from bokeh.plotting import figure
from bokeh.io import show

geojsondata_point = ‘’’
{
“type”: “FeatureCollection”,
“features”: [
{ “type”: “Feature”,
“geometry”: {
“type”: “Point”,
“coordinates”: [0, 0]
},
“properties”: {
“name”: “Point”
}
}
]
}
‘’’

geojsondata_square = ‘’’
{
“type”: “FeatureCollection”,
“features”: [
{ “type”: “Feature”,
“geometry”: {
“type”: “Polygon”,
“coordinates”: [[0, 0], [1, 0], [1, 1], [0, 1]]
},
“properties”: {
“name”: “Square”
}
}
]
}
‘’’

geo_source_point = GeoJSONDataSource(geojson=geojsondata_point)
geo_source_square = GeoJSONDataSource(geojson=geojsondata_square)

p = figure()
p.circle(x=‘x’, y=‘y’, source=geo_source_point, size=10, color=‘blue’)
p.line(x=‘xs’, y=‘ys’, source=geo_source_square, color=‘red’)

show(p)

``

That works perfectly, many thanks Rutger!

Interestingly, it still works if you don’t close the ring - I guess the patch process must do this automatically.

Regards,
Marcus.

Great! Bokeh probably closes it automatically then, which is nice. Many GIS packages auto-fix these obvious errors, but the geojson specs are clear about it. So to maximize compatibility its best to respect it if possible.

A LinearRing is closed LineString with 4 or more positions. The first and last
positions are equivalent (they represent equivalent points). Though a
LinearRing is not explicitly represented as a GeoJSON geometry type, it is
referred to in the Polygon geometry type definition.

https://geojson.org/geojson-spec.html#linestring

Regards,
Rutger

···

On Wednesday, July 12, 2017 at 3:35:42 PM UTC+2, Marcus Donnelly wrote:

That works perfectly, many thanks Rutger!

Interestingly, it still works if you don’t close the ring - I guess the patch process must do this automatically.

Regards,
Marcus.

That’s very helpful, thanks again.

Marcus.