Drawing Circles on Maps with Basemap Library

  • Share this:

Code introduction


This function uses the Basemap library to draw circles on a map centered at a specific latitude and longitude with a given radius.


Technology Stack : Basemap, Numpy

Code Type : Function

Code Difficulty : Intermediate


                
                    
import numpy as np
from mpl_toolkits.basemap import Basemap

def draw_map_with_circles(lat, lon, radius, color='b'):
    """
    This function creates a map with circles drawn on it using Basemap library.
    :param lat: Latitude center of the circle
    :param lon: Longitude center of the circle
    :param radius: Radius of the circle in kilometers
    :param color: Color of the circle
    """
    # Create a map object
    m = Basemap(projection='merc', llcrnrlat=-60, urcrnrlat=90, llcrnrlon=-180, urcrnrlon=180, lat_ts=20, resolution='c')
    m.drawcoastlines()
    m.fillcontinents(color='coral', lake_color='aqua')
    m.drawmapboundary(fill_color='aqua')

    # Draw a circle on the map
    m.drawcircle(lon, lat, radius/111.32, color=color, linewidth=1)

# Example usage:
# draw_map_with_circles(lat=45.0, lon=-73.0, radius=1000, color='r')                
              
Tags: