2017-06-27 20:49:53 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-06-17 13:19:28 +00:00
|
|
|
"crypto/sha256"
|
2017-06-27 20:49:53 +00:00
|
|
|
"fmt"
|
|
|
|
"image/color"
|
|
|
|
"io"
|
2018-06-17 13:19:28 +00:00
|
|
|
"strings"
|
2017-06-27 20:49:53 +00:00
|
|
|
|
2018-04-03 19:14:16 +00:00
|
|
|
staticMap "github.com/Luzifer/go-staticmaps"
|
2017-06-27 20:49:53 +00:00
|
|
|
"github.com/fogleman/gg"
|
|
|
|
"github.com/golang/geo/s2"
|
|
|
|
)
|
|
|
|
|
|
|
|
var markerColors = map[string]color.Color{
|
|
|
|
"black": color.RGBA{R: 145, G: 145, B: 145, A: 0xff},
|
|
|
|
"brown": color.RGBA{R: 178, G: 154, B: 123, A: 0xff},
|
|
|
|
"green": color.RGBA{R: 168, G: 196, B: 68, A: 0xff},
|
|
|
|
"purple": color.RGBA{R: 177, G: 150, B: 191, A: 0xff},
|
|
|
|
"yellow": color.RGBA{R: 237, G: 201, B: 107, A: 0xff},
|
|
|
|
"blue": color.RGBA{R: 163, G: 196, B: 253, A: 0xff},
|
|
|
|
"gray": color.RGBA{R: 204, G: 204, B: 204, A: 0xff},
|
|
|
|
"orange": color.RGBA{R: 229, G: 165, B: 68, A: 0xff},
|
|
|
|
"red": color.RGBA{R: 246, G: 118, B: 112, A: 0xff},
|
|
|
|
"white": color.RGBA{R: 245, G: 244, B: 241, A: 0xff},
|
|
|
|
}
|
|
|
|
|
|
|
|
type markerSize float64
|
|
|
|
|
|
|
|
var markerSizes = map[string]markerSize{
|
|
|
|
"tiny": 10,
|
|
|
|
"mid": 15,
|
|
|
|
"small": 20,
|
|
|
|
}
|
|
|
|
|
|
|
|
type marker struct {
|
|
|
|
pos s2.LatLng
|
|
|
|
color color.Color
|
|
|
|
size markerSize
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m marker) String() string {
|
|
|
|
r, g, b, a := m.color.RGBA()
|
|
|
|
return fmt.Sprintf("%s|%.0f|%d,%d,%d,%d", m.pos.String(), m.size, r, g, b, a)
|
|
|
|
}
|
|
|
|
|
2018-06-17 13:19:28 +00:00
|
|
|
type generateMapConfig struct {
|
|
|
|
Center s2.LatLng
|
|
|
|
Zoom int
|
|
|
|
Markers []marker
|
|
|
|
Width int
|
|
|
|
Height int
|
|
|
|
DisableAttribution bool
|
2018-06-17 13:30:32 +00:00
|
|
|
Overlays []*staticMap.TileProvider
|
2018-06-17 13:19:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g generateMapConfig) getCacheKey() string {
|
|
|
|
markerString := []string{}
|
|
|
|
for _, m := range g.Markers {
|
|
|
|
markerString = append(markerString, m.String())
|
|
|
|
}
|
2018-06-17 13:30:32 +00:00
|
|
|
|
|
|
|
overlayString := []string{}
|
|
|
|
for _, o := range g.Overlays {
|
|
|
|
overlayString = append(overlayString, o.URLPattern)
|
|
|
|
}
|
|
|
|
|
|
|
|
hashString := fmt.Sprintf("%s:::%s|%d|%s|%dx%d|%v|%s",
|
|
|
|
version,
|
2018-06-17 13:19:28 +00:00
|
|
|
g.Center.String(),
|
|
|
|
g.Zoom,
|
|
|
|
strings.Join(markerString, "+"),
|
|
|
|
g.Width,
|
|
|
|
g.Height,
|
2018-06-17 13:30:32 +00:00
|
|
|
g.DisableAttribution,
|
|
|
|
fmt.Sprintf("%x", sha256.Sum256([]byte(strings.Join(overlayString, "::")))),
|
|
|
|
)
|
2018-06-17 13:19:28 +00:00
|
|
|
|
|
|
|
return fmt.Sprintf("%x", sha256.Sum256([]byte(hashString)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateMap(opts generateMapConfig) (io.Reader, error) {
|
2017-06-27 20:49:53 +00:00
|
|
|
ctx := staticMap.NewContext()
|
2018-04-03 19:14:16 +00:00
|
|
|
ctx.SetUserAgent(fmt.Sprintf("Mozilla/5.0+(compatible; staticmap/%s; https://github.com/Luzifer/staticmap)", version))
|
|
|
|
|
2018-06-17 13:19:28 +00:00
|
|
|
ctx.SetSize(opts.Width, opts.Height)
|
|
|
|
ctx.SetCenter(opts.Center)
|
|
|
|
ctx.SetZoom(opts.Zoom)
|
2017-06-27 20:49:53 +00:00
|
|
|
|
2018-06-17 13:19:28 +00:00
|
|
|
if opts.DisableAttribution {
|
2018-06-17 12:59:52 +00:00
|
|
|
ctx.OverrideAttribution("")
|
2018-04-03 19:20:34 +00:00
|
|
|
}
|
|
|
|
|
2018-06-17 13:19:28 +00:00
|
|
|
if opts.Markers != nil {
|
|
|
|
for _, m := range opts.Markers {
|
2017-06-27 20:49:53 +00:00
|
|
|
ctx.AddMarker(staticMap.NewMarker(m.pos, m.color, float64(m.size)))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-17 13:30:32 +00:00
|
|
|
if opts.Overlays != nil {
|
|
|
|
for _, o := range opts.Overlays {
|
|
|
|
ctx.AddOverlay(o)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-27 20:49:53 +00:00
|
|
|
img, err := ctx.Render()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
pngCtx := gg.NewContextForImage(img)
|
|
|
|
pngBuf := new(bytes.Buffer)
|
|
|
|
return pngBuf, pngCtx.EncodePNG(pngBuf)
|
|
|
|
}
|