komoot / staticmap

A small, python-based library for creating map images with lines, markers and polygons.
https://www.komoot.com
Other
290 stars 65 forks source link

add attribution / license to image #20

Open Henner opened 5 years ago

Henner commented 5 years ago

OSM images are only allowed to use, if you add a license hint: https://www.openstreetmap.org/copyright/en

Can you please add a function for this?

Henner commented 5 years ago

I did it like this:

class AttribStaticMap(StaticMap, object):
    def __init__(self, *args, **kwargs):
        self.attribution = u'© OpenStreetMap-Mitwirkende'
        super(AttribStaticMap, self).__init__(*args, **kwargs)

    def _draw_features(self, image):
        super(AttribStaticMap, self)._draw_features(image)

        txt = Image.new('RGBA', image.size, (255, 255, 255, 0))
        # get a font
        fnt = ImageFont.truetype('FreeMono.ttf', 12)
        # fnt = ImageFont.load_default()
        # get a drawing context
        d = ImageDraw.Draw(txt)

        textSize = fnt.getsize(self.attribution)
        textPosition = (image.size[0] - textSize[0], image.size[1] - textSize[1])
        offset = 2
        options = {'fill': (255, 255, 255, 180)}
        d.rectangle([(textPosition[0] - (2 * offset), textPosition[1] - (2 * offset)), (textSize[0] + textPosition[0] + (2 * offset), textSize[1] + textPosition[1] + (2 * offset))], **options)

        # draw text, full opacity
        d.text((textPosition[0] - offset, textPosition[1] - offset), self.attribution, font=fnt, fill='black')

        image.paste(txt, (0, 0), txt)