コード例 #1
0
    def buildTiles(self, items, attributes):
        """
        Build tiles of given items

        :param items: list of items to build
        :param attributes: attributes for layout
        """
        matrix = {}

        def addItem(tx, ty, px, py, **itemparams):
            if '{}|{}'.format(tx, ty) not in matrix:
                matrix['{}|{}'.format(tx, ty)] = []
            matrix['{}|{}'.format(tx, ty)].append([px, py, itemparams])

        params = {}

        for zoom in self.ZOOMLEVELS:

            if not os.path.exists('{}/{}'.format(self.DESTPATH, zoom)):  # create directory
                os.makedirs('{}/{}'.format(self.DESTPATH, zoom))

            for item in items:
                _last = None
                for node in item.parameters['nodes']:
                    coord = deg2num(float(node['lat']), float(node['lon']), zoom)

                    if _last is not None:

                        if _last[0] <= coord[0]:  # eval tiles in x direction
                            dx = range(_last[0], coord[0] + 1)
                        else:
                            dx = range(_last[0], coord[0] - 1, -1)

                        if _last[1] <= coord[1]:  # eval tiles in y direction
                            dy = range(_last[1], coord[1] + 1)
                        else:
                            dy = range(_last[1], coord[1] - 1, -1)

                        for x in dx:  # loop through tiles
                            for y in dy:
                                lstart = (_last[2] + (_last[0] - x) * 256, _last[3] + (_last[1] - y) * 256)  # start point
                                lend = (coord[2] + (coord[0] - x) * 256, coord[3] + (coord[1] - y) * 256)  # end point

                                if os.path.exists('{}/{}/{}-{}.png'.format(self.DESTPATH, zoom, x, y)):
                                    img = Image.open('{}/{}/{}-{}.png'.format(self.DESTPATH, zoom, x, y))
                                else:
                                    img = Image.new('RGBA', (256, 256))
                                draw = ImageDraw.Draw(img)

                                draw.line([lstart, lend], fill=self.LINECOLOR, width=(zoom - 15) * 2)  # draw line
                                img.save('{}/{}/{}-{}.png'.format(self.DESTPATH, zoom, x, y))

                    _last = coord
コード例 #2
0
ファイル: layout_hydrant.py プロジェクト: sambandi/eMonitor
    def buildTiles(self, items, attributes):
        """
        Build tiles for given configuration

        :param items: list of items to build
        :param attributes: parameters
        """
        font = ImageFont.truetype("emonitor/modules/mapitems/inc/font.ttf", 12)

        for zoom in self.ZOOMLEVELS:
            matrix = {}
            if not os.path.exists('{}/{}'.format(self.DESTPATH, zoom)):
                os.makedirs('{}/{}'.format(self.DESTPATH, zoom))

            for item in items:
                coord = deg2num(float(item.parameters['lat']), float(item.parameters['lon']), zoom)

                def addItem(tx, ty, px, py, **itemparams):
                    if '{}|{}'.format(tx, ty) not in matrix:
                        matrix['{}|{}'.format(tx, ty)] = []
                    matrix['{}|{}'.format(tx, ty)].append([px, py, itemparams])

                params = {}
                for param in [a for a in attributes if a in item.parameters]:
                    params[param] = item.parameters[param]

                addItem(coord[0], coord[1], coord[2], coord[3], params=params)  # q5
                if coord[2] + self.HALFX > 256:
                    addItem(coord[0] + 1, coord[1], coord[2] - 256, coord[3], params=params)  # q6
                if coord[2] - self.HALFX < 0:
                    addItem(coord[0] - 1, coord[1], coord[2] + 256, coord[3], params=params)  # q4
                if coord[3] + self.HALFY > 256:
                    addItem(coord[0], coord[1] + 1, coord[2], coord[3] - 256, params=params)  # q8
                if coord[3] - self.HALFY < 0:
                    addItem(coord[0], coord[1] - 1, coord[2], coord[3] + 256, params=params)  # q2
                if coord[2] + self.HALFX > 256 and coord[3] + self.HALFY > 256:
                    addItem(coord[0] + 1, coord[1] + 1, coord[2] - 256, coord[3] - 256, params=params)  # q9
                elif coord[2] - self.HALFX < 0 and coord[3] - self.HALFY < 0:
                    addItem(coord[0] - 1, coord[1] - 1, coord[2] + 256, coord[3] + 256, params=params)  # q1

            for tile in matrix:
                img = Image.new('RGBA', (256, 256))
                draw = ImageDraw.Draw(img)

                if zoom == 18:
                    """
                    icon=ellipse;20;20 # objecttype, x dimension, y dimension
                    color=255;0;0
                    textattribute=fire_hydrant:diameter
                    """
                    for p in matrix[tile]:
                        draw.ellipse((p[0] - 10, p[1] - 10, p[0] + 10, p[1] + 10), outline=(255, 0, 0))
                        if 'fire_hydrant:diameter' in p[2]['params']:
                            draw.text((p[0] - self.HALFX, p[1] + self.HALFY), "{}".format(p[2]['params']['fire_hydrant:diameter']), (255, 0, 0), font=font)
                if zoom == 17:
                    """
                    icon=ellipse;10;10 # objecttype, x dimension, y dimension
                    color=255;0;0 # rgb color value
                    """
                    for p in matrix[tile]:
                        draw.ellipse((p[0] - 5, p[1] - 5, p[0] + 5, p[1] + 5), fill=(255, 0, 0))
                if zoom == 16:
                    """
                    icon=ellipse;4;4 # objecttype, x dimension, y dimension
                    color=255;0;0 # rgb color value
                    """
                    for p in matrix[tile]:
                        draw.ellipse((p[0] - 2, p[1] - 2, p[0] + 2, p[1] + 2), fill=(255, 0, 0))

                img.save('{}/{}/{}-{}.png'.format(self.DESTPATH, zoom, tile.split('|')[0], tile.split('|')[1]))
