这是代码:
import datashader as ds
import pandas as pd
from colorcet import fire
from datashader import transfer_functions as tf
from datashader.utils import lnglat_to_meters
import holoviews as hv
import geoviews as gv
from holoviews.operation.datashader import datashade, spread, aggregate
hv.extension('bokeh')
df = pd.read_csv('...')
agg = ds.Canvas().points(df, 'x', 'y', agg=ds.count())
img = tf.shade(agg.where(agg['x']>0), cmap=fire)
url = 'https://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{Z}/{Y}/{X}.jpg'
tile_opts = dict(width=1000,height=600,xaxis=None,yaxis=None,show_grid=False,bgcolor='black')
map_tiles = gv.WMTS(url).opts(style=dict(alpha=1.0), plot=tile_opts)
points = hv.Points(df, ['x', 'y'])
#points = img # <-- Using this does not work
ds_points = spread(datashade(points, width=1000, height=600, cmap=fire), px=2)
map_tiles * ds_points
Points
上面的代码基于 pandas 数据帧中的数据创建了一个 holoviews对象,并在 holoviews 中使用spread()
和datashade()
函数在地图上绘制点。但是,我想在将数据绘制在地图上之前对数据进行一些转换。我尝试使用 datashader 中已有的功能,但我无法弄清楚如何将xarray.Image
datashader 创建的对象转换为Point
可以绘制在地图图块顶部的 holoviews 对象。
编辑
我无法在注释中正确格式化代码,所以我将其放在这里。
我尝试将以下作为退化案例:
from custom_operation import CustomOperation
points = hv.Points(df, ['x', 'y'])
CustomOperation(rasterize(points))
其中CustomOperation
定义为:
from holoviews.operation import Operation
class CustomOperation(Operation):
def _process(self, element, key=None):
return element
这会产生以下错误:
AttributeError: 'Image' object has no attribute 'get'