TheHiddenCake
TheHiddenCake

Reputation: 11

How to add lines on top of python datashader points canvas?

I am trying to plot (longitude, latitude points), and on top, illustrate the shortest path from A to B with some lines.

  cvs = ds.Canvas(plot_width=3000, plot_height=3000, x_range=(minLon, maxLon), y_range=(minLat, maxLat))
    cv1 = ds.Canvas(plot_width=3000, plot_height=3000, x_range=(minLon, maxLon), y_range=(minLat, maxLat))
    agg = cvs.points(df, 'x', 'y')  # this is the histogram
    agg1 = cv1.line(positionsOfNodesInShortesPathDF, 'x', 'y', line_width=5)
    
    img = ds.tf.set_background(ds.tf.shade(agg, how="cbrt", cmap=cc.fire), "black").to_pil() # create a rasterized image
    
    path = ds.tf.set_background(ds.tf.shade(agg1, how="log", cmap=cc.fire), "black").to_pil() # create a rasterized image

And for now, I am making the background of the path image transparent like so

    newImage = []
    for item in path.getdata():
        if item[:3] == (0, 0, 0):
            newImage.append((0, 0, 0, 0))
        else:
            newImage.append(item)

    path.putdata(newImage)
    
    

    im.alpha_composite(img, path).save("combined.png")

Which results in this output:enter image description here

But - this doesn't seem scaleable. My question is therefore, if there is another workaround for this? What if I to highlight other specific points of my map - e.g. start end end point?

Upvotes: 0

Views: 189

Answers (1)

James A. Bednar
James A. Bednar

Reputation: 3255

The background of the path image should already be transparent; tf.set_background is what changes it from transparent to black. So if you skip setting the background, you should be able to composite path directly on top of img, then set the background to black on the final result. Same for any other points you want to overlay.

You can also use tf.stack(img, path) to do the compositing.

Upvotes: 0

Related Questions