1- import quackosm as qosm
2- from argparse import ArgumentParser
31from typing import Callable
42from pathlib import Path
53import geopandas
6- from classopt import classopt , config
7- from time import strftime
4+ import typer
85
96
10- @classopt
11- class CLIOpt :
12- parquet : Path = config (long = True )
13- geojson_dir : Path = config (long = True )
14-
157def get_sometag (target_tag : str ) -> Callable [dict , str | None ]:
168 def func (tags ):
179 if target_tag in tags :
1810 return tags [target_tag ]
1911 else :
2012 return None
13+
2114 return func
2215
16+
2317def is_atm (tags ):
2418 if "amenity" in tags and tags ["amenity" ] == "atm" :
2519 return True
@@ -28,47 +22,56 @@ def is_atm(tags):
2822 else :
2923 return False
3024
25+
3126def is_bank (tags ):
3227 if "amenity" in tags and tags ["amenity" ] == "bank" :
3328 return True
3429 else :
3530 return False
3631
32+
3733def is_convenience (tags ):
3834 if "shop" in tags and tags ["shop" ] == "convenience" :
3935 return True
4036 return False
4137
38+
4239def get_openinghours (tags ):
4340 atm_opening = get_sometag ("opening_hours:atm" )(tags )
4441 if atm_opening is None :
4542 return get_sometag ("opening_hours" )(tags )
4643 return atm_opening
4744
4845
49- if __name__ == "__main__" :
50- # argparser = ArgumentParser()
51- # argparser.add_argument("-f", "--parquet", type=Path)
52- # argparser.add_argument("-t", "--geojson_dir", type=Path)
53- # argparser.parse_args()
54- opt : CLIOpt = CLIOpt .from_args ()
55-
56- gdf = geopandas .read_parquet (opt .parquet )
46+ def main (parquet : Path , geojson_dir : Path = Path .cwd ()):
47+ gdf = geopandas .read_parquet (parquet )
5748 gdf ["tags" ] = gdf ["tags" ].apply (lambda l : {i [0 ]: i [1 ] for i in l })
5849 gdf ["brand" ] = gdf ["tags" ].apply (get_sometag ("brand" ))
5950 gdf ["name" ] = gdf ["tags" ].apply (get_sometag ("name" ))
6051 gdf ["atm" ] = gdf ["tags" ].apply (is_atm )
6152 gdf ["bank" ] = gdf ["tags" ].apply (is_bank )
6253 gdf ["convenience" ] = gdf ["tags" ].apply (is_convenience )
6354 gdf ["opening_hours" ] = gdf ["tags" ].apply (get_openinghours )
55+
6456 gdf .geometry = gdf .representative_point ()
6557 gdf .geometry = gdf .geometry .set_precision (grid_size = 0.0000001 )
58+
6659 atm_gdf = gdf [gdf ["atm" ]]
6760 atm_gdf = atm_gdf [["feature_id" , "brand" , "name" , "opening_hours" , "geometry" ]]
68- atm_gdf .to_file (opt .geojson_dir / f"atm.json" , driver = "GeoJSON" )
61+ atm_gdf .to_file (geojson_dir / "atm.json" , driver = "GeoJSON" , separator = ("," , ":" ))
62+
6963 bank_gdf = gdf [gdf ["bank" ] & ~ gdf ["atm" ]]
7064 bank_gdf = bank_gdf [["feature_id" , "brand" , "name" , "opening_hours" , "geometry" ]]
71- bank_gdf .to_file (opt .geojson_dir / f"bank.json" , driver = "GeoJSON" )
65+ bank_gdf .to_file (geojson_dir / "bank.json" , driver = "GeoJSON" , separator = ("," , ":" ))
66+
7267 convenience_gdf = gdf [gdf ["convenience" ] & ~ gdf ["atm" ]]
73- convenience_gdf = convenience_gdf [["feature_id" , "brand" , "name" , "opening_hours" , "geometry" ]]
74- convenience_gdf .to_file (opt .geojson_dir / f"convenience.json" , driver = "GeoJSON" )
68+ convenience_gdf = convenience_gdf [
69+ ["feature_id" , "brand" , "name" , "opening_hours" , "geometry" ]
70+ ]
71+ convenience_gdf .to_file (
72+ geojson_dir / "convenience.json" , driver = "GeoJSON" , separator = ("," , ":" )
73+ )
74+
75+
76+ if __name__ == "__main__" :
77+ typer .run (main )
0 commit comments