From 926bb5e94172ca72841ae0db1b20f388bd44e300 Mon Sep 17 00:00:00 2001 From: Matthias Schaub Date: Wed, 21 Aug 2024 17:56:00 +0200 Subject: [PATCH] feat: add fallback attribution if esri api key is unset --- config/sample.config.toml | 1 - docs/configuration.md | 28 +++++++++++++++------------- sketch_map_tool/definitions.py | 25 ++++++++++++++++--------- tests/unit/test_definitions.py | 26 ++++++++++++++++++++------ 4 files changed, 51 insertions(+), 29 deletions(-) diff --git a/config/sample.config.toml b/config/sample.config.toml index 71d60a94..4b69d39d 100644 --- a/config/sample.config.toml +++ b/config/sample.config.toml @@ -1,6 +1,5 @@ # required configuration variables neptune_api_token = "h0dHBzOi8aHR06E0Z...jMifQ" -esri-api-key = "" # required configuration variables for docker compose setup # broker-url = "redis://redis:6379" # result-backend = "db+postgresql://smt:smt@postgres:5432" diff --git a/docs/configuration.md b/docs/configuration.md index 05c65235..23069273 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -15,20 +15,14 @@ To create a new configuration file simply copy the sample configuration file and cp config/sample.config.toml config/config.toml ``` +## Default Configuration + +For a list of all configuration variables and their default values please take a look at [config.py](sketch_map_tool/config.py). + ## Required Configuration All lot of configuration values come with defaults. Required configuration values are: - `neptune_api_token` -- `esri-api-key` - -### ArcGIS ESRI - -To get an ArcGIS/ESRI API key sign-up for [ArcGIS Location Platform](https://location.arcgis.com/sign-up/) -and follow [this tutorial](https://developers.arcgis.com/documentation/security-and-authentication/api-key-authentication/tutorials/create-an-api-key/). - -Notes: -1. During registration enter your username into the "Your portal URL" and "Your portal display name" fields (not `heigit`). -2. During API key generation keep the referrer field empty. ### neptune.ai @@ -36,7 +30,6 @@ Ask the team to get an invite the Sketch Map Tool project on neptuine.ai. To get the API key go to "Project Metadata" and copy the key from the example code. - ## Configuration for Docker Compose For running the services using Docker Compose set broker URL and result backend to: @@ -46,6 +39,15 @@ broker-url = "redis://redis:6379" result-backend = "db+postgresql://smt:smt@postgres:5432" ``` -## Default Configuration +## Misc -For a list of all configuration variables and their default values please take a look at [config.py](sketch_map_tool/config.py). +### ArcGIS/ESRI API Key + +To retrieve up-to-date attribution an ArcGIS/ESRI API key is needed. +For local development you do not need one. +To get an ArcGIS/ESRI API key sign-up for [ArcGIS Location Platform](https://location.arcgis.com/sign-up/) +and follow [this tutorial](https://developers.arcgis.com/documentation/security-and-authentication/api-key-authentication/tutorials/create-an-api-key/). + +Notes: +1. During registration enter your username into the "Your portal URL" and "Your portal display name" fields (not `heigit`). +2. During API key generation keep the referrer field empty. diff --git a/sketch_map_tool/definitions.py b/sketch_map_tool/definitions.py index 62212ba5..63ac06c9 100644 --- a/sketch_map_tool/definitions.py +++ b/sketch_map_tool/definitions.py @@ -34,17 +34,24 @@ def get_attribution(layer: Layer) -> str: url = ( "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/ArcGIS:Imagery" ) - params = {"type": "style", "token": get_config_value("esri-api-key")} - response = requests.get(url, params, timeout=10) - result = response.json() - sources = result["sources"] - if len(sources) != 2: + token = get_config_value("esri-api-key") + if token == "": + sources = "Esri, Maxar, Earthstar Geographics, and the GIS User Community" logging.warning( - "Attribution retrieved from ESRI API has unexpected format." + "No ESRI API key configured. " + + " To retrieve up-to-date attribution from ESRI please add one." ) - sources.pop("esri", None) - attribution = "Powered by Esri
" + list(sources.values())[0]["attribution"] - return attribution + else: + params = {"type": "style", "token": token} + response = requests.get(url, params, timeout=10) + result = response.json() + result["sources"].pop("esri", None) + sources = list(result["sources"].values())[0]["attribution"] + if len(sources) != 2: + logging.warning( + "Attribution retrieved from ESRI API has unexpected format." + ) + return "Powered by Esri
" + sources else: return "Powered by OpenStreetMap
©openstreetmap.org/copyright" diff --git a/tests/unit/test_definitions.py b/tests/unit/test_definitions.py index 5d9539c1..02bdaf54 100644 --- a/tests/unit/test_definitions.py +++ b/tests/unit/test_definitions.py @@ -1,4 +1,5 @@ from sketch_map_tool import definitions +from sketch_map_tool.models import Layer def test_get_literatur_references(): @@ -11,10 +12,23 @@ def test_get_literatur_references(): def test_get_attribution(layer): # It is possible that the attribution text retrieved from the ESRI API changes result = definitions.get_attribution(layer) - assert result in ( - ( - "Powered by Esri
Source: Esri, Maxar, GeoEye, Earthstar Geographics, " - + "CNES/Airbus DS, USDA, USGS, AeroGRID, IGN, and the GIS User Community" - ), - ("Powered by OpenStreetMap
©openstreetmap.org/copyright"), + if layer == "osm": + assert result == "Powered by OpenStreetMap
©openstreetmap.org/copyright" + if layer == "esri-world-imagery": + assert result == ( + "Powered by Esri
Source: Esri, Maxar, GeoEye, Earthstar " + + "Geographics, CNES/Airbus DS, USDA, USGS, AeroGRID, IGN, and the GIS " + + "User Community" + ) + + +def test_get_attribution_no_esri_esri_api_key(monkeypatch): + monkeypatch.setattr( + "sketch_map_tool.definitions.get_config_value", + lambda _: "", + ) + result = definitions.get_attribution(Layer("esri-world-imagery")) + assert result == ( + "Powered by Esri
Esri, Maxar, Earthstar Geographics, and the GIS User " + + "Community" )