Skip to content

Commit 448214c

Browse files
Merge pull request #122 from wmde/coordinateEquality
Rewrite globeCoordinate.GlobeCoordinate.equals
2 parents 8f05e0e + a50a48e commit 448214c

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

lib/globeCoordinate/globeCoordinate.GlobeCoordinate.js

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,23 @@
127127
* @return {boolean}
128128
*/
129129
equals: function( otherGlobeCoordinate ) {
130-
if( !( otherGlobeCoordinate instanceof globeCoordinate.GlobeCoordinate ) ) {
130+
if ( !( otherGlobeCoordinate instanceof SELF )
131+
|| otherGlobeCoordinate._globe !== this._globe
132+
) {
131133
return false;
132134
}
133135

134-
var gc1Iso6709 = globeCoordinate.iso6709( this.getDecimal() ),
135-
gc2Iso6709 = globeCoordinate.iso6709( otherGlobeCoordinate.getDecimal() );
136-
137-
return ( this._precision === otherGlobeCoordinate._precision
138-
|| Math.abs( this._precision - otherGlobeCoordinate._precision ) < 0.00000001 )
139-
&& gc1Iso6709 === gc2Iso6709
140-
&& this._globe === otherGlobeCoordinate._globe;
136+
// 0.00000001° corresponds to approx. 1 mm on Earth and can always be considered equal.
137+
var oneMillimeter = 0.00000001,
138+
epsilon = Math.max(
139+
// A change worth 1/2 precision might already become a visible change
140+
Math.min( this._precision, otherGlobeCoordinate._precision ) / 2,
141+
oneMillimeter
142+
);
143+
144+
return Math.abs( otherGlobeCoordinate._precision - this._precision ) < oneMillimeter
145+
&& Math.abs( otherGlobeCoordinate._latitude - this._latitude ) < epsilon
146+
&& Math.abs( otherGlobeCoordinate._longitude - this._longitude ) < epsilon;
141147
}
142148
};
143149

0 commit comments

Comments
 (0)