import matplotlib.pyplot as plt
import pyproj
from shapely import Point, Polygon
from shapely.ops import transform
"figure.facecolor"] = (1, 1, 1, 0) # RGBA tuple with alpha=0
plt.rcParams["axes.facecolor"] = (1, 1, 1, 0) # RGBA tuple with alpha=0 plt.rcParams[
Shapely
= Polygon([[0, 0], [1, 0], [1, 1], [0, 1], [0, 0]])
pol1
print(pol1.area)
print(pol1.length)
print(pol1.bounds)
print(pol1.centroid)
print(pol1.representative_point())
print(pol1.exterior)
print(pol1.exterior.xy)
= plt.subplots(figsize=(4, 4))
fig, ax *pol1.exterior.xy)
ax.plot(*pol1.centroid.xy, "o")
ax.plot(# ax.set_aspect('equal')
plt.show()
1.0
4.0
(0.0, 0.0, 1.0, 1.0)
POINT (0.5 0.5)
POINT (0.5 0.5)
LINEARRING (0 0, 1 0, 1 1, 0 1, 0 0)
(array('d', [0.0, 1.0, 1.0, 0.0, 0.0]), array('d', [0.0, 0.0, 1.0, 1.0, 0.0]))
def set_limits(ax, x0, xN, y0, yN):
ax.set_xlim(x0, xN)range(x0, xN + 1))
ax.set_xticks(
ax.set_ylim(y0, yN)range(y0, yN + 1))
ax.set_yticks("equal") ax.set_aspect(
from shapely.plotting import plot_polygon, plot_points
plot_polygon??
Signature:
plot_polygon(
polygon,
ax=None,
add_points=True,
color=None,
facecolor=None,
edgecolor=None,
linewidth=None,
**kwargs,
)
Source:
def plot_polygon(
polygon,
ax=None,
add_points=True,
color=None,
facecolor=None,
edgecolor=None,
linewidth=None,
**kwargs
):
"""
Plot a (Multi)Polygon.
Note: this function is experimental, and mainly targetting (interactive)
exploration, debugging and illustration purposes.
Parameters
----------
polygon : shapely.Polygon or shapely.MultiPolygon
ax : matplotlib Axes, default None
The axes on which to draw the plot. If not specified, will get the
current active axes or create a new figure.
add_points : bool, default True
If True, also plot the coordinates (vertices) as points.
color : matplotlib color specification
Color for both the polygon fill (face) and boundary (edge). By default,
the fill is using an alpha of 0.3. You can specify `facecolor` and
`edgecolor` separately for greater control.
facecolor : matplotlib color specification
Color for the polygon fill.
edgecolor : matplotlib color specification
Color for the polygon boundary.
linewidth : float
The line width for the polygon boundary.
**kwargs
Additional keyword arguments passed to the matplotlib Patch.
Returns
-------
Matplotlib artist (PathPatch), if `add_points` is false.
A tuple of Matplotlib artists (PathPatch, Line2D), if `add_points` is true.
"""
from matplotlib import colors
if ax is None:
ax = _default_ax()
if color is None:
color = "C0"
color = colors.to_rgba(color)
if facecolor is None:
facecolor = list(color)
facecolor[-1] = 0.3
facecolor = tuple(facecolor)
if edgecolor is None:
edgecolor = color
patch = patch_from_polygon(
polygon, facecolor=facecolor, edgecolor=edgecolor, linewidth=linewidth, **kwargs
)
ax.add_patch(patch)
ax.autoscale_view()
if add_points:
line = plot_points(polygon, ax=ax, color=color)
return patch, line
return patch
File: ~/.pyenv/versions/3.11.1/envs/py3111/lib/python3.11/site-packages/shapely/plotting.py
Type: function
import matplotlib.pyplot as plt
from matplotlib.patches import PathPatch
from matplotlib.path import Path
from shapely.geometry import Polygon
import numpy as np
def plot_shapely_polygon(polygon, ax=None, **kwargs):
if isinstance(polygon, Polygon):
= [polygon]
polygons else:
= list(polygon.geoms)
polygons
for polygon in polygons:
= np.array(polygon.exterior.coords)
exterior_coords = [np.array(ring.coords) for ring in polygon.interiors]
interior_coords
= [Path.MOVETO]
codes * (len(exterior_coords) - 2))
codes.extend([Path.LINETO]
codes.append(Path.CLOSEPOLY)
for ring in interior_coords:
codes.append(Path.MOVETO)* (len(ring) - 2))
codes.extend([Path.LINETO]
codes.append(Path.CLOSEPOLY)
= np.vstack([exterior_coords] + interior_coords)
all_coords
= Path(all_coords, codes)
path = PathPatch(path, **kwargs)
patch ax.add_patch(patch)
= plt.subplots(1, 4, figsize=(4, 16), dpi=90)
fig, axes
= [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)]
ext int = [(1, 0), (0.5, 0.5), (1, 1), (1.5, 0.5), (1, 0)][::-1]
= Polygon(ext, [int])
polygon
=axes[0], add_points=False)
plot_polygon(polygon, ax# plot_points(polygon, ax=axes[0], alpha=0.7)
0].set_title("a) valid")
axes[
0], -1, 3, -1, 3)
set_limits(axes[
= [(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)]
ext int = [(1, 0), (0, 1), (0.5, 1.5), (1.5, 0.5), (1, 0)][::-1]
= Polygon(ext, [int])
polygon
=axes[1], add_points=False)
plot_polygon(polygon, ax=axes[1], alpha=0.7)
plot_points(polygon, ax
1].set_title("b) invalid")
axes[
1], -1, 3, -1, 3)
set_limits(axes[
= polygon.exterior.xy
x, y
2].fill(x, y, alpha=0.5, fc="r")
axes[2].plot(x, y, marker="o", color="r")
axes[
for interior in polygon.interiors:
= interior.xy
x, y 2].fill(x, y, alpha=0, fc=(1, 1, 1, 0)) # fill with white to create a hole effect
axes[2].plot(x, y, marker="o", color="r")
axes[
# Customize plot
2].set_title("Shapely Polygon")
axes[
2], -1, 3, -1, 3)
set_limits(axes[
= np.array(polygon.exterior.coords)
exterior_coords = [np.array(ring.coords) for ring in polygon.interiors]
interior_coords
= [Path.MOVETO]
codes * (len(exterior_coords) - 2))
codes.extend([Path.LINETO]
codes.append(Path.CLOSEPOLY)
for ring in interior_coords:
codes.append(Path.MOVETO)* (len(ring) - 2))
codes.extend([Path.LINETO]
codes.append(Path.CLOSEPOLY)
= np.vstack([exterior_coords] + interior_coords)
all_coords
= Path(all_coords, codes)
path = PathPatch(path, facecolor="r", edgecolor="r", alpha=0.5)
patch 3].add_patch(patch)
axes[
3].plot(x, y, marker="o", color="r")
axes[
3].set_title("Matplotlib PathPatch")
axes[
3], -1, 3, -1, 3)
set_limits(axes[
plt.show()
all_coords
array([[0. , 0. ],
[0. , 2. ],
[2. , 2. ],
[2. , 0. ],
[0. , 0. ],
[1. , 0. ],
[1.5, 0.5],
[0.5, 1.5],
[0. , 1. ],
[1. , 0. ]])
Transforms
= Point(-72.2495, 43.886)
wgs84_pt
= pyproj.CRS("EPSG:4326")
wgs84 = pyproj.CRS("EPSG:32618")
utm
= pyproj.Transformer.from_crs(wgs84, utm, always_xy=True).transform
project = transform(project, wgs84_pt)
utm_point
utm_point.xy
(array('d', [720944.1103566973]), array('d', [4862889.031679545]))