Код пишем согласно документу
https://geopandas.org/en/stable/docs/reference.html
(.env) boris@boris-All-Series:~/MATPLOTLIBSR/SHAPELY$ cat shapelyPolygons1.py
from shapely.geometry import Polygon
import matplotlib.pyplot as plt
import geopandas as geogpd
polyg_first = Polygon([(0,5),(1,1),(5,0),(5,1),(4,3)])
gpl = geogpd.GeoSeries(polyg_first)
gpl.plot()
plt.show()
Есть опция работать и чисто с shapely.geometry, избегая импорта geopandas
(.env) boris@boris-All-Series:~/MATPLOTLIBSR/SHAPELY$ cat shapelyPolygons2.py
import numpy as np
import shapely.geometry as sgp
import matplotlib.pyplot as plt
import matplotlib.patches as patches
def add_polygon_patch(coords, ax, fc='blue'):
patch = patches.Polygon(np.array(coords.xy).T, fc=fc)
ax.add_patch(patch)
border = [(-10, -10), (-10, 10), (10, 10), (10, -10)]
holes = [[(-7, -5), (4, 7), (7, -2), (3,-2), (2,-9)]]
region = sgp.Polygon(shell=border, holes=holes)
fig, ax = plt.subplots(1, 1)
add_polygon_patch(region.exterior, ax)
for interior in region.interiors:
add_polygon_patch(interior, ax, 'orange')
ax.axis('equal')
plt.show()
Объект Shapely Polygon имеет атрибут external. Объект Shapely MultiPolygon имеет последовательность объектов Polygon. Вы должны перебирать эти полигоны. Вы можете сделать это, используя геометрические атрибуты MultiPolygon.
(.env) boris@boris-All-Series:~/MATPLOTLIBSR/SHAPELY$ cat shapelyPolygons3.py
import shapely.geometry as sgp
import shapely.ops as so
import matplotlib.pyplot as plt
area1 = sgp.Polygon([(0,0),(0,1),(1,1),(1,0),(0,0)])
area2 = sgp.box(0.5,0.5,1.5,1.5)
area3 = sgp.box(4,4,5,5)
new_shape = so.unary_union([area1, area2, area3])
fig, axs = plt.subplots()
axs.set_aspect('equal', 'datalim')
for geom in new_shape.geoms:
xs, ys = geom.exterior.xy
axs.fill(xs, ys, alpha=0.5, fc='black', ec='none')
plt.show()
No comments:
Post a Comment