-
Notifications
You must be signed in to change notification settings - Fork 0
/
map.go
153 lines (135 loc) · 3.16 KB
/
map.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
// Copyright 2015 Sam L'ecuyer. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package mapping
import (
"code.google.com/p/sadbox/color"
"encoding/xml"
"fmt"
"github.com/samlecuyer/ecumene/geom"
proj "github.com/samlecuyer/projectron"
"os"
"math"
)
const (
defaultSrsOut string = "+title=WGS 84 / Pseudo-Mercator +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs"
)
type Map struct {
XMLName xml.Name `xml:"Map"`
Styles []*Style `xml:"Style"`
Layers []*Layer `xml:"Layer"`
Stroke color.Hex `xml:"stroke,attr"`
BgColor color.Hex `xml:"bgcolor,attr"`
Srs proj.Projection `xml:"srs,attr"`
bounds geom.Bbox
}
type Include struct {
XMLName xml.Name `xml:"Include"`
Styles []*Style `xml:"Style"`
Layers []*Layer `xml:"Layer"`
}
func (m *Map) Bounds() geom.Bbox {
return m.bounds
}
func (m *Map) setBounds(extent string) (err error) {
factor := math.Pi / 180.0
var x0, y0, x1, y1 float64
_, err = fmt.Sscanf(extent, "%f %f %f %f", &x0, &y0, &x1, &y1)
m.bounds[0] = x0 * factor
m.bounds[1] = y1 * factor
m.bounds[2] = x1 * factor
m.bounds[3] = y0 * factor
return
}
func (m *Map) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var err error
for _, attr := range start.Attr {
if attr.Name.Local == "extent" {
m.setBounds(attr.Value)
}
if attr.Name.Local == "bgcolor" {
m.BgColor = color.Hex(attr.Value)
}
if attr.Name.Local == "srs" {
m.Srs, _ = proj.NewProjection(attr.Value)
}
}
if m.Srs == nil {
m.Srs, err = proj.NewProjection(defaultSrsOut)
if err != nil {
fmt.Println(err.Error())
}
}
for {
e, err := d.Token()
if err != nil {
return err
}
switch e := e.(type) {
case xml.EndElement:
if e.Name.Local == start.Name.Local {
return nil
}
case xml.StartElement:
switch e.Name.Local {
case "Style":
style := new(Style)
if err := d.DecodeElement(style, &e); err != nil {
return err
}
m.Styles = append(m.Styles, style)
case "Layer":
layer := new(Layer)
if err := d.DecodeElement(layer, &e); err != nil {
return err
}
m.Layers = append(m.Layers, layer)
case "Include":
var name string
if err := d.DecodeElement(&name, &e); err != nil {
return err
}
include, err := loadInclude(name)
if err != nil {
return err
}
m.Styles = append(m.Styles, include.Styles...)
m.Layers = append(m.Layers, include.Layers...)
}
}
}
return d.Skip()
}
func NewMap(path string) (*Map, error) {
return loadFile(path)
}
func loadFile(path string) (*Map, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
m := new(Map)
decoder := xml.NewDecoder(file)
decoder.Decode(m)
return m, nil
}
func loadInclude(path string) (*Include, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
m := new(Include)
decoder := xml.NewDecoder(file)
decoder.Decode(m)
return m, nil
}
func (m *Map) FindStyle(name string) *Style {
for _, style := range m.Styles {
if style.Name == name {
return style
}
}
return nil
}