Пример #1
0
    def get_images(self, chips: List[dict]) -> Iterator[Tuple[dict, bytes]]:
        """return bounds of original tile filled with the 4 child chips 1 zoom level up in bytes"""
        for chip in chips:
            w_lst = []
            for i in range(2):
                for j in range(2):
                    window = Window(i * 256, j * 256, 256, 256)
                    w_lst.append(window)

            child_tiles = children(chip.get('x'), chip.get('y'), chip.get('z')) #get this from database (tile_zoom)
            child_tiles.sort()

            with MemoryFile() as memfile:
                with memfile.open(driver='jpeg', height=512, width=512, count=3, dtype=rasterio.uint8) as dataset:
                    for num, t in enumerate(child_tiles):
                        url = chip.get('url').replace(str(chip.get('x')), str(t.x), 1).replace(str(chip.get('y')), str(t.y), 1).replace(str(chip.get('z')), str(t.z), 1)

                        r = requests.get(url)
                        img = np.array(Image.open(io.BytesIO(r.content)), dtype=np.uint8)
                        try:
                            img = img.reshape((256, 256, 3)) # 4 channels returned from some endpoints, but not all
                        except ValueError:
                            img = img.reshape((256, 256, 4))
                        img = img[:, :, :3]
                        img = np.rollaxis(img, 2, 0)
                        dataset.write(img, window=w_lst[num])
                dataset_b = memfile.read() #but this fails
                yield(
                    chip,
                    dataset_b)
Пример #2
0
def all_descendant_tiles(x, y, zoom, max_zoom):
    """Return all subtiles contained within a tile"""
    if zoom < max_zoom:
        for child_tile in mercantile.children(x, y, zoom):
            yield child_tile
            yield from all_descendant_tiles(child_tile.x, child_tile.y,
                                            child_tile.z, max_zoom)
Пример #3
0
def children(ctx, input, depth):
    """Takes a [x, y, z] tile as input and writes its children to stdout
    in the same form.

    $ echo "[486, 332, 10]" | mercantile parent

    Output:

    [243, 166, 9]
    """
    verbosity = ctx.obj['verbosity']
    logger = logging.getLogger('mercantile')
    try:
        src = click.open_file(input).readlines()
    except IOError:
        src = [input]
    stdout = click.get_text_stream('stdout')

    try:
        for line in src:
            line = line.strip()
            tiles = [json.loads(line)[:3]]
            for i in range(depth):
                tiles = sum([mercantile.children(t) for t in tiles], [])
            for t in tiles:
                output = json.dumps(t)
                stdout.write(output)
                stdout.write('\n')
        sys.exit(0)
    except Exception:
        logger.exception("Failed. Exception caught")
        sys.exit(1)
Пример #4
0
def test_children():
    x, y, z = 243, 166, 9
    children = mercantile.children(x, y, z)
    assert len(children) == 4

    # Four sub-tiles (children) when zooming in
    #
    #    -------
    #   | a | b |
    #    ---|---
    #   | d | c |
    #    -------
    #
    # with:
    #
    #   a=(2x,     2y, z + 1)    b=(2x + 1,     2y, z + 1)
    #
    #   d=(2x, 2y + 1, z + 1)    c=(2x + 1, 2y + 1, z + 1)
    #
    # See: https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Subtiles

    a, b, c, d = children

    assert a == mercantile.Tile(2 * x, 2 * y, z + 1)
    assert b == mercantile.Tile(2 * x + 1, 2 * y, z + 1)
    assert c == mercantile.Tile(2 * x + 1, 2 * y + 1, z + 1)
    assert d == mercantile.Tile(2 * x, 2 * y + 1, z + 1)
Пример #5
0
    def add_tiles(self, zMin, zMax):
        zooms = np.arange(zMax - zMin + 2) + zMin - 1

        obj = {zMin - 1: [mercantile.tile(-122.4, 37.5, zMin - 1)]}

        basepath = '%s/jpg' % (self.path)
        if not os.path.isdir(basepath):
            os.mkdir(basepath)

        for i in range(1, len(zooms)):
            tiles = []
            os.mkdir("%s/%s" % (basepath, zooms[i]))
            for t in obj[zooms[i - 1]]:
                for tt in mercantile.children(t):
                    tiles.append(tt)
                    if os.path.isdir("%s/%s/%s" % (basepath, zooms[i], tt.x)):
                        shutil.copy(
                            self.imgs[int(np.random.rand() + 0.1)],
                            "%s/%s/%s/%s.jpg" %
                            (basepath, zooms[i], tt.x, tt.y))
                    else:
                        os.mkdir("%s/%s/%s" % (basepath, zooms[i], tt.x))
                        shutil.copy(
                            self.imgs[int(np.random.rand() + 0.1)],
                            "%s/%s/%s/%s.jpg" %
                            (basepath, zooms[i], tt.x, tt.y))
            obj[zooms[i]] = tiles
