I'm pretty new to Python so the answer to this question is probably quite simple, but I've looked everywhere and tried a lot but couldn't find the answer.
Simplifying a polygon using Shapely may result in an empty polygon. I want to replace the polygon with a point if the polygon is empty. Something that would work like:
if mypoly is empty:
mypoly = [(0,0)]
A geometry is empty if it does not have any points. Curves and multicurves may be closed. The values of some geometry subtypes (linestrings, multipoints, and multilinestrings) are either simple or non-simple.
Shapely is a BSD-licensed Python package for manipulation and analysis of planar geometric objects. It is based on the widely deployed GEOS (the engine of PostGIS) and JTS (from which GEOS is ported) libraries.
There are basically two ways of conducting PIP in Shapely: using a function called within() that checks if a point is within a polygon. using a function called contains() that checks if a polygon contains a point.
A LineString has zero area and non-zero length. >>> from shapely.geometry import LineString >>> line = LineString([(0, 0), (1, 1)]) >>> line. area 0.0 >>> line. length 1.4142135623730951. Its x-y bounding box is a (minx, miny, maxx, maxy) tuple.
Given that mypoly
is a shapely polygon, you can check if it's empty using is_empty
which is built in to Shapely to check for empty ones.
from shapely.geometry import Point
if mypoly.is_empty:
mypoly = Point(0, 0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With