OWSLib Documentation

repository·master·Indexed 19 days ago

https://github.com/geopython/owslib

A Python client library for interacting with Open Geospatial Consortium (OGC) web services. OWSLib provides standardized wrappers for accessing metadata and performing operations across various standards, including WMS, WFS, WCS, CSW, WPS, and WMTS, as well as modern OGC API implementations for Features, Coverages, Maps, Records, and Processes. It also supports metadata standards such as NASA DIF, ISO 19139, and Dublin Core.

Tokens
41.8K
Snippets
130
Records
197
Agent score
65%

What's inside OWSLib

  1. Overview of OWSLib

    master

    OWSLib is a Python package designed for client programming with Open Geospatial Consortium (OGC) web service interface standards. It provides a common API for accessing service metadata and includes wrappers for various OGC interfaces, including:

    • WMS: Web Map Service
    • WFS: Web Feature Service
    • WCS: Web Coverage Service
    • CSW: Catalogue Service for the Web
    • WPS: Web Processing Service
    • WMTS: Web Map Tile Service (Note: some services may be beta quality)
  2. Set proxy environment variables in Linux or Windows

    master

    If you want to configure proxies for your entire shell session, use the following commands depending on your operating system.

    Linux/macOS (Bash)

    Use export to set HTTP_PROXY, HTTPS_PROXY, and ALL_PROXY (which supports socks5 protocols).

    Windows (PowerShell)

    Use the $env: prefix to set environment variables.

    For more advanced proxy configurations, refer to the requests library documentation.

    # Linux (Bash)
    $ export HTTP_PROXY="http://10.10.1.10:3128"
    $ export HTTPS_PROXY="http://10.10.1.10:1080"
    $ export ALL_PROXY="socks5://10.10.1.10:3434"
    
    # Windows (PowerShell)
    $env:HTTP_PROXY = "http://10.10.1.10:3128"
    $env:HTTPS_PROXY = "http://10.10.1.10:1080"
    $env:ALL_PROXY = "socks5://10.10.1.10:3434"
  3. Apply OGC filters to WFS requests

    master

    To perform filtered queries in WFS, you must construct an XML filter string.

    For WFS 1.1.0 (FE 1.1), use owslib.fes to build the filter and owslib.etree to convert it to an XML string.

    For WFS 2.0.0 (FE 2.0), use owslib.fes2 to build the filter and owslib.etree to convert it to an XML string.

    # Example for WFS 2.0 using fes2
    from owslib.fes2 import *
    from owslib.etree import etree
    from owslib.wfs import WebFeatureService
    
    wfs11 = WebFeatureService(url='http://geoserv.weichand.de:8080/geoserver/wfs', version='2.0.0')
    
    filter_obj = Filter(
        PropertyIsLike(propertyname='bez_gem', literal='Ingolstadt', wildCard='*')
    )
    filterxml = etree.tostring(filter_obj.toXML()).decode("utf-8")
    
    response = wfs11.getfeature(typename='bvv:gmd_ex', filter=filterxml)
  4. Run tests and linting in OWSLib

    master

    After setting up the development environment, you can verify the codebase using pytest for the test suite and flake8 for linting.

    • Run tests: Use python3 -m pytest to execute the test suite.
    • Run linting: Use flake8 owslib/ to check for style violations in the source directory.
    # Run the test suite
    python3 -m pytest
    
    # Run linting
    flake8 owslib/
  5. Set up a development environment for OWSLib

    master

    To develop OWSLib, create a virtual environment, clone the repository, and install the package in editable mode along with the development dependencies.

    1. Create and activate a virtual environment.
    2. Clone the OWSLib repository.
    3. Install the package with the [dev] extra using pip install -e ".[dev]" to ensure all testing and linting tools are available.
    python3 -m venv owslibenv
    source owslibenv/bin/activate
    
    git clone https://github.com/geopython/OWSLib.git
    cd OWSLib
    
    pip install -e ".[dev]"
  6. Use WebFeatureService (WFS) to fetch vector data

    master

    Use the WebFeatureService class to connect to a WFS endpoint. You can inspect available FeatureTypes via .contents and retrieve their schemas using .get_schema(typename).

    To download features, use .getfeature() with the following parameters:

    • typename: The name of the feature type.
    • bbox: Bounding box coordinates.
    • srsname: The spatial reference system.
    • outputFormat: The desired format (e.g., 'application/json', 'GML2').
    • maxfeatures: Limit the number of features returned.
    • startindex: For pagination.
    • filter: An XML string representing an OGC filter.
    • storedQueryID: (WFS 2.0 only) The ID of a stored query.
    • storedQueryParams: (WFS 2.0 only) A dictionary of parameters for the stored query.
    from owslib.wfs import WebFeatureService
    
    # Connect to WFS 2.0
    wfs20 = WebFeatureService(url='https://services.rce.geovoorziening.nl/dijken/wfs', version='2.0.0')
    
    # Get schema for a layer
    schema = wfs20.get_schema('dijken:dijklijnenkaart_rce')
    
    # Download features as JSON
    response = wfs20.getfeature(typename='dijken:dijklijnenkaart_rce', 
                                bbox=(173700,440400,178700,441400), 
                                srsname='EPSG:28992', 
                                outputFormat='application/json')
    print(response.read())
  7. Configure OWSLib logging

    master

    OWSLib uses standard Python module-based named loggers. You can capture and configure these logs in your application using the logging module. This is useful for debugging service interactions.

    import logging
    
    # Get a logger for your current module
    LOGGER = logging.getLogger(__name__)
    
    # Configure a handler (e.g., to console)
    ch = logging.StreamHandler()
    ch.setLevel(logging.DEBUG)
    ch.setFormatter(logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s'))
    
    # Add the handler and set the level
    LOGGER.addHandler(ch)
    LOGGER.setLevel(logging.DEBUG)
  8. Explore OWSLib examples via Jupyter Notebooks

    master

    You can interactively explore OWSLib functionality using Jupyter Notebooks. These notebooks are available online through Binder or can be viewed via NBViewer. The examples cover various service types, such as WMS (Web Map Service).

    https://mybinder.org/v2/gh/geopython/OWSLib.git/master?filepath=docs/source/notebooks
  9. Use WebMapService (WMS) to inspect and request imagery

    master

    Use the WebMapService class to connect to a WMS endpoint. You can inspect service metadata (title, version, abstract), list available layers via .contents, and retrieve specific layer details like bounding boxes and supported CRS options. To request imagery, use the .getmap() method specifying the layers, size, bounding box (bbox), spatial reference system (srs), and output format.

    from owslib.wms import WebMapService
    
    # Connect to service
    wms = WebMapService('https://mesonet.agron.iastate.edu/cgi-bin/mapserv/mapserv?map=/opt/iem/data/wms/goes/west_ir.map&SERVICE=WMS&REQUEST=GetCapabilities')
    
    # Inspect metadata
    print(wms.identification.title)
    print(list(wms.contents))
    
    # Request an image
    img = wms.getmap(layers=['goes_west_ir'],
                     size=(300, 250),
                     bbox=(-126, 24, -66, 50),
                     srs='EPSG:4326',
                     format='image/png')
    
    with open('iem_goes_ir.png', 'wb') as fh:
        fh.write(img.read())