This repository has been archived by the owner on Feb 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransformation.go
75 lines (68 loc) · 2.37 KB
/
transformation.go
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
package turfgo
// LineDiff take two lines and gives an array of lines by subracting second from first. Single coordinate overlaps are ignored.
// Line should not have duplicate values.
func LineDiff(firstLine *LineString, secondLine *LineString) []*LineString {
diffSegments := []*LineString{}
fPoints := firstLine.Points
sPoints := secondLine.Points
for i := 0; i < len(fPoints)-1; i++ {
if !containLocationPair(sPoints, fPoints[i], fPoints[i+1]) {
diffSegments = append(diffSegments, NewLineString([]*Point{fPoints[i], fPoints[i+1]}))
}
}
return reduceDiffSegment(diffSegments)
}
// LineDiffPercentage take two lines and give the percentage of difference between first and second line with respect to first line.
// Single coordinate overlaps are ignored. Line should not have duplicate values.
func LineDiffPercentage(firstLine *LineString, secondLine *LineString) float64 {
totalPoints := len(firstLine.Points)
if totalPoints == 0 {
return 0
}
diff := LineDiff(firstLine, secondLine)
if len(diff) == 1 && totalPoints == len(diff[0].Points) {
return 100
}
diffPoints := 0
for _, line := range diff {
isStartingSegment := isEqualLocation(firstLine.Points[0], line.Points[0])
isEndingSegment := isEqualLocation(firstLine.Points[len(firstLine.Points)-1], line.Points[len(line.Points)-1])
diffPoints += len(line.Points)
if isStartingSegment || isEndingSegment {
diffPoints--
} else {
diffPoints -= 2
}
}
return (float64(diffPoints) / float64(totalPoints)) * 100
}
func reduceDiffSegment(segments []*LineString) []*LineString {
if len(segments) == 0 {
return segments
}
result := []*LineString{}
previousSeg := segments[0]
for i := 1; i < len(segments); i++ {
currentSeg := segments[i]
pLen := len(previousSeg.Points)
previousSegLastPoint := previousSeg.Points[pLen-1]
currentSegFirstPoint := currentSeg.Points[0]
if isEqualLocation(previousSegLastPoint, currentSegFirstPoint) {
mergedPoints := append(previousSeg.Points, currentSeg.Points[1])
previousSeg = NewLineString(mergedPoints)
} else {
result = append(result, previousSeg)
previousSeg = currentSeg
}
}
result = append(result, previousSeg)
return result
}
func containLocationPair(points []*Point, point1, point2 *Point) bool {
for i := 0; i < len(points)-1; i++ {
if isEqualLocation(point1, points[i]) && isEqualLocation(point2, points[i+1]) {
return true
}
}
return false
}