forked from geosquare/geojson-equality
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.ts
250 lines (222 loc) · 6.32 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import {
Feature,
LineString,
Position,
GeoJSON,
Point,
Polygon,
GeometryCollection,
FeatureCollection,
MultiLineString,
MultiPoint,
MultiPolygon,
GeoJsonProperties,
} from "geojson";
/**
* GeoJSON equality checking utility.
* Adapted from https://github.com/geosquare/geojson-equality
*
* @memberof helpers
* @type {Class}
*/
class GeojsonEquality {
private precision: number;
private direction = false;
private compareProperties = true;
constructor(opts?: {
precision?: number;
direction?: boolean;
compareProperties?: boolean;
}) {
this.precision = 10 ** -(opts?.precision ?? 17);
this.direction = opts?.direction ?? false;
this.compareProperties = opts?.compareProperties ?? true;
}
compare(g1: GeoJSON, g2: GeoJSON): boolean {
if (g1.type !== g2.type) {
return false;
}
if (!sameLength(g1, g2)) {
return false;
}
switch (g1.type) {
case "Point":
return this.compareCoord(g1.coordinates, (g2 as Point).coordinates);
case "LineString":
return this.compareLine(g1.coordinates, (g2 as LineString).coordinates);
case "Polygon":
return this.comparePolygon(g1, g2 as Polygon);
case "GeometryCollection":
return this.compareGeometryCollection(g1, g2 as GeometryCollection);
case "Feature":
return this.compareFeature(g1, g2 as Feature);
case "FeatureCollection":
return this.compareFeatureCollection(g1, g2 as FeatureCollection);
default:
if (g1.type.startsWith("Multi")) {
const g1s = explode(g1);
const g2s = explode(
g2 as MultiLineString | MultiPoint | MultiPolygon
);
return g1s.every((g1part) =>
g2s.some((g2part) => this.compare(g1part as any, g2part as any))
);
}
}
return false;
}
private compareCoord(c1: Position, c2: Position) {
return (
c1.length === c2.length &&
c1.every((c, i) => Math.abs(c - c2[i]) < this.precision)
);
}
private compareLine(
path1: Position[],
path2: Position[],
ind = 0,
isPoly = false
): boolean {
if (!sameLength(path1, path2)) {
return false;
}
const p1 = path1;
let p2 = path2;
if (isPoly && !this.compareCoord(p1[0], p2[0])) {
// fix start index of both to same point
const startIndex = this.fixStartIndex(p2, p1);
if (!startIndex) {
return false;
} else {
p2 = startIndex;
}
}
// for linestring ind =0 and for polygon ind =1
const sameDirection = this.compareCoord(p1[ind], p2[ind]);
if (this.direction || sameDirection) {
return this.comparePath(p1, p2);
} else {
if (this.compareCoord(p1[ind], p2[p2.length - (1 + ind)])) {
return this.comparePath(p1.slice().reverse(), p2);
}
return false;
}
}
private fixStartIndex(sourcePath: Position[], targetPath: Position[]) {
//make sourcePath first point same as of targetPath
let correctPath,
ind = -1;
for (let i = 0; i < sourcePath.length; i++) {
if (this.compareCoord(sourcePath[i], targetPath[0])) {
ind = i;
break;
}
}
if (ind >= 0) {
correctPath = ([] as Position[]).concat(
sourcePath.slice(ind, sourcePath.length),
sourcePath.slice(1, ind + 1)
);
}
return correctPath;
}
private comparePath(p1: Position[], p2: Position[]) {
return p1.every((c, i) => this.compareCoord(c, p2[i]));
}
private comparePolygon(g1: Polygon, g2: Polygon) {
if (this.compareLine(g1.coordinates[0], g2.coordinates[0], 1, true)) {
const holes1 = g1.coordinates.slice(1, g1.coordinates.length);
const holes2 = g2.coordinates.slice(1, g2.coordinates.length);
return holes1.every((h1) =>
holes2.some((h2) => this.compareLine(h1, h2, 1, true))
);
}
return false;
}
private compareGeometryCollection(
g1: GeometryCollection,
g2: GeometryCollection
) {
return (
sameLength(g1.geometries, g2.geometries) &&
this.compareBBox(g1, g2) &&
g1.geometries.every((g, i) => this.compare(g, g2.geometries[i]))
);
}
private compareFeature(g1: Feature, g2: Feature) {
return (
g1.id === g2.id &&
(this.compareProperties ? equal(g1.properties, g2.properties) : true) &&
this.compareBBox(g1, g2) &&
this.compare(g1.geometry, g2.geometry)
);
}
private compareFeatureCollection(
g1: FeatureCollection,
g2: FeatureCollection
) {
return (
sameLength(g1.features, g2.features) &&
this.compareBBox(g1, g2) &&
g1.features.every((f, i) => this.compare(f, g2.features[i]))
);
}
private compareBBox(g1: GeoJSON, g2: GeoJSON): boolean {
return (
Boolean(!g1.bbox && !g2.bbox) ||
(g1.bbox && g2.bbox ? this.compareCoord(g1.bbox, g2.bbox) : false)
);
}
}
function sameLength(g1: any, g2: any) {
return g1.coordinates
? g1.coordinates.length === g2.coordinates.length
: g1.length === g2.length;
}
function explode(g: MultiLineString | MultiPoint | MultiPolygon) {
return g.coordinates.map((part) => ({
type: g.type.replace("Multi", ""),
coordinates: part,
}));
}
function geojsonEquality(
g1: GeoJSON,
g2: GeoJSON,
opts?: {
precision?: number;
direction?: boolean;
compareProperties?: boolean;
}
): boolean {
const eq = new GeojsonEquality(opts);
return eq.compare(g1, g2);
}
// Adapted from https://medium.com/syncfusion/5-different-ways-to-deep-compare-javascript-objects-6708a0da9f05
function equal(object1: GeoJsonProperties, object2: GeoJsonProperties) {
if (object1 === null && object2 === null) {
return true;
}
if (object1 === null || object2 === null) {
return false;
}
const objKeys1 = Object.keys(object1);
const objKeys2 = Object.keys(object2);
if (objKeys1.length !== objKeys2.length) return false;
for (var key of objKeys1) {
const value1 = object1[key];
const value2 = object2[key];
const isObjects = isObject(value1) && isObject(value2);
if (
(isObjects && !equal(value1, value2)) ||
(!isObjects && value1 !== value2)
) {
return false;
}
}
return true;
}
const isObject = (object: any) => {
return object != null && typeof object === "object";
};
export { GeojsonEquality, geojsonEquality };
export default GeojsonEquality;