如何使用 .explore() 显示 geopandas 交互式地图
How to show geopandas interactive map with .explore()
我制作了一个 geopandas 数据框,我想使用 geopandas_dataframe.explore()
创建一个交互式地图。这是我的代码。首先,我创建了 geopandas 数据框,我检查了数据类型,然后尝试使用 gdf.explore()
映射数据框。不幸的是,我的代码没有错误地完成并且没有显示地图。
代码:
geometry = [Point(xy) for xy in zip(df[1], df[0])]
gdf = geopandas.GeoDataFrame(df, geometry=geometry)
print(gdf.head())
print(gdf.dtypes)
gdf.explore()
输出:
0 1 geometry
0 51.858306 5.778404 POINT (5.77840 51.85831)
1 51.858322 5.778410 POINT (5.77841 51.85832)
2 51.858338 5.778416 POINT (5.77842 51.85834)
3 51.858354 5.778422 POINT (5.77842 51.85835)
4 51.858370 5.778429 POINT (5.77843 51.85837)
0 float64
1 float64
geometry geometry
dtype: object
Process finished with exit code 0
为什么我没有地图?我已经尝试 gdf.show()
但那不存在。我需要做什么才能显示 geopandas 地图?
你在用什么IDE?在 Jupyter Notebook 中,您的代码(稍作修改)适用于我。但是,当我 运行 它在 PyCharm 中时,我得到“进程完成,退出代码为 0”,没有情节。
import geopandas as gpd
import pandas as pd
from shapely.geometry import Point
data_dict = {'x': {0: 51.858306, 1: 51.858322, 2: 51.858338, 3: 51.858354, 4: 51.85837},
'y': {0: 5.778404, 1: 5.77841, 2: 5.778416, 3: 5.778422, 4: 5.778429}}
df = pd.DataFrame(data_dict)
geometry = [Point(xy) for xy in zip(df['x'], df['y'])]
gdf = gpd.GeoDataFrame(df, geometry=geometry)
print(gdf.head())
print(gdf.dtypes)
gdf.explore()
编辑:看来您可以将 folium 图形保存到 html。这对我有用 PyCharm.
m = gdf.explore()
outfp = r"<your dir path>\base_map.html"
m.save(outfp)
explore()
有一个微妙的要求。 GeoDataFrame 中的列名必须是字符串。在您的情况下,它们的编号为零和一。
- 重命名这些列:
gdf = gdf.rename(columns={c:str(c) for c in gdf.columns})
import geopandas as gpd
from shapely.geometry import Point
import pandas as pd
# simulate dataframe in question, generates a warning, ignore it
world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
df = world["geometry"].centroid.apply(lambda g: pd.Series(g.coords[0][::-1]))
geometry = [Point(xy) for xy in zip(df[1], df[0])]
gdf = gpd.GeoDataFrame(data=df, geometry=geometry, crs="epsg:4326")
# explore does not like column names that are not strings...
gdf = gdf.rename(columns={c:str(c) for c in gdf.columns})
# print(gdf.head())
# print(gdf.dtypes)
gdf.explore()
我制作了一个 geopandas 数据框,我想使用 geopandas_dataframe.explore()
创建一个交互式地图。这是我的代码。首先,我创建了 geopandas 数据框,我检查了数据类型,然后尝试使用 gdf.explore()
映射数据框。不幸的是,我的代码没有错误地完成并且没有显示地图。
代码:
geometry = [Point(xy) for xy in zip(df[1], df[0])]
gdf = geopandas.GeoDataFrame(df, geometry=geometry)
print(gdf.head())
print(gdf.dtypes)
gdf.explore()
输出:
0 1 geometry
0 51.858306 5.778404 POINT (5.77840 51.85831)
1 51.858322 5.778410 POINT (5.77841 51.85832)
2 51.858338 5.778416 POINT (5.77842 51.85834)
3 51.858354 5.778422 POINT (5.77842 51.85835)
4 51.858370 5.778429 POINT (5.77843 51.85837)
0 float64
1 float64
geometry geometry
dtype: object
Process finished with exit code 0
为什么我没有地图?我已经尝试 gdf.show()
但那不存在。我需要做什么才能显示 geopandas 地图?
你在用什么IDE?在 Jupyter Notebook 中,您的代码(稍作修改)适用于我。但是,当我 运行 它在 PyCharm 中时,我得到“进程完成,退出代码为 0”,没有情节。
import geopandas as gpd
import pandas as pd
from shapely.geometry import Point
data_dict = {'x': {0: 51.858306, 1: 51.858322, 2: 51.858338, 3: 51.858354, 4: 51.85837},
'y': {0: 5.778404, 1: 5.77841, 2: 5.778416, 3: 5.778422, 4: 5.778429}}
df = pd.DataFrame(data_dict)
geometry = [Point(xy) for xy in zip(df['x'], df['y'])]
gdf = gpd.GeoDataFrame(df, geometry=geometry)
print(gdf.head())
print(gdf.dtypes)
gdf.explore()
编辑:看来您可以将 folium 图形保存到 html。这对我有用 PyCharm.
m = gdf.explore()
outfp = r"<your dir path>\base_map.html"
m.save(outfp)
explore()
有一个微妙的要求。 GeoDataFrame 中的列名必须是字符串。在您的情况下,它们的编号为零和一。- 重命名这些列:
gdf = gdf.rename(columns={c:str(c) for c in gdf.columns})
import geopandas as gpd
from shapely.geometry import Point
import pandas as pd
# simulate dataframe in question, generates a warning, ignore it
world = gpd.read_file(gpd.datasets.get_path("naturalearth_lowres"))
df = world["geometry"].centroid.apply(lambda g: pd.Series(g.coords[0][::-1]))
geometry = [Point(xy) for xy in zip(df[1], df[0])]
gdf = gpd.GeoDataFrame(data=df, geometry=geometry, crs="epsg:4326")
# explore does not like column names that are not strings...
gdf = gdf.rename(columns={c:str(c) for c in gdf.columns})
# print(gdf.head())
# print(gdf.dtypes)
gdf.explore()