使用线型Python将圆拆分为两个

2024-09-30 22:14:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我用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,
    pyproj.Proj(local_azimuthal_projection),
    pyproj.Proj("+proj=longlat +datum=WGS84 +no_defs"),
)

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)

但是,这只会产生相同的圆,而不是两个形状。 在结束时,我想使用分割的一部分,以形成另一个形状


Tags: tofromimportlocaltransformpartialpointproj
1条回答
网友
1楼 · 发布于 2024-09-30 22:14:48

要分割圆或多边形,可以对另一个多边形使用空间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作为公共边的多边形。 确保两个多边形一起足够大,以覆盖要拆分的整个多边形。然后你用这个多边形来做这个工作

如果希望从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

相关问题 更多 >