"""
Generate static map images
"""
from flask import url_for, current_app
from redwind import util
import hashlib
import os
import urllib.parse

# get_map_image(600, 400, 33, -88, 13, [])
# get_map_image(600, 400, 33, -88, 13, [Marker(33, -88)])


class Marker:
    def __init__(self, lat, lng, icon='dot-small-blue'):
        self.lat = lat
        self.lng = lng
        self.icon = icon


def get_map_image(width, height, maxzoom, markers):
    # create the URL
    args = [
        ('width', width),
        ('height', height),
        ('maxzoom', maxzoom),
        ('basemap', 'streets'),
    ] + [
        ('marker[]', 'lat:{};lng:{};icon:{}'.format(m.lat, m.lng, m.icon))
        for m in markers
    ]

    query = urllib.parse.urlencode(args)

    m = hashlib.md5()
    m.update(bytes(query, 'utf-8'))
    hash = m.hexdigest()

    relpath = os.path.join('map', hash + '.png')
    abspath = os.path.join(util.image_root_path(),
                           current_app.static_folder, relpath)

    if not os.path.exists(abspath):
        map_url = 'http://static-maps.kylewm.com/img.php?' + query
        util.download_resource(map_url, abspath)

    return url_for('static', filename=relpath)

@@ -37,91 +37,33 @@
 rom 
-flask import url_for, current_app%0Afrom redwind import util%0Aimport hashlib%0Aimport os
+redwind import imageproxy
 %0Aimp
@@ -654,347 +654,48 @@
     
-query = urllib.parse.urlencode(args)%0A%0A    m = hashlib.md5()%0A    m.update(bytes(query, 'utf-8'))%0A    hash = m.hexdigest()%0A%0A    relpath = os.path.join('map', hash + '.png')%0A    abspath = os.path.join(util.image_root_path(),%0A                           current_app.static_folder, relpath)%0A%0A    if not os.path.exists(abspath):%0A        map_url =
+return imageproxy.construct_url(%0A       
  'ht
@@ -735,16 +735,8 @@
 hp?'
- + query
 %0A   
@@ -744,93 +744,36 @@
     
-util.download_resource(map_url, abspath)%0A%0A    return url_for('static', filename=relpath
++ urllib.parse.urlencode(args)
 )%0A

use imageproxy instead of custom logic for fetching and caching maps