斯普利特在每个点上它跨越了多边形匀称线串

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

我有横跨各种多边形,存储为GeoJsons线串。我想每个多边形区域内分割线成各个部分。我一直无法然而还没有实现这一目标。这是我到目前为止有重复的例子:

from shapely.geometry import Point, LineString, Polygon
from shapely.ops import split
from matplotlib import pyplot as plt

poly = {
    'type': "Feature",
    'geometry': {
        "type": "Polygon",
        "coordinates": ([(2,2),(2,4),(4,4),(4,2)]),
    },
    'properties': {
        'id': 'A'
    }
}

line = {
    'type': "Feature",
    'geometry': {
        "type": "Linestring",
        "coordinates": ([(3,3),(5,1)]),
    },
    'properties': {
        'id': 'A'
    }
}

poly = Polygon(poly['geometry']['coordinates'])
line = LineString(line['geometry']['coordinates'])

x,y = poly.exterior.xy
x2,y2 = line.coords

plt.plot(x, y, x2, y2)
plt.show()

此代码产生以下方多边形线串交叉它:enter image description here

然后我试着通过多边形分割线,像这样:

new_lines = split(line, poly)

但我得到以下输出这似乎不正确的是:

GEOMETRYCOLLECTION (LINESTRING (3 3, 4 2), LINESTRING (4 2, 5 1))

我期待三线,一个现有的方形的多边形内,然后两个多边形外侧分别现有。

python geometry shapely
1个回答
0
投票

它看起来像拆分工作如预期,但行的坐标绘制在一个误导的方式 - 应该使用.xy获得行xsys与多边形:

from shapely.geometry import Point, LineString, Polygon
from shapely.ops import split
from matplotlib import pyplot as plt

poly = Polygon([(2,2),(2,4),(4,4),(4,2)])
original_line = LineString([(3,3),(5,1)])
line = LineString([(3,1), (3,5)])
x, y = poly.exterior.xy
x2, y2 = line.xy
x3, y3 = original_line.xy

plt.plot(x, y, x2, y2)
plt.show()

plt.plot(x, y, x3, y3)
plt.show()

image with original coords

image with expected coords

>>> print(split(poly, line))
GEOMETRYCOLLECTION (LINESTRING (3 1, 3 2), LINESTRING (3 2, 3 4), LINESTRING (3 4, 3 5))
© www.soinside.com 2019 - 2024. All rights reserved.