使用 LineString Python Shapely 将圆一分为二

问题描述

我使用 Shapely 创建了一个圆,并想使用 Linestring 将其分成两个。

我创建的圆圈如下

from functools import partial

import pyproj
from shapely import geometry
from shapely.geometry import Point,polygon,shape,MultiPoint,Linestring,mapping 
from shapely.ops import transform,split

radius = 92600
lon = 54.08
lat = 17.05

local_azimuthal_projection = "+proj=aeqd +R=6371000 +units=m +lat_0={} +lon_0={}".format(
    lat,lon
)
wgs84_to_aeqd = partial(
    pyproj.transform,pyproj.Proj("+proj=longlat +datum=wgs84 +no_defs"),pyproj.Proj(local_azimuthal_projection),)
aeqd_to_wgs84 = partial(
    pyproj.transform,)

center = Point(float(lon),float(lat))
point_transformed = transform(wgs84_to_aeqd,center)
buffer = point_transformed.buffer(radius)
# Get the polygon with lat lon coordinates
circle_poly = transform(aeqd_to_wgs84,buffer)

对于行拆分器,我有以下内容

splitter = LingString([Point(54.79,16.90),Point(53.56,16.65)])

现在我想看到两个分割的形状,所以我使用了分割功能

result = split(circle_poly,splitter)

然而,这只会产生相同的圆,而不是两个形状。 最后我想用分裂的一部分来形成另一种形状。

解决方法

要分割圆或多边形,您可以对另一个多边形使用空间 difference 操作。 Shapely 不允许使用 line 这样做。

"Shapely can not represent the difference between an object and a lower dimensional object (such as the difference between a polygon and a line or point) as a single object."

See document:

在您的情况下,您可以构建两个以 line 作为公共边的多边形。 确保 2 个多边形加在一起足够大以覆盖您要分割的整个多边形。然后你使用这些多边形来完成这项工作。

如果您想要 difference 操作的粗略结果, 您可以通过buffer操作将线变成细多边形,并使用它。

对于第二种方法,这里是相关代码:

the_line = LineString([(54.95105,17.048144),(53.40473921,17.577181)])
buff_line = the_line.buffer(0.000001)  #is polygon

# the `difference` operation between 2 polygons
multi_pgon = circle_poly.difference(buff_line)

结果是一个多多边形几何对象。

diff-oper