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
|
// Copyright 2016 Florian Pigorsch. All rights reserved.
//
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"log"
"os"
"sort"
"github.com/flopp/go-coordsparser"
"github.com/flopp/go-staticmaps"
"github.com/fogleman/gg"
"github.com/golang/geo/s2"
"github.com/jessevdk/go-flags"
)
func getTileProviderOrExit(name string) *sm.TileProvider {
tileProviders := sm.GetTileProviders()
tp := tileProviders[name]
if tp != nil {
return tp
}
if name != "list" {
fmt.Println("Bad map type:", name)
}
fmt.Println("Possible map types (to be used with --type/-t):")
// print sorted keys
keys := make([]string, 0, len(tileProviders))
for k := range tileProviders {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
fmt.Println(k)
}
os.Exit(0)
return nil
}
func main() {
var opts struct {
// ClearCache bool `long:"clear-cache" description:"Clears the tile cache"`
Width int `long:"width" description:"Width of the generated static map image" value-name:"PIXELS" default:"512"`
Height int `long:"height" description:"Height of the generated static map image" value-name:"PIXELS" default:"512"`
Output string `short:"o" long:"output" description:"Output file name" value-name:"FILENAME" default:"map.png"`
Type string `short:"t" long:"type" description:"Select the map type; list possible map types with '--type list'" value-name:"MAPTYPE"`
Center string `short:"c" long:"center" description:"Center coordinates (lat,lng) of the static map" value-name:"LATLNG"`
Zoom int `short:"z" long:"zoom" description:"Zoom factor" value-name:"ZOOMLEVEL"`
Markers []string `short:"m" long:"marker" description:"Add a marker to the static map" value-name:"MARKER"`
Paths []string `short:"p" long:"path" description:"Add a path to the static map" value-name:"PATH"`
Areas []string `short:"a" long:"area" description:"Add an area to the static map" value-name:"AREA"`
}
parser := flags.NewParser(&opts, flags.HelpFlag|flags.PassDoubleDash)
parser.LongDescription = `Creates a static map`
_, err := parser.Parse()
if parser.FindOptionByLongName("help").IsSet() {
parser.WriteHelp(os.Stdout)
os.Exit(0)
}
ctx := sm.NewContext()
if parser.FindOptionByLongName("type").IsSet() {
tp := getTileProviderOrExit(opts.Type)
if tp != nil {
ctx.SetTileProvider(tp)
}
}
ctx.SetSize(opts.Width, opts.Height)
if parser.FindOptionByLongName("zoom").IsSet() {
ctx.SetZoom(opts.Zoom)
}
if parser.FindOptionByLongName("center").IsSet() {
lat, lng, err := coordsparser.Parse(opts.Center)
if err != nil {
log.Fatal(err)
} else {
ctx.SetCenter(s2.LatLngFromDegrees(lat, lng))
}
}
for _, markerString := range opts.Markers {
markers, err := sm.ParseMarkerString(markerString)
if err != nil {
log.Fatal(err)
} else {
for _, marker := range markers {
ctx.AddMarker(marker)
}
}
}
for _, pathString := range opts.Paths {
paths, err := sm.ParsePathString(pathString)
if err != nil {
log.Fatal(err)
} else {
for _, path := range paths {
ctx.AddPath(path)
}
}
}
for _, areaString := range opts.Areas {
area, err := sm.ParseAreaString(areaString)
if err != nil {
log.Fatal(err)
} else {
ctx.AddArea(area)
}
}
img, err := ctx.Render()
if err != nil {
log.Fatal(err)
return
}
if err = gg.SavePNG(opts.Output, img); err != nil {
log.Fatal(err)
return
}
}
|