mirror of
https://github.com/Turfjs/turf.git
synced 2025-12-08 20:26:16 +00:00
* Remove GeojsonEquality library from turf-helpers/lib/ in favour of the third party geojson-equality-ts (which is essentially a copy of the code we're removing from turf-helpers/lib/). This allows us to remove the dependency on deep-equal which was causing an unintended increase in bundle size for users of turf-helpers. Updated boolean-equal and boolean-overlap to use geojson-equality-ts directly. * Removing keepNames from tsup config. --------- Co-authored-by: Tim Welch <tim.j.welch@gmail.com>
55 lines
1.6 KiB
TypeScript
55 lines
1.6 KiB
TypeScript
import { Feature, Geometry } from "geojson";
|
|
import { geojsonEquality } from "geojson-equality-ts";
|
|
import { cleanCoords } from "@turf/clean-coords";
|
|
import { getGeom } from "@turf/invariant";
|
|
|
|
/**
|
|
* Determine whether two geometries of the same type have identical X,Y coordinate values.
|
|
* See http://edndoc.esri.com/arcsde/9.0/general_topics/understand_spatial_relations.htm
|
|
*
|
|
* @name booleanEqual
|
|
* @param {Geometry|Feature} feature1 GeoJSON input
|
|
* @param {Geometry|Feature} feature2 GeoJSON input
|
|
* @param {Object} [options={}] Optional parameters
|
|
* @param {number} [options.precision=6] decimal precision to use when comparing coordinates
|
|
* @returns {boolean} true if the objects are equal, false otherwise
|
|
* @example
|
|
* var pt1 = turf.point([0, 0]);
|
|
* var pt2 = turf.point([0, 0]);
|
|
* var pt3 = turf.point([1, 1]);
|
|
*
|
|
* turf.booleanEqual(pt1, pt2);
|
|
* //= true
|
|
* turf.booleanEqual(pt2, pt3);
|
|
* //= false
|
|
*/
|
|
function booleanEqual(
|
|
feature1: Feature<any> | Geometry,
|
|
feature2: Feature<any> | Geometry,
|
|
options: {
|
|
precision?: number;
|
|
} = {}
|
|
): boolean {
|
|
let precision = options.precision;
|
|
|
|
precision =
|
|
precision === undefined || precision === null || isNaN(precision)
|
|
? 6
|
|
: precision;
|
|
|
|
if (typeof precision !== "number" || !(precision >= 0)) {
|
|
throw new Error("precision must be a positive number");
|
|
}
|
|
|
|
const type1 = getGeom(feature1).type;
|
|
const type2 = getGeom(feature2).type;
|
|
if (type1 !== type2) return false;
|
|
|
|
return geojsonEquality(cleanCoords(feature1), cleanCoords(feature2), {
|
|
precision,
|
|
});
|
|
}
|
|
|
|
export { booleanEqual };
|
|
export default booleanEqual;
|