python 加载本地shp文件绘制地图能够滚轮缩放,添加点元素,绘线
时间: 2024-06-11 20:10:18 浏览: 13
和面元素,具体实现方法如下:
1. 安装需要的 Python 库,包括:geopandas、matplotlib 和 descartes。
2. 使用 geopandas 读取本地的 shp 文件,得到一个 GeoDataFrame 对象。例如:
```
import geopandas as gpd
map_df = gpd.read_file('path/to/your/shpfile.shp')
```
3. 使用 matplotlib 创建一个画布,并将地图数据绘制在画布上。例如:
```
import matplotlib.pyplot as plt
fig, ax = plt.subplots()
map_df.plot(ax=ax)
```
4. 使用 matplotlib 的交互模式,使得地图能够滚轮缩放。例如:
```
plt.axis('equal')
plt.axis('off')
plt.tight_layout()
plt.show(block=False)
plt.rcParams['toolbar'] = 'toolmanager'
toolmanager = plt.get_current_fig_manager().toolbar
toolmanager.add_tool('Zoom', 'mpl_toolkits.toolbar.icons.Zoom', 'Zoom')
toolmanager.add_tool('Pan', 'mpl_toolkits.toolbar.icons.Pan', 'Pan')
```
5. 添加点元素。首先,创建一个包含点坐标和属性的 Pandas DataFrame 对象。然后,将 DataFrame 转换为 GeoDataFrame 对象,并将其绘制在地图上。例如:
```
import pandas as pd
points = pd.DataFrame({
'Name': ['Point 1', 'Point 2', 'Point 3'],
'Latitude': [38.8951, 38.8904, 38.8887],
'Longitude': [-77.0364, -77.0320, -77.0321]
})
from shapely.geometry import Point
geometry = [Point(xy) for xy in zip(points.Longitude, points.Latitude)]
points_gdf = gpd.GeoDataFrame(points, geometry=geometry)
points_gdf.plot(ax=ax, color='red', markersize=10)
```
6. 绘制线和面元素。首先,创建一个包含线或面的几何对象和属性的 GeoDataFrame 对象。然后,将其绘制在地图上。例如:
```
from shapely.geometry import LineString, Polygon
lines = gpd.GeoDataFrame({
'Name': ['Line 1'],
'geometry': [LineString([(0, 0), (1, 1)])]
})
lines.plot(ax=ax, color='green')
polygons = gpd.GeoDataFrame({
'Name': ['Polygon 1'],
'geometry': [Polygon([(0, 0), (1, 1), (1, 0)])]
})
polygons.plot(ax=ax, color='blue', alpha=0.5)
```
完整代码示例:
```
import geopandas as gpd
import matplotlib.pyplot as plt
import pandas as pd
from shapely.geometry import Point, LineString, Polygon
# 读取地图数据
map_df = gpd.read_file('path/to/your/shpfile.shp')
# 创建画布
fig, ax = plt.subplots()
# 绘制地图
map_df.plot(ax=ax)
# 滚轮缩放
plt.axis('equal')
plt.axis('off')
plt.tight_layout()
plt.show(block=False)
plt.rcParams['toolbar'] = 'toolmanager'
toolmanager = plt.get_current_fig_manager().toolbar
toolmanager.add_tool('Zoom', 'mpl_toolkits.toolbar.icons.Zoom', 'Zoom')
toolmanager.add_tool('Pan', 'mpl_toolkits.toolbar.icons.Pan', 'Pan')
# 添加点元素
points = pd.DataFrame({
'Name': ['Point 1', 'Point 2', 'Point 3'],
'Latitude': [38.8951, 38.8904, 38.8887],
'Longitude': [-77.0364, -77.0320, -77.0321]
})
geometry = [Point(xy) for xy in zip(points.Longitude, points.Latitude)]
points_gdf = gpd.GeoDataFrame(points, geometry=geometry)
points_gdf.plot(ax=ax, color='red', markersize=10)
# 绘制线和面元素
lines = gpd.GeoDataFrame({
'Name': ['Line 1'],
'geometry': [LineString([(0, 0), (1, 1)])]
})
lines.plot(ax=ax, color='green')
polygons = gpd.GeoDataFrame({
'Name': ['Polygon 1'],
'geometry': [Polygon([(0, 0), (1, 1), (1, 0)])]
})
polygons.plot(ax=ax, color='blue', alpha=0.5)
# 显示画布
plt.show()
```
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)