-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencode.go
136 lines (122 loc) · 2.97 KB
/
encode.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
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
package main
import (
"encoding/json"
"github.com/TopoSimplify/plugin/geometry"
"github.com/intdxdt/geom"
"github.com/intdxdt/math"
"regexp"
"sort"
"strconv"
)
var numRegx = regexp.MustCompile("[0-9]+")
type PolyGeom struct {
index int
pos int
geom *geometry.Polyline
}
func indexPos(id string) (int, int) {
var vals = numRegx.FindAllString(id, -1)
var indx, _ = strconv.Atoi(vals[0])
var pos, _ = strconv.Atoi(vals[1])
return indx, pos
}
func groupById(plns []*geometry.Polyline) map[int][]PolyGeom {
var dictionary = make(map[int][]PolyGeom, 0)
for _, pln := range plns {
var indx, pos = indexPos(pln.Id)
dictionary[indx] = append(dictionary[indx], PolyGeom{indx, pos, pln})
}
return dictionary
}
func groupPlnsAsGeoJSONS(plns []*geometry.Polyline) []string {
var dictionary = groupById(plns)
var indxs = indices(dictionary)
var jsons = make([]string, 0, len(plns))
for idx := range indxs {
var plns = dictionary[idx]
sort.SliceStable(plns, func(i, j int) bool {
return plns[i].pos < plns[j].pos
})
var s = createGeoJSONString(plns)
jsons = append(jsons, s)
}
return jsons
}
func isCoords3D(coords []geom.Point) bool {
var bln = false
for _, pt := range coords {
bln = !math.FloatEqual(pt[2], 0)
if bln {
break
}
}
return bln
}
func extractCoords(coords []geom.Point) [][]float64 {
var is3D = isCoords3D(coords)
var coordinates = make([][]float64, 0, len(coords))
for _, pt := range coords {
var c = append([]float64{}, pt[:]...)
if !is3D {
c = c[:2]
}
coordinates = append(coordinates, c)
}
return coordinates
}
func getMeta(g PolyGeom) map[string]interface{} {
var meta = g.geom.Meta
var properties map[string]interface{}
if meta == "null" {
properties = make(map[string]interface{}, 0)
} else {
var err = json.Unmarshal([]byte(meta), &properties)
if err != nil {
panic(err)
}
}
return properties
}
func createGeoJSONString(featLns []PolyGeom) string {
if len(featLns) == 1 {
var gpoly = featLns[0]
var simple = gpoly.geom.Simple.Points()
var coords = extractCoords(simple)
var meta = getMeta(gpoly)
var feature = geometry.LineStringFeature{
Type: "Feature",
Properties: meta,
Geometry: &geometry.LinearGeometry{
Type: "LineString",
Coordinates: coords,
},
}
var dat, _ = json.Marshal(feature)
return string(dat)
}
var meta = getMeta(featLns[0])
var coordinates = make([][][]float64, 0, len(featLns))
for _, gpoly := range featLns {
var simple = gpoly.geom.Simple.Points()
var coords = extractCoords(simple)
coordinates = append(coordinates, coords)
}
var feature = geometry.LineStringFeature{
Type: "Feature",
Properties: meta,
Geometry: &geometry.MultiLinearGeometry{
Type: "MultiLineString",
Coordinates: coordinates,
},
}
var dat, _ = json.Marshal(feature)
return string(dat)
}
func indices(dict map[int][]PolyGeom) []int {
keys := make([]int, 0, len(dict))
for k := range dict {
keys = append(keys, k)
}
sort.Ints(keys)
return keys
}