-
Notifications
You must be signed in to change notification settings - Fork 4
/
draw.go
149 lines (123 loc) · 3.33 KB
/
draw.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
package tiles
import (
"fmt"
"image"
"image/color"
"image/draw"
"math"
"time"
"code.google.com/p/draw2d/draw2d"
"code.google.com/p/freetype-go/freetype"
"code.google.com/p/freetype-go/freetype/truetype"
)
const tileSize = 256
func DrawTile(nwPt, sePt Pointer, zoom int64, font *truetype.Font, data *OsmData, debug bool) (image.Image, error) {
t := time.Now()
// Create white image
img := image.NewRGBA(image.Rect(0, 0, tileSize, tileSize))
draw.Draw(img, img.Bounds(), image.White, image.ZP, draw.Src)
// Plot some features
for fName, features := range data.Features {
if mapFeatures[fName].MinZoom > zoom {
continue
}
for _, feature := range features {
switch feature.Type {
case ItemTypeNode:
//TODO
case ItemTypeWay:
way := data.Ways[feature.Id]
//TODO: this ignores ways crossing over the tile
withinBounds := false
for _, node := range way.GetNodes(data.Nodes) {
if node.Lon() > nwPt.Lon() && node.Lon() < sePt.Lon() &&
node.Lat() < nwPt.Lat() && node.Lat() > sePt.Lat() {
withinBounds = true
}
}
if !withinBounds {
continue
}
coords := [][]float64{}
for _, node := range way.GetNodes(data.Nodes) {
x, y := getRelativeXY(nwPt, node, float64(zoom))
coords = append(coords, []float64{x, y})
}
drawPolyLine(img, color.Black, coords)
case ItemTypeRelation:
//TODO
}
}
}
// Debugging
if debug {
red := color.RGBA{0xff, 0x00, 0x00, 0xff}
// Top/left border
for i := 0; i < tileSize; i++ {
img.Set(0, i, red)
img.Set(i, 0, red)
}
// Tile location
err := drawText(img, font, red, tileSize/2, 20, fmt.Sprintf("%f, %f", nwPt.Lon(), nwPt.Lat()))
if err != nil {
return nil, err
}
// Tile location
err = drawText(img, font, red, tileSize/2, tileSize-20, time.Since(t).String())
if err != nil {
return nil, err
}
}
return img, nil
}
func drawText(img *image.RGBA, font *truetype.Font, color color.Color, x, y int, s string) error {
var ptSize float64 = 12
ctx := freetype.NewContext()
ctx.SetDPI(72)
ctx.SetFont(font)
ctx.SetFontSize(ptSize)
ctx.SetClip(img.Bounds())
ctx.SetDst(img)
ctx.SetSrc(image.NewUniform(color))
ctx.SetHinting(freetype.FullHinting)
width := int(widthOfString(font, ptSize, s))
pt := freetype.Pt(x-width/2, y+int(ctx.PointToFix32(ptSize)>>8)/2)
_, err := ctx.DrawString(s, pt)
if err != nil {
return err
}
return nil
}
// https://code.google.com/p/plotinum/source/browse/vg/font.go#160
func widthOfString(font *truetype.Font, size float64, s string) float64 {
// scale converts truetype.FUnit to float64
scale := size / float64(font.FUnitsPerEm())
width := 0
prev, hasPrev := truetype.Index(0), false
for _, rune := range s {
index := font.Index(rune)
if hasPrev {
width += int(font.Kerning(font.FUnitsPerEm(), prev, index))
}
width += int(font.HMetric(font.FUnitsPerEm(), index).AdvanceWidth)
prev, hasPrev = index, true
}
return float64(width) * scale
}
func round(n float64) int {
//TODO: this is incorrect
return int(math.Floor(n))
}
func drawPolyLine(img *image.RGBA, color color.Color, coords [][]float64) {
path := draw2d.NewPathStorage()
for i, coord := range coords {
if i == 0 {
path.MoveTo(coord[0], coord[1])
} else {
path.LineTo(coord[0], coord[1])
}
}
gc := draw2d.NewGraphicContext(img)
gc.SetStrokeColor(color)
gc.Stroke(path)
}