
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from matplotlib import pyplot
from descartes import PolygonPatch
from shapely.ops import polygonize
from shapely.geometry import Polygon, LineString, Point
from utils import SIZE, BLUE
from utils import plot_coords_line
from utils import plot_line
from utils import set_plot_bounds
# setup matplotlib figure that will display the results
fig = pyplot.figure(1, figsize=SIZE, dpi=90, facecolor="white")
# add a little more space around subplots
fig.subplots_adjust(hspace=.5)
# ###################################
# first plot
# display sample line and circle
# ###################################
# first figure upper left drawing
# 121 represents the number_rows, num_cols, subplot number
ax = fig.add_subplot(121)
# our demonstration geometries to see the details
line = LineString([(0, 1), (3, 1), (0, 0)])
polygon = Polygon(Point(1.5, 1).buffer(1))
# use of descartes to create polygon in matplotlib
patch1 = PolygonPatch(polygon, fc=BLUE,
ec=BLUE, alpha=0.5, zorder=1)
# add circle to axis in figure
ax.add_patch(patch1)
# add line using our function above
plot_line(ax, line)
# draw the line nodes using our function
plot_coords_line(ax, line)
# subplot title text
ax.set_title('Input line and circle')
# define axis ranges as list [x-min, x-max]
# added 1.5 units around object so not touching the sides
x_range = [polygon.bounds[0] - 1.5, polygon.bounds[2] + 1.5]
# y-range [y-min, y-max]
y_range = [polygon.bounds[1] - 1.0, polygon.bounds[3] + 1.0]
# set the x and y axis limits
ax.set_xlim(x_range)
ax.set_ylim(y_range)
# assing the aspect ratio
ax.set_aspect(1)
#####################################
# second plot
# display sample intersection
# ###################################
ax = fig.add_subplot(122)
# convert circle polygon to linestring of circle boundary
cirle_as_line = polygon.boundary
# combine new boundary lines with the input set of lines
result_union_lines = cirle_as_line.union(line)
# re-create polygons from unioned lines
new_polygons = polygonize(result_union_lines)
# stores the final split up polygons
new_cut_ply = []
# identify which new polygon we want to keep
for poly in new_polygons:
# check if new poly is inside original otherwise ignore it
if poly.centroid.within(polygon):
# center_pt = poly.centroid
# ax.plot(center_pt.x, center_pt.y, 'o', color='#999999')
print("creating new split polygon")
patch3 = PolygonPatch(poly, fc='purple', alpha=0.5, zorder=2)
ax.add_patch(patch3)
# add only polygons that overlap original for export
new_cut_ply.append(poly)
else:
# draw centroid of new polygon NOT inside original polygon
# center_pt = poly.centroid
# ax.plot(center_pt.x, center_pt.y, 'o', color='#FF1813')
print("This polygon is outside of the input features")
# write title of second plot
ax.set_title('Line intersects circle')
# define the area that plot will fit into
x_range = set_plot_bounds(polygon, 1.5)['xrange']
y_range = set_plot_bounds(polygon, 1)['yrange']
ax.set_xlim(*x_range)
ax.set_ylim(*y_range)
ax.set_aspect(1)
pyplot.show()

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了