-
Notifications
You must be signed in to change notification settings - Fork 0
/
index-old.js
82 lines (69 loc) · 3.54 KB
/
index-old.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
const express = require('express')
const turf = require('@turf/turf')
const testDataSample1 = require('./data/plowpath')
const transformer = require('./src/transformer')
const app = express()
const port = 3000
const requestHandler = async (req, res) => {
transformer.sortByDate(testDataSample1)
const groupedIds = transformer.groupByAssetId(testDataSample1)
const optimizedPath = Object.keys(groupedIds).map((key) => {
const finalPaths = []
const currentGroup = groupedIds[key]
transformer.addTurfPath(currentGroup)
const pushNewPath = (pathArray, currentSegment) => {
let isIntersected = false
if (pathArray.length) {
for (let cursor = 0; cursor < pathArray.length; cursor++) {
const latestSegment = pathArray[cursor].turfPath
let oldSegment = currentSegment.turfPath
const intersection = turf.lineIntersect(latestSegment, oldSegment)
if (intersection.features.length > 2) {
const firstIntersection = intersection.features[0].geometry
const lastIntersection = intersection.features[intersection.features.length - 1].geometry
const distanceLatestToFirst = transformer.calculateDistance(latestSegment.geometry.coordinates[0], firstIntersection.coordinates)
const distanceCurrentToFirst = transformer.calculateDistance(oldSegment.geometry.coordinates[0], firstIntersection.coordinates)
const firstDistanceDifference = distanceLatestToFirst - distanceCurrentToFirst
const closestPath = transformer.slicePathFromIntersectionToEnd(lastIntersection.coordinates, oldSegment.geometry.coordinates)
const newPath = transformer.buildNewPath(currentSegment.timestamp, closestPath)
pathArray.push(newPath)
cursor++
isIntersected = true
if (Math.abs(firstDistanceDifference) > 0.01) { //IF: latest Path are closed to old Path
const closestPath = transformer.slicePathFromStartToIntersection(firstIntersection.coordinates, oldSegment.geometry.coordinates)
const newPath = transformer.buildNewPath(currentSegment.timestamp, closestPath)
pathArray.push(newPath)
cursor++
}else{
console.log(firstDistanceDifference)
}
}
}
}
if (!isIntersected) {
pathArray.push(currentSegment)
}
}
let count = 0
for (let i = 0; i < currentGroup.length; i++) {
pushNewPath(finalPaths, currentGroup[i])
count++
}
console.log(count)
for (let i = 0; i < finalPaths.length; i++) {
if (!finalPaths[i].readings) {
finalPaths[i].readings = transformer.convertToReading(finalPaths[i].turfPath.geometry.coordinates)
}
delete finalPaths[i].turfPath
}
return finalPaths
})
const finalPath = optimizedPath.reduce((prev, curr) => {
return prev.concat(curr)
})
console.log("requestHandler -> optimizedPath", optimizedPath.length)
res.status(200)
res.json(finalPath)
}
app.get('/', requestHandler)
app.listen(port, () => console.log(`Example app listening at http://localhost:${port}`))