forked from go-spatial/tegola
-
Notifications
You must be signed in to change notification settings - Fork 0
/
svg.go
255 lines (232 loc) · 7.14 KB
/
svg.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package svg
import (
"encoding/xml"
"fmt"
"io"
"log"
svg "github.com/ajstarks/svgo"
"github.com/go-spatial/tegola"
"github.com/go-spatial/tegola/maths"
)
const DefaultSpacing = 10
type Canvas struct {
*svg.SVG
Board MinMax
Region MinMax
}
func (canvas *Canvas) DrawPoint(x, y int, fill string) {
canvas.Gstyle("text-anchor:middle;font-size:8;fill:white;stroke:black")
canvas.Circle(x, y, 1, "fill:"+fill)
canvas.Text(x, y+5, fmt.Sprintf("(%v %v)", x, y))
canvas.Gend()
}
func drawGrid(canvas *Canvas, mm *MinMax, n int, label bool, id, style, pointstyle, ostyle string) {
x, y, w, h := int(mm.MinX), int(mm.MinY), int(mm.Width()), int(mm.Height())
canvas.Group(fmt.Sprintf(`id="%v"`, id), fmt.Sprintf(`style="%v"`, style))
// Draw all the horizontal and vertical lines.
for i := x; i < x+w; i += n {
canvas.Line(i, y, i, y+h)
}
for i := y; i < y+h; i += n {
canvas.Line(x, i, x+w, i)
}
if !label {
canvas.Gend()
return
}
canvas.Gend()
canvas.Group(fmt.Sprintf(`id="%v"`, id+"_origin"), fmt.Sprintf(`style="%v"`, ostyle))
canvas.Line(0, y, 0, h)
canvas.Line(x, 0, w, 0)
canvas.Gend()
canvas.Group(fmt.Sprintf(`id="%v"`, id+"_points"), fmt.Sprintf(`style="%v"`, pointstyle))
for i := x; i < w; i += n {
canvas.Circle(i, y, 2, "fill:black")
canvas.Text(i, y-5, fmt.Sprintf("% 3v", i))
}
for i := x; i < h; i += n {
canvas.Circle(x, i, 2, "fill:black")
canvas.Text(x-10, i+2, fmt.Sprintf("% 3v", i))
}
canvas.Gend()
}
func (canvas *Canvas) Comment(s string) *Canvas {
fmt.Fprint(canvas.Writer, "<!-- \n")
xml.Escape(canvas.Writer, []byte(s))
fmt.Fprint(canvas.Writer, "\n -->")
return canvas
}
func (canvas *Canvas) Commentf(format string, a ...interface{}) *Canvas {
fmt.Fprint(canvas.Writer, "<!-- \n")
xml.Escape(canvas.Writer, []byte(fmt.Sprintf(format, a...)))
fmt.Fprint(canvas.Writer, "\n -->")
return canvas
}
func (canvas *Canvas) DrawGrid(n int, label bool, style string) {
drawGrid(canvas, &canvas.Board, n, label, fmt.Sprintf("board_%v", n), style, "text-anchor:middle;font-size:8;fill:white;stroke:black", "stroke:black")
}
func (canvas *Canvas) DrawRegion(withGrid bool) {
canvas.Group(`id="region"`, `style="opacity:0.2"`)
canvas.Rect(int(canvas.Region.MinX), int(canvas.Region.MinY), int(canvas.Region.Width()), int(canvas.Region.Height()), "stroke-dasharray:5,5;fill:red;opacity:0.3;"+fmt.Sprintf(";stroke:rgb(%v,%v,%v)", 0, 200, 0))
if withGrid {
drawGrid(canvas, &canvas.Region, 10, false, "region_10", "stroke:red;opacity:0.2", "text-anchor:middle;font-size:8;fill:white;stroke:red", "stroke:red")
drawGrid(canvas, &canvas.Region, 100, true, "region_100", "stroke:red;opacity:0.3", "text-anchor:middle;font-size:8;fill:white;stroke:red", "stroke:red")
}
canvas.Gend()
}
func (canvas *Canvas) DrawPolygon(p tegola.Polygon, id string, style string, pointStyle string, drawPoints bool) int {
var points []maths.Pt
canvas.Group(`id="`+id+`"`, `style="opacity:1"`)
canvas.Gid("polygon_path")
path := ""
pointCount := 0
for _, l := range p.Sublines() {
pts := l.Subpoints()
if len(pts) == 0 {
continue
}
idx := len(pts)
// If the first and last point is the same skipp the last point.
if pts[0].X() == pts[idx-1].X() && pts[0].Y() == pts[idx-1].Y() {
idx = len(pts) - 1
}
if idx <= 0 {
continue
}
for i, pt := range pts[:idx] {
points = append(points, maths.Pt{X: pt.X(), Y: pt.Y()})
if i == 0 {
path += "M "
} else {
path += "L "
}
path += fmt.Sprintf("%v %v ", pt.X(), pt.Y())
pointCount++
}
path += "Z "
}
canvas.Commentf("Point Count: %v", pointCount)
canvas.Path(path, fmt.Sprintf(`id="%v_%v"`, id, pointCount), style)
canvas.Gend()
canvas.Gend()
return pointCount
}
func (canvas *Canvas) DrawMultiPolygon(mp tegola.MultiPolygon, id string, style string, pointStyle string, drawPoints bool) int {
canvas.Gid(id)
count := 0
for i, p := range mp.Polygons() {
count += canvas.DrawPolygon(p, fmt.Sprintf("%v_mp_%v", id, i), style, pointStyle, drawPoints)
}
canvas.Gend()
return count
}
func (canvas *Canvas) DrawLine(l tegola.LineString, id string, style string, pointStyle string, drawPoints bool) {
var points []maths.Pt
pts := l.Subpoints()
canvas.Gid(id)
path := ""
canvas.Gid(id + "_line_path")
for i, pt := range pts {
points = append(points, maths.Pt{X: pt.X(), Y: pt.Y()})
if i == 0 {
path += "M "
} else {
path += "L "
}
path += fmt.Sprintf("%v %v ", pt.X(), pt.Y())
}
canvas.Path(path, style)
canvas.Gend()
if drawPoints {
canvas.Gid(id + "_points")
for _, pt := range points {
x, y := int(pt.X), int(pt.Y)
if pointStyle == "" {
pointStyle = "fill:black"
}
canvas.Circle(x, y, 1, pointStyle)
canvas.Group(fmt.Sprintf(`id="pt%v_%v" style="text-anchor:middle;font-size:8;fill:white;stroke:black;opacity:0.7"`, x, y))
canvas.Text(x, y+5, fmt.Sprintf("(%v %v)", x, y))
canvas.Gend()
}
canvas.Gend()
}
canvas.Gend()
}
func (canvas *Canvas) DrawMathSegments(ls []maths.Line, s ...string) {
log.Printf("Drawing lines(%v) ", len(ls))
for _, line := range ls {
canvas.Line(
int(line[0].X),
int(line[0].Y),
int(line[1].X),
int(line[1].Y),
s...,
)
}
}
func (canvas *Canvas) DrawMathPoints(pts []maths.Pt, s ...string) {
log.Printf("Drawing Points (%v)", len(pts))
prefix := "M"
var path string
for i := range pts {
path += fmt.Sprintf("%v %v %v ", prefix, pts[i].X, pts[i].Y)
prefix = "L"
}
canvas.Path(path, s...)
}
func (canvas *Canvas) DrawMultiLine(ml tegola.MultiLine, id string, style string, pointStyle string, drawPoints bool) {
canvas.Gid(id)
for i, l := range ml.Lines() {
canvas.DrawLine(l, fmt.Sprintf("%v_%v", id, i), style, pointStyle, drawPoints)
}
canvas.Gend()
}
func (canvas *Canvas) DrawGeometry(geo tegola.Geometry, id string, style string, pointStyle string, drawPoints bool) int {
count := 0
switch g := geo.(type) {
case tegola.MultiLine:
canvas.DrawMultiLine(g, "multiline_"+id, style, pointStyle, drawPoints)
case tegola.MultiPolygon:
count += canvas.DrawMultiPolygon(g, "multipolygon_"+id, style, pointStyle, drawPoints)
case tegola.Polygon:
count += canvas.DrawPolygon(g, "polygon_"+id, style, pointStyle, drawPoints)
case tegola.LineString:
canvas.DrawLine(g, "line_"+id, style, pointStyle, drawPoints)
case tegola.Point:
canvas.Gid("point_" + id)
canvas.DrawPoint(int(g.X()), int(g.Y()), pointStyle)
canvas.Gend()
case tegola.MultiPoint:
canvas.Gid("multipoint_" + id)
for i, p := range g.Points() {
canvas.Gid(fmt.Sprintf("mp_%v", i))
canvas.DrawPoint(int(p.X()), int(p.Y()), pointStyle)
canvas.Gend()
}
canvas.Gend()
}
return count
}
func (canvas *Canvas) Init(writer io.Writer, w, h int, grid bool) *Canvas {
if canvas == nil {
panic("Canvas can not be nil!")
}
canvas.SVG = svg.New(writer)
canvas.Startview(w, h, int(canvas.Board.MinX-20), int(canvas.Board.MinY-20), int(canvas.Board.MaxX+20), int(canvas.Board.MaxY+20))
if grid {
canvas.GroupFn([]string{
`id="grid"`,
}, func(canvas *Canvas) {
canvas.DrawGrid(10, false, "stroke:gray")
canvas.DrawGrid(100, true, "stroke:black")
},
)
}
return canvas
}
func (canvas *Canvas) GroupFn(attr []string, fn func(c *Canvas)) {
canvas.SVG.Group(attr...)
fn(canvas)
canvas.SVG.Gend()
}