Update app.py
Browse files
app.py
CHANGED
|
@@ -1,63 +1,65 @@
|
|
| 1 |
import folium
|
| 2 |
import geopandas as gpd
|
| 3 |
import gradio as gr
|
|
|
|
|
|
|
| 4 |
|
|
|
|
| 5 |
GEOJSON_PATH = "data/meiji_clean.geojson"
|
| 6 |
-
|
| 7 |
-
# 読み込み
|
| 8 |
gdf = gpd.read_file(GEOJSON_PATH)
|
| 9 |
|
| 10 |
-
#
|
| 11 |
for col in ["pref", "district", "city", "oaza"]:
|
| 12 |
if col in gdf.columns:
|
| 13 |
gdf[col] = gdf[col].astype(str)
|
| 14 |
-
else:
|
| 15 |
-
print(f"WARNING: column {col} not found in geojson")
|
| 16 |
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
| 18 |
if not modern_name:
|
| 19 |
return "地名を入力してください。", None
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
gdf["pref"].str.contains(modern_name, na=False)
|
| 26 |
-
)
|
| 27 |
|
| 28 |
-
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
folium.GeoJson(
|
| 36 |
-
data=
|
| 37 |
-
name="Matched Meiji Area",
|
| 38 |
tooltip=folium.GeoJsonTooltip(
|
| 39 |
fields=["pref", "district", "city", "oaza"],
|
| 40 |
-
aliases=["都道府県", "郡", "市町村(明治期)", "大字"]
|
| 41 |
-
localize=True
|
| 42 |
),
|
| 43 |
style_function=lambda x: {
|
| 44 |
-
"color": "
|
| 45 |
"weight": 2,
|
| 46 |
-
"fillOpacity": 0.
|
| 47 |
}
|
| 48 |
).add_to(m)
|
| 49 |
|
| 50 |
-
return f"{len(
|
|
|
|
| 51 |
|
| 52 |
iface = gr.Interface(
|
| 53 |
-
fn=
|
| 54 |
-
inputs=gr.Textbox(label="
|
| 55 |
-
outputs=[
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
],
|
| 59 |
-
title="現代 → 明治期 地名検索ビューア",
|
| 60 |
-
description="現代地名を入力すると、該当する明治期の町村・大字を地図上に表示します。",
|
| 61 |
)
|
| 62 |
|
| 63 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
import folium
|
| 2 |
import geopandas as gpd
|
| 3 |
import gradio as gr
|
| 4 |
+
from shapely.geometry import Point
|
| 5 |
+
from geopy.geocoders import Nominatim
|
| 6 |
|
| 7 |
+
# 明治期ポリゴン
|
| 8 |
GEOJSON_PATH = "data/meiji_clean.geojson"
|
|
|
|
|
|
|
| 9 |
gdf = gpd.read_file(GEOJSON_PATH)
|
| 10 |
|
| 11 |
+
# 文字列化
|
| 12 |
for col in ["pref", "district", "city", "oaza"]:
|
| 13 |
if col in gdf.columns:
|
| 14 |
gdf[col] = gdf[col].astype(str)
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
geolocator = Nominatim(user_agent="meiji_search_app")
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def search_modern_to_meiji(modern_name):
|
| 20 |
if not modern_name:
|
| 21 |
return "地名を入力してください。", None
|
| 22 |
|
| 23 |
+
# 1. 現代地名 → 中心座標取得
|
| 24 |
+
location = geolocator.geocode(modern_name)
|
| 25 |
+
if location is None:
|
| 26 |
+
return f"『{modern_name}』が見つかりませんでした。", None
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
point = Point(location.longitude, location.latitude)
|
| 29 |
|
| 30 |
+
# 2. Meiji ポリゴンと空間一致(Point in Polygon)
|
| 31 |
+
hits = gdf[gdf.contains(point)]
|
| 32 |
|
| 33 |
+
if len(hits) == 0:
|
| 34 |
+
return f"現代地名『{modern_name}』と一致する明治期の地名は見つかりません。", None
|
| 35 |
+
|
| 36 |
+
# 3. 地図描画
|
| 37 |
+
m = folium.Map(location=[location.latitude, location.longitude], zoom_start=11)
|
| 38 |
+
|
| 39 |
+
folium.Marker([location.latitude, location.longitude], tooltip="現在地").add_to(m)
|
| 40 |
|
| 41 |
folium.GeoJson(
|
| 42 |
+
data=hits.to_json(),
|
|
|
|
| 43 |
tooltip=folium.GeoJsonTooltip(
|
| 44 |
fields=["pref", "district", "city", "oaza"],
|
| 45 |
+
aliases=["都道府県", "郡", "市町村(明治期)", "大字"]
|
|
|
|
| 46 |
),
|
| 47 |
style_function=lambda x: {
|
| 48 |
+
"color": "red",
|
| 49 |
"weight": 2,
|
| 50 |
+
"fillOpacity": 0.3
|
| 51 |
}
|
| 52 |
).add_to(m)
|
| 53 |
|
| 54 |
+
return f"{len(hits)} 件ヒットしました(位置照合)", m._repr_html_()
|
| 55 |
+
|
| 56 |
|
| 57 |
iface = gr.Interface(
|
| 58 |
+
fn=search_modern_to_meiji,
|
| 59 |
+
inputs=gr.Textbox(label="現代の地名(例:高槻市 / 枚方市 / 茨木市 / 長岡京市)"),
|
| 60 |
+
outputs=[gr.Textbox(label="検索結果"), gr.HTML(label="明治期の該当地区")],
|
| 61 |
+
title="現代 → 明治期 地名変換(位置照合方式)",
|
| 62 |
+
description="現代の地名から中心点を取得し、その地点が属する明治期の村界を返します。"
|
|
|
|
|
|
|
|
|
|
| 63 |
)
|
| 64 |
|
| 65 |
iface.launch(server_name="0.0.0.0", server_port=7860)
|