Exemple #1
0
    def __init__(self, bbox, db):
        self.bbox = bbox
        height, width = bbox.spheric_sizes()

        # Compute number of squares, assumming a size of 500 meters
        # per square
        self.width_square_count = width / 500
        self.height_square_count = height / 500

        # Compute the size in angles of the squares
        self.width_square_angle = abs(bbox.get_top_left()[1] - bbox.get_bottom_right()[1]) / self.width_square_count
        self.height_square_angle = abs(bbox.get_top_left()[0] - bbox.get_bottom_right()[0]) / self.height_square_count

        # Compute the lists of longitudes and latitudes of the
        # horizontal and vertical lines delimiting the square
        self.vertical_lines = [
            bbox.get_top_left()[1] + x * self.width_square_angle
            for x in xrange(0, int(math.floor(self.width_square_count)) + 1)
        ]
        self.horizontal_lines = [
            bbox.get_top_left()[0] - x * self.height_square_angle
            for x in xrange(0, int(math.floor(self.height_square_count)) + 1)
        ]

        # Compute the lists of labels
        self.vertical_labels = [
            utils.gen_vertical_square_label(x) for x in xrange(0, int(math.ceil(self.width_square_count)))
        ]
        self.horizontal_labels = [
            utils.gen_horizontal_square_label(x) for x in xrange(0, int(math.ceil(self.height_square_count)))
        ]
        l.debug("vertical lines: %s" % self.vertical_lines)
        l.debug("horizontal lines: %s" % self.horizontal_lines)
        l.debug("vertical labels: %s" % self.vertical_labels)
        l.debug("horizontal labels: %s" % self.horizontal_labels)
Exemple #2
0
def _user_readable_label(squares):
    """Creates a label usable in the street index adjacent to the map
       from a square list."""

    def couple_compare(x,y):
        a = y[0] - x[0]
        if a:
            return a
        return y[1] - x[1]

    def distance(a,b):
        return (b[0]-a[0])**2 + (b[1]-a[1])**2

    minx = min([x[0] for x in squares])
    maxx = max([x[0] for x in squares])
    miny = min([x[1] for x in squares])
    maxy = max([x[1] for x in squares])
    if len(squares) == 1:
        label = (utils.gen_vertical_square_label(squares[0][0]) +
                 utils.gen_horizontal_square_label(squares[0][1]))
    elif minx == maxx:
        label = ('%s%s-%s' % (utils.gen_vertical_square_label(minx),
                              utils.gen_horizontal_square_label(miny),
                              utils.gen_horizontal_square_label(maxy)))
    elif miny == maxy:
        label = ('%s-%s%s' % (utils.gen_vertical_square_label(minx),
                              utils.gen_vertical_square_label(maxx),
                              utils.gen_horizontal_square_label(miny)))
    elif (maxx - minx + 1) * (maxy - miny + 1) == len(squares):
        label = ('%s-%s%s-%s' % (utils.gen_vertical_square_label(minx),
                                 utils.gen_vertical_square_label(maxx),
                                 utils.gen_horizontal_square_label(miny),
                                 utils.gen_horizontal_square_label(maxy)))
    else:
        squares_x_first = sorted(squares, couple_compare)
        squares_y_first = sorted(squares, lambda x,y: couple_compare(y,x))
        if (distance(squares_x_first[0], squares_x_first[-1]) >
            distance(squares_y_first[0], squares_y_first[-1])):
            first = squares_x_first[0]
            last = squares_x_first[-1]
        else:
            first = squares_y_first[0]
            last = squares_y_first[-1]

        label = '%s%s...%s%s' % (utils.gen_vertical_square_label(first[0]),
                                 utils.gen_horizontal_square_label(first[1]),
                                 utils.gen_vertical_square_label(last[0]),
                                 utils.gen_horizontal_square_label(last[1]))
    return label