Python Folium Relearning(5) from May 2025(Shown or Unshown Zoom Contorer and Scale)

引き続きFoliumの学習を進めていきます。(参考URL→FoliumHP(User Guide) _URL: https://python-visualization.github.io/folium/latest/user_guide.html

まずは、ScaleのInsert in MapとZoom Contro ButtonのShown/ UnshownのSettingです。It is easy.

>Code<

import folium

# 地図の中心となる緯度経度を設定
center_lat = 35.6895
center_lon = 139.6917

# Foliumの地図オブジェクトを作成
m = folium.Map(
    location=[center_lat, center_lon],
    zoom_start=6,
    tiles = 'CartoDB Dark Matter',
    control_scale = True, # 左下にScaleBarがDrawされます
    zoom_control = False # Zoom Control BoxをNot Shown
)

m

>Output< MapのBottomLeftにScaleBarが表示され、Zoom Control Buttonが消えました。

 LimitsのSettingはVery Convenientでございます。これは少し詳しく学習したいと思います。 国土数値情報の行政区域情報をDownloadして、日本の国土を包含する長方形の4つの隅角の緯度経度を抽出して、LimitをSetします。 Gemini先生に質問したところ、'total_bounds'を利用するというめちゃExcellentな回答をいただきました。改めてPythonのLibraryやMethodの豊富さ・膨大さを感じました。

>Code< 国土数値情報の行政区域情報を取得

import urllib.request
import zipfile
import geopandas as gpd

def download_map_data(url, zip_file_name, shp_file_name):
    zip_file_path = 'unzipped_data/' + zip_file_name

# URLからデータをダウンロード
    urllib.request.urlretrieve(url, zip_file_name)

# ZIPファイルを解凍
    with zipfile.ZipFile(zip_file_name, 'r') as zip_ref:
        zip_ref.extractall('unzipped_data')  # 解凍先のディレクトリを指定

# Shapefileを読み込む
#shapefile_path = 'unzipped_data/W05-06_36-g_Stream.shp'
    shp_file_path = 'unzipped_data/' + shp_file_name
    gdf = gpd.read_file(shp_file_path)

    return gdf

# ダウンロードとデータ読み込みのループ
url = 'http://nlftp.mlit.go.jp/ksj/gml/data/N03/N03-2024/N03-20240101_GML.zip'
zip_file_name = 'N03-20240101_GML.zip'
shp_file_name = 'N03-20240101.shp'

# データをダウンロードして読み込む
gdf = download_map_data(url, zip_file_name, shp_file_name)

# データを表示
gdf.head()

>Output<

N03_001N03_002N03_003N03_004N03_005N03_007geometry
0北海道石狩振興局None札幌市中央区01101POLYGON ((141.25694 42.99782, 141.2563 42.9978...
1北海道石狩振興局None札幌市北区01102POLYGON ((141.33325 43.07505, 141.3326 43.0757...
2北海道石狩振興局None札幌市東区01103POLYGON ((141.37341 43.0684, 141.37333 43.0684...
3北海道石狩振興局None札幌市白石区01104POLYGON ((141.38202 43.04832, 141.38193 43.048...
4北海道石狩振興局None札幌市豊平区01105POLYGON ((141.36371 42.94154, 141.36378 42.941...

 次にtotal_boundsを使用して、日本国土を包含する長方形の4つの隅角のLonLatをGetします。

>Code<


# 日本国土を包含する長方形のバウンディングボックスを取得
min_lon, min_lat, max_lon, max_lat = gdf.total_bounds

# 4つの隅角の緯度経度
top_left = (max_lat, min_lon)     # 北西
top_right = (max_lat, max_lon)    # 北東
bottom_left = (min_lat, min_lon)  # 南西
bottom_right = (min_lat, max_lon) # 南東

print(f"北西 (緯度, 経度): {top_left}")
print(f"北東 (緯度, 経度): {top_right}")
print(f"南西 (緯度, 経度): {bottom_left}")
print(f"南東 (緯度, 経度): {bottom_right}")

>Output<

北西 (緯度, 経度): (np.float64(45.557243414), np.float64(122.932606368)) 北東 (緯度, 経度): (np.float64(45.557243414), np.float64(153.986675123)) 南西 (緯度, 経度): (np.float64(20.422746414), np.float64(122.932606368)) 南東 (緯度, 経度): (np.float64(20.422746414), np.float64(153.986675123))

Getしたmin_lon, min_lat, max_lon, max_latを、foliumの引数に入力した上で4つの隅角点にCircleMarker(丸印)をDrowします。

>Code<

import folium

# 地図の中心となる緯度経度を設定
center_lat = 35.6895
center_lon = 139.6917

# Foliumの地図オブジェクトを作成
m = folium.Map(
    max_bounds = True, # これでShown AreaをLimitします
    location=[center_lat, center_lon],
    zoom_start=4,
    min_lat=min_lat, # This one is define limitations of shown area
    max_lat=max_lat, # This one is define limitations of shown area
    min_lon=min_lon, # This one is define limitations of shown area
    max_lon=max_lon, # This one is define limitations of shown area
    tiles = 'CartoDB Positron',
    control_scale = True, # 左下にScaleBarがDrawされます
    zoom_control = False # Zoom Control BoxをNot Shown
)

folium.CircleMarker([max_lat, min_lon], radius=5, tooltip="Upper Left Corner").add_to(m)
folium.CircleMarker([min_lat, min_lon], radius=5, tooltip="Lower Left Corner").add_to(m)
folium.CircleMarker([min_lat, max_lon], radius=5, tooltip="Lower Right Corner").add_to(m)
folium.CircleMarker([max_lat, max_lon], radius=5, tooltip="Upper Right Corner").add_to(m)

m

>Output<



Make this Notebook Trusted to load map: File -> Trust Notebook

前回:https://shikuuk.blogspot.com/2025/05/test_28.html

次回:https://shikuuk.blogspot.com/2025/06/python-folium-relearning6-from-may.html

コメント