Пример #6
0
    def add_tiles(self, zMin, zMax):
        zooms = np.arange(zMax - zMin + 2) + zMin - 1

        obj = {
            zMin - 1: [mercantile.tile(-122.4, 37.5, zMin - 1)]
        }

        basepath = '%s/jpg' % (self.path)
        if not os.path.isdir(basepath):
            os.mkdir(basepath)

        for i in xrange(1, len(zooms)):
            tiles = []
            os.mkdir("%s/%s" % (basepath, zooms[i])) 
            for t in obj[zooms[i - 1]]:
                for tt in mercantile.children(t):
                    tiles.append(tt)
                    if os.path.isdir("%s/%s/%s" % (basepath, zooms[i], tt.x)):
                        shutil.copy(self.imgs[int(np.random.rand() + 0.1)],
                                    "%s/%s/%s/%s.jpg" % (basepath, zooms[i], tt.x, tt.y))
                    else:
                        os.mkdir("%s/%s/%s" % (basepath, zooms[i], tt.x))
                        shutil.copy(self.imgs[int(np.random.rand() + 0.1)],
                                    "%s/%s/%s/%s.jpg" % (basepath, zooms[i], tt.x, tt.y))
            obj[zooms[i]] = tiles
Пример #7
0
 def prefetch(self,
              tile: Tile,
              host_dir: str = None,
              verbose: bool = False):
     x, y, z = tile
     if z != int(z):
         raise ValueError("Fractional zooms not allowed!")
     if x == int(x) and y == int(y):
         x, y = int(x), int(y)
         tile_parents = [parent(tile, zoom=i) for i in range(z + 1)]
         for tile_parent in tile_parents:
             if tile_parent in self.cached_tiles:
                 return
         if z < 14:
             blob = children(tile, zoom=14)
             if verbose:
                 from tqdm import tqdm
                 blob = tqdm(blob, desc="OpenStreetMap Prefetch")
             for child in blob:
                 self.prefetch(child)
         else:
             self.__get_from_osmnx(tile)
         if host_dir is not None:
             self.save(host_dir)
         return
     source_tiles = [
         Tile(np.trunc(x), np.trunc(y), z),
         Tile(np.trunc(x), np.ceil(y), z),
         Tile(np.ceil(x), np.trunc(y), z),
         Tile(np.ceil(x), np.ceil(y), z),
     ]
     for tile in source_tiles:
         self.prefetch(tile)
     return
Пример #8
0
def children(ctx, input, depth):
    """Takes a [x, y, z] tile as input and writes its children to stdout
    in the same form.

    $ echo "[486, 332, 10]" | mercantile parent

    Output:

    [243, 166, 9]
    """
    verbosity = ctx.obj['verbosity']
    logger = logging.getLogger('mercantile')
    try:
        src = click.open_file(input).readlines()
    except IOError:
        src = [input]
    stdout = click.get_text_stream('stdout')

    try:
        for line in src:
            line = line.strip()
            tiles = [json.loads(line)[:3]]
            for i in range(depth):
                tiles = sum([mercantile.children(t) for t in tiles], [])
            for t in tiles:
                output = json.dumps(t)
                stdout.write(output)
                stdout.write('\n')
        sys.exit(0)
    except Exception:
        logger.exception("Failed. Exception caught")
        sys.exit(1)
Пример #9
0
def children(ctx, input, depth):
    """Takes [x, y, z] tiles as input and writes children to stdout
    in the same form.

    Input may be a compact newline-delimited sequences of JSON or
    a pretty-printed ASCII RS-delimited sequence of JSON (like
    https://tools.ietf.org/html/rfc8142 and
    https://tools.ietf.org/html/rfc7159).

    Example:

    \b
    echo "[486, 332, 10]" | mercantile children
    [972, 664, 11]
    [973, 664, 11]
    [973, 665, 11]
    [972, 665, 11]

    """
    src = normalize_input(input)
    for line in iter_lines(src):
        tiles = [json.loads(line)[:3]]
        for i in range(depth):
            tiles = sum([mercantile.children(t) for t in tiles], [])
        for t in tiles:
            output = json.dumps(t)
            click.echo(output)
