简体   繁体   English

Datashader:GeoDataFrames 的分类颜色映射

[英]Datashader: categorical colormapping of GeoDataFrames

Installed packages安装包

datashader 0.13.0, holoviews 1.14.4, geoviews 1.9.1., bokeh 2.3.2.数据着色器 0.13.0,全息视图 1.14.4,地理视图 1.9.1.,散景 2.3.2。

What I'm trying to do我想做什么

I'm trying to recreate a choropleth map with one color mapped to one category in a large GeoDataFrame using Datashader, following this example in the Pipeline page and this as well as this SO, which all differ slightly in their syntax, and all use points as the example, rather than polygons.我正在尝试使用 Datashader 重新创建一个 choropleth 地图,其中一种颜色映射到一个大型 GeoDataFrame 中的一个类别,使用 Datashader,遵循管道页面中的这个示例以及这个以及这个SO,它们的语法都略有不同,并且都使用点作为示例,而不是多边形。

Reproducible code sample可重现的代码示例

Below a small sample of the full dataset.下面是完整数据集的小样本。

import pandas as pd
import geopandas as gpd
from spatialpandas import GeoDataFrame
import datashader as ds
import datashader.transfer_functions as tf


d = {'geometry': {0: 'POLYGON ((13.80961103741604 51.04076975651729, 13.80965521888065 51.04079016168103, 13.80963851766593 51.04080454197601, 13.80959433642561 51.04078412781548, 13.80961103741604 51.04076975651729))',
  1729: 'POLYGON ((13.80839606906416 51.03845025070634, 13.80827635138927 51.03836030644977, 13.80840483855695 51.03829244374037, 13.80852462026795 51.03838211873356, 13.80839606906416 51.03845025070634))',
  2646: 'POLYGON ((13.80894179055831 51.04544128170094, 13.80952887156242 51.0450399782091, 13.80954152432486 51.04504668985658, 13.80896834397535 51.04545611172818, 13.80894179055831 51.04544128170094))'},
 'category': {0: 'Within_500m', 1729: 'Outside_500m', 2646: 'River/stream'}}

gdf = gpd.GeoDataFrame(pd.DataFrame(d), geometry=gpd.GeoSeries.from_wkt(pd.DataFrame(d)['geometry']), crs="EPSG:4326")

gdf['category'] = gdf['category'].astype('category')

spatialpdGDF = GeoDataFrame(gdf)

color_key = {'Within_500m': 'red', 'Outside_500m': 'lightgrey', 'River/stream': 'lightblue'}
canvas = ds.Canvas(plot_width=1000, plot_height=1000)
agg = canvas.polygons(spatialpdGDF, 'geometry', agg=ds.count_cat('category'))
tf.shade(agg, color_key=color_key)

Expected behaviour预期行为

I would expect all polys to be rasterized and displayed in a single color for each of the categories.我希望所有多边形都被光栅化并为每个类别以单一颜色显示。

Observed behaviour观察到的行为

The full dataset results in an almost white image, some outlines are very faintly visible.完整的数据集产生了一张几乎是白色的图像,一些轮廓非常模糊。

在此处输入图片说明

If I change the background color, some of the polys stand out more, though even the title is only faintly visible.如果我更改背景颜色,一些多边形会更加突出,尽管即使是标题也只是依稀可见。

tf.Images(tf.set_background(tf.shade(agg, color_key=color_key, name="Custom color key"), "black"))

在此处输入图片说明

Does this have to do with Datashader calculating, as the Pipeline notebook mentions, "the transparency and color of each pixel according to each category's contribution to that pixel"?这是否与 Datashader 计算有关,正如流水线笔记本中提到的,“根据每个类别对该像素的贡献,每个像素的透明度和颜色”? But since each category is the sole contributor to each pixel (ie there is no spatial overlap with other categories in this case), why does the alpha seem to be set so low that one cannot see anything?但是由于每个类别都是每个像素的唯一贡献者(即在这种情况下与其他类别没有空间重叠),为什么 alpha 似乎设置得如此之低以至于人们看不到任何东西? I also tried the agg=ds.by('category') aggregator with the same result.我还尝试了agg=ds.by('category')聚合器,结果相同。

Incidentally, if I delete the 'category' column (which causes an "input must be numeric" error otherwise) and use GeoViews in combination with HoloViews rasterize I can visualise the polys using one color without problem, but I haven't figured out how to use this approach to plot multiple datashaded GDFs with different color mapping on the same Bokeh/or mpl plot (the usual HoloViews "overlay multiplication" does not work in that case).顺便说一句,如果我删除“类别”列(否则会导致“输入必须为数字”错误)并将 GeoViews 与 HoloViews rasterize结合使用,我可以毫无问题地使用一种颜色可视化多边形,但我还没有弄清楚如何使用这种方法在同一个散景/或 mpl 图上绘制具有不同颜色映射的多个数据阴影 GDF(通常的 HoloViews“叠加乘法”在这种情况下不起作用)。

import geoviews as gv
from holoviews.operation.datashader import rasterize

gv.extension('bokeh')

del gdf['category']

rasterize(gv.Polygons(gdf)).opts(cmap=['red'])

Try agg=ds.by('category', ds.any()) , which will ignore polygons that overlap in any pixel.尝试agg=ds.by('category', ds.any()) ,它将忽略在任何像素中重叠的多边形。 ds.count_cat('category') is now an alias for ds.by('category', ds.count()) , but as of Datashader 0.12.1 you are no longer limited to just count , and can eg use any to discard information about overlaps. ds.count_cat('category')现在是ds.by('category', ds.count())的别名,但是从 Datashader 0.12.1 开始,您不再仅限于count ,并且可以例如使用any丢弃有关重叠的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM