This repository has been archived by the owner on Mar 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfeature_overlap.js
332 lines (314 loc) · 9.76 KB
/
feature_overlap.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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
'use strict';
var featureCollection = require('../lib/get_vector_tile_features');
var area = require('turf-area');
var intersect = require('turf-intersect');
var cover = require('tile-cover');
var featureFilter = require('feature-filter');
var arrayIntersection = require('lodash.intersection');
var difference = require('@turf/difference');
var buffer = require('@turf/buffer');
var limits = {
min_zoom: 16,
max_zoom: 16
};
module.exports = feature_overlap;
var primaryTags = ['natural', 'highway', 'building', 'leisure'];
var featuresJSON = [
'any',
['==', 'natural', 'water'],
['==', 'highway', 'primary'],
['==', 'highway', 'secondary'],
['==', 'highway', 'tertiary'],
['==', 'highway', 'trunk'],
['==', 'building', 'motorway'],
['==', 'building', 'yes'],
['==', 'leisure', 'park']
];
var excludeFeaturesJSON = [
'all',
['!has', 'layer'],
['!has', 'tunnel'],
['!has', 'bridge'],
['!has', 'covered'],
['!has', 'ford'],
['!==', 'location', 'underground'],
['!==', 'location', 'overground']
];
var layersMapping = {
highway: ['water', 'building'],
natural: ['road', 'building'],
building: ['road', 'water'],
leisure: ['road', 'building', 'landuse']
};
/*
Both closed polygon
1 : feature incoming overlaps completely the other feature
0 : feature incoming overlaps some area with the other feature
-1: feature incoming is completely inside the other feature
One open and another closed way
1 : Way is intersecting the other feature near the center
0 : Way is intersecting the other feature near the edges
-1 : Way is completely inside the other feature
*/
var Mapping = {
highway: {
water: [1, -1],
building: [1, -1]
},
natural: {
road: [1],
building: [1, 0]
},
building: {
road: [1],
water: [1, 0, -1]
},
leisure: {
road: [1],
building: [1, 0],
landuse: [1, 0]
}
};
var filterFeatures = featureFilter(featuresJSON);
var excludeFeaturesFilter = featureFilter(excludeFeaturesJSON);
function feature_overlap(newVersion, oldVersion, callback) {
var result = {};
if (
!newVersion.deleted &&
newVersion.geometry &&
newVersion.properties &&
filterFeatures(newVersion) &&
excludeFeaturesFilter(newVersion) &&
newVersion['properties']['osm:version'] === 1
) {
var featurePrimaryTag = arrayIntersection(
Object.keys(newVersion['properties']),
primaryTags
);
if (featurePrimaryTag.length === 0) {
return callback(null, false);
}
if (
newVersion['properties']['osm:type'] === 'relation' &&
newVersion.properties.relations.length <= 0
) {
return callback(null, false);
}
var tiles;
try {
tiles = cover.tiles(newVersion['geometry'], limits);
} catch (err) {
console.log(
'Error caught tiles',
JSON.stringify(err),
JSON.stringify(newVersion)
);
return callback(null, false);
}
var layers = layersMapping[featurePrimaryTag[0]];
featureCollection(tiles, layers, function(err, result) {
if (err) {
console.log(JSON.stringify(newVersion), tiles, layers);
return callback(err);
}
var overlaps = getOverLappingFeatures(newVersion, result);
if (overlaps.length > 0)
return callback(null, {'result:feature_overlap': overlaps.length});
else
return callback(null, false);
});
} else {
return callback(null, false);
}
}
function getOverLappingFeatures(incomingFeature, featureCollections) {
var overlaps = [];
var relationMembers = [];
relationMembers.push(incomingFeature['properties']['osm:id']);
if (incomingFeature['properties']['osm:type'] === 'relation') {
incomingFeature.properties.relations.forEach(function(relationMember) {
relationMembers.push(Number(relationMember['properties']['ref']));
});
if (featureCollections) {
incomingFeature.properties.relations.forEach(function(relationMember) {
featureCollections.forEach(function(featureCollection) {
featureCollection.features.forEach(function(feature) {
if (!isExcluded(feature.properties)) {
var intersection;
try {
intersection = intersect(relationMember, feature);
} catch (err) {
console.log(
'Error caught intersection',
JSON.stringify(err),
JSON.stringify(relationMember),
JSON.stringify(feature)
);
}
if (intersection) {
var areaIntersection = area(intersection);
if (areaIntersection > 0) {
var diff;
try {
diff = difference(intersection, incomingFeature);
} catch (err) {
console.log(
'Error caught difference',
JSON.stringify(err),
JSON.stringify(relationMember),
JSON.stringify(feature)
);
}
if (!diff) {
overlaps.push(feature);
}
} else {
overlaps.push(feature);
}
}
}
});
});
});
}
} else if (featureCollections) {
featureCollections.forEach(function(featureCollection) {
featureCollection.features.forEach(function(feature) {
if (!isExcluded(feature.properties)) {
var incomingFeatureArea = area(incomingFeature);
var featureArea = area(feature);
var intersection;
try {
intersection = intersect(incomingFeature, feature);
} catch (err) {
console.log(err);
}
if (intersection) {
var bufferThreshold;
if (incomingFeatureArea > 0 && featureArea > 0) {
var incomingFeatureDiff, featureDiff;
try {
incomingFeatureDiff = difference(incomingFeature, intersection);
} catch (err) {
console.log(
'Error caught difference',
JSON.stringify(err),
JSON.stringify(incomingFeature),
JSON.stringify(intersection)
);
}
try {
featureDiff = difference(feature, intersection);
} catch (err) {
console.log(
'Error caught difference',
JSON.stringify(err),
JSON.stringify(incomingFeature),
JSON.stringify(intersection)
);
}
if (incomingFeatureDiff && featureDiff) {
var intersectionArea = area(intersection);
if (
intersectionArea / incomingFeatureArea > 0.5 ||
intersectionArea / featureArea > 0.5
) {
overlaps.push(feature);
}
} else if (incomingFeatureDiff && !featureDiff) {
overlaps.push(feature);
}
} else if (incomingFeatureArea > 0 && featureArea === 0) {
bufferThreshold = -3.5183 *
Math.pow(10, -10) *
incomingFeatureArea -
0.006;
var buffered = buffer(incomingFeature, bufferThreshold);
var bufferedIntersection;
try {
bufferedIntersection = intersect(buffered, feature);
} catch (err) {
console.log(
'Error caught intersection',
JSON.stringify(err),
JSON.stringify(buffered),
JSON.stringify(feature)
);
}
if (bufferedIntersection) {
console.log(JSON.stringify(feature));
overlaps.push(feature);
}
} else if (incomingFeatureArea === 0 && featureArea > 0) {
bufferThreshold = -3.5183 * Math.pow(10, -10) * featureArea -
0.006;
var buffered1 = buffer(feature, bufferThreshold);
var bufferedIntersection1;
try {
bufferedIntersection1 = intersect(buffered1, incomingFeature);
} catch (err) {
console.log(
'Error caught intersection',
JSON.stringify(err),
JSON.stringify(buffered1),
JSON.stringify(incomingFeature)
);
}
if (bufferedIntersection1) {
overlaps.push(feature);
}
}
}
}
});
});
}
return overlaps;
}
var allowedFeatureType = [
'building',
'primary',
'secondary',
'tertiary',
'motorway',
'trunk',
'residential',
'unclassified'
];
var allowedFeatureClass = [
'primary',
'secondary',
'tertiary',
'motorway',
'trunk',
'street'
];
var landuseFeaturesClass = [
'agriculture',
'cemetery',
'glacier',
'hospital',
'industrial',
'piste',
'school',
'wood',
'aboriginal_lands'
];
function isExcluded(properties) {
var underground = properties.underground
? properties.underground === 'true'
: false;
var layer = properties.layer ? properties.layer !== 0 : false;
var structure = properties.structure
? properties.structure !== 'none'
: false;
var type = properties.type
? allowedFeatureType.indexOf(properties.type) === -1 &&
landuseFeaturesClass.indexOf(properties.class) === -1
: false;
var featureClass = properties.class
? allowedFeatureClass.indexOf(properties.class) === -1 &&
landuseFeaturesClass.indexOf(properties.class) === -1
: false;
return underground || layer || structure || type || featureClass;
}