2023-11-01 20:54:19 +00:00
|
|
|
"""Nominatim."""
|
2021-07-08 13:33:32 +01:00
|
|
|
|
|
|
|
import json
|
2023-11-01 20:54:19 +00:00
|
|
|
import typing
|
|
|
|
from collections import OrderedDict
|
|
|
|
|
2021-07-08 13:33:32 +01:00
|
|
|
import requests
|
|
|
|
|
2023-11-01 20:54:19 +00:00
|
|
|
from . import CallParams
|
|
|
|
|
|
|
|
Hit = dict[str, typing.Any]
|
|
|
|
|
2021-07-08 13:33:32 +01:00
|
|
|
|
|
|
|
class SearchError(Exception):
|
2023-11-01 20:54:19 +00:00
|
|
|
"""Search error."""
|
2021-07-08 13:33:32 +01:00
|
|
|
|
|
|
|
|
2023-11-01 20:54:19 +00:00
|
|
|
def lookup_with_params(**kwargs: str) -> list[Hit]:
|
2021-07-08 13:33:32 +01:00
|
|
|
url = "http://nominatim.openstreetmap.org/search"
|
|
|
|
|
2023-11-01 20:54:19 +00:00
|
|
|
params: CallParams = {
|
2021-07-08 13:33:32 +01:00
|
|
|
"format": "jsonv2",
|
|
|
|
"addressdetails": 1,
|
|
|
|
"extratags": 1,
|
2021-07-11 16:15:27 +01:00
|
|
|
"limit": 30,
|
2021-07-08 13:33:32 +01:00
|
|
|
"namedetails": 1,
|
|
|
|
"accept-language": "en",
|
|
|
|
"polygon_text": 0,
|
|
|
|
}
|
|
|
|
params.update(kwargs)
|
|
|
|
r = requests.get(url, params=params)
|
|
|
|
if r.status_code == 500:
|
|
|
|
raise SearchError
|
|
|
|
|
|
|
|
try:
|
2023-11-01 20:54:19 +00:00
|
|
|
reply: list[Hit] = json.loads(r.text, object_pairs_hook=OrderedDict)
|
|
|
|
return reply
|
2021-07-08 13:33:32 +01:00
|
|
|
except json.decoder.JSONDecodeError:
|
|
|
|
raise SearchError(r)
|
|
|
|
|
|
|
|
|
2023-11-01 20:54:19 +00:00
|
|
|
def lookup(q: str) -> list[Hit]:
|
|
|
|
"""Nominatim search."""
|
2021-07-08 13:33:32 +01:00
|
|
|
return lookup_with_params(q=q)
|
|
|
|
|
|
|
|
|
2023-11-01 20:54:19 +00:00
|
|
|
def get_us_county(county: str, state: str) -> Hit | None:
|
|
|
|
"""Search for US county and return resulting hit."""
|
2021-07-08 13:33:32 +01:00
|
|
|
if " " not in county and "county" not in county:
|
|
|
|
county += " county"
|
|
|
|
results = lookup(q="{}, {}".format(county, state))
|
|
|
|
|
2023-11-01 20:54:19 +00:00
|
|
|
def pred(hit: Hit) -> typing.TypeGuard[Hit]:
|
2021-07-08 13:33:32 +01:00
|
|
|
return (
|
|
|
|
"osm_type" in hit
|
|
|
|
and hit["osm_type"] != "node"
|
|
|
|
and county in hit["display_name"].lower()
|
|
|
|
)
|
|
|
|
|
|
|
|
return next(filter(pred, results), None)
|
|
|
|
|
|
|
|
|
2023-11-01 20:54:19 +00:00
|
|
|
def get_us_city(name: str, state: str) -> Hit | None:
|
|
|
|
"""Search for US city and return resulting hit."""
|
2021-07-08 13:33:32 +01:00
|
|
|
results = lookup_with_params(city=name, state=state)
|
|
|
|
if len(results) != 1:
|
|
|
|
results = [
|
|
|
|
hit for hit in results if hit["type"] == "city" or hit["osm_type"] == "node"
|
|
|
|
]
|
|
|
|
if len(results) != 1:
|
|
|
|
print("more than one")
|
2023-11-01 20:54:19 +00:00
|
|
|
return None
|
2021-07-08 13:33:32 +01:00
|
|
|
hit = results[0]
|
|
|
|
if hit["type"] not in ("administrative", "city"):
|
|
|
|
print("not a city")
|
2023-11-01 20:54:19 +00:00
|
|
|
return None
|
2021-07-08 13:33:32 +01:00
|
|
|
if hit["osm_type"] == "node":
|
|
|
|
print("node")
|
2023-11-01 20:54:19 +00:00
|
|
|
return None
|
2021-07-08 13:33:32 +01:00
|
|
|
if not hit["display_name"].startswith(name):
|
|
|
|
print("wrong name")
|
2023-11-01 20:54:19 +00:00
|
|
|
return None
|
2021-07-08 13:33:32 +01:00
|
|
|
assert "osm_type" in hit and "osm_id" in hit and "geotext" in hit
|
|
|
|
return hit
|
|
|
|
|
|
|
|
|
2023-11-01 20:54:19 +00:00
|
|
|
def get_hit_name(hit: Hit) -> str:
|
|
|
|
"""Get name from hit."""
|
2021-07-08 13:33:32 +01:00
|
|
|
address = hit.get("address")
|
|
|
|
if not address:
|
2023-11-01 20:54:19 +00:00
|
|
|
assert isinstance(hit["display_name"], str)
|
2021-07-08 13:33:32 +01:00
|
|
|
return hit["display_name"]
|
|
|
|
|
|
|
|
address_values = list(address.values())
|
|
|
|
n1 = address_values[0]
|
|
|
|
if len(address) == 1:
|
2023-11-01 20:54:19 +00:00
|
|
|
assert isinstance(n1, str)
|
2021-07-08 13:33:32 +01:00
|
|
|
return n1
|
|
|
|
|
2023-05-13 14:01:28 +01:00
|
|
|
country = address.pop("country", None)
|
|
|
|
country_code = address.pop("country_code", None)
|
2021-07-08 13:33:32 +01:00
|
|
|
if country_code:
|
|
|
|
country_code == country_code.lower()
|
|
|
|
|
2021-10-22 10:01:46 +01:00
|
|
|
if country_code == "us" and "state" in address:
|
2021-07-08 13:33:32 +01:00
|
|
|
state = address["state"]
|
|
|
|
return f"{n1}, {state}, USA"
|
|
|
|
|
|
|
|
if country_code == "gb":
|
|
|
|
country = "UK"
|
|
|
|
|
|
|
|
if len(address) == 1:
|
|
|
|
return f"{n1}, {country}"
|
|
|
|
else:
|
|
|
|
n2 = address_values[1]
|
|
|
|
return f"{n1}, {n2}, {country}"
|
|
|
|
|
2021-11-14 07:58:40 +00:00
|
|
|
|
2023-11-01 20:54:19 +00:00
|
|
|
def get_hit_label(hit: Hit) -> str:
|
|
|
|
"""Parse hit and generate label."""
|
2024-05-04 08:05:55 +01:00
|
|
|
tags = hit["extratags"] or {}
|
2021-07-08 13:33:32 +01:00
|
|
|
designation = tags.get("designation")
|
|
|
|
category = hit["category"]
|
|
|
|
hit_type = hit["type"]
|
|
|
|
|
|
|
|
if designation:
|
2023-11-01 20:54:19 +00:00
|
|
|
assert isinstance(designation, str)
|
2021-07-08 13:33:32 +01:00
|
|
|
return designation.replace("_", " ")
|
|
|
|
|
|
|
|
if category == "boundary" and hit_type == "administrative":
|
|
|
|
place = tags.get("place") or tags.get("linked_place")
|
|
|
|
|
|
|
|
if place:
|
|
|
|
return f"{place} {category}"
|
|
|
|
|
|
|
|
if category == "place":
|
|
|
|
return f"{hit_type}"
|
|
|
|
|
|
|
|
return f"{hit_type} {category}"
|