【问题标题】:Geopandas and bokeh extract xs and ys from dataGeopandas 和 bokeh 从数据中提取 xs 和 ys
【发布时间】:2019-05-07 19:52:10
【问题描述】:

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

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]

【问题讨论】:

  • 如果您提供最小但完整且可运行的代码,其中包含包含和指向您的数据的链接,这将有所帮助
  • 我很快就会这样做。提前致谢
  • 您在屏幕截图中的Polygon 数据以及在您的CSV 文件中的数据不完整POLYGON ((3.314971144228537 51.34578095153609,... 我认为转换过程中出了点问题...?

标签: pandas data-visualization bokeh geopandas geomap


【解决方案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)

结果:

【讨论】:

  • 非常感谢您的帮助。我真的很感激。
  • 与我的 df 匹配的值和代码中的坐标它们可以正常工作我还可以在创建 CDS 时向其中添加数据,这很容易。我正在尝试创建一个 def 以从 str 列表中提取,尽管它没有成功,你能否从你的代码中一步一步地给我一个快速的步骤,以便我可以更好地理解它并停止烦你,对不起,我很抱歉只是现在学习它。
  • def countriesN(df, names): country = [c for c in list(names)] countries = [] [countries.append(c) if type(item) == Polygon else [countries .append(c) for i in list(item)] for item, c in zip(df.geometry, names)]
  • 我不得不说我不是专家,所以也许其他解决方案可能会更好。说明:europe.geometry 保存 geodata。通常它是Polygon 对象的列表,但是当一个国家,如俄罗斯,有许多单独的部分时,它是Multipolygon,即Polygons 的列表。为了便于处理,我只用多边形创建了一个列表。然后因为多边形比国家多,我不得不重复一些国家(如俄罗斯),所以提供给ColumnDataSource 的列表长度相同。
  • countries 列表中,国家/地区重复的次数等于MultipolygonPolygons 的数量,以便源列表中的国家/地区位置可以匹配源中的多边形位置。如果俄罗斯有 4 个形状(块),则“俄罗斯”一词在国家列表中出现 4 次。现在清楚了吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-01
  • 1970-01-01
  • 2017-02-16
  • 1970-01-01
  • 1970-01-01
  • 2021-12-26
  • 1970-01-01
相关资源
最近更新 更多