diff --git a/geocode/wikidata.py b/geocode/wikidata.py index ff07ba0..b7f8b79 100644 --- a/geocode/wikidata.py +++ b/geocode/wikidata.py @@ -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, diff --git a/lookup.py b/lookup.py index 043adfb..1cb97c8 100755 --- a/lookup.py +++ b/lookup.py @@ -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