Skip to content

Commit

Permalink
feat: add fallback attribution if esri api key is unset
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasschaub committed Aug 21, 2024
1 parent b66f82e commit b0d861a
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 29 deletions.
1 change: 0 additions & 1 deletion config/sample.config.toml
Original file line number Diff line number Diff line change
@@ -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"
26 changes: 13 additions & 13 deletions docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,32 @@ 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
### neptune.ai

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.

## 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.

### neptune.ai

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:
Expand All @@ -45,7 +49,3 @@ For running the services using Docker Compose set broker URL and result backend
broker-url = "redis://redis:6379"
result-backend = "db+postgresql://smt:smt@postgres:5432"
```

## 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).
25 changes: 16 additions & 9 deletions sketch_map_tool/definitions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<br />" + 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<br />" + sources
else:
return "Powered by OpenStreetMap<br />©openstreetmap.org/copyright"

Expand Down
26 changes: 20 additions & 6 deletions tests/unit/test_definitions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from sketch_map_tool import definitions
from sketch_map_tool.models import Layer


def test_get_literatur_references():
Expand All @@ -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<br />Source: Esri, Maxar, GeoEye, Earthstar Geographics, "
+ "CNES/Airbus DS, USDA, USGS, AeroGRID, IGN, and the GIS User Community"
),
("Powered by OpenStreetMap<br />©openstreetmap.org/copyright"),
if layer == "osm":
assert result == "Powered by OpenStreetMap<br />©openstreetmap.org/copyright"
if layer == "esri-world-imagery":
assert result == (
"Powered by Esri<br />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<br />Esri, Maxar, Earthstar Geographics, and the GIS User "
+ "Community"
)

0 comments on commit b0d861a

Please sign in to comment.