-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
195 lines (181 loc) · 5.02 KB
/
index.js
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
//index.js
var {dequal: deepEqual} = require('dequal');
var Equality = function(opt) {
this.precision = opt && opt.precision ? opt.precision : 17;
this.direction = opt && opt.direction ? opt.direction : false;
this.pseudoNode = opt && opt.pseudoNode ? opt.pseudoNode : false;
this.objectComparator = opt && opt.objectComparator ? opt.objectComparator : objectComparator;
};
Equality.prototype.compare = function(g1,g2) {
if (g1.type !== g2.type || !sameLength(g1,g2)) return false;
switch(g1.type) {
case 'Point':
return this.compareCoord(g1.coordinates, g2.coordinates);
break;
case 'LineString':
return this.compareLine(g1.coordinates, g2.coordinates,0,false);
break;
case 'Polygon':
return this.comparePolygon(g1,g2);
break;
case 'GeometryCollection':
return this.compareGeometryCollection(g1, g2);
case 'Feature':
return this.compareFeature(g1, g2);
case 'FeatureCollection':
return this.compareFeatureCollection(g1, g2);
default:
if (g1.type.indexOf('Multi') === 0) {
var context = this;
var g1s = explode(g1);
var g2s = explode(g2);
return g1s.every(function(g1part) {
return this.some(function(g2part) {
return context.compare(g1part,g2part);
});
},g2s);
}
}
return false;
};
function explode(g) {
return g.coordinates.map(function(part) {
return {
type: g.type.replace('Multi', ''),
coordinates: part}
});
}
//compare length of coordinates/array
function sameLength(g1,g2) {
return g1.hasOwnProperty('coordinates') ?
g1.coordinates.length === g2.coordinates.length
: g1.length === g2.length;
}
// compare the two coordinates [x,y]
Equality.prototype.compareCoord = function(c1,c2) {
if (c1.length !== c2.length) {
return false;
}
for (var i=0; i < c1.length; i++) {
if (c1[i].toFixed(this.precision) !== c2[i].toFixed(this.precision)) {
return false;
}
}
return true;
};
Equality.prototype.compareLine = function(path1,path2,ind,isPoly) {
if (!sameLength(path1,path2)) return false;
var p1 = this.pseudoNode ? path1 : this.removePseudo(path1);
var p2 = this.pseudoNode ? path2 : this.removePseudo(path2);
if (isPoly && !this.compareCoord(p1[0],p2[0])) {
// fix start index of both to same point
p2 = this.fixStartIndex(p2,p1);
if(!p2) return;
}
// for linestring ind =0 and for polygon ind =1
var 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;
}
};
Equality.prototype.fixStartIndex = function(sourcePath,targetPath) {
//make sourcePath first point same as of targetPath
var correctPath,ind = -1;
for (var i=0; i< sourcePath.length; i++) {
if(this.compareCoord(sourcePath[i],targetPath[0])) {
ind = i;
break;
}
}
if (ind >= 0) {
correctPath = [].concat(
sourcePath.slice(ind,sourcePath.length),
sourcePath.slice(1,ind+1));
}
return correctPath;
};
Equality.prototype.comparePath = function (p1,p2) {
var cont = this;
return p1.every(function(c,i) {
return cont.compareCoord(c,this[i]);
},p2);
};
Equality.prototype.comparePolygon = function(g1,g2) {
if (this.compareLine(g1.coordinates[0],g2.coordinates[0],1,true)) {
var holes1 = g1.coordinates.slice(1,g1.coordinates.length);
var holes2 = g2.coordinates.slice(1,g2.coordinates.length);
var cont = this;
return holes1.every(function(h1) {
return this.some(function(h2) {
return cont.compareLine(h1,h2,1,true);
});
},holes2);
} else {
return false;
}
};
Equality.prototype.compareGeometryCollection= function(g1,g2) {
if (
!sameLength(g1.geometries, g2.geometries) ||
!this.compareBBox(g1,g2)
) {
return false;
}
for (var i=0; i < g1.geometries.length; i++) {
if (!this.compare(g1.geometries[i], g2.geometries[i])) {
return false;
}
}
return true
};
Equality.prototype.compareFeature = function(g1,g2) {
if (
g1.id !== g2.id ||
!this.objectComparator(g1.properties, g2.properties) ||
!this.compareBBox(g1,g2)
) {
return false;
}
return this.compare(g1.geometry, g2.geometry);
};
Equality.prototype.compareFeatureCollection = function(g1,g2) {
if (
!sameLength(g1.features, g2.features) ||
!this.compareBBox(g1,g2)
) {
return false;
}
for (var i=0; i < g1.features.length; i++) {
if (!this.compare(g1.features[i], g2.features[i])) {
return false;
}
}
return true
};
Equality.prototype.compareBBox = function(g1,g2) {
if (
(!g1.bbox && !g2.bbox) ||
(
g1.bbox && g2.bbox &&
this.compareCoord(g1.bbox, g2.bbox)
)
) {
return true;
}
return false;
};
Equality.prototype.removePseudo = function(path) {
//TODO to be implement
return path;
};
function objectComparator(obj1, obj2) {
return deepEqual(obj1, obj2, {strict: true});
}
module.exports = Equality;