question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Throw error when creating a MultiPoylgon with infinite edges

See original GitHub issue

Expected behavior and actual behavior.

As per the doc:

Figure 7. … on the right, a MultiPolygon that is invalid because its members touch at an infinite number of points (along a line).

Thus shapely should throw an error when creating a MultiPoylgon with infinite edges. (I caught this error when trying to save such a geometry with GeoPandas, and it would always convert the MultiPolygon into a single Polygon.)

Steps to reproduce the problem.

This test should throw an error:

from shapely.geometry import Polygon, MultiPolygon

polygon1 = Polygon(((0, 0), 
                    (0, 1), 
                    (1, 1),
                    (1, 0),
                    (0, 0)))

polygon2 = Polygon(((1, 0), 
                    (1, 1), 
                    (2, 1),
                    (2, 0),
                    (1, 0)))

multipolygon = MultiPolygon([polygon1, polygon2])

Operating system

Linux/Ubuntu

Shapely version and provenance

Shapely 1.6.4.post2 Python 3.5

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
sgilliescommented, Nov 5, 2018

@asmith26 the GEOS library used by Shapely has a global context but that context doesn’t give us anything to strictly require valid geometries. We’d need to do this entirely in Python. If you want it to be entirely transparent, so that Polygon(..) raises an exception when invalid, I think the thing to do would be to write a Python context manager that patches all the Shapely geometry constructors when __enter__() is called and then restores them when __exit__() is called. I like writing code like this, but I’m not keen to include it in Shapely. Adding features to the library requires more tests and more documentation and I’m super short on time right now.

1reaction
asmith26commented, Nov 1, 2018

Thank @kannes thanks for this information.

Perhaps there could be a configuration option that one could add to the start of their script to e.g. automatically check validity of all geometries created by the script (and perhaps throw an error instead of just saying “False”). (I appreciate such configurations may add unwanted complications, but I thought I would ask in case something similar already exists or if it inspires other suggestions.)

Read more comments on GitHub >

github_iconTop Results From Across the Web

geom_sf(): "polygon edge not found" intermittent error #2252
I am using geom_sf to do some county maps based on tigris data. I successfully followed some examples, but when I try to...
Read more >
Merging a list of Polygons to Multipolygons - Stack Overflow
Great Answer! Taking directly MultiPolgyon() does not throw errors but results in faulty results. Tested when combining polygons and ...
Read more >
Advanced geospatial analysis - Turf.js
Finds the tangents of a (Multi)Polygon from a Point. ... Infinity - convex hull. ... Throws. Error - if geoJson is invalid. npm...
Read more >
Splitting an arbitrary polygon by a line - The Infinite Loop
Essentially, one iterates over the polygon's vertices, checks on which side of the split line they lie and adds them accordingly either to...
Read more >
Polygon node order will not correct - sql - GIS Stack Exchange
A common reason for this error is that a polygon has the wrong ring orientation. To create a larger than hemisphere geography instance, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found