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.

Polygon approximate equality vs. exact equality.

See original GitHub issue

Expected behavior and actual behavior.

I expected polygon1.almost_equals(polygon2) to always be true if polygon1.equals(polygon2) is true. That is, I expected approximate equality to hold if exact equality holds.

Actually, approximate equality might not hold when exact equality holds for polygons. This generalizes to multipolygons, and has to do with the order in which primitive parts are provided to the constructor.

Steps to reproduce the problem.

import shapely.geometry as g
import numpy as np

unit_square = np.array([(0,0), (0,1), (1,1), (1,0)])
unit_hole = unit_square * .5 + .25
smaller_hole = unit_hole * .25

# reverse the order of the holes
pgon1 = geom.Polygon(shell=unit_square, holes=[unit_hole, smaller_hole])
pgon2 = geom.Polygon(shell=unit_square, holes=[smaller_hole, unit_hole])

# both are valid
pgon1.is_valid
# True
pgon2.is_valid
# True

# The relates string matches the equality predicate:
pgon1.relate(pgon2)
# "2FFF1FFF2"
# They're equivalent:
pgon1.equals(pgon2)
# True
# But not approximately equivalent
pgon1.almost_equals(pgon2)

# likewise in multipolygons, rearranging the order of elements within a polygon or across 
# elements of the multipolygon keeps equality but breaks almost-equality
mpgon1 = geom.MultiPolygon([pgon1, geom.Polygon(unit_square + 1.1)])
mpgon2 = geom.MultiPolygon([pgon2, geom.Polygon(unit_square + 1.1)])
mpgon3 = geom.MultiPolygon([geom.Polygon(unit_square + 1.1), pgon1])
mpgon4 = geom.MultiPolygon([geom.Polygon(unit_square + 1.1), pgon2])
mpgons = [mpgon1, mpgon2, mpgon3, mpgon4]

almost_equal_table = [[mpgon.almost_equals(other_mpgon) for mpgon in mpgons]
                       for other_mpgon in mpgons]
print(np.array(almost_equal_table))
# array([[ True, False, False, False],
#       [False,  True, False, False],
#       [False, False,  True, False],
#       [False, False, False,  True]], dtype=bool)

equal_table = [[mpgon.equals(other_mpgon) for mpgon in mpgons]
                for other_mpgon in mpgons]
print(equal_table)
# array([[ True,  True,  True,  True],
#        [ True,  True,  True,  True],
#        [ True,  True,  True,  True],
#        [ True,  True,  True,  True]], dtype=bool)

Operating system

GNU/Linux (4.13.1-1-Arch)

Shapely version and provenance

version 1.6.1 installed from pip.

Comments

More of a question-issue than a bug-issue… is this intended behavior?

If this is intended behavior, it makes sense from an implementation perspective; doing pairwise approximate comparisons between primitives to figure out which part was approx equal to any other part wouldn’t be fun. That said, I think disclaiming this in the docstring of almost_equals might reduce surprise. I figured it’d behave like equals, like a fuzzed/buffered contains & within.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
ljwolfcommented, Oct 26, 2017

Will do. Should land by EOD Friday.

On Thu, Oct 26, 2017, 18:01 Sean Gillies notifications@github.com wrote:

@ljwolf https://github.com/ljwolf we’d greatly appreciate such a PR! Can you aim it at the maint-1.6 branch? I’ll pull it from there into master.

— You are receiving this because you were mentioned.

Reply to this email directly, view it on GitHub https://github.com/Toblerity/Shapely/issues/535#issuecomment-339732114, or mute the thread https://github.com/notifications/unsubscribe-auth/ACJY82MaAoTv1pveW_ULCywZAkRvjMBFks5swLrUgaJpZM4QGquO .

– Levi John Wolf Lecturer in Quantitatve Human Geography | University of Bristol Associate Member | Center for Multilevel Modeling, University of Bristol Visiting Fellow | Center for Spatial Data Science, University of Chicago ljwolf.org

0reactions
sgilliescommented, Nov 26, 2017

Closed via #537.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Polygon approximate equality vs. exact equality. #535 - GitHub
Actually, approximate equality might not hold when exact equality holds for polygons. This generalizes to multipolygons, and has to do with the ...
Read more >
Difference between "≈", "≃", and "≅" - Math Stack Exchange
∼ is a similarity in geometry and can be used to show that two things are asymptotically equal (they become more equal as...
Read more >
APPROXIMATION BY POLYGONS AND POLYHEDRA
K with radius v\ and conversely K is contained in the neighbourhood ... ample, the triangle inequality is satisfied, that is, if H,...
Read more >
Regular polygon - Wikipedia
In Euclidean geometry, a regular polygon is a polygon that is direct equiangular (all angles are equal in measure) and equilateral (all sides...
Read more >
Aggregates (Per Minute) - Polygon.io
... (greater than or equal to) to search ranges of values. You can also use the field name without any extension to query...
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