You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在卫星影像上叠加绘制GeodataFrame?

在卫星影像上叠加GeodataFrame的可运行示例

一、使用Contextily加载在线卫星底图并叠加GeoDataFrame

针对你遇到的在线底图加载问题,大概率是CRS不匹配或源访问限制,以下是稳定可运行的代码:

import geopandas as gpd
import matplotlib.pyplot as plt
import contextily as ctx
from shapely.geometry import Polygon

# 1. 创建示例GeoDataFrame(替换为你的数据)
# 初始数据用WGS84(EPSG:4326),后续转换为Web Mercator(contextily底图标准CRS)
polygon = Polygon([(116.3, 39.9), (116.4, 39.9), (116.4, 40.0), (116.3, 40.0)])
gdf = gpd.GeoDataFrame({'geometry': [polygon]}, crs="EPSG:4326")

# 2. 转换GeoDataFrame到Web Mercator
gdf_web = gdf.to_crs(epsg=3857)

# 3. 绘图并叠加底图
fig, ax = plt.subplots(figsize=(10, 10))
gdf_web.plot(ax=ax, facecolor='none', edgecolor='red', linewidth=2)

# 加载稳定的Esri卫星底图(若仍失败,可替换为ctx.providers.Stamen.TerrainBackground)
ctx.add_basemap(ax, source=ctx.providers.Esri.WorldImagery, crs=gdf_web.crs.to_string())

plt.title("在线卫星底图叠加GeoDataFrame")
plt.show()

关键注意事项:

  • 必须确保GeoDataFrame与底图CRS一致(Web Mercator,EPSG:3857)
  • 若Esri源仍无法访问,可替换为其他稳定源:ctx.providers.OpenStreetMap.Mapnik、ctx.providers.Stamen.Terrain等

二、加载本地Sentinel-2影像并叠加GeoDataFrame

针对本地S2影像无法显示的问题,需确保正确读取影像波段并匹配CRS,以下是可运行代码:

import geopandas as gpd
import rioxarray as rxr
import matplotlib.pyplot as plt
from shapely.geometry import Polygon

# 1. 创建示例GeoDataFrame(替换为你的数据)
polygon = Polygon([(116.3, 39.9), (116.4, 39.9), (116.4, 40.0), (116.3, 40.0)])
gdf = gpd.GeoDataFrame({'geometry': [polygon]}, crs="EPSG:4326")

# 2. 读取本地Sentinel-2影像(替换为你的影像路径,支持.tif/.jp2格式)
# 选择RGB波段(S2的B4=红, B3=绿, B2=蓝)
s2_img = rxr.open_rasterio("你的S2影像路径.tif").sel(band=[4,3,2])

# 3. 转换GeoDataFrame到影像的CRS
gdf_img_crs = gdf.to_crs(s2_img.rio.crs)

# 4. 绘图:先画卫星影像,再叠加GeoDataFrame
fig, ax = plt.subplots(figsize=(10, 10))
# 拉伸影像对比度(S2原始值需做线性拉伸)
s2_img.plot.imshow(ax=ax, robust=True, extent=s2_img.rio.bounds())
# 叠加矢量数据
gdf_img_crs.plot(ax=ax, facecolor='none', edgecolor='yellow', linewidth=3)

plt.title("本地Sentinel-2影像叠加GeoDataFrame")
plt.show()

关键注意事项:

  • 必须确保GeoDataFrame转换为影像的原生CRS
  • Sentinel-2影像需选择正确的RGB波段(B4/B3/B2),并通过robust=True做对比度拉伸,否则会因值范围过大显示全白/全黑
  • 若用plt.imread读取,需手动获取影像的地理范围和CRS,不如rioxarray便捷

内容的提问来源于stack exchange,提问作者ImanolUr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 09:57:20