Skip to content

Instantly share code, notes, and snippets.

@bbuechler
Last active December 22, 2022 19:13
Show Gist options
  • Save bbuechler/73beea11341fa3ddda97c3494d30ae64 to your computer and use it in GitHub Desktop.
Save bbuechler/73beea11341fa3ddda97c3494d30ae64 to your computer and use it in GitHub Desktop.
Find NW/NE/SE/SW corners of a polygon
import shapely.wkt
#wkt = 'POLYGON ((-164.488235 53.59074, -163.863495 55.320713, -167.797485 55.721722, -168.259628 53.987499, -164.488235 53.59074))'
wkt = 'POLYGON ((-166.071014 55.533596, -165.424973 53.755867, -161.644806 54.152309, -162.122131 55.934521, -166.071014 55.533596))'
# Load polygon and order the points by LATITUDE
poly = shapely.wkt.loads(wkt)
points = poly.exterior.coords[:4]
points.sort(key = lambda x: x[1])
# Compare the longitude of the two southern points and 2 northern points
(sw, se) = (points[0], points[1]) if points[0][0] < points[1][0] else (points[1], points[0])
(nw, ne) = (points[2], points[3]) if points[2][0] < points[3][0] else (points[3], points[2])
print(f"NE Corner: GEOMETRYCOLLECTION({wkt},POINT({ne[0]} {ne[1]}))")
print(f"NW Corner: GEOMETRYCOLLECTION({wkt},POINT({nw[0]} {nw[1]}))")
print(f"SE Corner: GEOMETRYCOLLECTION({wkt},POINT({se[0]} {se[1]}))")
print(f"SW Corner: GEOMETRYCOLLECTION({wkt},POINT({sw[0]} {sw[1]}))")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment