0

I found this post on Geopandas and bokeh extract xs and ys from data

What I need is basically the same thing but for the map of the whole world (extract XS and ys from GeoPandas and convert into bokeh readable format). I am struggling with the fact the world data has both polygons and multi polygons.

If anyone can help, that would be much appreciated. Thanks!

Niko
  • 55
  • 5
  • Do you have a GeoJSON. Bokeh provides a `GeoJSONDataSource` wich can handle this. May this [SO post](https://stackoverflow.com/questions/40226189/bokeh-is-not-rendering-properly-multipolygon-islands-from-geojson) helps you. – mosc9575 Jan 26 '21 at 19:28
  • @mosc9575 yes, I have everything installed. I solved it (will post a solution). Now the only thing I am running into is the runtime error. I found some sort of solution for that (is included in the code). I've seen people also say that one way of solving it is to downgrade bokeh. Thanks for answering! – Niko Jan 27 '21 at 07:36

2 Answers2

0

Ok, so I solved it but it needs polishing. If you have any ideas how to improve, let me know. I am also running into a runtime error and that is either solved by uncommenting certain parts of code (marked in the code) or by downgrading bokeh. I have not tried downgrading bokeh, I just saw that answer somewhere.

(Also, I have never posted on SO before so I am not really sure how this whole thing works so be gentle).

https://github.com/nikosarcevic/GeoMapping

Niko
  • 55
  • 5
  • Hi Niko. Please check my answer. Hope this works on your machine, too. I think this simplifys your code. – mosc9575 Jan 27 '21 at 16:05
  • Hi back, @mosc9575 - I just checked and it works! TYSM!Super grateful! It definitely looks simpler than my solution. I cannot comment on json as at times it feels like witchcraft XD – Niko Jan 27 '21 at 16:27
  • one more question, @mosc9575 - did you encounter runtime error when re-running the cell? I get this often so I have to restart the kernel which is super annoying RuntimeError: Models must be owned by only a single document, Selection(id='1060', ...) is already in a doc – Niko Jan 27 '21 at 16:32
  • And no. I did not have any problems with the runtime. Do you have problems with my code? – mosc9575 Jan 27 '21 at 16:40
  • no, yours runs smoothly! I also noticed I am not running into errors with my solution now when I copied all parts of the code into one cell. I was reading the csv (am over plotting some data over the world map) and defining pandas df and source in a separate cell. who knows. vielen dank! – Niko Jan 27 '21 at 17:21
  • Would you consider to mark my answer as acccepted? – mosc9575 Jan 27 '21 at 17:28
  • @mosc9575 definitely! I did but I am still fresh here so my answer/rating does not show up (that is what popped up when I rated it). – Niko Jan 27 '21 at 17:54
0

This is how you can run a GeoJSON using pandas GeoJSONDataSource like I mentioned in my comment.

from bokeh.models import GeoJSONDataSource
from bokeh.plotting import figure, show, output_notebook
import geopandas as gp

output_notebook()

world = gp.read_file(gp.datasets.get_path('naturalearth_lowres'))
geo_source = GeoJSONDataSource(geojson=world.to_json())

p = figure(title='World', tooltips=[('Country', '@name')],
           x_range=(-180, 180), y_range=(-90, 90), 
           x_axis_location=None, y_axis_location=None,
           plot_width=1000, plot_height=500
          )
p.patches('xs', 'ys', fill_alpha=0.4, fill_color='grey', 
          line_color='black', line_width=0.5, source=geo_source
         )
show(p)

Output

world

mosc9575
  • 5,618
  • 2
  • 9
  • 32