Example #1
0
    def generateGrid(self, size):
        ''' generate a grid overlaying the city

        :param size: dimension of the cell for the grid
        :rtype: an array of cells that forms the grid
        '''
        file = 'city-{0}_size-{1}m.p'.format(self.name, size)
        path = settings.GRIDS_DIR + file
        grids = []
        try:
            grids = pickle.load(open(path, "rb"))
        except EnvironmentError:
            start = Point(self.box['nw']['lon'], self.box['nw']['lat'])
            lat = lon = start
            while self.geom.envelope.intersects(lat):
                nw = lat
                ne = getNextPoint(nw, size, 90)
                sw = getNextPoint(nw, size, 180)
                se = getNextPoint(ne, size, 180)
                lat = ne
                linearRing = LinearRing(nw, ne, se, sw, nw)
                polygon = Polygon(linearRing)
                if polygon.intersects(self.geom):
                    grids.append(polygon)
                if not self.geom.envelope.intersects(lat):
                    lon = getNextPoint(lon, size, 180)
                    lat = lon
            pickle.dump(grids, open(path, "wb"))
        return grids
def getTilesOfBoundery(boundery, zoom):
    """Get all tiles that covers boundery."""
    box = list(list(boundery.polygon.envelope)[0])
    startPoint = box[1]
    endPoint = box[3]

    firstTile = deg2num(startPoint[1], startPoint[0], zoom)
    lastTile = deg2num(endPoint[1], endPoint[0], zoom)

    results = []
    startingPoint = [firstTile[0], firstTile[1]]
    while startingPoint[0] >= lastTile[0]:
        while startingPoint[1] >= lastTile[1]:
            tile = (startingPoint[0], startingPoint[1])

            tileBoxNW = num2deg(tile[0], tile[1], zoom)
            tileBoxSE = num2deg(tile[0] + 1, tile[1] + 1, zoom)

            tileWindow = [tileBoxNW, tileBoxSE]

            tileBox = [
                (tileBoxNW[1], tileBoxNW[0]),
                (tileBoxNW[1], tileBoxSE[0]),
                (tileBoxSE[1], tileBoxNW[0]),
                (tileBoxSE[1], tileBoxSE[0]),
                (tileBoxNW[1], tileBoxNW[0])
            ]

            tilePolygon = Polygon(tileBox)

            if tilePolygon.intersects(boundery.polygon):
                results.append({
                    'box': tileWindow,
                    'image': getTileByXYZoom(tile[0], tile[1], zoom)
                })

            startingPoint[1] -= 1
        startingPoint[1] = firstTile[1]
        startingPoint[0] -= 1
    return results