在 Folium 地图上添加 1KM 网格

问题描述 投票:0回答:1

有人可以建议一种在 folium 地图上添加 1KM 宽的网格/经纬网的方法吗?这里的 folium 示例使用纬度/经度间隔,但不渲染网格。


import folium

# Bangkok coordinates
center_lat = 13.7563
center_lon = 100.4911

m = folium.Map(location=[center_lat, center_lon], zoom_start=11)


# Interval in degrees (1 kilometer ≈ 0.00694444 degrees)
interval = 0.00694444

# Create grid lines
grid_lines = []

# East-west lines (from -90 to 90 latitude)
for lat in range(-90, 91, int(1 / interval)):
  west_point = (-180, lat)
  east_point = (180, lat)
  grid_lines.append(folium.PolyLine([west_point, east_point], color="black", weight=0.5, opacity=0.5))

# North-south lines (from -180 to 180 longitude)
for lon in range(-180, 181, int(1 / interval)):
  south_point = (lon, -90)
  north_point = (lon, 90)
  grid_lines.append(folium.PolyLine([south_point, north_point], color="black", weight=0.5, opacity=0.5))

# Add lines to the map
for line in grid_lines:
  line.add_to(m)

# Display the map
m
python maps folium
1个回答
0
投票

您的 grid 实际上会在 map 上渲染,但它并不像您预期的那样,因为您使用的是

range
而不是
numpy.arange
。此外,您还反转了两个 for 循环中每个
PolyLine
的位置。所以,这是一个可能的修复方法:

import folium
import numpy as np

CLAT, CLON = 13.7563, 100.4911

m = folium.Map(location=[CLAT, CLON], zoom_start=11)

INTERVAL = 1 / 111  # 1 km to degrees, gives ~0.009

LINE_STYLE = {"color": "black", "weight": 0.5, "opacity": 0.5}

def add_gl(m, s, e, hztl, **kwargs):
    for v in np.arange(s, e + 1, INTERVAL):
        coords = [[v, -180], [v, 180]] if hztl else [[-90, v], [90, v]]
        folium.PolyLine(coords, **kwargs).add_to(m)

add_gl(m, -90, 90, hztl=True, **LINE_STYLE)
add_gl(m, -180, 180, hztl=False, **LINE_STYLE)

NB:地图在我的 JupyterLab 上显示大约需要 40 秒,而且导航相对麻烦/缓慢。

输出(

m
):

© www.soinside.com 2019 - 2024. All rights reserved.