forked from go-spatial/tegola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gostring.go
113 lines (102 loc) · 3.49 KB
/
gostring.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
package tegola
import (
"fmt"
"strings"
)
const goStringPointFormat = "{%.0f,%.0f}, "
func defaultPointDecorator(pt Point) string { return fmt.Sprintf(goStringPointFormat, pt.X(), pt.Y()) }
func pointDecorator(p Point, withType bool, indent int, comment string, ptDecorator func(pt Point) string) string {
const (
ptFormat = "%v%v%v // %v\n"
)
if ptDecorator == nil {
ptDecorator = defaultPointDecorator
}
indentString := strings.Repeat("\t", indent)
pstr := strings.Trim(ptDecorator(p), ", ")
tn := ""
if withType {
tn = "basic.Point"
}
return fmt.Sprintf(ptFormat, indentString, tn, pstr, comment)
}
func lineDecorator(l LineString, withType bool, indent int, ptsPerLine int, comment string, pointDecorator func(pt Point) string) string {
if ptsPerLine == 0 {
ptsPerLine = 10
}
const (
lineFormat = "%v%v{ // basic.Line len(%06d) %v.\n%v%v}"
pointLineFormat = "%v\t%v // %06d — %06d\n"
)
typeName := ""
if withType {
typeName = "basic.Line"
}
if pointDecorator == nil {
pointDecorator = defaultPointDecorator
}
indentString := strings.Repeat("\t", indent)
var byteString, bytestr []rune
lastI := -1
pts := l.Subpoints()
for i, p := range pts {
byteString = append(byteString, []rune(pointDecorator(p))...)
if (i+1)%ptsPerLine == 0 {
bytestr = append(bytestr, []rune(fmt.Sprintf(pointLineFormat, indentString, string(byteString), lastI+1, i))...)
byteString = byteString[:0] // truncate string.
lastI = i
}
}
if len(byteString) > 0 {
bytestr = append(bytestr, []rune(fmt.Sprintf(pointLineFormat, indentString, string(byteString), lastI+1, len(pts)-1))...)
byteString = byteString[:0] // truncate string.
}
return fmt.Sprintf(lineFormat, indentString, typeName, len(pts), comment, string(bytestr), indentString)
}
func polygonDecorator(p Polygon, withType bool, indent int, ptsPerLine int, comment string, pointDecorator func(pt Point) string) string {
const (
polygonFormat = "%v%v{ // basic.Polygon len(%06d)%v.\n%v\n%v}"
)
typeName := ""
if withType {
typeName = "basic.Polygon"
}
indentString := strings.Repeat("\t", indent)
lines := ""
lns := p.Sublines()
for i, line := range lns {
lines += lineDecorator(line, false, indent+1, ptsPerLine, fmt.Sprintf(" line(%02d)", i), pointDecorator) + ",\n"
}
return fmt.Sprintf(polygonFormat, indentString, typeName, len(lns), comment, lines, indentString)
}
func multiPolygonDecorator(mp MultiPolygon, withType bool, indent int, ptsPerLine int, comment string, pointDecorator func(pt Point) string) string {
const (
polygonFormat = "%v%v{ // basic.MultiPolygon len(%02d)%v.\n%v\n%v}"
)
typeName := ""
if withType {
typeName = "basic.MultiPolygon"
}
indentString := strings.Repeat("\t", indent)
polygons := ""
plygs := mp.Polygons()
for i, p := range plygs {
polygons += polygonDecorator(p, false, indent+1, ptsPerLine, fmt.Sprintf(" polygon(%02d)", i), pointDecorator) + ",\n"
}
return fmt.Sprintf(polygonFormat, indentString, typeName, len(plygs), comment, polygons, indentString)
}
func GeometeryDecorator(g Geometry, ptsPerLine int, comment string, ptDecorator func(pt Point) string) string {
switch gg := g.(type) {
case Point:
return pointDecorator(gg, true, 0, comment, ptDecorator)
case LineString:
return lineDecorator(gg, true, 0, ptsPerLine, comment, ptDecorator)
case Polygon:
return polygonDecorator(gg, true, 0, ptsPerLine, comment, ptDecorator)
case MultiPolygon:
return multiPolygonDecorator(gg, true, 0, ptsPerLine, comment, ptDecorator)
//case MultiLine:
default:
return ""
}
}