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

使用Python和Earth Engine调整GeoJSON中LineString坐标的首尾顺序

批量反转GeoJSON中LineString坐标顺序解决方案

你可以根据自己的使用环境选择对应方案:

本地Python处理方案

适合直接处理本地存储的.geojson文件,支持高效处理数千条要素:

  • 方案1(推荐,基于geopandas):
    先安装依赖:pip install geopandas
    参考代码:
    import geopandas as gpd
    
    # 读取原始GeoJSON文件
    gdf = gpd.read_file("你的原始文件路径.geojson")
    
    # 反转所有LineString的坐标顺序
    gdf.geometry = gdf.geometry.apply(lambda geom: geom.reverse())
    
    # 保存处理后的GeoJSON
    gdf.to_file("反转后的文件路径.geojson", driver="GeoJSON", encoding="utf-8")
    
  • 方案2(基于原生json库,无需额外安装空间库):
    import json
    
    # 读取原始文件
    with open("原始文件路径.geojson", "r", encoding="utf-8") as f:
        data = json.load(f)
    
    # 遍历每个要素反转坐标
    for feat in data["features"]:
        if feat["geometry"]["type"] == "LineString":
            feat["geometry"]["coordinates"] = feat["geometry"]["coordinates"][::-1]
    
    # 保存处理后的新文件
    with open("反转后的文件路径.geojson", "w", encoding="utf-8") as f:
        json.dump(data, f, indent=2, ensure_ascii=False)
    

Google Earth Engine(GEE)处理方案

如果你的样带数据已经上传为GEE的FeatureCollection,可以直接在GEE环境中批量处理:

  • JavaScript版GEE代码:
    // 替换为你自己的样带集合资源ID
    var transects = ee.FeatureCollection("你的资源ID");
    
    // 定义反转LineString坐标的函数
    var reverseLine = function(feature) {
      var coords = feature.geometry().coordinates().reverse();
      return feature.setGeometry(ee.Geometry.LineString(coords));
    };
    
    // 批量处理所有样带
    var reversedTransects = transects.map(reverseLine);
    
    // 可直接用于后续分析,也可导出为GeoJSON到云盘
    Export.table.toDrive({
      collection: reversedTransects,
      description: 'reversed_transects',
      fileFormat: 'GeoJSON'
    });
    
  • Python版GEE API代码:
    import ee
    ee.Initialize()
    
    # 替换为你自己的样带集合资源ID
    transects = ee.FeatureCollection("你的资源ID")
    
    def reverse_line(feature):
        coords = feature.geometry().coordinates().reverse()
        return feature.setGeometry(ee.Geometry.LineString(coords))
    
    reversed_transects = transects.map(reverse_line)
    # 后续可直接调用分析接口,或执行导出操作
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 03:24:04