Repository navigation
Expand file tree
/
Copy pathreduce.js
More file actions
54 lines (46 loc) · 1.89 KB
/
Copy pathreduce.js
File metadata and controls
54 lines (46 loc) · 1.89 KB
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
const turf = require('@turf/turf')
const invariant = require('@turf/invariant')
const days = global.mapOptions.days
const extent = global.mapOptions.extent
const users = global.mapOptions.users
const overlap = global.mapOptions.overlap
module.exports = (data, tile, writeData, done) => {
const features = []
const timestamp = Date.now() / 1000 - days * 24 * 60 * 60
const unique = {}
for (let feature of data.qatiles.osm.features) {
if (feature.geometry.type === 'LineString') {
// Only find buildings
if (!feature.properties.highway) { continue }
// Filter by Date
if (days > 0) { if (!(Number(feature.properties['@timestamp']) > timestamp)) { continue } }
// Filter by Usernames
if (users.length) { if (users.indexOf(feature.properties['@user']) === -1) { continue } }
// Must intersect within extent
if (turf.intersect(feature, extent)) {
let duplicates = 0
feature = turf.truncate(feature)
// Detect duplicate nodes (must intersect both previous & current)
turf.coordReduce(feature, (previousCoord, currentCoord) => {
const previousId = unique[previousCoord.join(',')]
const currentId = unique[currentCoord.join(',')]
// Must have unique ideas based on the same way
if (previousId && currentId && previousId === currentId) {
duplicates++
}
return currentCoord
})
// Add coordinates to unique
turf.coordEach(feature, coord => {
const key = coord.join(',')
unique[key] = feature.properties['@id']
})
// Ignore ways with less than 5 coordinates
if (feature.geometry.coordinates.length < 5) continue
// Only show duplicates with greater than % overlap
if (duplicates / feature.geometry.coordinates.length > overlap) features.push(feature)
}
}
}
done(null, features)
}