Is there a way to move the map that the whole great circle route would be shown? (basemap)

42 Views Asked by At

I tried to make a program for my paperwork that will live show the range between two cities and i wanted and i got a problem that i cant handle.

Actual map looks like this: enter image description here

Code:

from mpl_toolkits.basemap import Basemap
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider

# Create new figure, axes instances.
fig = plt.figure()
ax = fig.add_axes([0.1, 0.3, 0.8, 0.6])  # Adjusted axes position for the map.

# Setup mercator map projection.
m = Basemap(llcrnrlon=-180., llcrnrlat=-60., urcrnrlon=180., urcrnrlat=85.,
            rsphere=(6378137.00, 6356752.3142),
            resolution='l', projection='merc',
            lat_0=40., lon_0=-20., lat_ts=20.)

# Define latitude and longitude coordinates for Singapore (sglat, sglon) and San Francisco (sflat, sflon).
sglat = 1.35
sglon = 103.82
sflat = 37.77
sflon = -122.42

# Calculate points along the great circle route.
num_points = 1000
lons, lats = m.gcpoints(sglon, sglat, sflon, sflat, num_points)

# Draw great circle route between Singapore and San Francisco.
m.drawgreatcircle(sglon, sglat, sflon, sflat, linewidth=2, color='b')
m.drawcoastlines()
m.fillcontinents()

# Draw parallels and meridians.
m.drawparallels(np.arange(-90, 90, 20), labels=[1, 1, 0, 1])
m.drawmeridians(np.arange(-180, 180, 30), labels=[1, 1, 0, 1])

ax.set_title('Great Circle from Singapore to San Francisco')

# Create a slider for animation.
slider_ax = fig.add_axes([0.25, 0.1, 0.5, 0.03])
slider = Slider(slider_ax, 'Position', 0, 1, valinit=0)

# Initialize the marker.
marker, = ax.plot([], [], marker='o', markersize=8, color='r')


# Function to update marker position based on slider value.
def update_marker(val):
    position = int(val * (num_points - 1))  # Calculate the index along the route.
    lon, lat = lons[position], lats[position]
    marker.set_data([lon], [lat])  # Pass lon and lat as sequences
    fig.canvas.draw_idle()


# Connect the slider to the update function.
slider.on_changed(update_marker)

plt.show()

So generally i want the whole route to be shown in the middle. Thanks a lot for helping.

1

There are 1 best solutions below

1
Jesse Sealand On

Solution:

Offset lon_0 'the map center longitude' to be 180 degrees to the left of the center of your travel path (straight line distance).

In this case you would have to declare your route prior to defining your BaseMap. You can the use the route data to calculate your longitude offset.