Пример #10
0
def all_descendant_tiles(x, y, zoom, max_zoom):
    """Return all subtiles contained within a tile"""
    if zoom < max_zoom:
        for child_tile in mercantile.children(x, y, zoom):
            yield child_tile
            yield from all_descendant_tiles(child_tile.x, child_tile.y,
                                            child_tile.z, max_zoom)
Пример #11
0
def find_child_land_tiles(tile: mercantile.Tile, gdf: gpd.GeoDataFrame,
                          maxzoom: int) -> List[mercantile.Tile]:
    """Recursively find tiles at desired zoom that intersect land areas

    Args:
        - tile: tile to recursively search within
        - gdf: GeoDataFrame with geometries of land masses
        - maxzoom: zoom at which to stop recursing

    Returns:
        List of tiles intersecting land
    """
    land_tiles = []

    for child in mercantile.children(tile):
        tile_geom = shape(mercantile.feature(tile)['geometry'])

        intersects_land = gdf.intersects(tile_geom).any()

        if not intersects_land:
            continue

        if child.z == maxzoom:
            land_tiles.append(child)
            continue

        land_tiles.extend(find_child_land_tiles(child, gdf, maxzoom))

    return land_tiles
Пример #12
0
def download_tile_tms(tile, imagery, folder, kwargs):
    """Download a satellite image tile from a tms endpoint"""

    image_format = get_image_format(imagery, kwargs)

    if os.environ.get('ACCESS_TOKEN'):
        token = os.environ.get('ACCESS_TOKEN')
        imagery = imagery.format_map(SafeDict(ACCESS_TOKEN=token))

    r = requests.get(url(tile.split('-'), imagery),
                     auth=kwargs.get('http_auth'))
    tile_img = op.join(folder, '{}{}'.format(tile, image_format))
    tile = tile.split('-')

    over_zoom = kwargs.get('over_zoom')
    if over_zoom:
        new_zoom = over_zoom + kwargs.get('zoom')
        # get children
        child_tiles = children(int(tile[0]),
                               int(tile[1]),
                               int(tile[2]),
                               zoom=new_zoom)
        child_tiles.sort()

        new_dim = 256 * (2 * over_zoom)

        w_lst = []
        for i in range(2 * over_zoom):
            for j in range(2 * over_zoom):
                window = Window(i * 256, j * 256, 256, 256)
                w_lst.append(window)

        # request children
        with rasterio.open(tile_img,
                           'w',
                           driver='jpeg',
                           height=new_dim,
                           width=new_dim,
                           count=3,
                           dtype=rasterio.uint8) as w:
            for num, t in enumerate(child_tiles):
                t = [str(t[0]), str(t[1]), str(t[2])]
                r = requests.get(url(t, imagery), auth=kwargs.get('http_auth'))
                img = np.array(Image.open(io.BytesIO(r.content)),
                               dtype=np.uint8)
                try:
                    img = img.reshape(
                        (256, 256, 3)
                    )  # 4 channels returned from some endpoints, but not all
                except ValueError:
                    img = img.reshape((256, 256, 4))
                img = img[:, :, :3]
                img = np.rollaxis(img, 2, 0)
                w.write(img, window=w_lst[num])
    else:
        r = requests.get(url(tile, imagery), auth=kwargs.get('http_auth'))
        with open(tile_img, 'wb') as w:
            w.write(r.content)
    return tile_img
Пример #13
0
 def siblings(self) -> List["TileID"]:
     """
     Returns a list of this tile's siblings.
     """
     return [
         TileID(mt)
         for mt in mercantile.children(mercantile.parent(self.asmrcantile))
     ]
Пример #14
0
def all_descendant_tiles(x, y, zoom, max_zoom):
    """
    Return all child tiles contained within a tile defined by x, y, zoom
    down to the max_zom level.
    """
    if zoom < max_zoom:
        for child_tile in mercantile.children(x, y, zoom):
            yield child_tile
            yield from all_descendant_tiles(child_tile.x, child_tile.y,
                                            child_tile.z, max_zoom)
