mirror of
https://github.com/Luzifer/staticmap.git
synced 2024-12-20 21:01:18 +00:00
Allow disabling of attribution rendering
Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
parent
1abd72dfdc
commit
cfc21d4049
7 changed files with 34 additions and 19 deletions
6
Gopkg.lock
generated
6
Gopkg.lock
generated
|
@ -2,10 +2,10 @@
|
||||||
|
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
branch = "master"
|
branch = "allow-disable-of-attribution-rendering"
|
||||||
name = "github.com/Luzifer/go-staticmaps"
|
name = "github.com/Luzifer/go-staticmaps"
|
||||||
packages = ["."]
|
packages = ["."]
|
||||||
revision = "5f69ec7945b506b1d33edd717e84e479818e2365"
|
revision = "d701c2c232ad9cbcac3e48e2c34e98544191c2fe"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
name = "github.com/Luzifer/go_helpers"
|
name = "github.com/Luzifer/go_helpers"
|
||||||
|
@ -166,6 +166,6 @@
|
||||||
[solve-meta]
|
[solve-meta]
|
||||||
analyzer-name = "dep"
|
analyzer-name = "dep"
|
||||||
analyzer-version = 1
|
analyzer-version = 1
|
||||||
inputs-digest = "7c8497a8311887c02aced01ebdf91a73dc82b48920f8f9ebd5ad643204043ce2"
|
inputs-digest = "bc8b02eb09921c3dcef7328c41ec92c8442d151672053769293e0c476f4a2188"
|
||||||
solver-name = "gps-cdcl"
|
solver-name = "gps-cdcl"
|
||||||
solver-version = 1
|
solver-version = 1
|
||||||
|
|
|
@ -26,7 +26,7 @@
|
||||||
|
|
||||||
|
|
||||||
[[constraint]]
|
[[constraint]]
|
||||||
branch = "master"
|
branch = "allow-disable-of-attribution-rendering"
|
||||||
name = "github.com/Luzifer/go-staticmaps"
|
name = "github.com/Luzifer/go-staticmaps"
|
||||||
|
|
||||||
[[constraint]]
|
[[constraint]]
|
||||||
|
|
6
cache.go
6
cache.go
|
@ -9,14 +9,14 @@ import (
|
||||||
"github.com/golang/geo/s2"
|
"github.com/golang/geo/s2"
|
||||||
)
|
)
|
||||||
|
|
||||||
type cacheFunction func(center s2.LatLng, zoom int, marker []marker, x, y int) (io.ReadCloser, error)
|
type cacheFunction func(center s2.LatLng, zoom int, marker []marker, x, y int, disableAttribution bool) (io.ReadCloser, error)
|
||||||
|
|
||||||
func cacheKeyHelper(center s2.LatLng, zoom int, marker []marker, x, y int) string {
|
func cacheKeyHelper(center s2.LatLng, zoom int, marker []marker, x, y int, disableAttribution bool) string {
|
||||||
markerString := []string{}
|
markerString := []string{}
|
||||||
for _, m := range marker {
|
for _, m := range marker {
|
||||||
markerString = append(markerString, m.String())
|
markerString = append(markerString, m.String())
|
||||||
}
|
}
|
||||||
hashString := fmt.Sprintf("%s|%d|%s|%dx%d", center.String(), zoom, strings.Join(markerString, "+"), x, y)
|
hashString := fmt.Sprintf("%s|%d|%s|%dx%d|%v", center.String(), zoom, strings.Join(markerString, "+"), x, y, disableAttribution)
|
||||||
|
|
||||||
return fmt.Sprintf("%x", sha256.Sum256([]byte(hashString)))
|
return fmt.Sprintf("%x", sha256.Sum256([]byte(hashString)))
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,8 +11,8 @@ import (
|
||||||
"github.com/golang/geo/s2"
|
"github.com/golang/geo/s2"
|
||||||
)
|
)
|
||||||
|
|
||||||
func filesystemCache(center s2.LatLng, zoom int, marker []marker, x, y int) (io.ReadCloser, error) {
|
func filesystemCache(center s2.LatLng, zoom int, marker []marker, x, y int, disableAttribution bool) (io.ReadCloser, error) {
|
||||||
cacheKey := cacheKeyHelper(center, zoom, marker, x, y)
|
cacheKey := cacheKeyHelper(center, zoom, marker, x, y, disableAttribution)
|
||||||
cacheFileName := path.Join(cfg.CacheDir, cacheKey[0:2], cacheKey+".png")
|
cacheFileName := path.Join(cfg.CacheDir, cacheKey[0:2], cacheKey+".png")
|
||||||
|
|
||||||
if info, err := os.Stat(cacheFileName); err == nil && info.ModTime().Add(cfg.ForceCache).After(time.Now()) {
|
if info, err := os.Stat(cacheFileName); err == nil && info.ModTime().Add(cfg.ForceCache).After(time.Now()) {
|
||||||
|
@ -20,7 +20,7 @@ func filesystemCache(center s2.LatLng, zoom int, marker []marker, x, y int) (io.
|
||||||
}
|
}
|
||||||
|
|
||||||
// No cache hit, generate a new map
|
// No cache hit, generate a new map
|
||||||
mapReader, err := generateMap(center, zoom, marker, x, y)
|
mapReader, err := generateMap(center, zoom, marker, x, y, disableAttribution)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
15
main.go
15
main.go
|
@ -65,12 +65,13 @@ func main() {
|
||||||
|
|
||||||
func handleMapRequest(res http.ResponseWriter, r *http.Request) {
|
func handleMapRequest(res http.ResponseWriter, r *http.Request) {
|
||||||
var (
|
var (
|
||||||
center *s2.LatLng
|
center *s2.LatLng
|
||||||
err error
|
disableAttribution bool = r.URL.Query().Get("no-attribution") == "true"
|
||||||
mapReader io.ReadCloser
|
err error
|
||||||
markers []marker
|
mapReader io.ReadCloser
|
||||||
x, y int
|
markers []marker
|
||||||
zoom int
|
x, y int
|
||||||
|
zoom int
|
||||||
)
|
)
|
||||||
|
|
||||||
if center, err = parseCoordinate(r.URL.Query().Get("center")); err != nil {
|
if center, err = parseCoordinate(r.URL.Query().Get("center")); err != nil {
|
||||||
|
@ -93,7 +94,7 @@ func handleMapRequest(res http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if mapReader, err = cacheFunc(*center, zoom, markers, x, y); err != nil {
|
if mapReader, err = cacheFunc(*center, zoom, markers, x, y, disableAttribution); err != nil {
|
||||||
log.Errorf("Map render failed: %s (Request: %s)", err, r.URL.String())
|
log.Errorf("Map render failed: %s (Request: %s)", err, r.URL.String())
|
||||||
http.Error(res, fmt.Sprintf("I experienced difficulties rendering your map: %s", err), http.StatusInternalServerError)
|
http.Error(res, fmt.Sprintf("I experienced difficulties rendering your map: %s", err), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
|
6
map.go
6
map.go
|
@ -43,7 +43,7 @@ func (m marker) String() string {
|
||||||
return fmt.Sprintf("%s|%.0f|%d,%d,%d,%d", m.pos.String(), m.size, r, g, b, a)
|
return fmt.Sprintf("%s|%.0f|%d,%d,%d,%d", m.pos.String(), m.size, r, g, b, a)
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateMap(center s2.LatLng, zoom int, marker []marker, x, y int) (io.Reader, error) {
|
func generateMap(center s2.LatLng, zoom int, marker []marker, x, y int, disableAttribution bool) (io.Reader, error) {
|
||||||
ctx := staticMap.NewContext()
|
ctx := staticMap.NewContext()
|
||||||
ctx.SetUserAgent(fmt.Sprintf("Mozilla/5.0+(compatible; staticmap/%s; https://github.com/Luzifer/staticmap)", version))
|
ctx.SetUserAgent(fmt.Sprintf("Mozilla/5.0+(compatible; staticmap/%s; https://github.com/Luzifer/staticmap)", version))
|
||||||
|
|
||||||
|
@ -51,6 +51,10 @@ func generateMap(center s2.LatLng, zoom int, marker []marker, x, y int) (io.Read
|
||||||
ctx.SetCenter(center)
|
ctx.SetCenter(center)
|
||||||
ctx.SetZoom(zoom)
|
ctx.SetZoom(zoom)
|
||||||
|
|
||||||
|
if disableAttribution {
|
||||||
|
ctx.ForceNoAttribution()
|
||||||
|
}
|
||||||
|
|
||||||
if marker != nil {
|
if marker != nil {
|
||||||
for _, m := range marker {
|
for _, m := range marker {
|
||||||
ctx.AddMarker(staticMap.NewMarker(m.pos, m.color, float64(m.size)))
|
ctx.AddMarker(staticMap.NewMarker(m.pos, m.color, float64(m.size)))
|
||||||
|
|
12
vendor/github.com/Luzifer/go-staticmaps/context.go
generated
vendored
12
vendor/github.com/Luzifer/go-staticmaps/context.go
generated
vendored
|
@ -42,6 +42,8 @@ type Context struct {
|
||||||
|
|
||||||
userAgent string
|
userAgent string
|
||||||
tileProvider *TileProvider
|
tileProvider *TileProvider
|
||||||
|
|
||||||
|
forceNoAttribution bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewContext creates a new instance of Context
|
// NewContext creates a new instance of Context
|
||||||
|
@ -147,6 +149,14 @@ func (m *Context) ClearOverlays() {
|
||||||
m.overlays = nil
|
m.overlays = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ForceNoAttribution disables attribution rendering
|
||||||
|
//
|
||||||
|
// Pay attention you might be violating the terms of usage for the
|
||||||
|
// selected map provider - only use the function if you are aware of this!
|
||||||
|
func (m *Context) ForceNoAttribution() {
|
||||||
|
m.forceNoAttribution = true
|
||||||
|
}
|
||||||
|
|
||||||
func (m *Context) determineBounds() s2.Rect {
|
func (m *Context) determineBounds() s2.Rect {
|
||||||
r := s2.EmptyRect()
|
r := s2.EmptyRect()
|
||||||
for _, marker := range m.markers {
|
for _, marker := range m.markers {
|
||||||
|
@ -381,7 +391,7 @@ func (m *Context) Render() (image.Image, error) {
|
||||||
draw.Src)
|
draw.Src)
|
||||||
|
|
||||||
// draw attribution
|
// draw attribution
|
||||||
if m.tileProvider.Attribution == "" {
|
if m.tileProvider.Attribution == "" || m.forceNoAttribution {
|
||||||
return croppedImg, nil
|
return croppedImg, nil
|
||||||
}
|
}
|
||||||
_, textHeight := gc.MeasureString(m.tileProvider.Attribution)
|
_, textHeight := gc.MeasureString(m.tileProvider.Attribution)
|
||||||
|
|
Loading…
Reference in a new issue