CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/pypi-leafmap

A Python package for geospatial analysis and interactive mapping in a Jupyter environment.

54

0.80x

Evaluation54%

0.80x

Agent success when using this tile

Overview
Eval results
Files

interactive-maps.mddocs/

Interactive Maps

Comprehensive interactive mapping functionality with multiple backend support. Leafmap automatically selects the appropriate backend based on the runtime environment and provides a consistent API across all backends.

Capabilities

Map Creation and Configuration

Create interactive maps with customizable center, zoom, and basemap settings. The Map class provides the foundation for all interactive mapping functionality.

class Map:
    def __init__(self, center=[20, 0], zoom=2, basemap='HYBRID', **kwargs):
        """
        Create an interactive map.
        
        Args:
            center (list): Map center as [latitude, longitude]
            zoom (int): Initial zoom level (1-20)
            basemap (str): Base map provider name
            **kwargs: Backend-specific options
        """
    
    def add_basemap(self, basemap='HYBRID', show=True, **kwargs):
        """
        Add a basemap to the map.
        
        Args:
            basemap (str): Basemap provider name or custom definition
            show (bool): Whether to show the basemap immediately
            **kwargs: Basemap-specific options
        """
    
    def add_layer_control(self, position='topright', **kwargs):
        """
        Add a layer control widget to toggle layers.
        
        Args:
            position (str): Control position ('topright', 'topleft', 'bottomright', 'bottomleft')
            **kwargs: Control-specific options
        """
    
    def add_fullscreen_control(self, **kwargs):
        """
        Add a fullscreen control button.
        
        Args:
            **kwargs: Control-specific options
        """
    
    def set_center(self, lon, lat, zoom=None):
        """
        Center map at specific coordinates.
        
        Args:
            lon (float): Longitude coordinate
            lat (float): Latitude coordinate  
            zoom (int): Optional zoom level
        """
    
    def zoom_to_bounds(self, bounds):
        """
        Zoom map to fit bounding box.
        
        Args:
            bounds (list): Bounding box [minx, miny, maxx, maxy]
        """
    
    def find_layer(self, name):
        """
        Find layer by name.
        
        Args:
            name (str): Layer name to search for
            
        Returns:
            Layer object or None if not found
        """
    
    def get_layer_names(self):
        """
        Get list of all layer names.
        
        Returns:
            list: Layer names
        """

Layer Management

Add and manage various layer types including tiles, WMS, WMTS, vector tiles, and PMTiles with comprehensive styling and interaction options.

def add_layer(self, layer, **kwargs):
    """
    Add a generic layer to the map.
    
    Args:
        layer: Layer object (backend-specific)
        **kwargs: Layer-specific options
    """

def add_tile_layer(self, url, name='Untitled', attribution='', **kwargs):
    """
    Add an XYZ tile layer.
    
    Args:
        url (str): Tile URL template with {x}, {y}, {z} placeholders
        name (str): Layer name for display
        attribution (str): Attribution text
        **kwargs: Tile layer options (opacity, etc.)
    """

def add_wms_layer(self, url, layers, name='Untitled', **kwargs):
    """
    Add a WMS layer.
    
    Args:
        url (str): WMS service URL
        layers (str): Comma-separated layer names
        name (str): Layer name for display
        **kwargs: WMS-specific options (format, transparent, etc.)
    """

def add_wmts_layer(self, url, layer, **kwargs):
    """
    Add a WMTS layer.
    
    Args:
        url (str): WMTS service URL
        layer (str): Layer identifier
        **kwargs: WMTS-specific options
    """

def add_vector_tile(self, url, **kwargs):
    """
    Add a vector tile layer.
    
    Args:
        url (str): Vector tile URL template
        **kwargs: Vector tile styling options
    """

def add_pmtiles(self, url, **kwargs):
    """
    Add a PMTiles layer.
    
    Args:
        url (str): PMTiles file URL
        **kwargs: PMTiles-specific options
    """

Interactive Features

Add interactive elements including markers, drawing tools, measurement tools, and custom controls for enhanced user interaction.

def add_markers(self, markers, **kwargs):
    """
    Add point markers to the map.
    
    Args:
        markers (list): List of marker coordinates [[lat, lon], ...]
        **kwargs: Marker styling options (color, size, popup, etc.)
    """

def add_circle_markers_from_xy(self, data, x, y, **kwargs):
    """
    Add circle markers from coordinate data.
    
    Args:
        data: DataFrame or similar with coordinate columns
        x (str): Column name for longitude/x coordinates
        y (str): Column name for latitude/y coordinates
        **kwargs: Circle marker options (radius, color, etc.)
    """

def add_draw_control(self, **kwargs):
    """
    Add drawing tools for interactive geometry creation.
    
    Args:
        **kwargs: Drawing control options (draw_options, edit_options, etc.)
    """

