-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updating write_tags file and adding basic CLI
- Loading branch information
Showing
6 changed files
with
90 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
pydantic>=2.8.2 | ||
pydantic>=2.8.0 | ||
pytest>=8.3.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,19 @@ | ||
import json | ||
import re | ||
|
||
with open("scripts/tags.json", "r+", encoding="utf-8") as f: | ||
tags = json.load(f) | ||
|
||
tags_filled = {k: v for k, v in tags.items() if v} | ||
|
||
with open("src/overturetoosm/resources.py", "w+", encoding="utf-8") as f: | ||
f.write( | ||
f'''"""A mapping of Overture tags to OSM tags.""" | ||
new_dict_str = json.dumps(tags_filled, indent=4).replace(":", ": ") | ||
pattern = r"places_tags: Dict\[str, Dict\[str, str\]\]\s*=\s*\{.*\}" | ||
replacement = f"places_tags: Dict[str, Dict[str, str]] = {new_dict_str}" | ||
|
||
with open("src/overturetoosm/resources.py", "r", encoding="utf-8") as f: | ||
contents = f.read() | ||
|
||
tags: dict[str, dict[str, str]] = {json.dumps(tags_filled, indent=4)} | ||
"""dict[str, dict[str, str]]: A mapping of Overture to OSM tags, | ||
excluding blank values. This is downstream from the `scripts/tag.json` | ||
file.""" | ||
''' | ||
) | ||
replace = re.sub(pattern, replacement, contents, flags=re.DOTALL) | ||
|
||
with open("src/overturetoosm/resources.py", "w+", encoding="utf-8") as f: | ||
f.write(replace) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
"""Entrypoint for the package.""" | ||
|
||
from .cli import main | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
"""Command line interface for the overturetoosm package.""" | ||
|
||
import argparse | ||
import json | ||
from typing import Dict | ||
|
||
from . import process_address, process_building, process_geojson, process_place | ||
|
||
|
||
def parse_kwargs(pairs) -> Dict[str, str]: | ||
"""Parse key-value pairs into a dictionary.""" | ||
kwargs = {} | ||
for pair in pairs: | ||
key, value = pair.split("=") | ||
kwargs[key] = value | ||
return kwargs | ||
|
||
|
||
def main(): | ||
"""Configure the argument parser for the CLI.""" | ||
parser = argparse.ArgumentParser( | ||
description="Convert Overture data to the OSM schema in the GeoJSON format." | ||
) | ||
|
||
parser.add_argument( | ||
"-i", "--input", required=True, help="Path to the input GeoJSON file" | ||
) | ||
out = parser.add_argument_group("Output options") | ||
output_group = out.add_mutually_exclusive_group(required=True) | ||
output_group.add_argument("-o", "--output", help="Path to the output GeoJSON file") | ||
output_group.add_argument( | ||
"--in-place", | ||
action="store_true", | ||
help="Convert the input file in place (overwrites the original file)", | ||
) | ||
|
||
parser.add_argument( | ||
"-t", | ||
"--type", | ||
choices=["place", "building", "address"], | ||
type=str, | ||
required=True, | ||
help="Type of feature to convert (place, building, or address)", | ||
) | ||
parser.add_argument("kwargs", nargs="*", help="Additional arguments") | ||
|
||
args = parser.parse_args() | ||
|
||
kwargs = parse_kwargs(args.kwargs) | ||
|
||
fx_dict = { | ||
"place": process_place, | ||
"building": process_building, | ||
"address": process_address, | ||
} | ||
|
||
with open(args.input, "r", encoding="utf-8") as f: | ||
contents: dict = json.load(f) | ||
conf = float(kwargs.get("confidence", 0)) | ||
kwargs.pop("confidence", None) | ||
geojson = process_geojson( | ||
contents, fx=fx_dict.get(args.type, None), confidence=conf, options=kwargs | ||
) | ||
|
||
if args.in_place: | ||
with open(args.input, "w+", encoding="utf-8") as f: | ||
json.dump(geojson, f, indent=4) | ||
else: | ||
with open(args.output, "w+", encoding="utf-8") as f: | ||
json.dump(geojson, f, indent=4) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters