Skip to content

opengeos/qgis-map

Repository files navigation

qgis-map

image image

A Python library that provides a high-level, leafmap-like API for working with PyQGIS

Features

  • High-level API: Simple, intuitive methods inspired by leafmap
  • Basemap support: Easy access to OpenStreetMap, Esri, Google, CartoDB, and more
  • Vector layers: Add shapefiles, GeoJSON, GeoPackage, and other vector formats
  • Raster layers: Add GeoTIFFs, COGs, and apply color ramps
  • Point data visualization: Create heatmaps and styled point layers from CSV/DataFrame
  • Data-driven styling: Automatic choropleth maps with classification and legends
  • STAC integration: Load Cloud Optimized GeoTIFFs from STAC catalogs
  • Time slider: Create interactive time-based visualizations with dockable panels
  • Dockable panels: Create custom interactive panels in QGIS
  • Enhanced utilities: Layer opacity control, pattern-based layer search, zoom to GeoDataFrame

Installation

Use pip to install:

pip install qgis-map

Use conda to install:

conda install -c conda-forge qgis-map

Quick Start

Use qgis-map within the QGIS Python console or in a PyQGIS script:

from qgis_map import Map

# Create a map with a basemap
m = Map(basemap="OpenStreetMap")

# Add vector data
m.add_vector("path/to/data.geojson", layer_name="My Data", zoom_to_layer=True)

# Add raster data with a color ramp
m.add_raster("path/to/dem.tif", colormap="terrain", layer_name="Elevation")

Usage Examples

Adding Basemaps

from qgis_map import Map, get_basemap_names

# See all available basemaps
print(get_basemap_names())

# Create map with a basemap
m = Map(basemap="CartoDB.DarkMatter")

# Add additional basemaps
m.add_basemap("Esri.WorldImagery", layer_name="Satellite")
m.add_basemap("HYBRID")  # Google Hybrid

# Add custom XYZ tiles
m.add_basemap(
    "https://custom.tiles.server/{z}/{x}/{y}.png",
    layer_name="Custom Tiles"
)

Adding Vector Layers

from qgis_map import Map

m = Map()

# Add a shapefile
m.add_vector("countries.shp", layer_name="Countries")

# Add GeoJSON with styling
m.add_vector(
    "cities.geojson",
    layer_name="Cities",
    style={
        "color": "#3388ff",
        "stroke_color": "#000000",
        "stroke_width": 1
    },
    zoom_to_layer=True
)

# Add a GeoDataFrame
import geopandas as gpd
gdf = gpd.read_file("data.gpkg")
m.add_gdf(gdf, layer_name="My GeoDataFrame")

Adding Raster Layers

from qgis_map import Map

m = Map()

# Add a GeoTIFF
m.add_raster("elevation.tif", layer_name="DEM")

# Add with color ramp
m.add_raster(
    "temperature.tif",
    colormap="RdYlBu",
    vmin=-10,
    vmax=40,
    layer_name="Temperature"
)

# Add a Cloud Optimized GeoTIFF (COG)
m.add_cog(
    "https://example.com/data.tif",
    layer_name="Remote COG",
    zoom_to_layer=True
)

# Add a WMS layer
m.add_wms(
    url="https://ows.mundialis.de/services/service",
    layers="TOPO-OSM-WMS",
    layer_name="Topo WMS"
)

Time Slider

from qgis_map import Map

m = Map()

# Create a time slider to switch between layers
layers = {
    "2020": "data/ndvi_2020.tif",
    "2021": "data/ndvi_2021.tif",
    "2022": "data/ndvi_2022.tif",
    "2023": "data/ndvi_2023.tif",
}
m.add_time_slider(layers=layers, time_interval=2)

Advanced Features

Point Data Visualization

from qgis_map import Map

m = Map()

# Add points from CSV with data-driven styling
m.add_points_from_xy(
    "cities.csv",
    x="longitude",
    y="latitude",
    color_column="region",
    size_column="population",
    popup_fields=["name", "population", "region"],
    layer_name="Cities"
)

