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 :] 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.""" """Geosearch via WDQS."""
if isinstance(lat, float): lat_str, lon_str = f"{lat:f}", f"{lon:f}"
lat = f"{lat:f}" query = render_template("sparql/geosearch.sparql", lat=lat_str, lon=lon_str)
if isinstance(lon, float):
lon = f"{lon:f}"
query = render_template("sparql/geosearch.sparql", lat=lat, lon=lon)
return wdqs(query) return wdqs(query)
def geosearch(lat: str | float, lon: str | float) -> Row | None: def geosearch(lat: float, lon: float) -> Row | None:
"""Geosearch.""" """Geosearch."""
default_max_dist = 1 default_max_dist = 1
rows = geosearch_query(lat, lon) 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)) 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.""" """Lookup place in Wikidata by name."""
query = render_template( query = render_template(
"sparql/lookup_by_name.sparql", name=repr(name), lat=str(lat), lon=str(lon) "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]] 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.""" """Build dict."""
coords = {"lat": lat, "lon": lon} coords = {"lat": lat, "lon": lon}
if hit is None: if hit is None:
return {"commons_cat": None, "missing": True, "coords": coords} return {"commons_cat": None, "missing": True, "coords": coords}
assert isinstance(hit["commons_cat"], str)
commons_cat = hit["commons_cat"] commons_cat = hit["commons_cat"]
ret: WikidataDict = { ret: WikidataDict = {
"coords": coords, "coords": coords,

View file

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