pythonlayercartopy

How to overlay a vector annotation image on the top of a cartopy map?


How to overlay a vector annotation image on the top of a cartopy map?

import cartopy.io.img_tiles as cimgt
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
from cartopy.mpl.gridliner import LONGITUDE_FORMATTER, LATITUDE_FORMATTER

class TDT(cmigt.GoogleWTS):
    def _image_url(self, tile):
        x, y, z = tile
        url = 'http://t1.tianditu.gov.cn/DataServer?T=vec_w&x=%s&y=%s&l=%s&tk=%s' % (x, y, z, mykey)
        return url
        
class TDT_1(cmigt.GoogleWTS):
    def _image_url(self, tile):
        x, y, z = tile
        url = 'http://t1.tianditu.gov.cn/DataServer?T=cva_w&x=%s&y=%s&l=%s&tk=%s' % (x, y, z, mykey)
        return url
        
def make_map(projection=ccrs.PlateCarree()):
    fig, ax = plt.subplots(figsize=(9, 13),subplot_kw=dict(projection=projection))
    gl = ax.gridlines(draw_labels=True)
    gl.xlabels_top = gl.ylabels_right = False
    gl.xformatter = LONGITUDE_FORMATTER
    gl.yformatter = LATITUDE_FORMATTER
    return fig, ax
    
extent = [105, 106, 35.5, 36.5]

request = TDT()
request_1=TDT_1()
fig, ax = make_map(projection=request.crs)
ax.set_extent(extent) 
ax.add_image(request, 10)
ax.add_image(request_1, 10)  #Can't to add another image,how to let it to display on the top of the map.
plt.show()  

I want to get the merged image with the Vector annotation, but I only see one of the image. How do i do?


Solution

  • When you plot two images on top of each other, the top image will hide the bottom completly unless you allow some transparency on the top image. In your case, you can set the second image with the following code:

    ax.add_image(request_1, 10, alpha=0.6)
    

    The value of alpha:

    0 is 100% transparent
    1 is 100% opaque
    

    you need a good value between 0 and 1.

    In some situation, zorder option may be needed.

    ax.add_image(request, 10, zorder=10)
    ax.add_image(request_1, 10, alpha=0.6, zorder=20)
    

    Hope this helps.