pandasdata-visualizationbokehgeopandasgeomap

Geopandas and bokeh extract xs and ys from data


enter image description hereI am trying to read geodata stored in a CSV file using geopandas and create a map of Europe for college purposes. I extract the geometry values from geopandas DB and add it to my df, though I apparently need to use geojson file. I have spent literally a day going through few tutorials and examples, though I did not manage to link it. If anyone could help, it will be much appreciated. The intention is to add a glyph with a green color to indicate how well each country is doing in Europe based on the mean column.

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
europe = (world.loc[world['continent'] == 'Europe'])
europe.head()

geo_source = GeoJSONDataSource(geojson=europe.to_json())

palette = ['#b9ef96', '#9ae968', '#7be23a', '#6cdf23', '#64dd17']
color_mapper = LogColorMapper(palette=palette)

p = figure(plot_height=600, title='Europe', x_range=(-30,60), y_range= 
(30,85))
p.patches('xs', 'ys', fill_alpha=0.7,
         fill_color='green', line_color='black', line_width=0.5,
         source=geo_source)


show(p)

df_map1 = pd.read_csv('countries_geom.csv', delimiter='\t', index_col=0)
df_map1
df_source = ColumnDataSource(df_map1)

world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
europe = (world.loc[world['continent'] == 'Europe'])
europe.head()

geo_source = GeoJSONDataSource(geojson=europe.to_json())

palette = ['#b9ef96', '#9ae968', '#7be23a', '#6cdf23', '#64dd17']
color_mapper = LogColorMapper(palette=palette)

p = figure(plot_height=600, title='Europe', x_range=(-30,60), y_range= 
(30,85))
p.patches('xs', 'ys', fill_alpha=0.7,
         fill_color='green', line_color='black', line_width=0.5,
         source=geo_source)


    show(p)

df_map1 = pd.read_csv('countries_geom.csv', delimiter='\t', index_col=0)
df_map1
df_source = ColumnDataSource(df_map1)[![df_map_image][1]][1]

Solution

  • If you just want to draw the map of Europe then this is the code (for Bokeh v1.1.0):

    from bokeh.models import ColumnDataSource
    from bokeh.plotting import figure, show
    from shapely.geometry import Polygon
    import geopandas as gp
    
    world = gp.read_file(gp.datasets.get_path('naturalearth_lowres'))
    europe = (world.loc[world['continent'] == 'Europe'])
    names = [country for country in europe.name]
    
    countries = []
    [countries.append(country) if type(item) == Polygon else [countries.append(country) for i in list(item)] for item, country in zip(europe.geometry, names)]
    
    polygons = []
    [polygons.append(item) if type(item) == Polygon else [polygons.append(i) for i in list(item)] for item in europe.geometry]
    
    xs, ys = [], []
    xs = [list(polygon.boundary.coords.xy[0]) for polygon in polygons]
    ys = [list(polygon.boundary.coords.xy[1]) for polygon in polygons]
    
    source = ColumnDataSource(dict(xs = xs, ys = ys, countries = countries))
    
    p = figure(title = 'Europe', tools = 'pan, wheel_zoom, box_zoom, reset, hover, save', tooltips = [('Countries', '@countries')],
               x_range = (-30, 60), y_range = (30, 85), x_axis_location = None, y_axis_location = None)
    
    p.patches('xs', 'ys', fill_alpha = 0.7, fill_color = 'green', line_color = 'black', line_width = 0.5, source = source)
    show(p)
    

    enter image description here Result: