def handle_noargs(self, **options):
        template = open(
            os.path.join(os.path.dirname(__file__), 'markers',
                         'base.svg')).read()
        marker_dir = get_marker_dir()

        if not os.path.exists(marker_dir):
            os.makedirs(marker_dir)

        for color, index in itertools.product(MARKER_COLORS, MARKER_RANGE):
            if os.path.exists(
                    os.path.join(marker_dir, '%s_%d.png' % (color[0], index))):
                continue

            out = template % {
                'label': str(index),
                'fill': color[1],
                'stroke': color[2],
                'text_color': color[3],
            }

            f, infile = tempfile.mkstemp()
            os.write(f, out)
            os.close(f)

            filename = os.path.join(marker_dir,
                                    '%s_%d.png' % (color[0], index))
            subprocess.check_call('convert -background none "%s" "%s"' %
                                  (infile, filename),
                                  shell=True)
            os.unlink(infile)

        template = open(
            os.path.join(os.path.dirname(__file__), 'markers',
                         'star-base.svg')).read()

        for color in MARKER_COLORS:
            if os.path.exists(
                    os.path.join(marker_dir, '%s_star.png' % color[0])):
                continue

            out = template % {'fill': color[1], 'stroke': color[2]}

            f, infile = tempfile.mkstemp()
            os.write(f, out)
            os.close(f)

            filename = os.path.join(marker_dir, '%s_star.png' % color[0])
            subprocess.check_call('convert -background none "%s" "%s"' %
                                  (infile, filename),
                                  shell=True)
            os.unlink(infile)
    def handle_noargs(self, **options):
        template = open(os.path.join(os.path.dirname(__file__), 'markers', 'base.svg')).read()
        marker_dir = get_marker_dir()
        
        if not os.path.exists(marker_dir):
            os.makedirs(marker_dir)
        

        for color, index in itertools.product(MARKER_COLORS, MARKER_RANGE):
            if os.path.exists(os.path.join(marker_dir, '%s-%d.png' % (color[0], index))):
                continue
            
            out = template % {
                'label': str(index),
                'fill': color[1],
                'stroke': color[2],
                'text_color': color[3],
            }
            
            f, infile = tempfile.mkstemp()
            os.write(f, out)
            os.close(f)
            
            filename = os.path.join(marker_dir, '%s-%d.png' % (color[0], index))
            subprocess.call(['convert', '-background', 'none', infile, filename])
            os.unlink(infile)
        
        template = open(os.path.join(os.path.dirname(__file__), 'markers', 'star-base.svg')).read()
            
        for color in MARKER_COLORS:
            if os.path.exists(os.path.join(marker_dir, '%s-star.png' % color[0])):
                continue
            
            out = template % {'fill': color[1], 'stroke': color[2]}
            
            f, infile = tempfile.mkstemp()
            os.write(f, out)
            os.close(f)
            
            filename = os.path.join(marker_dir, '%s-star.png' % color[0])
            subprocess.call(['convert', '-background', 'none', infile, filename])
            os.unlink(infile)
    def handle_noargs(self, **options):
        template = open(os.path.join(os.path.dirname(__file__), "markers", "base.svg")).read()
        marker_dir = get_marker_dir()

        if not os.path.exists(marker_dir):
            os.makedirs(marker_dir)

        for color, index in itertools.product(MARKER_COLORS, MARKER_RANGE):
            if os.path.exists(os.path.join(marker_dir, "%s-%d.png" % (color[0], index))):
                continue

            out = template % {"label": str(index), "fill": color[1], "stroke": color[2], "text_color": color[3]}

            f, infile = tempfile.mkstemp()
            os.write(f, out)
            os.close(f)

            filename = os.path.join(marker_dir, "%s-%d.png" % (color[0], index))
            subprocess.check_call('convert -background none "%s" "%s"' % (infile, filename), shell=True)
            os.unlink(infile)

        template = open(os.path.join(os.path.dirname(__file__), "markers", "star-base.svg")).read()

        for color in MARKER_COLORS:
            if os.path.exists(os.path.join(marker_dir, "%s-star.png" % color[0])):
                continue

            out = template % {"fill": color[1], "stroke": color[2]}

            f, infile = tempfile.mkstemp()
            os.write(f, out)
            os.close(f)

            filename = os.path.join(marker_dir, "%s-star.png" % color[0])
            subprocess.check_call('convert -background none "%s" "%s"' % (infile, filename), shell=True)
            os.unlink(infile)
