Geopandas 和 bokeh 从数据中提取 xs 和 ys

Geopandas and bokeh extract xs and ys from data

我正在尝试使用 geopandas 读取存储在 CSV 文件中的地理数据,并为大学目的创建欧洲地图。我从 geopandas 数据库中提取几何值并将其添加到我的 df,尽管我显然需要使用 geojson 文件。 我几乎花了一天的时间浏览一些教程和示例,但我没能做到 link。 如果有人可以提供帮助,将不胜感激。 目的是添加一个绿色的字形,以根据均值列指示每个国家/地区在欧洲的表现。

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]

如果您只想绘制欧洲地图,那么这是代码(适用于 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)

结果: