0
zoom = 3
def plot_map():
    
    start = datetime.now() 
    print("start to Plot ... " , start, zoom )
    fig, ax = plt.subplots(figsize = (10,6))
        
    ax.set_xlim(xlim)
    ax.set_ylim(ylim)
    
    print("before plotting stations ..." , datetime.now() - start)
    stations.plot(figsize=(10,10), ax=ax, markersize=1, color='b', edgecolor='k', zorder=3)
  
    print("before plotting railways..." , datetime.now() - start)
    rw = railways.plot(figsize=(10,10), ax=ax, edgecolor='r', zorder=2)
    #india_map.plot(ax=ax, color='w', edgecolor='g', zorder=1)
    
    #plt.show()
    print("before plotting basemap ..." , datetime.now() - start)
    cx.add_basemap(rw, crs=railways.crs, zoom=zoom)
    
    print("before plotting plt.show ..." , datetime.now() - start)
    plt.show()

    print("ending plotting ..." , datetime.now() - start)
    

xlim = ([india_map.total_bounds[0],  india_map.total_bounds[2]])
ylim = ([india_map.total_bounds[1],  india_map.total_bounds[3]])

def zoom_in(zoom):
    xlim[0] = (xlim[0]+xlim[1])/2 - 3*(xlim[1]-xlim[0])/10
    xlim[1] = (xlim[0]+xlim[1])/2 + 3*(xlim[1]-xlim[0])/10
    ylim[0] = (ylim[0]+ylim[1])/2 - 3*(ylim[1]-ylim[0])/10
    ylim[1] = (ylim[0]+ylim[1])/2 + 3*(ylim[1]-ylim[0])/10
    zoom += 1
    return zoom
    
def zoom_out(zoom):
    xlim[0] = (xlim[0]+xlim[1])/2 - 5*(xlim[1]-xlim[0])/6
    xlim[1] = (xlim[0]+xlim[1])/2 + 5*(xlim[1]-xlim[0])/6
    ylim[0] = (ylim[0]+ylim[1])/2 - 5*(ylim[1]-ylim[0])/6
    ylim[1] = (ylim[0]+ylim[1])/2 + 5*(ylim[1]-ylim[0])/6
    zoom -= 1
    return zoom
    
def pan_left():
    xlim[0] = (xlim[0]+xlim[1])/2 - 4*(xlim[1]-xlim[0])/10 - (xlim[1]-xlim[0])/2
    xlim[1] = (xlim[0]+xlim[1])/2 - 4*(xlim[1]-xlim[0])/10 + (xlim[1]-xlim[0])/2
    
def pan_right():
    xlim[0] = (xlim[0]+xlim[1])/2 + 4*(xlim[1]-xlim[0])/10 - (xlim[1]-xlim[0])/2
    xlim[1] = (xlim[0]+xlim[1])/2 + 4*(xlim[1]-xlim[0])/10 + (xlim[1]-xlim[0])/2
    
def pan_up():
    ylim[0] = (ylim[0]+ylim[1])/2 + 4*(ylim[1]-ylim[0])/10 - (ylim[1]-ylim[0])/2
    ylim[1] = (ylim[0]+ylim[1])/2 + 4*(ylim[1]-ylim[0])/10 + (ylim[1]-ylim[0])/2
    
def pan_down():
    ylim[0] = (ylim[0]+ylim[1])/2 - 4*(ylim[1]-ylim[0])/10 - (ylim[1]-ylim[0])/2
    ylim[1] = (ylim[0]+ylim[1])/2 - 4*(ylim[1]-ylim[0])/10 + (ylim[1]-ylim[0])/2
    
#CSTM / VT station = plot_map([72.836, 72.838], [18.943, 18.947])
#xlim = [72.836, 72.838]
#ylim = [18.943, 18.947]

#print("before plot_map")

plot_map()

#print("before cmd prompt")

#time.sleep(10)
acc_cmds = ["in", "out", "left", "right", "up", "down"]

cmd = input("Do you want to zoom in, zoom out, pan left, pan right, pan up or pan down? Type the corresponding keyword: ")
if cmd not in acc_cmds:
    cmd = input("Enter exactly any of 'in', 'out', 'left', 'right', 'up', 'down'. Else the program will be terminated: ")
while cmd in acc_cmds:
    if cmd == "in":
        zoom = zoom_in(zoom)
    elif cmd == "out":
        zoom = zoom_out(zoom)
    elif cmd == "left":
        pan_left()
    elif cmd == "right":
        pan_right()
    elif cmd == "up":
        pan_up()
    elif cmd == "down":
        pan_down()
    plot_map()
    cmd = input("Do you want to zoom in, zoom out, pan left, pan right, pan up or pan down? Type the corresponding keyword: ")
    if cmd not in acc_cmds:
        cmd = input("Enter exactly any of 'in', 'out', 'left', 'right', 'up', 'down'. Else the program will be terminated: ")
else:
    print("Program ended.") 

撇开用于绘制铁路 geopandas 地图的代码的其他主要部分不谈,我想知道我应该在 zoom_in 和 zoom_out 函数中设置什么等式,这样当我放大/缩小时,我会准确地进行一级缩放在 contextily.add_basemap 的缩放参数中向下/向上。

我在分辨率方面遇到了问题,因为当我放大 10-15 倍时,底图的分辨率不再匹配地图的限制(xlim,ylim)。

4

0 回答 0