GeoViews

repository·main·Indexed 20 days ago

https://github.com/holoviz/geoviews

A Python library for geographic visualizations built on HoloViews and Cartopy. It enables plotting of multidimensional meteorological, oceanographic, and other geographic datasets using Matplotlib or Bokeh backends. GeoViews introduces GeoElement plot types—such as Feature, WMTS, Tiles, Points, and VectorField—which are specialized HoloViews Elements with geographic projections based on cartopy.crs.

Tokens
36.4K
Snippets
133
Records
143
Agent score
70%

What's inside GeoViews

  1. What is GeoViews?

    main

    GeoViews is a Python library designed for exploring and visualizing geographic data. It is built on top of HoloViews and provides geographic plot types based on the Cartopy library.

    GeoViews supports plotting with either Matplotlib or Bokeh backends. It introduces a family of GeoElement plot types, which are specialized HoloViews Elements with associated geographic projections based on cartopy.crs.

    Key GeoElement types include:

    • Feature
    • WMTS
    • Tiles
    • Points
    • Contours
    • Image
    • QuadMesh
    • TriMesh
    • RGB
    • HSV
    • Labels
    • Graph
    • HexTiles
    • VectorField
    • Text

    These elements can be overlaid using HoloViews expressions, for example: gv.Image(temperature) * gv.Feature(cartopy.feature.COASTLINE).

  2. Explore the GeoViews API structure

    main

    GeoViews is organized into several subpackages that handle different aspects of geographic visualization. Use these subpackages to find specific functionality:

    • geoviews.annotators: Helper functions and classes to annotate visual elements.
    • geoviews.data: Data interface classes enabling GeoViews to work with various data types.
    • geoviews.element: Core elements that form the basis of geographic visualizations.
    • geoviews.links: Tools for linking different elements and streams.
    • geoviews.models: Custom models extending GeoViews' capabilities.
    • geoviews.operation: Operations applied to transform existing elements or data structures.
    • geoviews.plotting: Base plotting classes and utilities.
    • geoviews.plotting.bokeh: Bokeh-specific plotting classes and utilities.
    • geoviews.plotting.matplotlib: Matplotlib-specific plotting classes and utilities.
    • geoviews.streams: Stream classes providing interactivity for dynamic maps.
    • geoviews.util: High-level utilities supporting GeoViews functionality.
  3. Run GeoViews examples

    main

    You can quickly download and run the built-in GeoViews examples using the geoviews CLI command. This command copies the examples and fetches the necessary data.

    geoviews examples
    cd geoviews-examples
    jupyter notebook
  4. How to learn and explore GeoViews

    main

    To effectively use GeoViews, follow this recommended learning path:

    1. Start with the User Guide: Read the narrative documentation to understand high-level concepts and workflows.
    2. Use Interactive Help: Utilize IPython or Jupyter Notebook online help features. GeoViews components are designed to support tab-completion and provide inline help, which is often sufficient for understanding specific method signatures and attributes during active development.
  5. Build GeoViews documentation and packages

    main

    Use the following tasks to build documentation or distribution packages:

    • Documentation: Builds the full documentation site. Note that because much of the documentation uses notebooks, this can take approximately one hour.
    • Packages: Builds distribution files for Pip, Conda, and NPM.
    # Build documentation
    pixi run docs-build
    
    # Build distribution packages
    pixi run build-pip
    pixi run build-conda
    pixi run build-npm
  6. Set up a GeoViews development environment with Pixi

    main

    GeoViews uses Pixi to manage its complex set of dependencies and environments. To develop on GeoViews, you should first clone your fork of the repository and then use Pixi to manage tasks and environments.

    Core Pixi Concepts

    • Tasks: Executable commands run via pixi run <task-name>.
    • Environments: Virtual environments containing specific package sets. Use the -e or --environment flag to target a specific environment (e.g., pixi run -e test-ui <task>).
    • Lock-file: pixi.lock contains the environment definitions.

    Note: The .pixi directory created in your source folder can be large; avoid placing your source code in cloud-synced directories (like Dropbox or OneDrive).

    # Install dependencies and create environments
    pixi install
    
    # Download required data for tests and examples
    pixi run -e download-data download-data
  7. Cache WMTS tiles for offline use

    main

    To use Web Map Tile Services (WMTS) offline, you must download the tiles from the server and store them locally. Using Cartopy utilities, you can retrieve tiles and store them in a NumPy binary format (.npy).

    Warning: When caching high zoom levels, always specify x_bounds and y_bounds for your specific region of interest. Attempting to cache global extents at high zoom levels can lead to rate limiting or being banned from the tile provider due to the exponential increase in requests.

    Example zoom level tile counts for global extents:

    • z=0: 1 tile
    • z=5: 1,024 tiles
    • z=10: 1,048,576 tiles
    • z=15: 1,073,741,824 tiles
    from pathlib import Path
    import cartopy.crs as ccrs
    import cartopy.io.img_tiles as cimgt
    import numpy as np
    from PIL import Image
    from shapely import box
    
    def cache_tiles(
        tile_source,
        max_target_z=1,
        x_bounds=(-180, 180),
        y_bounds=(-90, 90),
        cache_dir="tiles",
    ):
        """Caches map tiles within specified bounds from a given tile source."""
        if not isinstance(tile_source, cimgt.GoogleWTS):
            tile_source = getattr(cimgt, tile_source)
        tiles = tile_source(cache=cache_dir)
    
        bbox = ccrs.GOOGLE_MERCATOR.transform_points(
            ccrs.PlateCarree(), x=np.array(x_bounds), y=np.array(y_bounds)
        )[:, :-1].flatten()  # drop Z, then convert to x0, y0, x1, y1
        target_domain = box(*bbox)
    
        for target_z in range(max_target_z):
            tiles.image_for_domain(target_domain, target_z)
        return Path(cache_dir) / tile_source.__name__
    
    # Example: Cache OpenStreetMaps tiles up to zoom level 6
    cache_dir = cache_tiles("OSM", max_target_z=6)
  8. Save GeoViews plots for offline viewing

    main

    When working on a machine that is completely offline, you must use resources=INLINE when saving Bokeh-based GeoViews outputs. If you do not set this, the resulting HTML file will appear empty because it will attempt to load Bokeh resources from the internet.

    import geoviews as gv
    from bokeh.resources import INLINE
    
    gv.extension("bokeh")
    
    coastline = gv.feature.coastline()
    borders = gv.feature.borders()
    world = (coastline * borders).opts(global_extent=True)
    
    # Use resources=INLINE to ensure the HTML is self-contained
    gv.save(world, "world.html", resources=INLINE)
  9. Install GeoViews in editable mode

    main

    To make development easier, you can install GeoViews in editable mode. This allows changes to the source code to be reflected immediately without re-installing.

    Important: You must run this command for each environment you intend to use. For example, to install it into the test-ui environment, use the -e flag.

    # Install in the default environment
    pixi run install
    
    # Install in a specific environment (e.g., test-ui)
    pixi run -e test-ui install
  10. Install GeoViews via Conda

    main

    To install the full GeoViews package and all its dependencies, use the pyviz channel. It is recommended to update your environment first to ensure compatibility.

    conda update --all
    conda install -c pyviz geoviews

    If you only need the minimal dependencies required to run GeoViews, you can install geoviews-core instead:

    conda install -c pyviz geoviews-core

    To access the latest development release, use the dev label:

    conda install -c pyviz/label/dev geoviews
    conda install -c pyviz geoviews
  11. Install GeoViews with additional conda-forge dependencies

    main

    If you need to use libraries like Iris or xesmf alongside GeoViews, create a new environment including both the pyviz and conda-forge channels:

    conda create -n env-name -c pyviz -c conda-forge geoviews iris xesmf
    conda activate env-name