Beispiel #4
0
def get_map(points, width, height, filename, zoom=None, lon_center=None, lat_center=None):
    """
    Generates a map for the passed in arguments, saving that to filename
    
    @param points: The points where markers on the map should be added. This
                   should be a list of tuples corresponding to the points where
                   markers should be added. These tuples should be in the form
                   (latitude, longitude, colour, index), where acceptable values
                   of colour are specified in @C{utils.MARKER_COLOURS}, and
                   index is the number to appear on the marker, or None if
                   you want a star to appear
    @type points: list
    @param width: The width of the generated map image, in pixels
    @type width: int
    @param height: The height of the generated map image, in pixels
    @type height: int
    @param filename: The name of the file to write the generated map to
    @type filename: str
    @param zoom: The maximum zoom level which to generate this map at
    @type zoom: int
    @param lon_center: The actual center of the generated map
    @type lon_center: float
    @param lat_center: The actual center of the generated map
    @type lat_center: float
    """
    
    lon_min, lon_max = minmax(p[0] for p in points)
    lat_min, lat_max = minmax(p[1] for p in points)
    
    if not zoom:
        size = min(width, height)
        if lat_min != lat_max:
            zoom = int(log2(360/abs(lat_min - lat_max)) + log2(size/256)-1.0)
        else:
            zoom = 16
    
    points = [(get_tile_ref(p[0], p[1], zoom), p[2], p[3]) for p in points]

    lon_range, lat_range = lon_max - lon_min, lat_min - lat_max
    if not lat_center:
        lon_center, lat_center = (lon_min + lon_max)/2, (lat_min + lat_max)/2
    
    tx_min, tx_max = map(int, minmax(p[0][0] for p in points))
    ty_min, ty_max = map(int, minmax(p[0][1] for p in points))
    ty_max, tx_max = ty_max+1, tx_max+1
    
    cx, cy = get_tile_ref(lon_center, lat_center, zoom)
    oxc = int((cx - tx_min) * 256 - width/2)
    oyc = int((cy - ty_min) * 256 - height/2)
    ox, oy = oxc, oyc-10

    tx_min_ = int(tx_min + ox/256)
    tx_max_ = int(tx_max + (width+ox)/256)
    ty_min_ = int(ty_min + oy/256)
    ty_max_ = int(ty_max + (height+oy)/256)
    tiles = [{ 'ref':(tx, ty) }
        for tx in range(tx_min_, tx_max_) for ty in range(ty_min_, ty_max_)]
    
    # Create a new blank image for us to add the tiles on to
    image = PIL.Image.new('RGBA', (width, height))
    
    # Keep track of if the image if malformed or not
    malformed = False
    
    # Lots of different tiles contain the parts we're interested in, so take the
    # parts of those tiles, and copy them into our new image
    for tile in tiles:
        offx = (tile['ref'][0] - tx_min) * 256 - ox
        offy = (tile['ref'][1] - ty_min) * 256 - oy
        
        if not (-256 < offx and offx < width and -256 < offy and offy < height):
            continue
        
        try:
            tile_data = OSMTile.get_data(tile['ref'][0], tile['ref'][1], zoom)
            tile['surface'] = PIL.Image.open(tile_data)
        except Exception as e:
            logger.exception('Failed to fetch OSM tile')
            tile['surface'] = PIL.Image.open(os.path.join(os.path.dirname(__file__), 'fallback', 'fallback.png'))
            malformed = True
        
        image.paste(tile['surface'], ((tile['ref'][0] - tx_min) * 256 - ox, (tile['ref'][1] - ty_min) * 256 - oy))
    
    # Now add the markers to the image
    points.sort(key=lambda p:p[0][1])
    marker_dir = get_marker_dir()
    for (tx, ty), color, index in points:
        if index is None:
            off, fn = (10, 10), "%s-star.png" % color
        else:
            off, fn = (10, 25), "%s-%d.png" % (color, index)
        fn = os.path.join(marker_dir, fn)
        marker = PIL.Image.open(fn)
        off = (
            int((tx - tx_min) * 256 - off[0] - ox),
            int((ty - ty_min) * 256 - off[1] - oy),
        )
        image.paste(marker, (off[0], off[1]), marker)
    
    image.save(filename, 'png')
    
    if malformed:
        raise MapGenerationError((lon_center, lat_center))
    return lon_center, lat_center
