xyzservices

repository·main·Indexed 42 days ago

https://github.com/geopandas/xyzservices

A lightweight Python library providing a repository of XYZ tile services for raster basemaps. It includes a Python API for accessing tile providers via the TileProvider and Bunch classes, as well as a standalone JSON database for use in non-Python environments. Features include tools for building tile URLs, managing API tokens for providers like Mapbox and Stadia Maps, and loading providers from Quick Map Services (QMS).

Tokens
4.2K
Snippets
22
Records
25
Agent score
53%

What's inside xyzservices

  1. Use the xyzservices Gallery to preview basemaps

    main

    The xyzservices Gallery allows you to visually browse and preview the available XYZ tile providers.

    Important Considerations:

    • API Keys: Some providers require an API key. You must provide your own key and use the validate button in the gallery interface to load the tiles.
    • Domain Whitelisting: Certain providers (such as Stadia) may require you to whitelist your domain in their respective service settings before tiles will load correctly.
  2. What is a TileProvider object?

    main

    A TileProvider object is a dictionary-like object (with attribute access) that contains the configuration for an XYZ tile service.

    Key characteristics:

    • Required fields: It must contain a url string with {z}/{x}/{y} placeholders.
    • Optional metadata: It may include attribution (text for plots), max_zoom (maximum available zoom level), name, and html_attribution.
    • Hierarchical structure: Many providers are organized into groups. For example, xyz.OpenStreetMap is a group, and xyz.OpenStreetMap.Mapnik is a specific TileProvider instance.
    • Status check: If a provider is non-functional, it may have a status attribute set to "broken".
    import xyzservices.providers as xyz
    
    # Accessing a specific provider
    provider = xyz.OpenStreetMap.Mapnik
    print(type(provider))  # <class 'xyzservices.providers.TileProvider'>
    print(provider['url'])
  3. Use Stadia Maps with an API key

    main

    Stadia Maps can be used by whitelisting your domain in your account settings. Alternatively, you can use an API token by manually appending an api_key placeholder to the provider's URL.

    provider = xyz.Stadia.AlidadeSmooth(api_key="<insert api_key here>")
    provider["url"] = provider["url"] + "?api_key={api_key}"  # adding API key placeholder
  4. Use HERE and HEREv3 tiles with an API key

    main

    For HEREv3 layers, register at the HERE developer portal to obtain an apiKey. You can provide the key in two ways:

    1. Modify the existing object: Overriding the attribute will alter the existing object in the xyz module.
    2. Call the object: Calling the object as a function returns a copy with the key applied, leaving the original object unchanged.

    For legacy projects, you can use app_id and app_code instead.

    # Overriding the attribute will alter the existing object
    xyz.HEREv3.terrainDay["apiKey"] = "my-private-api-key"
    
    # Calling the object will return a copy
    xyz.HEREv3.terrainDay(apiKey="my-private-api-key")
    
    # Legacy projects
    xyz.HERE.terrainDay(app_id="my-private-app-id", app_code="my-app-code")
  5. Use the TileProvider class

    main

    The TileProvider class is the primary interface for interacting with XYZ tile providers. It provides methods to construct tile URLs and manage authentication requirements.

    Key methods:

    • build_url(...): Generates a tile URL based on the provider's template.
    • requires_token(): Indicates if the provider requires an access token.
    • from_qms(...): Creates a provider from QMS (QuickMapServices) data.
    from xyzservices import TileProvider
    # Usage depends on specific implementation details of build_url and from_qms
  6. Use the Bunch class for provider collections

    main

    The Bunch class is used to manage collections of providers (provider bunches). It provides utility methods for navigating and filtering provider data.

    Key methods:

    • filter(...): Filters the collection of providers.
    • flatten(): Flattens the provider structure.
    • query_name(...): Queries providers by name.
  7. Access XYZ tile providers via the Python API

    main

    The xyzservices.providers module (commonly imported as xyz) acts as a Bunch (an enhanced dictionary) containing all available XYZ tile sources.

    In interactive environments like Jupyter, xyz provides a collapsible inventory of providers. You can explore available providers using xyz.keys().

    Accessing a specific provider returns a TileProvider object containing metadata such as the tile URL and attribution.

    import xyzservices.providers as xyz
    
    # Access provider details
    print(xyz.OpenStreetMap.Mapnik.url)
    print(xyz.OpenStreetMap.Mapnik.attribution)
  8. Use Jawg Maps with an access token

    main

    Register at Jawg.io to obtain an access token. You can access default maps or your own customized maps by specifying the accessToken and an optional variant (the map ID).

    xyz.Jawg.Streets(
        accessToken="<insert access token here>",
        variant="<insert map id here or blank for default variant>"
    )