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

如何使用GeoPandas读取KML文件中的多边形几何要素?

解决GeoPandas读取KML仅获取点要素的问题

核心方案:遍历筛选多边形要素

Fiona的KML驱动默认读取可能受KML层级结构(如Folder嵌套)或几何类型过滤影响,导致多边形要素未被加载。可以通过遍历所有要素并筛选目标几何类型的方式提取多边形:

import fiona
import geopandas as gpd
from shapely.geometry import shape

inputfile = 'path to kml file'

# 启用KML读写驱动
fiona.supported_drivers['KML'] = 'rw'

# 遍历KML文件,筛选多边形/多多边形要素
polygon_features = []
with fiona.open(inputfile, driver='KML') as src:
    for feature in src:
        geom_type = feature['geometry']['type']
        if geom_type in ('Polygon', 'MultiPolygon'):
            polygon_features.append({
                'geometry': shape(feature['geometry']),
                'properties': feature['properties']
            })

# 转换为GeoDataFrame,KML默认坐标系为WGS84(EPSG:4326)
sp = gpd.GeoDataFrame.from_features(polygon_features, crs="EPSG:4326")

额外排查步骤

  1. 检查KML结构:确认多边形要素的Placemark标签内包含<Polygon>或<MultiPolygon>节点,而非嵌套在未被Fiona识别的复杂结构中。
  2. 验证KML要素:用ogrinfo命令行工具查看KML的完整要素信息,确认多边形确实存在:
ogrinfo -al "path to kml file"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.14 05:06:02