| import json |
| import gradio as gr |
| from shapely.geometry import shape, Point |
|
|
| |
| |
| |
| with open("data/meiji_village.geojson", "r", encoding="utf-8") as f: |
| geo = json.load(f)["features"] |
|
|
| |
| village_index = [] |
| for feat in geo: |
| props = feat["properties"] |
| geom = shape(feat["geometry"]) |
| village_index.append({ |
| "pref": props.get("pref"), |
| "gun": props.get("gun"), |
| "mura": props.get("mura"), |
| "id": props.get("id"), |
| "geometry": geom |
| }) |
|
|
|
|
| |
| |
| |
| def search_place(name, lat=None, lon=None): |
| results = [] |
|
|
| |
| if name: |
| key = name.strip() |
| for v in village_index: |
| if key in v["mura"] or key in v["gun"]: |
| results.append({ |
| "都道府県": v["pref"], |
| "郡": v["gun"], |
| "村": v["mura"], |
| "ID": v["id"] |
| }) |
|
|
| |
| if lat and lon: |
| pt = Point(float(lon), float(lat)) |
| for v in village_index: |
| if v["geometry"].contains(pt): |
| results.append({ |
| "都道府県": v["pref"], |
| "郡": v["gun"], |
| "村": v["mura"], |
| "ID": v["id"] |
| }) |
|
|
| if len(results) == 0: |
| return "該当なし" |
|
|
| return results |
|
|
|
|
| |
| |
| |
| def run_search(name, latitude, longitude): |
| return search_place(name, latitude, longitude) |
|
|
|
|
| with gr.Blocks() as app: |
| gr.Markdown("# 古地名 → 明治22年村界検索(京都+大阪)") |
|
|
| with gr.Row(): |
| name = gr.Textbox(label="地名(村名・郡名)") |
| lat = gr.Textbox(label="緯度 lat(任意)") |
| lon = gr.Textbox(label="経度 lon(任意)") |
|
|
| btn = gr.Button("検索") |
| output = gr.JSON(label="結果") |
|
|
| btn.click(run_search, inputs=[name, lat, lon], outputs=output) |
|
|
| app.launch() |
|
|