Пример #15
0
def all_descendant_tiles(x, y, zoom, max_zoom):
    """
    Return all child tiles contained within a tile defined by x, y, zoom
    down to the max_zom level.
    """
    if zoom < max_zoom:
        for child_tile in mercantile.children(x, y, zoom):
            yield child_tile
            yield from all_descendant_tiles(child_tile.x, child_tile.y,
                                            child_tile.z, max_zoom)
Пример #16
0
def get_strided_tiles(root: Tile, zoom: int = 15):
    child_bounds = children(root, zoom=zoom)
    min_x = min((child.x for child in child_bounds))
    min_y = min((child.y for child in child_bounds))
    max_x = max((child.x for child in child_bounds))
    max_y = max((child.y for child in child_bounds))
    n = max_x - min_x + 1
    strided_tiles = []
    for x, y in product(np.arange(min_x, max_x + 0.1, 0.5),
                        np.arange(min_y, max_y + 0.1, 0.5)):
        strided_tiles += [Tile(float(x), float(y), zoom)]
    return strided_tiles
Пример #17
0
def download_tile_tms(tile, imagery, folder, zoom, supertile):
    """Download a satellite image tile from a tms endpoint"""

    image_format = get_image_format(imagery['url'])
    r = requests.get(url(tile.split('-'), imagery['url']))
    tile_img = op.join(folder, '{}{}'.format(tile, image_format))
    tile = tile.split('-')

    #super-tile special case
    if supertile:
        new_zoom = zoom + 1  #get zoom from ml-enabler database
        # get children
        child_tiles = children(int(tile[0]),
                               int(tile[1]),
                               int(tile[2]),
                               zoom=new_zoom)
        child_tiles.sort()

        new_dim = 256 * (2 * (new_zoom - zoom))

        w_lst = []
        for i in range(2 * (new_zoom - zoom)):
            for j in range(2 * (new_zoom - zoom)):
                window = Window(i * 256, j * 256, 256, 256)
                w_lst.append(window)

        # request children
        with rasterio.open(tile_img,
                           'w',
                           driver='jpeg',
                           height=new_dim,
                           width=new_dim,
                           count=3,
                           dtype=rasterio.uint8) as w:
            for num, t in enumerate(child_tiles):
                t = [str(t[0]), str(t[1]), str(t[2])]
                r = requests.get(url(t, imagery['url']))
                img = np.array(Image.open(io.BytesIO(r.content)),
                               dtype=np.uint8)
                try:
                    img = img.reshape(
                        (256, 256, 3)
                    )  # 4 channels returned from some endpoints, but not all
                except ValueError:
                    img = img.reshape((256, 256, 4))
                img = img[:, :, :3]
                img = np.rollaxis(img, 2, 0)
                w.write(img, window=w_lst[num])
    else:
        r = requests.get(url(tile, imagery['url']))
        with open(tile_img, 'wb') as w:
            w.write(r.content)
    return tile_img
def get_down_tiles(x, y, z, target_zoom):
    assert z <= target_zoom, 'target zoom less than zoom %s <= %s' % (z, target_zoom)
    k = (x, y, z, target_zoom)
    if k not in cache_down:
        if z == target_zoom:
            result = [(x, y, z)]
        else:
            result = []
            for t in mercantile.children(x, y, z):
                result += get_down_tiles(t.x, t.y, t.z, target_zoom)
        cache_down[k] = tuple(result)
        return result
    return cache_down[k]
def get_down_tiles(x, y, z, target_zoom):
    assert z <= target_zoom, 'target zoom less than zoom %s <= %s' % (
        z, target_zoom)
    k = (x, y, z, target_zoom)
    if k not in cache_down:
        if z == target_zoom:
            result = [(x, y, z)]
        else:
            result = []
            for t in mercantile.children(x, y, z):
                result += get_down_tiles(t.x, t.y, t.z, target_zoom)
        cache_down[k] = tuple(result)
        return result
    return cache_down[k]
Пример #20
0
def children(quadkeys, levels=1):
    if not type(quadkeys) is list:
        quadkeys = [
            quadkeys,
        ]
    for level in range(levels):
        childrenKeys = []
        for qk in quadkeys:
            childrenKeys.extend([
                mercantile.quadkey(t) \
                    for t in mercantile.children(mercantile.quadkey_to_tile(qk))
                ])
        quadkeys = childrenKeys
    return quadkeys