# Create a heatmap from point data
m.add_heatmap(
    "earthquakes.csv",
    latitude="lat",
    longitude="lon",
    value="magnitude",
    radius=30,
    color_ramp="YlOrRd",
    layer_name="Earthquake Heatmap"
)

# Add circle markers with fixed styling
m.add_circle_markers_from_xy(
    "points.csv",
    radius=15,
    color="#ff0000",
    stroke_color="#000000"
)

Data-Driven Styling

from qgis_map import Map

m = Map()

# Add choropleth map with automatic classification
m.add_styled_vector(
    "states.shp",
    column="population",
    scheme="Quantiles",  # or "EqualInterval", "NaturalBreaks", "StandardDeviation"
    k=5,
    color_ramp="YlOrRd",
    legend=True,
    legend_title="Population"
)

STAC Integration

from qgis_map import Map

m = Map()

# Load STAC item as COG layer
m.add_stac_layer(
    url="https://earth-search.aws.element84.com/v1/collections/sentinel-2-l2a/items/S2A_...",
    assets="visual",
    layer_name="Sentinel-2"
)

Enhanced GeoJSON Loading

from qgis_map import Map

m = Map()

# Load from URL
m.add_geojson("https://example.com/data.geojson")

# Load from Python dictionary
geojson_dict = {
    "type": "FeatureCollection",
    "features": [...]
}
m.add_geojson(geojson_dict, layer_name="Custom GeoJSON")

# Load from file
m.add_geojson("local_data.geojson", style={"color": "#ff0000"})

Utility Methods

from qgis_map import Map

m = Map()
m.add_basemap("OpenStreetMap")
m.add_vector("data.geojson", layer_name="My Layer")

# Get layer names
print(m.get_layer_names())

# Find layers by pattern
layers = m.find_layer("My*")  # Supports wildcards

# Zoom to a layer
m.zoom_to_layer("My Layer")

# Zoom to bounds
m.zoom_to_bounds((-122.5, 37.5, -121.5, 38.5))

# Zoom to GeoDataFrame
import geopandas as gpd
gdf = gpd.read_file("data.geojson")
m.zoom_to_gdf(gdf)

# Set center and zoom
m.set_center(lat=37.7749, lon=-122.4194, zoom=12)

# Set layer opacity
m.layer_opacity("My Layer", 0.5)  # 50% transparency

# Remove a layer
m.remove_layer("My Layer")

# Clear all layers (keep basemap)
m.clear_layers(keep_basemap=True)

# Export to image
m.to_image("output.png", width=1920, height=1080)

Creating Custom Dock Widgets

from qgis_map import Map
from PyQt5.QtWidgets import QLabel, QVBoxLayout, QWidget

m = Map()

# Create a custom widget
widget = QWidget()
layout = QVBoxLayout()
layout.addWidget(QLabel("Custom Panel Content"))
widget.setLayout(layout)

# Add as dockable panel
dock = m.create_dock_widget("My Panel", widget, position="right")

Available Basemaps

  • OpenStreetMap: OpenStreetMap, OSM
  • CartoDB: CartoDB.Positron, CartoDB.DarkMatter, CartoDB.Voyager
  • Stadia/Stamen: Stadia.StamenToner, Stadia.StamenTerrain, Stadia.StamenWatercolor
  • Esri: Esri.WorldStreetMap, Esri.WorldImagery, Esri.WorldTopoMap, Esri.WorldTerrain, Esri.NatGeoWorldMap, Esri.OceanBasemap
  • Google: ROADMAP, SATELLITE, TERRAIN, HYBRID
  • Other: OpenTopoMap

API Reference

See the full API documentation for detailed information about all available methods.

Credits

This package was inspired by leafmap and is built on PyQGIS.

About

A Python library that provides a high-level, leafmap-like API for working with PyQGIS

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages