使用 Python 将 LineString 转换为多边形

问题描述 投票:0回答:1

我正在尝试将几何图形从 type: LineString 转换为 type:Polygon,其中每行数据都是坐标列表,同时我尝试为列表中的每个坐标添加 2 英里半径作为缓冲区,输出应该是多边形。请参阅以下示例数据:

{

"type": "FeatureCollection",

"name": "Sample_lines",

"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },

"features": [

{ "type": "Feature", "properties": { "OBJECTID": 123, "GLOBAL_ID": "8CAB8A", "IDENT": "41",  "TYPE": "N",  "Shape__Length": 0.2733 }, "geometry": { "type": "LineString", "coordinates": [ [ -112.400011882673994, 41.0833390325461, 0.0 ], [ -112.56667894652, 41.300005042600802, 0.0 ] ] } },

{ "type": "Feature", "properties": { "OBJECTID": 124, "GLOBAL_ID": "9ACAVB", "IDENT": "45",  "TYPE": "N",  "Shape__Length": 0.1573 }, "geometry": { "type": "LineString", "coordinates": [ [ -112.56667894652, 41.300005042600802, 0.0 ], [ -112.650011982188005, 41.4333400501312, 0.0 ] ] } },

{ "type": "Feature", "properties": { "OBJECTID": 125, "GLOBAL_ID": "5ACBFA", "IDENT": "48",  "TYPE": "N",  "Shape__Length": 0.4599 }, "geometry": { "type": "LineString", "coordinates": [ [ -112.650011982188005, 41.4333400501312, 0.0 ], [ -113.100012081374004, 41.5000060205737, 0.0 ] ] } }

]

}

这是我到目前为止所尝试过的,它抛出如下错误:
缓冲区 = gpd.points_from_xy([(x,y)]) 类型错误:points_from_xy() 缺少 1 个必需的位置参数:'y'。

请参阅下面的代码我试图做什么:


import json
import geopandas as gpd
from shapely.geometry import MultiPolygon

# Load geojson 
with open('Sample_lines.geojson') as f:
    gj = json.load(f)

features = []
for f in gj['features']:

    coords = f['geometry']['coordinates']
    print(coords[0][0])
    print(coords[0][1])
    print(tuple(coords[0]))
    print(coords)
    #quit()
    # Buffer points along line
    buffers = []
    for x,y,z in tuple(coords):
        buffer = gpd.points_from_xy([(x,y)])
        buffer = buffer.to_crs(epsg=4326)
        buffer = buffer.buffer(2)
        buffers.append(buffer)
        
    # Create multipolygon from buffers    
    mpolygon = MultiPolygon(buffers)
    
    # Create feature
    features.append(
        {'geometry': gpd.GeoSeries(mpolygon).__geo_interface__,
         'properties': f['properties']}
    )

# Output new GeoJSON    
new_gj = {
  "type": "FeatureCollection",
  "features": features
}

# output in .GeoJSON

with open('lines2Polygon.geojson','w') as f:
    dump(new_gj, f)

print(new_gj)

我哪里错了?预先感谢您的时间和支持。

python pandas geojson geopandas shapely
1个回答
0
投票

有一些错误:

  • 坐标数组不应该被转换为元组,它们已经准备好循环了
  • 你应该通过points_from_xy中的crs

创建的多边形将无效,因为所有单独的缓冲区都会重叠。我想您更愿意在各个缓冲区上应用 shapely.union_all 以避免这些重叠?我将其包含在下面的脚本中。

以下脚本运行:

from pathlib import Path
import json
import geopandas as gpd
from matplotlib import pyplot as plt
import shapely
from shapely import plotting

# Load geojson
with open(Path(__file__).with_suffix(".geojson")) as f:
    gj = json.load(f)

features = []
for f in gj["features"]:
    coords = f["geometry"]["coordinates"]
    print(coords[0][0])
    print(coords[0][1])
    print(tuple(coords[0]))
    print(coords)
    # quit()
    # Buffer points along line
    buffers = []
    for x, y, z in coords:
        buffer = gpd.points_from_xy([x], [y], crs=4326)
        buffer = buffer.buffer(2)
        buffers.append(buffer)

    # Create multipolygon from buffers
    # mpolygon = shapely.MultiPolygon(buffers)
    mpolygon = shapely.union_all(buffers)
    plotting.plot_polygon(mpolygon)

    # Create feature
    features.append(
        {
            "geometry": gpd.GeoSeries(mpolygon).__geo_interface__,
            "properties": f["properties"],
        }
    )

# Output new GeoJSON
new_gj = {"type": "FeatureCollection", "features": features}

# output in .GeoJSON
with open("lines2Polygon.geojson", "w") as f:
    json.dump(new_gj, f)

print(new_gj)
plt.show()
© www.soinside.com 2019 - 2024. All rights reserved.