Пример #21
0
def generate_tiles(tile, max_zoom, metatile=1, materialize_zooms=None):
    tiles = []
    metatile = min(metatile, 2**tile.z)

    for dx in range(0, metatile):
        for dy in range(0, metatile):
            tiles.append(Tile(tile.x + dx, tile.y + dy, tile.z))

    for z in range(tile.z, max_zoom + 1):
        if materialize_zooms is None or z in materialize_zooms:
            a, tiles = itertools.tee(tiles, 2)

            yield from a

        tiles = itertools.chain.from_iterable(
            mercantile.children(t) for t in tiles)
Пример #22
0
def get_assets(url: str, x: int, y: int, z: int) -> Tuple[str]:
    """Get assets."""
    mosaic_def = fetch_mosaic_definition(url)
    min_zoom = mosaic_def["minzoom"]
    max_zoom = mosaic_def["maxzoom"]
    if z > max_zoom or z < min_zoom:
        return []  # return empty asset

    mercator_tile = mercantile.Tile(x=x, y=y, z=z)
    quadkey_zoom = mosaic_def.get("quadkey_zoom", min_zoom)  # 0.0.2

    # get parent
    if mercator_tile.z > quadkey_zoom:
        depth = mercator_tile.z - quadkey_zoom
        for i in range(depth):
            mercator_tile = mercantile.parent(mercator_tile)
        quadkey = [mercantile.quadkey(*mercator_tile)]

    # get child
    elif mercator_tile.z < quadkey_zoom:
        depth = quadkey_zoom - mercator_tile.z
        mercator_tiles = [mercator_tile]
        for i in range(depth):
            mercator_tiles = sum([mercantile.children(t) for t in mercator_tiles], [])

        mercator_tiles = list(filter(lambda t: t.z == quadkey_zoom, mercator_tiles))
        quadkey = [mercantile.quadkey(*tile) for tile in mercator_tiles]
    else:
        quadkey = [mercantile.quadkey(*mercator_tile)]

    assets = list(
        itertools.chain.from_iterable(
            [mosaic_def["tiles"].get(qk, []) for qk in quadkey]
        )
    )

    # check if we have a mosaic in the url (.json/.gz)
    return list(
        itertools.chain.from_iterable(
            [
                get_assets(asset, x, y, z)
                if os.path.splitext(asset)[1] in [".json", ".gz"]
                else [asset]
                for asset in assets
            ]
        )
    )
Пример #23
0
def test_simplify():
    children = mercantile.children(243, 166, 9, zoom=12)
    assert len(children) == 64
    children = children[:-3]
    children.append(children[0])
    simplified = mercantile.simplify(children)
    targets = [
        (487, 332, 10),
        (486, 332, 10),
        (487, 333, 10),
        (973, 667, 11),
        (973, 666, 11),
        (972, 666, 11),
        (1944, 1334, 12),
    ]
    for target in targets:
        assert target in simplified
Пример #24
0
def test_burn_tile_center_lines_roundtrip():
    tiles = list(mercantile.children([0, 0, 0]))
    bounds = (mercantile.bounds(*t) for t in tiles)
    coords = (((e - w) / 2 + w, (n - s) / 2 + s) for w, s, e, n in bounds)

    features = {
        "type": "Feature",
        "properties": {},
        "geometry": {"type": "LineString", "coordinates": list(coords)}
    }


    runner = CliRunner()
    result = runner.invoke(cli, ["burn", "1"], input=json.dumps(features))

    output_tiles = [json.loads(t) for t in result.output.split("\n") if t]
    assert sorted(output_tiles) == sorted([list(t) for t in tiles])