Beispiel #5
0
        try:
            tile_data = OSMTile.get_data(tile['ref'][0], tile['ref'][1], zoom)
            tile['surface'] = PIL.Image.open(tile_data)
        except Exception, e:
            tile['surface'] = PIL.Image.open(
                os.path.join(os.path.dirname(__file__), 'fallback',
                             'fallback.png'))
            malformed = True

        image.paste(tile['surface'], ((tile['ref'][0] - tx_min) * 256 - ox,
                                      (tile['ref'][1] - ty_min) * 256 - oy))

    # Now add the markers to the image
    points.sort(key=lambda p: p[0][1])
    marker_dir = get_marker_dir()
    for (tx, ty), color, index in points:
        if index is None:
            off, fn = (10, 10), "%s-star.png" % color
        else:
            off, fn = (10, 25), "%s-%d.png" % (color, index)
        fn = os.path.join(marker_dir, fn)
        marker = PIL.Image.open(fn)
        off = (
            int((tx - tx_min) * 256 - off[0] - ox),
            int((ty - ty_min) * 256 - off[1] - oy),
        )
        image.paste(marker, (off[0], off[1]), marker)

    image.save(filename, 'png')
Beispiel #6
0
def get_map(points,
            width,
            height,
            filename,
            zoom=None,
            lon_center=None,
            lat_center=None):
    """
    Generates a map for the passed in arguments, saving that to filename
    
    @param points: The points where markers on the map should be added. This
                   should be a list of tuples corresponding to the points where
                   markers should be added. These tuples should be in the form
                   (latitude, longitude, colour, index), where acceptable values
                   of colour are specified in @C{utils.MARKER_COLOURS}, and
                   index is the number to appear on the marker, or None if
                   you want a star to appear
    @type points: list
    @param width: The width of the generated map image, in pixels
    @type width: int
    @param height: The height of the generated map image, in pixels
    @type height: int
    @param filename: The name of the file to write the generated map to
    @type filename: str
    @param zoom: The maximum zoom level which to generate this map at
    @type zoom: int
    @param lon_center: The actual center of the generated map
    @type lon_center: float
    @param lat_center: The actual center of the generated map
    @type lat_center: float
    """

    lon_min, lon_max = minmax(p[0] for p in points)
    lat_min, lat_max = minmax(p[1] for p in points)

    if not zoom:
        size = min(width, height)
        if lat_min != lat_max:
            zoom = int(
                log2(360 / abs(lat_min - lat_max)) + log2(size / 256) - 1.0)
        else:
            zoom = 16

    points = [(get_tile_ref(p[0], p[1], zoom), p[2], p[3]) for p in points]

    lon_range, lat_range = lon_max - lon_min, lat_min - lat_max
    if not lat_center:
        lon_center, lat_center = (lon_min + lon_max) / 2, (lat_min +
                                                           lat_max) / 2

    tx_min, tx_max = map(int, minmax(p[0][0] for p in points))
    ty_min, ty_max = map(int, minmax(p[0][1] for p in points))
    ty_max, tx_max = ty_max + 1, tx_max + 1

    cx, cy = get_tile_ref(lon_center, lat_center, zoom)
    oxc = int((cx - tx_min) * 256 - width / 2)
    oyc = int((cy - ty_min) * 256 - height / 2)
    ox, oy = oxc, oyc - 10

    tx_min_ = int(tx_min + ox / 256)
    tx_max_ = int(tx_max + (width + ox) / 256)
    ty_min_ = int(ty_min + oy / 256)
    ty_max_ = int(ty_max + (height + oy) / 256)
    tiles = [{
        'ref': (tx, ty)
    } for tx in range(tx_min_, tx_max_) for ty in range(ty_min_, ty_max_)]

    # Create a new blank image for us to add the tiles on to
    image = PIL.Image.new('RGBA', (width, height))

    # Keep track of if the image if malformed or not
    malformed = False

    # Lots of different tiles contain the parts we're interested in, so take the
    # parts of those tiles, and copy them into our new image
    for tile in tiles:
        offx = (tile['ref'][0] - tx_min) * 256 - ox
        offy = (tile['ref'][1] - ty_min) * 256 - oy

        if not (-256 < offx and offx < width and -256 < offy
                and offy < height):
            continue

        try:
            tile_data = OSMTile.get_data(tile['ref'][0], tile['ref'][1], zoom)
            tile['surface'] = PIL.Image.open(tile_data)
        except Exception as e:
            logger.exception('Failed to fetch OSM tile')
            tile['surface'] = PIL.Image.open(
                os.path.join(os.path.dirname(__file__), 'fallback',
                             'fallback.png'))
            malformed = True

        image.paste(tile['surface'], ((tile['ref'][0] - tx_min) * 256 - ox,
                                      (tile['ref'][1] - ty_min) * 256 - oy))

    # Now add the markers to the image
    points.sort(key=lambda p: p[0][1])
    marker_dir = get_marker_dir()
    for (tx, ty), color, index in points:
        if index is None:
            off, fn = (10, 10), "%s-star.png" % color
        else:
            off, fn = (10, 25), "%s-%d.png" % (color, index)
        fn = os.path.join(marker_dir, fn)
        marker = PIL.Image.open(fn)
        off = (
            int((tx - tx_min) * 256 - off[0] - ox),
            int((ty - ty_min) * 256 - off[1] - oy),
        )
        image.paste(marker, (off[0], off[1]), marker)

    image.save(filename, 'png')

    if malformed:
        raise MapGenerationError((lon_center, lat_center))
    return lon_center, lat_center
Beispiel #7
0
     
     if not (-256 < offx and offx < width and -256 < offy and offy < height):
         continue
     
     try:
         tile_data = OSMTile.get_data(tile['ref'][0], tile['ref'][1], zoom)
         tile['surface'] = PIL.Image.open(tile_data)
     except Exception, e:
         tile['surface'] = PIL.Image.open(os.path.join(os.path.dirname(__file__), 'fallback', 'fallback.png'))
         malformed = True
     
     image.paste(tile['surface'], ((tile['ref'][0] - tx_min) * 256 - ox, (tile['ref'][1] - ty_min) * 256 - oy))
 
 # Now add the markers to the image
 points.sort(key=lambda p:p[0][1])
 marker_dir = get_marker_dir()
 for (tx, ty), color, index in points:
     if index is None:
         off, fn = (10, 10), "%s-star.png" % color
     else:
         off, fn = (10, 25), "%s-%d.png" % (color, index)
     fn = os.path.join(marker_dir, fn)
     marker = PIL.Image.open(fn)
     off = (
         int((tx - tx_min) * 256 - off[0] - ox),
         int((ty - ty_min) * 256 - off[1] - oy),
     )
     image.paste(marker, (off[0], off[1]), marker)
 
 image.save(filename, 'png')