From 2513cbc7b41e19895810088b2044ff868a068851 Mon Sep 17 00:00:00 2001 From: Matěj Cepl Date: Wed, 7 Dec 2022 19:09:25 +0100 Subject: Find center of URL on the command line. --- pyproject.toml | 2 +- src/osm_where.py | 27 ++++++++++++++++++++++----- 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index c63ec7b..07dc9e0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,7 +1,7 @@ [project] name = "osm_where" description = "Get geo: URI for given locality from OSM" -version = "0.1.0" +version = "0.2.0" requires-python = ">=3.6" authors = [ { name = "Matěj Cepl", email = "mcepl@cepl.eu" } diff --git a/src/osm_where.py b/src/osm_where.py index 52e7b9b..3d466a3 100755 --- a/src/osm_where.py +++ b/src/osm_where.py @@ -1,19 +1,22 @@ #!/usr/bin/env python3 import argparse +import urllib.parse import xdg.BaseDirectory from OSMPythonTools.cachingStrategy import CachingStrategy, JSON -from OSMPythonTools.overpass import Overpass +from OSMPythonTools.api import Api -CachingStrategy.use(JSON, cacheDir=xdg.BaseDirectory.save_cache_path('osm_where')) +from shapely.geometry import shape + +CachingStrategy.use(JSON, cacheDir=xdg.BaseDirectory.save_cache_path("osm_where")) import logging logging.getLogger("OSMPythonTools").setLevel(logging.ERROR) -def get_URI(name, area, lang): +def parse_name(name, area, lang): overpass = Overpass() result = overpass.query( @@ -26,16 +29,30 @@ def get_URI(name, area, lang): return None +def parse_URL(url): + api = Api() + + parsed_url = urllib.parse.urlparse(url) + way = api.query(parsed_url.path) + geom = shape(way.geometry()) + return geom.centroid.y, geom.centroid.x + + def main(): parser = argparse.ArgumentParser( prog="osm_where", description="Get geo: URI for given locality from OSM" ) - parser.add_argument("name") + parser.add_argument("url", nargs="?", help="URL of the location") + parser.add_argument("-n", "--name", help="name of the location") parser.add_argument("-a", "--area", default="UA", help="ISO 3166 area code") parser.add_argument("-l", "--lang", default="en", help="ISO 639-1 language code") args = parser.parse_args() - geo = get_URI(args.name, args.area, args.lang) + if args.name: + geo = parse_name(args.name, args.area, args.lang) + else: + geo = parse_URL(args.url) + if geo: print(f"geo:{geo[0]},{geo[1]}") -- cgit