Пример #25
0
    def get_images(self, tiles: List[Tile]) -> Iterator[Tuple[Tile, bytes]]:
        """return bounds of original tile filled with the 4 child tiles 1 zoom level up in bytes"""
        for tile in tiles:
            print('in SuperTileDownloader get images function')
            print(tile)
            w_lst = []
            for i in range(2):
                for j in range(2):
                    window = Window(i * 256, j * 256, 256, 256)
                    w_lst.append(window)
            z = 1 + tile.z
            child_tiles = children(tile,
                                   zoom=z)  #get this from database (tile_zoom)
            child_tiles.sort()
            print('in supertile get_images')
            print(child_tiles)

            with MemoryFile() as memfile:
                with memfile.open(driver='jpeg',
                                  height=512,
                                  width=512,
                                  count=3,
                                  dtype=rasterio.uint8) as dataset:
                    for num, t in enumerate(child_tiles):
                        print(num)
                        print(t)
                        url = self.imagery.format(x=t.x, y=t.y, z=t.z)
                        print(url)
                        r = requests.get(url)
                        img = np.array(Image.open(io.BytesIO(r.content)),
                                       dtype=np.uint8)
                        try:
                            img = img.reshape(
                                (256, 256, 3)
                            )  # 4 channels returned from some endpoints, but not all
                        except ValueError:
                            img = img.reshape((256, 256, 4))
                        img = img[:, :, :3]
                        img = np.rollaxis(img, 2, 0)
                        print(w_lst[num])
                        print()
                        dataset.write(img, window=w_lst[num])
                dataset_b = memfile.read()  #but this fails
                yield (tile, dataset_b)
Пример #26
0
def children(ctx, input, depth):
    """Takes a [x, y, z] tile as input and writes its children to stdout
    in the same form.

    $ echo "[486, 332, 10]" | mercantile parent

    Output:

    [243, 166, 9]
    """
    src = normalize_input(input)
    for line in iter_lines(src):
        line = line.strip()
        tiles = [json.loads(line)[:3]]
        for i in range(depth):
            tiles = sum([mercantile.children(t) for t in tiles], [])
        for t in tiles:
            output = json.dumps(t)
            click.echo(output)
Пример #27
0
def children(ctx, input, depth):
    """Takes a [x, y, z] tile as input and writes its children to stdout
    in the same form.

    $ echo "[486, 332, 10]" | mercantile parent

    Output:

    [243, 166, 9]
    """
    src = normalize_input(input)
    for line in iter_lines(src):
        line = line.strip()
        tiles = [json.loads(line)[:3]]
        for i in range(depth):
            tiles = sum([mercantile.children(t) for t in tiles], [])
        for t in tiles:
            output = json.dumps(t)
            click.echo(output)
Пример #28
0
def process_tile(tile_list, aoi):
    # main function to compare a list of tiles to an input geometry
    # will eventually return a list of tiles completely within the aoi (all zoom levels possible)
    # and tiles that intersect the aoi (must be of max_z because that's as low as we go)

    within_list = []
    intersect_list = []

    max_z = 12

    for t in tile_list:

        # a tile either is completely within, completely outside, or intersects
        tile_geom = shape(mercantile.feature(t)['geometry'])

        # if it's within, great- our work is done
        if aoi.contains(tile_geom):
            within_list.append(t)

        elif tile_geom.intersects(aoi):

            # if it intersects and is < max_z, subdivide it and start the process again
            if t.z < max_z:

                # find the four children of this tile and check them for within/intersect/outside-ness
                tile_children = mercantile.children(t)
                new_within_list, new_intersect_list = process_tile(
                    tile_children, aoi)

                # add the results to our initial lists
                within_list.extend(new_within_list)
                intersect_list.extend(new_intersect_list)

            # if it intersects and is at max_z, add it to our intersect list
            else:
                intersect_list.append(t)

        # and if it's outside our geometry, drop it
        else:
            pass

    return within_list, intersect_list
    def get_images(self, tiles: List[Tile]) -> Iterator[Tuple[Tile, bytes]]:
        """return images cropped to a given model_image_size from an imagery endpoint"""
        for tile in tiles:
            url = self.imagery.format(x=tile.x, y=tile.y, z=tile.z)
            r = requests.get(url)
            with MemoryFile(BytesIO(r.content)) as memfile:
                with memfile.open() as dataset:
                    # because of the tile indexing, we assume all tiles are square
                    sz = dataset.width
                    zoom_offset = sz // self.model_image_size - 1

                    tile_indices = children(tile, zoom=zoom_offset + tile.z)
                    tile_indices.sort()

                    for i in range (2 ** zoom_offset):
                        for j in range(2 ** zoom_offset):
                            window = Window(i * sz, j * sz, (i + 1) * sz, (j + 1) * sz)
                            yield (
                              tile_indices[i + j],
                              dataset.read(window=window)
                             )
