Created
March 31, 2025 13:11
-
-
Save smellman/2a67f144e27f4dbfbb403786c6dcf485 to your computer and use it in GitHub Desktop.
OpenStreetMap: PLATEAU imports_listのwikiテンプレートを作成するためのスクリプト
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
import os | |
import glob | |
import argparse | |
""" | |
This script is used to generate wiki table from a given directory. | |
See: https://wiki.openstreetmap.org/wiki/JA:MLIT_PLATEAU/imports_list | |
""" | |
def get_bldg_meshcodes(path: str, epsg: str) -> list[str]: | |
files = glob.glob(os.path.join(path, f"*_bldg_{epsg}_op.gml")) | |
meshcodes = [] | |
for file in files: | |
# Extract the meshcode from the filename | |
meshcode = os.path.basename(file).split("_")[0] | |
meshcodes.append(meshcode) | |
# Sort the meshcodes | |
meshcodes.sort() | |
return meshcodes | |
def title(text: str) -> str: | |
return f"=== {text} ===\n" | |
def header() -> str: | |
header_text = """{|class="wikitable" | |
! 3次メッシュコード!!インポート実施日!!実施者!!備考 | |
""" | |
return header_text | |
def footer() -> str: | |
return "|}\n" | |
def body(meshcode: str) -> str: | |
body_text = f"""|- | |
| {meshcode} || || || | |
""" | |
return body_text | |
def generate_wiki_table(title_text: str, meshcodes: list[str]) -> str: | |
wiki_text = title(title_text) | |
wiki_text += header() | |
for meshcode in meshcodes: | |
wiki_text += body(meshcode) | |
wiki_text += footer() | |
return wiki_text | |
def main(): | |
parser = argparse.ArgumentParser(description="Generate a wiki table for MLIT PLATEAU/imports list from files.") | |
parser.add_argument("path", type=str, help="Path to the directory containing meshcode files.") | |
parser.add_argument("epsg", type=str, help="EPSG code for the meshcode files.") | |
parser.add_argument("title", type=str, help="Title for the wiki table.") | |
args = parser.parse_args() | |
path = args.path | |
epsg = args.epsg | |
title_text = args.title | |
# Check if the path exists | |
if not os.path.exists(path): | |
print(f"Path {path} does not exist.") | |
return | |
# Get the list of meshcodes | |
meshcodes = get_bldg_meshcodes(path, epsg) | |
# Check if any meshcodes were found | |
if not meshcodes: | |
print(f"No meshcode files found in {path} with EPSG {epsg}.") | |
return | |
# Generate the wiki table | |
wiki_table = generate_wiki_table(title_text, meshcodes) | |
# Print the wiki table | |
print(wiki_table) | |
if __name__ == "__main__": | |
main() | |
# The script can be run from the command line with the following command: | |
# python plateau_wiki.py <path> <epsg> <title> | |
# example: | |
# $ python3 plateau_wiki.py /Users/btm/develop/osm/plateau/chiba_chuou/12100_chiba-shi_city_2024_citygml_1_op/udx/bldg 6697 千葉県千葉市中央区|pbcopy |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment