Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

shapely is_valid for polygons in 3D

I'm trying to validate some polygons that are on planes with is_valid, but I get Too few points in geometry component at or near point for polygons where the z is not constant.

Is there a way to validate these other polygons?

Here's an example:

from shapely.geometry import Polygon


poly1 = Polygon([(0,0), (1,1), (1,0)])
print(poly1.is_valid)
# True

# z=1
poly2 = Polygon([(0,0,1), (1,1,1), (1,0,1)])
print(poly2.is_valid)
# True

# x=1
poly3 = Polygon([(1,0,0), (1,1,1), (1,1,0)])
print(poly3.is_valid) 
# Too few points in geometry component at or near point 1 0 0
# False
like image 613
user3747260 Avatar asked Oct 19 '25 15:10

user3747260


1 Answers

The problem is that shapely in fact ignores the z coordinate. So, as far as shapely can tell you are building a polygon with the points [(1,0),(1,1), (1,1)] that aren't enough to build a polygon.

See this other SO question for more information: python-polygon-does-not-close-shapely.

IMHO, shapely shouldn't allow three dimension coordinates, because it brings this kind of confusions.

like image 143
eguaio Avatar answered Oct 22 '25 06:10

eguaio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!