def add_measure_control(self, **kwargs):
    """
    Add measurement tools for distance and area calculation.
    
    Args:
        **kwargs: Measurement control options
    """

def add_toolbar(self, **kwargs):
    """
    Add a custom toolbar with various tools.
    
    Args:
        **kwargs: Toolbar configuration options
    """

Map Comparison and Split Views

Create side-by-side map comparisons and split views for data comparison and analysis.

def split_map(self, left_layer=None, right_layer=None, **kwargs):
    """
    Create a split-screen map comparison.
    
    Args:
        left_layer: Layer for left side of comparison
        right_layer: Layer for right side of comparison
        **kwargs: Split map options (add_close_button, etc.)
    """

def linked_maps(self, rows=2, cols=2, **kwargs):
    """
    Create linked maps that pan and zoom together.
    
    Args:
        rows (int): Number of map rows
        cols (int): Number of map columns
        **kwargs: Linked map options
    """

Export and Output

Export maps to various formats including HTML, images, and integration with web frameworks.

def to_html(self, filename=None, **kwargs):
    """
    Export map to HTML file.
    
    Args:
        filename (str): Output HTML file path
        **kwargs: Export options (title, width, height, etc.)
        
    Returns:
        str: HTML content if filename is None
    """

def to_image(self, filename=None, **kwargs):
    """
    Export map to image file.
    
    Args:
        filename (str): Output image file path
        **kwargs: Image export options (width, height, dpi, etc.)
        
    Returns:
        bytes: Image data if filename is None
    """

def to_streamlit(self, **kwargs):
    """
    Convert map for Streamlit display.
    
    Args:
        **kwargs: Streamlit-specific options
        
    Returns:
        streamlit component
    """

def save_draw_features(self, filename, **kwargs):
    """
    Save drawn features to file.
    
    Args:
        filename (str): Output file path
        **kwargs: Save options (format, crs, etc.)
    """

Backend-Specific Classes

IPyleaflet Backend (leafmap.leafmap.Map)

The default backend for standard Jupyter environments, providing full-featured interactive mapping with extensive widget support.

import leafmap.leafmap as leafmap

# Full API with 222 methods including:
# - Complete layer management
# - Interactive widgets and controls  
# - Advanced analysis tools
# - Export capabilities

Folium Backend (leafmap.foliumap.Map)

Optimized for restricted environments like Google Colab and marimo, providing core mapping functionality with HTML-based output.

import leafmap.foliumap as leafmap

# 149 methods with focus on:
# - Core mapping functionality
# - HTML export compatibility
# - Colab/marimo optimization

MapLibre GL JS Backend (leafmap.maplibregl.Map)

Advanced WebGL-based mapping with 3D capabilities and high-performance rendering.

import leafmap.maplibregl as leafmap

# 179 methods including:
# - 3D terrain visualization
# - WebGL acceleration
# - Advanced styling with JSON
# - Custom UI widgets

Plotly Backend (leafmap.plotlymap.Map)

Statistical mapping and dashboard creation with interactive plotting capabilities.

import leafmap.plotlymap as leafmap

# 50+ methods focused on:
# - Statistical visualization
# - Dashboard integration
# - Interactive plotting

Other Backends

Additional specialized backends for specific use cases:

import leafmap.deckgl as leafmap      # GPU-accelerated visualization
import leafmap.bokehmap as leafmap    # Server-based applications  
import leafmap.kepler as leafmap      # Advanced geospatial visualization
import leafmap.heremap as leafmap     # HERE Maps integration
import leafmap.mapbox as leafmap      # Custom Mapbox GL JS

Usage Examples

Basic Map Creation

import leafmap

# Create a basic map
m = leafmap.Map(center=[37.7749, -122.4194], zoom=10)

# Add basemap
m.add_basemap('SATELLITE')

# Add layer control
m.add_layer_control()

# Display
m

Backend-Specific Usage

# Force specific backend
import leafmap.maplibregl as leafmap

# Create 3D map
m = leafmap.Map(
    center=[37.7749, -122.4194], 
    zoom=10,
    pitch=60,
    bearing=45
)

# Add 3D terrain
m.add_3d_terrain()

Environment Detection

Leafmap automatically selects backends based on environment:

  • Standard Jupyter: ipyleaflet backend (leafmap.leafmap)
  • Google Colab: folium backend (leafmap.foliumap)
  • Marimo: folium backend (leafmap.foliumap)
  • Documentation: folium backend when USE_MKDOCS is set

Install with Tessl CLI

npx tessl i tessl/pypi-leafmap

docs

basemaps.md

cloud-data.md

data-visualization.md

file-io.md

geospatial-analysis.md

index.md

interactive-maps.md

osm-integration.md

statistical-plotting.md

tile.json