1
0
Fork 0
mirror of https://github.com/Luzifer/staticmap.git synced 2024-12-20 12:51:18 +00:00

Allow disabling of attribution rendering

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2018-04-03 21:20:34 +02:00
parent 1abd72dfdc
commit cfc21d4049
Signed by: luzifer
GPG key ID: DC2729FDD34BE99E
7 changed files with 34 additions and 19 deletions

6
Gopkg.lock generated
View file

@ -2,10 +2,10 @@
[[projects]]
branch = "master"
branch = "allow-disable-of-attribution-rendering"
name = "github.com/Luzifer/go-staticmaps"
packages = ["."]
revision = "5f69ec7945b506b1d33edd717e84e479818e2365"
revision = "d701c2c232ad9cbcac3e48e2c34e98544191c2fe"
[[projects]]
name = "github.com/Luzifer/go_helpers"
@ -166,6 +166,6 @@
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "7c8497a8311887c02aced01ebdf91a73dc82b48920f8f9ebd5ad643204043ce2"
inputs-digest = "bc8b02eb09921c3dcef7328c41ec92c8442d151672053769293e0c476f4a2188"
solver-name = "gps-cdcl"
solver-version = 1

View file

@ -26,7 +26,7 @@
[[constraint]]
branch = "master"
branch = "allow-disable-of-attribution-rendering"
name = "github.com/Luzifer/go-staticmaps"
[[constraint]]

View file

@ -9,14 +9,14 @@ import (
"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{}
for _, m := range marker {
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)))
}

View file

@ -11,8 +11,8 @@ import (
"github.com/golang/geo/s2"
)
func filesystemCache(center s2.LatLng, zoom int, marker []marker, x, y int) (io.ReadCloser, error) {
cacheKey := cacheKeyHelper(center, zoom, marker, x, y)
func filesystemCache(center s2.LatLng, zoom int, marker []marker, x, y int, disableAttribution bool) (io.ReadCloser, error) {
cacheKey := cacheKeyHelper(center, zoom, marker, x, y, disableAttribution)
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()) {
@ -20,7 +20,7 @@ func filesystemCache(center s2.LatLng, zoom int, marker []marker, x, y int) (io.
}
// 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 {
return nil, err
}

15
main.go
View file

@ -65,12 +65,13 @@ func main() {
func handleMapRequest(res http.ResponseWriter, r *http.Request) {
var (
center *s2.LatLng
err error
mapReader io.ReadCloser
markers []marker
x, y int
zoom int
center *s2.LatLng
disableAttribution bool = r.URL.Query().Get("no-attribution") == "true"
err error
mapReader io.ReadCloser
markers []marker
x, y int
zoom int
)
if center, err = parseCoordinate(r.URL.Query().Get("center")); err != nil {
@ -93,7 +94,7 @@ func handleMapRequest(res http.ResponseWriter, r *http.Request) {
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())
http.Error(res, fmt.Sprintf("I experienced difficulties rendering your map: %s", err), http.StatusInternalServerError)
return

6
map.go
View file

@ -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)
}
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.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.SetZoom(zoom)
if disableAttribution {
ctx.ForceNoAttribution()
}
if marker != nil {
for _, m := range marker {
ctx.AddMarker(staticMap.NewMarker(m.pos, m.color, float64(m.size)))

View file

@ -42,6 +42,8 @@ type Context struct {
userAgent string
tileProvider *TileProvider
forceNoAttribution bool
}
// NewContext creates a new instance of Context
@ -147,6 +149,14 @@ func (m *Context) ClearOverlays() {
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 {
r := s2.EmptyRect()
for _, marker := range m.markers {
@ -381,7 +391,7 @@ func (m *Context) Render() (image.Image, error) {
draw.Src)
// draw attribution
if m.tileProvider.Attribution == "" {
if m.tileProvider.Attribution == "" || m.forceNoAttribution {
return croppedImg, nil
}
_, textHeight := gc.MeasureString(m.tileProvider.Attribution)