コード例 #3
0
ファイル: layout_ffentry.py プロジェクト: sambandi/eMonitor
    def buildTiles(self, items, attributes):
        """
        Build tiles of given items

        :param items: list of items to build
        :param attributes: attributes for layout
        """
        matrix = {}

        def addItem(tx, ty, px, py, **itemparams):
            if '{}|{}'.format(tx, ty) not in matrix:
                matrix['{}|{}'.format(tx, ty)] = []
            matrix['{}|{}'.format(tx, ty)].append([px, py, itemparams])

        params = {}

        for zoom in self.ZOOMLEVELS:

            if not os.path.exists('{}/{}'.format(self.DESTPATH,
                                                 zoom)):  # create directory
                os.makedirs('{}/{}'.format(self.DESTPATH, zoom))

            for item in items:
                _last = None
                for node in item.parameters['nodes']:
                    coord = deg2num(float(node['lat']), float(node['lon']),
                                    zoom)

                    if _last is not None:

                        if _last[0] <= coord[0]:  # eval tiles in x direction
                            dx = range(_last[0], coord[0] + 1)
                        else:
                            dx = range(_last[0], coord[0] - 1, -1)

                        if _last[1] <= coord[1]:  # eval tiles in y direction
                            dy = range(_last[1], coord[1] + 1)
                        else:
                            dy = range(_last[1], coord[1] - 1, -1)

                        for x in dx:  # loop through tiles
                            for y in dy:
                                lstart = (_last[2] + (_last[0] - x) * 256,
                                          _last[3] + (_last[1] - y) * 256
                                          )  # start point
                                lend = (coord[2] + (coord[0] - x) * 256,
                                        coord[3] + (coord[1] - y) * 256
                                        )  # end point

                                if os.path.exists('{}/{}/{}-{}.png'.format(
                                        self.DESTPATH, zoom, x, y)):
                                    img = Image.open('{}/{}/{}-{}.png'.format(
                                        self.DESTPATH, zoom, x, y))
                                else:
                                    img = Image.new('RGBA', (256, 256))
                                draw = ImageDraw.Draw(img)

                                draw.line([lstart, lend],
                                          fill=self.LINECOLOR,
                                          width=(zoom - 15) * 2)  # draw line
                                img.save('{}/{}/{}-{}.png'.format(
                                    self.DESTPATH, zoom, x, y))

                    _last = coord