Pass lat/lon as floats

Closes: #18
This commit is contained in:
Edward Betts 2023-12-12 09:43:57 +00:00
parent 29426e2462
commit d001ec1155
2 changed files with 10 additions and 16 deletions

View file

@ -102,18 +102,14 @@ def wd_uri_to_qid(value: str) -> str:
return value[len(wd_entity) - 1 :]
def geosearch_query(lat: str | float, lon: str | float) -> list[Row]:
def geosearch_query(lat: float, lon: float) -> list[Row]:
"""Geosearch via WDQS."""
if isinstance(lat, float):
lat = f"{lat:f}"
if isinstance(lon, float):
lon = f"{lon:f}"
query = render_template("sparql/geosearch.sparql", lat=lat, lon=lon)
lat_str, lon_str = f"{lat:f}", f"{lon:f}"
query = render_template("sparql/geosearch.sparql", lat=lat_str, lon=lon_str)
return wdqs(query)
def geosearch(lat: str | float, lon: str | float) -> Row | None:
def geosearch(lat: float, lon: float) -> Row | None:
"""Geosearch."""
default_max_dist = 1
rows = geosearch_query(lat, lon)
@ -157,7 +153,7 @@ def lookup_gss_in_wikidata(gss: str) -> list[Row]:
return wdqs(render_template("sparql/lookup_gss.sparql", gss=gss))
def lookup_wikidata_by_name(name: str, lat: float | str, lon: float | str) -> list[Row]:
def lookup_wikidata_by_name(name: str, lat: float, lon: float) -> list[Row]:
"""Lookup place in Wikidata by name."""
query = render_template(
"sparql/lookup_by_name.sparql", name=repr(name), lat=str(lat), lon=str(lon)
@ -195,11 +191,12 @@ def get_commons_cat_from_gss(gss: str) -> Hit | None:
WikidataDict = dict[str, None | bool | str | int | dict[str, typing.Any]]
def build_dict(hit: Hit | None, lat: str | float, lon: str | float) -> WikidataDict:
def build_dict(hit: Hit | None, lat: float, lon: float) -> WikidataDict:
"""Build dict."""
coords = {"lat": lat, "lon": lon}
if hit is None:
return {"commons_cat": None, "missing": True, "coords": coords}
assert isinstance(hit["commons_cat"], str)
commons_cat = hit["commons_cat"]
ret: WikidataDict = {
"coords": coords,

View file

@ -73,7 +73,7 @@ def get_random_lat_lon() -> tuple[float, float]:
def do_lookup(
elements: "Query[model.Polygon]", lat: str | float, lon: str | float
elements: "Query[model.Polygon]", lat: float, lon: float
) -> wikidata.WikidataDict:
"""Do lookup."""
try:
@ -167,9 +167,7 @@ def hit_from_ref_gss_tag(tags: Tags) -> wikidata.Hit | None:
return wikidata.get_commons_cat_from_gss(gss) if gss else None
def hit_from_name(
tags: Tags, lat: str | float, lon: str | float
) -> wikidata.Hit | None:
def hit_from_name(tags: Tags, lat: float, lon: float) -> wikidata.Hit | None:
"""Use name to look for hit."""
if not (name := tags.get("name")):
return None
@ -181,10 +179,9 @@ def hit_from_name(
def osm_lookup(
elements: "Query[model.Polygon]", lat: str | float, lon: str | float
elements: "Query[model.Polygon]", lat: float, lon: float
) -> wikidata.Hit | None:
"""OSM lookup."""
ret: wikidata.Hit | None
for e in elements:
assert isinstance(e, model.Polygon)
assert e.tags