def create_super_tile_list(tile_object, zoom_level=2):

    """Generate the Tile List for The Tasking List

    Parameters
    ----------
    tile_object: mercantile tile object


    zoom_level : int Zoom Level for Tiles
        One or more zoom levels to superres.

    Returns
    ------
    tile_object_list: list of tile objects to gather
    tile_position_list: list of relative tile position
    """

    rel_tile_pos = np.asarray([(0, 0), (0, 1), (1, 1), (1, 0)])
    
    tile_object_list = [tile_object]
    tile_position_list = [(0, 0)]
    for zoom in range(zoom_level):
        child_tile_list = []
        child_tile_position = []
        for tile, tile_pos in zip(tile_object_list, tile_position_list):
            
            tile_pos_np = np.asarray(tile_pos)
            print(tile_pos_np)
            print(tile_pos_np*2+rel_tile_pos)
            
            child_tile_list.extend(mercantile.children(tile))
            child_tile_position.extend(tile_pos_np*2+rel_tile_pos)

        tile_object_list = child_tile_list
        tile_position_list = child_tile_position
        print(child_tile_position)

    return tile_object_list, tile_position_list
Пример #31
0
def find_quadkeys(mercator_tile: mercantile.Tile,
                  quadkey_zoom: int) -> List[str]:
    """
    Find quadkeys at desired zoom for tile

    Attributes
    ----------
    mercator_tile: mercantile.Tile
        Input tile to use when searching for quadkeys
    quadkey_zoom: int
        Zoom level

    Returns
    -------
    list
        List[str] of quadkeys

    """
    # get parent
    if mercator_tile.z > quadkey_zoom:
        depth = mercator_tile.z - quadkey_zoom
        for i in range(depth):
            mercator_tile = mercantile.parent(mercator_tile)
        return [mercantile.quadkey(*mercator_tile)]

    # get child
    elif mercator_tile.z < quadkey_zoom:
        depth = quadkey_zoom - mercator_tile.z
        mercator_tiles = [mercator_tile]
        for i in range(depth):
            mercator_tiles = sum(
                [mercantile.children(t) for t in mercator_tiles], [])

        mercator_tiles = list(
            filter(lambda t: t.z == quadkey_zoom, mercator_tiles))
        return [mercantile.quadkey(*tile) for tile in mercator_tiles]
    else:
        return [mercantile.quadkey(*mercator_tile)]
Пример #32
0
def test_children_multi():
    children = mercantile.children(243, 166, 9, zoom=11)
    assert len(children) == 16
    targets = [
        (972, 664, 11),
        (973, 664, 11),
        (973, 665, 11),
        (972, 665, 11),
        (974, 664, 11),
        (975, 664, 11),
        (975, 665, 11),
        (974, 665, 11),
        (974, 666, 11),
        (975, 666, 11),
        (975, 667, 11),
        (974, 667, 11),
        (972, 666, 11),
        (973, 666, 11),
        (973, 667, 11),
        (972, 667, 11),
    ]
    for target in targets:
        assert target in children
def generate_overview_for_zoom(existing_zoom, tile_dir, max_coords):
    """Generate overview tiles for a given zoom level

    Args:
        - existing_zoom: the zoom level for which tiles already exist
        - tile_dir: the root of directory with tiles
    """
    zoom_dir = tile_dir / str(existing_zoom)
    tile_coords = []
    for path in zoom_dir.glob('*/*.geojson'):
        y = int(path.stem)
        x = int(path.parents[0].name)
        tile_coords.append((x, y))

    tiles = [mercantile.Tile(x, y, existing_zoom) for x, y in tile_coords]
    parents = {mercantile.parent(t) for t in tiles}
    for parent in parents:
        # Which of its children exist?
        children = [c for c in mercantile.children(parent) if c in tiles]

        # If the parent only has one child, then you can assume the child was
        # already small enough, and just write
        if len(children) == 1:
            # Load the child's features
            features = load_features(tile=children[0], tile_dir=tile_dir)
            # And write to the parent's tile
            write_geojson(features=features, tile=parent, tile_dir=tile_dir)
            continue

        # Otherwise, we have more than one child.
        # Load all the features, then determine if there are too many
        features = []
        for child in children:
            features.extend(load_features(tile=child, tile_dir=tile_dir))

        features = simplify_features(features, max_coords)
        write_geojson(features=features, tile=parent, tile_dir=tile_dir)
