Drawing Map with Circles Using Basemap

  • Share this:

Code introduction


This function uses the Basemap library to draw a map within a specified latitude and longitude range and draws two circles centered at specified coordinates on the map.


Technology Stack : Basemap, NumPy, Matplotlib

Code Type : Drawing function

Code Difficulty : Intermediate


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

def draw_map_with_circles(lat1, lon1, lat2, lon2):
    """
    This function draws a map with two circles centered at specified coordinates.
    """
    m = Basemap(projection='merc', llcrnrlat=lat1, urcrnrlat=lat2, llcrnrlon=lon1, urcrnrlon=lon2, lat_ts=20, resolution='c')
    m.drawcoastlines()
    m.drawcountries()
    m.drawmapboundary(fill_color='aqua')
    m.fillcontinents(color='coral', lake_color='aqua')

    # Draw two circles
    x, y = m(lon1, lat1)
    m.plot(x, y, 'ro', markersize=5)  # Plot first circle center
    m.plot(x, y, 'ro', markersize=5)  # Plot second circle center

    radius = 500000  # Radius in meters
    theta = np.linspace(0, 2*np.pi, 100)
    for t in theta:
        x, y = m(lon1 + radius * np.cos(t), lat1 + radius * np.sin(t))
        m.plot(x, y, 'b', linewidth=2)  # Plot circle path

    plt.show()

# Example usage:
# draw_map_with_circles(-23.55, -46.63, -23.55, -46.63)