-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add comparison operators for some geometry types. (#22)
* implement ==, hash for AbstractPoint * add tests for comparison operators * implement isapprox for Points, with tests
- Loading branch information
Showing
3 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import Base: ==, hash, isapprox | ||
|
||
# Compare points by coordinate values. | ||
==(x::P, y::P) where {P <: AbstractPoint} = coordinates(x) == coordinates(y) | ||
|
||
# Hash the coordinates for consistency. | ||
hash(x::P) where {P <: AbstractPoint} = hash(coordinates(x)) | ||
|
||
# Compare points approximately by coordinate values. | ||
isapprox(x::P, y::P; kwargs...) where {P <: AbstractPoint} = isapprox(coordinates(x), coordinates(y); kwargs...) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,37 @@ | ||
using GeoInterface | ||
using Test | ||
|
||
@testset "Comparison operators" begin | ||
# Points are now compared by value: | ||
pt1, pt2, pt3 = Point([0.0, 0.0]), Point([0.0, 0.0]), Point([0.0, 1.0]) | ||
@test pt1 == pt1 | ||
@test pt1 == pt2 | ||
@test pt1 != pt3 | ||
|
||
# Also, the hash is based on the coordinates | ||
@test hash(pt1) == hash(pt1) | ||
@test hash(pt1) == hash(pt2) | ||
@test hash(pt1) != hash(pt3) | ||
|
||
# Implicitly, this should also work for `isequal`: | ||
@test isequal(pt1, pt1) | ||
@test isequal(pt1, pt2) | ||
@test !isequal(pt1, pt3) | ||
|
||
# Can also do approximate comparisons | ||
pt4 = Point([0.0, 1.001]) | ||
@test pt3 != pt4 | ||
@test pt3 ≉ pt4 atol=0.0001 | ||
@test pt3 ≉ pt4 rtol=0.0001 | ||
@test pt3 ≈ pt4 atol=0.001 | ||
@test pt3 ≈ pt4 rtol=0.001 | ||
|
||
# The same is not true for other geometry types: The representation is not | ||
# unique, so comparing the coordinates directly might be misleading. | ||
pg1 = Polygon([[[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]]]) | ||
pg2 = Polygon([[[0.0, 0.0], [1.0, 0.0], [0.0, 1.0]]]) | ||
pg3 = Polygon([[[1.0, 0.0], [0.0, 1.0], [0.0, 0.0]]]) | ||
@test pg1 == pg1 # same objects | ||
@test pg1 != pg2 # same values, but different objects | ||
@test pg1 != pg3 # equivalent, but not same values | ||
end |