#!/usr/bin/env python#-*-coding:utf-8-*- fromMatplotlibImportPyplot fromDescartesImportPolygonpatch fromShapely.opsImportpolygonize fromShapely.geometryImportPolygon, LineString, Point fromUtilsImportSIZE, BLUE fromUtilsImportPlot_coords_line fromUtilsImportPlot_line fromUtilsImportSet_plot_bounds#Setup Matplotlib figure that would display the resultsFig = pyplot.figure (1, Figsize=size, dpi=90, facecolor=" White")#add a little more space around subplotsFig.subplots_adjust (hspace=.5)# ####################################First plot#Display sample line and Circle# ####################################First figure upper left drawing#121 represents the Number_rows, Num_cols, subplot numberAx = fig.add_subplot (121)#Our demonstration geometries to see the detailsline = LineString ([(0, 1), (3, 1), (0, 0)]) Polygon= Polygon (Point (1.5, 1). Buffer (1))#Use of Descartes to create polygon in MatplotlibPatch1 = Polygonpatch (Polygon, fc=BLUE, EC=blue, alpha=0.5, zorder=1)#Add Circle to AxisAx.add_patch (PATCH1)#add line using our function aboveplot_line (ax, line)#draw the line nodes using our functionplot_coords_line (ax, line)#Subplot title textAx.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 sidesX_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 limitsAx.set_xlim (X_range) Ax.set_ylim (y_range)#assing the aspect ratioAx.set_aspect (1)######################################Second Plot#Display Sample Intersection# ###################################Ax= Fig.add_subplot (122)#Convert Circle Polygon to linestring of Circle boundaryCirle_as_line =polygon.boundary#combine new boundary lines with the input set of linesResult_union_lines =cirle_as_line.union (line)#re-create polygons from unioned linesNew_polygons =polygonize (result_union_lines)#stores the final split up polygonsNew_cut_ply = []#identify which new polygon we want to keep forPolyinchnew_polygons:#Check if new poly is inside original otherwise ignore it ifPoly.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 this overlap original for exportnew_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 plotAx.set_title ('Line intersects Circle')#define The area that plot would fit intoX_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 ()
Apply lines to cut polygons