Python如何将包含几何信息的字典列表转换为geopandas DataFrame?
字典列表转GeoPandas GeoDataFrame实现方案
你提供的列表中每个元素都是标准GeoJSON要素结构,可直接通过GeoPandas内置方法快速转换,无需手动拼接坐标。
方法1:直接调用内置接口(推荐)
无需额外处理几何对象,接口会自动解析GeoJSON结构生成标准空间字段:
import geopandas as gpd # 你的原始字典列表 myList = [ {'properties': {'raster_val': 159.6666717529297}, 'geometry': {'type': 'Polygon', 'coordinates': [[(0.016648035783361492, 5.810164488393165), (0.016648035783361492, 5.801840470501484), (0.024972053675042183, 5.801840470501484), (0.024972053675042183, 5.810164488393165), (0.016648035783361492, 5.810164488393165)]]}}, {'properties': {'raster_val': 176.6071014404297}, 'geometry': {'type': 'Polygon', 'coordinates': [[(0.024972053675042183, 5.810164488393165), (0.024972053675042183, 5.801840470501484), (0.033296071566722985, 5.801840470501484), (0.033296071566722985, 5.810164488393165), (0.024972053675042183, 5.810164488393165)]]}} ] # 生成GeoDataFrame gdf = gpd.GeoDataFrame.from_features(myList) # 可选:指定坐标系为WGS84 # gdf.crs = "EPSG:4326"
方法2:手动解析构造
如果需要自定义字段处理逻辑,可手动遍历生成:
import geopandas as gpd from shapely.geometry import Polygon res = [] for item in myList: # 提取属性值 raster_val = item['properties']['raster_val'] # 构造Polygon几何对象 geometry = Polygon(item['geometry']['coordinates'][0]) res.append({"raster_val": raster_val, "geometry": geometry}) gdf = gpd.GeoDataFrame(res, crs="EPSG:4326")
输出结果
两种方法得到的结果完全一致,geometry列原生存储Shapely Polygon格式空间数据,打印输出时自动显示为WKT文本格式,和你给出的预期效果一致:
raster_val geometry 0 159.666672 POLYGON ((0.01665 5.81016, 0.01665 5.80184, 0.0249... 1 176.607101 POLYGON ((0.02497 5.81016, 0.02497 5.80184, 0.0333...
内容的提问来源于stack exchange,提问作者emax
相关产品推荐
相关产品推荐

