-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreducer.js
228 lines (177 loc) · 6.59 KB
/
reducer.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
'use strict';
// Npm imports
const tilebelt = require('@mapbox/tilebelt');
const turfLength = require('@turf/length');
const bboxClip = require('@turf/bbox-clip');
// Custom imports
const compareLines = require('./lib/compareLines');
const iogeojson = require('./lib/io');
const cloneJson = require('./lib/utils').cloneJson;
// TODO: to be removed in the future
// accuracy error to account the trimming of line (multiline) geometries
const coverageErr = 0.1;
// tolerance distance to compare lines (in kilometers)
// max angle to consider two lines as parallel
const overlapTolerance = {
tolerance: 0.010,
angle: 15
};
// Line length measure unit
const lengthUnits = {
units: 'kilometers'
};
// TODO: complete this list
// Classification list to filter highways from the osm layer
const highwayList = [
'bridleway',
'cycleway',
'footway',
'living_street',
'motorway',
'motorway_link',
'path',
'primary',
'primary_link',
'residential',
'road',
'secondary',
'secondary_link',
'service',
'sidewalk',
'steps',
'tertiary',
'tertiary_link',
'track',
'trunk',
'trunk_link',
'unclassified' ];
/**
* Reducer from TileReduce
* @param data: data from the mbtiles database
* @param tile:
* @param writeData
* @param done: callback
* @returns {*}
*/
const reducer = function (data, tile, writeData, done) {
let result = {
gtruth: [],
osm: [],
pmissing: [],
missing: [],
update: []
};
// Bounding box for tile
const bbox = tilebelt.tileToBBOX(tile);
// TODO: Reading the entire dataset every time is obviously not efficient...
let groundtruth = iogeojson.readGeojson(global.mapOptions.groundtruthFile);
// If we save the complete geojson
// Cycle 1 time through osm
for (let i = 0; i < data.osmtiles.osm.length; i++) {
let osmfeature = data.osmtiles.osm.feature(i);
let osmgeom = osmfeature.toGeoJSON(tile[0], tile[1], tile[2]);
if(!osmfeature.properties.highway || highwayList.indexOf(osmfeature.properties.highway) === -1){
continue;
}
result.osm.push(osmgeom);
}
// Cycle through groundtruth and find diferences
groundtruth.features.forEach(function(gtfeature){
// Clip the feature into the bbox of the current tile
let clippedFeature = bboxClip(gtfeature.geometry, bbox);
let clippedGeometry = clippedFeature.geometry;
// If the geometry isn't within the bbox
if(clippedGeometry.coordinates.length === 0){
// Skip to next
return;
}
// We want to process the clipped geometry
// This is to account geometries that cross the boundary of tiles
gtfeature.geometry = clippedGeometry;
let gtfeatureOriginal = cloneJson(gtfeature);
// Store the current geometry to compare with the result
result.gtruth.push(gtfeature);
// Overall coverage between multiple osm geometries with the current groundtruth geometry.
// 0 = does not overlap, 1 = totally overlaps, ]0, 1[ = partially overlaps
let overallCoverage = 0.0;
// Indexes of matched osm features
let matchedIdx = [];
// Cycle through every filtered osm feature
for (let i = 0; i < result.osm.length; i++) {
let osmgeom = result.osm[i];
// How much B fits in A for A,B
let comparison = compareLines(osmgeom, gtfeature, overlapTolerance);
let overlaps = comparison.overlaps;
// If overlaps
if(overlaps.features.length > 0){
matchedIdx.push(i);
let lenFeature = turfLength(gtfeature, lengthUnits);
let lenOverlap = turfLength(overlaps, lengthUnits);
// Update the overall coverage percentage
overallCoverage = overallCoverage + ((lenOverlap / lenFeature) * (1.0 - overallCoverage));
if(overallCoverage <= 1.0 + coverageErr && overallCoverage > 1.0 - coverageErr){
// Completelly overlaps
break;
} else {
// Partially overlaps, continue...reset geometry to what's missing
gtfeature = comparison.missing;
}
}
}
// console.log("> Coverage was " + overallCoverage);
// Something went wrong if it hits any of these conditions
if(overallCoverage > 1.0 + coverageErr){
console.log("[Err] Invalid % > 1.0 ---> " + overallCoverage);
}
// Process the result
if(overallCoverage === 0.0){
// Didnt find a match
result.missing.push(gtfeatureOriginal);
} else if(overallCoverage <= 1.0 + coverageErr && overallCoverage > 1.0 - coverageErr){
// Found a match that completely overlaps
// do nothing for now...
// Update the properties of every feature matched
matchedIdx.forEach(function(hit){
let osmfeature = result.osm[hit];
let missingProperties = compareProperties(gtfeatureOriginal, osmfeature);
if(missingProperties){
let toUpdate = {
type: osmfeature.type,
geometry: osmfeature.geometry,
properties: {
'@id': osmfeature.properties['@id'],
'@uid': osmfeature.properties['@uid'],
'name': missingProperties['name']
}
};
result.update.push(toUpdate);
}
});
} else {
// Found a match that partially overlaps
// Store the missing segments
gtfeature.features.forEach(function(segment){
let clonedSegment = cloneJson(segment);
clonedSegment.properties.coverage = overallCoverage;
result.pmissing.push(clonedSegment);
});
}
});
return done(null, result);
};
const compareProperties = function (gtfeature, osmfeature){
let gtProps = gtfeature.properties;
let osmProps = osmfeature.properties;
// For now, only compare the name
if(!osmProps.ref && gtProps.name){
if(!osmProps.name || osmProps.name ===''){
return {name: gtProps.name};
} else {
// TODO: Compare names in a iLike way
return null;//{name: gtProps.name};
}
}
return null;
};
// Export reducer to be used by map
module.exports = reducer;