-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgeo.go
More file actions
242 lines (224 loc) · 5.86 KB
/
Copy pathgeo.go
File metadata and controls
242 lines (224 loc) · 5.86 KB
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
// Package decksh is a little language that generates deck markup
// geo data processing
package decksh
import (
"bufio"
"fmt"
"io"
"os"
"strconv"
"strings"
)
const (
DataFormat int = iota
UNK
KML
SHP
JSON
)
const (
dotfmt = "<ellipse xp=\"%.3f\" yp=\"%.3f\" wp=\"%.3f\" hr=\"100\" color=%s opacity=\"%v\"/>\n"
decklinefmt = "<line xp1=\"%.5f\" yp1=\"%.5f\" xp2=\"%.5f\" yp2=\"%.5f\" sp=\"%.5f\" color=%s opacity=%q/>\n"
textfmt = "<text align=%s xp=\"%.3f\" yp=\"%.3f\" sp=\"%.3f\" %s>%s</text>\n"
geoimgfmt = "<image name=\"%s\" xp=\"%.3f\" yp=\"%.3f\" width=%q height=%q/>\n"
shpdotfmt = "<ellipse xp=\"%.7f\" yp=\"%.7f\" hr=\"100\" color=%s opacity=%q wp=\"%.3f\"/>\n"
)
// geometry defines the canvas and map boundaries
type Geometry struct {
Xmin, Xmax float64
Ymin, Ymax float64
Latmin, Latmax float64
Longmin, Longmax float64
}
// locdata is location data: coordinates and a label
type Locdata struct {
X, Y []float64
Name []string
}
// evaluate reserved variables for canvas bounds (geoXmin, geoXmax, geoYmin, geoYmax)
// if errors occur use the defaults
func geocanvas() (float64, float64, float64, float64) {
xmin, err := strconv.ParseFloat(eval("geoXmin"), 64)
if err != nil {
xmin = 0.0
}
xmax, err := strconv.ParseFloat(eval("geoXmax"), 64)
if err != nil {
xmax = 100.0
}
ymin, err := strconv.ParseFloat(eval("geoYmin"), 64)
if err != nil {
ymin = 0.0
}
ymax, err := strconv.ParseFloat(eval("geoYmax"), 64)
if err != nil {
ymax = 100.0
}
return xmin, xmax, ymin, ymax
}
// evaluate reserved variables for set lat/long bounds,
// if errors occur use the defaults
func geolatlong() (float64, float64, float64, float64) {
latmin, err := strconv.ParseFloat(eval("geoLatMin"), 64)
if err != nil {
latmin = -90.0
}
latmax, err := strconv.ParseFloat(eval("geoLatMax"), 64)
if err != nil {
latmax = 90.0
}
longmin, err := strconv.ParseFloat(eval("geoLongMin"), 64)
if err != nil {
longmin = -180.0
}
longmax, err := strconv.ParseFloat(eval("geoLongMax"), 64)
if err != nil {
longmax = 180.0
}
return latmin, latmax, longmin, longmax
}
// makegeometry fills in the geometry from arguments
func makegeometry() Geometry {
var m Geometry
m.Xmin, m.Xmax, m.Ymin, m.Ymax = geocanvas()
m.Latmin, m.Latmax, m.Longmin, m.Longmax = geolatlong()
return m
}
// mapData maps raw lat/long coordinates to canvas coordinates
func mapData(x, y []float64, g Geometry) ([]float64, []float64) {
for i := range x {
x[i] = vmap(x[i], g.Longmin, g.Longmax, g.Xmin, g.Xmax)
y[i] = vmap(y[i], g.Latmin, g.Latmax, g.Ymin, g.Ymax)
}
return x, y
}
// geoDataFormat looks at a filename and returns the format type
// based on the suffix
func geoDataFormat(s string) int {
i := strings.LastIndex(s, ".")
if i < 0 {
return UNK
}
switch strings.ToLower(s[i:]) {
case ".shp":
return SHP
case ".kml":
return KML
case ".json", ".geojson":
return JSON
default:
return UNK
}
}
// readLoc reads lat/long pairs and an optional name from a file
func readLoc(r io.Reader, sep byte) (Locdata, error) {
var data Locdata
s := bufio.NewScanner(r)
ff := func(c rune) bool { return c == rune(sep) }
for s.Scan() {
t := s.Text()
if strings.HasPrefix(t, "geo:") { // convert geom: URI to tab-separated list
t = strings.Replace(t[4:], ",", "\t", 1)
}
f := strings.FieldsFunc(t, ff)
if len(f) < 2 { // not enough fields
continue
}
xp, err := strconv.ParseFloat(f[1], 64) // long
if err != nil {
continue
}
yp, err := strconv.ParseFloat(f[0], 64) // lat
if err != nil {
continue
}
data.X = append(data.X, xp)
data.Y = append(data.Y, yp)
if len(f) > 2 { // if name is present
data.Name = append(data.Name, f[2])
} else {
data.Name = append(data.Name, "")
}
}
return data, s.Err()
}
// colorop makes a color and optional opacity in the form of name:op
func colorop(color string) (string, string) {
ci := strings.Index(color, ":")
op := "100"
if ci > 0 && ci < len(color) {
op = color[ci+1:]
color = color[0:ci]
}
return color, op
}
// geodot places dots at geometric coordinates
func geodot(w io.Writer, x, y []float64, size float64, color string, op string) {
nc := len(x)
if nc != len(y) {
return
}
for i := range nc {
fmt.Fprintf(w, dotfmt, x[i], y[i], size, color, op)
}
}
// deckgeoimg places images at geometric coordinates
func deckgeoimg(w io.Writer, loc Locdata, width, height string) {
nc := len(loc.X)
for i := range nc {
if len(loc.Name[i]) == 0 {
fmt.Fprintf(os.Stderr, "missing image file")
continue
}
fmt.Fprintf(w, geoimgfmt,
loc.Name[i], loc.X[i], loc.Y[i], width, height)
}
}
// textadj adjusts alignment of text
func textadj(align string, size float64) (float64, float64) {
var xdiff, ydiff float64
size /= 2
switch align {
case "c", "ctext":
ydiff = size
case "d":
ydiff = -size * 2
case "u", "a":
ydiff = size
case "b", "btext", "text":
xdiff = size * 0.75
ydiff = -size * 0.66
case "e", "etext":
xdiff = -size * 0.75
ydiff = -size * 0.66
default:
ydiff = size
}
return xdiff, ydiff
}
// wordstack makes a vertical stack of string in s
func wordstack(w io.Writer, x, y float64, s []string, align string, size float64, fco string) {
ls := size * 1.5
for i := range s {
fmt.Fprintf(w, textfmt, align, x, y, size, fco, xmlesc(s[i]))
y -= ls
}
}
// "geotext" places text at geographic coordinates
func geotext(w io.Writer, x, y []float64, names []string, align string, size float64, fco string) {
xdiff, ydiff := textadj(unquote(align), size)
if align == `"u"` || align == `"a"` || align == `"d"` { // above and under are centered
align = `"c"`
}
for i := range x {
parts := strings.Split(names[i], "\\n")
wordstack(w, x[i]+xdiff, y[i]+ydiff, parts, align, size, fco)
}
}
func geopl(w io.Writer, x, y []float64, color string, shapesize float64) {
if shapesize > 0 {
shpdeckpolyline(w, x, y, color, shapesize)
} else {
deckpolygon(w, x, y, color)
}
}