Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a polygon is empty in Shapely?

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)]
like image 684
user2957487 Avatar asked Nov 05 '13 18:11

user2957487


People also ask

What is an empty geometry?

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.

What is wkt shapely?

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.

Is Point in polygon shapely?

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.

What is LineString in Python?

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.


1 Answers

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)
like image 58
min Avatar answered Oct 15 '22 16:10

min