Пример #34
0
def children(ctx, input, depth):
    """Takes [x, y, z] tiles as input and writes children to stdout
    in the same form.

    Input may be a compact newline-delimited sequences of JSON or
    a pretty-printed ASCII RS-delimited sequence of JSON (like
    https://tools.ietf.org/html/rfc8142 and
    https://tools.ietf.org/html/rfc7159).

    $ echo "[486, 332, 10]" | mercantile parent

    Output:

    [243, 166, 9]
    """
    src = normalize_input(input)
    for line in iter_lines(src):
        line = line.strip()
        tiles = [json.loads(line)[:3]]
        for i in range(depth):
            tiles = sum([mercantile.children(t) for t in tiles], [])
        for t in tiles:
            output = json.dumps(t)
            click.echo(output)
Пример #35
0
def test_children():
    children = mercantile.children(243, 166, 9)
    assert len(children) == 4
    assert (486, 332, 10) in children
Пример #36
0
def pyramid(sc, zoom, dtype, nodata, tiles, prefix, resampling="average"):
    meta = dict(
        driver="GTiff",
        crs="EPSG:3857",
        tiled=True,
        compress="deflate",
        predictor=2,
        sparse_ok=True,
        nodata=nodata,
        dtype=dtype,
        blockxsize=512,
        blockysize=512,
    )

    if np.dtype(dtype).kind == "f":
        meta["predictor"] = 3

    empty = ma.masked_array(np.full((CHUNK_SIZE, CHUNK_SIZE), nodata, dtype), fill_value=nodata)

    tile_count = tiles.count()

    print("%d tiles to process" % (tile_count))

    # TODO deal with multiple bands (probably with flatMapValues)
    min_zoom = 0
    for z in range(zoom - 1, min_zoom - 1, -1):
        print("Processing zoom %d" % (z))
        tile_count = max(1, tile_count / 4)

        print("Tile count: %d" % (tile_count))

        # generate a list of tiles at the current zoom (from available children)
        tiles = tiles.map(
            lambda child: mercantile.parent(child)
        ).distinct()

        tiles.keyBy(z_key).partitionBy(tiles.count()).values().mapPartitions(
            lambda partition: map(
                # for each parent tile:
                # 5. write it to disk
                lambda parent: write(meta, prefix)(
                    reduce(
                        # 3. merge
                        merge,
                        # 2. downsample them
                        itertools.ifilter(
                            None,
                            itertools.imap(
                                downsample,
                                # 1. fetch children
                                itertools.ifilter(
                                    None,
                                    itertools.imap(
                                        lambda tile: read_chunk(tile, prefix),
                                        mercantile.children(parent)
                                    )
                                )
                            )
                        ),
                        (None, empty.copy())
                    )
                ),
                partition
            )
        ).collect()
Пример #37
0
 def children(self) -> List["TileID"]:
     """
     Returns a list of this tile's 4 child tile ids.
     """
     return [TileID(mt) for mt in mercantile.children(self.asmrcantile)]
def queue_tile(tile, max_zoom, sources, output):
    queue_render(tile, sources, output)

    if tile.z < max_zoom:
        for child in mercantile.children(tile):
            queue_tile(child, max_zoom, sources, output)
Пример #39
0
def queue_tile(tile, max_zoom, remove_hash, from_s3, to_s3):
    queue_render(tile, remove_hash, from_s3, to_s3)

    if tile.z < max_zoom:
        for child in mercantile.children(tile):
            queue_tile(child, max_zoom, remove_hash, from_s3, to_s3)
Пример #40
0

if __name__ == '__main__':
    reader = csv.reader(sys.stdin, delimiter=DELIMITER)
    writer = csv.writer(sys.stdout, delimiter=DELIMITER)

    last_zoom = 0
    tiles = {}

    for row in reader:
        z = int(row[0])
        x = int(row[1])
        y = int(row[2])
        requests = int(row[3])

        children = mercantile.children(x, y, z)
        for child in children:
            fill_dict(tiles, child.z, child.y, child.x)
            tiles[child.z][child.y][child.x] = requests

        if z > 0:
            fill_dict(tiles, z, y, x)
            tiles[z][y][x] += requests

    for z in tiles:
        for y in tiles[z]:
            for x in tiles[z][y]:
                requests = tiles[z][y][x]
                center = calculate_center(x, y, z)
                writer.writerow([z, x, y, requests] + list(center))