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

如何基于GeoJSON数据在Python中绘制风羽图?

问题:基于GeoJSON格点风数据绘制风羽图

我有一个GeoJSON格式的格点风数据文件,所有几何要素均为点类型,每个要素的属性包含windSpeed(风速)和windDirection(风向)字段,想要用它生成风羽图。

我是Python新手,找到一个用NetCDF数据绘制风羽图的实现示例,但该示例依赖NetCDF数据源,我需要改成支持GeoJSON的方式。目前我尝试先通过geopandas将GeoJSON转成shapefile,再用gdal转成NetCDF,同时在修改示例适配数据结构,但不确定这个方法是否可行,也想知道有没有更优方案。

GeoJSON数据示例

{
    "type": "FeatureCollection",
    "totalFeatures": 2124,
    "features": [
        {
            "type": "Feature",
            "id": "1bdffe7b-af88-4f33-a792-e3a2a53c08b3",
            "geometry": {
                "type": "Point",
                "coordinates": [
                    -120,
                    40
                ]
            },
            "properties": {
                "altitude": 38615,
                "flightLevel": 400,
                "temperature": -60.619,
                "windSpeed": 18.9,
                "windDirection": 352.7
            }
        },
        ...
    ]
}

目前尝试的转换代码

import geopandas as gpd
import xarray as xr

df = gpd.read_file('./winds.json')
df[['altitude', 'flightLevel', 'temperature', 'windSpeed', 'windDirection', 'geometry']].to_file('./winds.shp')

from osgeo import gdal

inputfile = './winds.shp'
outputfile = './winds.nc'
# 这段代码还没跑通,暂时用命令行工具替代:ogr2ogr -F netCDF './winds.nc' './winds.shp'
gdal.Translate(outputfile, inputfile, format='NetCDF')
ds = xr.open_dataset('./winds.nc')

更优解决方案:直接从GeoPandas DataFrame绘制风羽图

不需要经过格式转换的中间步骤,直接用matplotlib结合metpy(Unidata示例依赖的库)即可处理GeoPandas数据,步骤如下:

  1. 安装依赖库
    先确保安装需要的包:

    pip install geopandas matplotlib metpy cartopy
    
  2. 直接读取GeoJSON并绘制风羽
    MetPy的WindBarb函数可直接接收坐标、风速、风向数据,无需转换格式:

    import geopandas as gpd
    import matplotlib.pyplot as plt
    import cartopy.crs as ccrs
    from metpy.plots import WindBarb
    from metpy.units import units
    
    # 读取GeoJSON文件
    gdf = gpd.read_file('./winds.json')
    
    # 提取核心数据
    lons = gdf.geometry.x.values
    lats = gdf.geometry.y.values
    # 根据你的数据实际单位调整,示例假设风速单位为节,若为m/s则替换为units.meters_per_second
    wind_speed = gdf['windSpeed'].values * units.knots
    wind_dir = gdf['windDirection'].values * units.degree
    
    # 创建绘图画布与地图投影
    fig, ax = plt.subplots(figsize=(10, 8), subplot_kw={'projection': ccrs.PlateCarree()})
    
    # 添加地图底图要素(可选)
    ax.coastlines()
    ax.gridlines(draw_labels=True)
    
    # 绘制风羽
    WindBarb(ax, lons, lats, wind_speed, wind_dir, pivot='middle', color='black')
    
    plt.title('格点风场风羽图')
    plt.show()
    

关键说明:

  • 风速单位需与MetPy兼容,根据数据实际情况调整(比如节、米/秒),MetPy的units模块会自动处理单位转换逻辑。
  • 如果需要叠加等高线等其他要素(和Unidata示例一致),只需从GeoDataFrame中提取对应字段(比如altitude),用matplotlib的等高线函数绘制即可。
  • 该方法跳过了格式转换的中间环节,避免了shapefile和NetCDF转换可能出现的字段丢失、坐标格式错误等问题,效率更高且更稳定。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 09:55:32