diff --git a/Gopkg.lock b/Gopkg.lock index 2787afa..314a7f7 100644 --- a/Gopkg.lock +++ b/Gopkg.lock @@ -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 diff --git a/Gopkg.toml b/Gopkg.toml index b088de0..57e20e6 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -26,7 +26,7 @@ [[constraint]] - branch = "master" + branch = "allow-disable-of-attribution-rendering" name = "github.com/Luzifer/go-staticmaps" [[constraint]] diff --git a/cache.go b/cache.go index 195007f..b7d42ed 100644 --- a/cache.go +++ b/cache.go @@ -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))) } diff --git a/cache_filesystem.go b/cache_filesystem.go index 196ffb4..5982e35 100644 --- a/cache_filesystem.go +++ b/cache_filesystem.go @@ -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 } diff --git a/main.go b/main.go index a8443a1..0fc13d5 100644 --- a/main.go +++ b/main.go @@ -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 diff --git a/map.go b/map.go index c93fd1e..b80e82c 100644 --- a/map.go +++ b/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) } -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))) diff --git a/vendor/github.com/Luzifer/go-staticmaps/context.go b/vendor/github.com/Luzifer/go-staticmaps/context.go index 77ad316..c1f5525 100644 --- a/vendor/github.com/Luzifer/go-staticmaps/context.go +++ b/vendor/github.com/Luzifer/go-staticmaps/context.go @@ -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)