How to use mercantile - 10 common examples

To help you get started, we’ve selected a few mercantile examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github consbio / tpkutils / tpkutils / __init__.py View on Github external
for tile in self.read_tiles(zoom, flip_y=True)
                if not (
                    drop_empty and hashlib.sha1(tile.data).hexdigest() in EMPTY_TILES
                )
            )

            mbtiles.write_tiles(tiles)

            if tile_bounds:
                # Calculate bounds based on maximum zoom to be exported
                highest_zoom = zoom[-1]
                min_row, max_row = mbtiles.row_range(highest_zoom)
                min_col, max_col = mbtiles.col_range(highest_zoom)

                # get upper left coordinate
                xmin, ymax = mercantile.ul(min_col, min_row, highest_zoom)

                # get bottom right coordinate
                # since we are using ul(), we need to go 1 tile beyond the range to get the right side of the
                # tiles we have
                xmax, ymin = mercantile.ul(max_col + 1, max_row + 1, highest_zoom)

                bounds = (xmin, ymin, xmax, ymax)

            else:
                bounds = self.bounds

            # Center zoom level is middle zoom level
            center = "{0:4f},{1:4f},{2}".format(
                bounds[0] + (bounds[2] - bounds[0]) / 2.0,
                bounds[1] + (bounds[3] - bounds[1]) / 2.0,
                (zoom[0] + zoom[-1]) // 2,
github mapbox / mercantile / mercantile / __init__.py View on Github external
--------

    >>> parent(Tile(0, 0, 2))
    Tile(x=0, y=0, z=1)

    >>> parent(Tile(0, 0, 2), zoom=0)
    Tile(x=0, y=0, z=0)

    """
    tile = _parse_tile_arg(*tile)

    # zoom is a keyword-only argument.
    zoom = kwargs.get("zoom", None)

    if zoom is not None and (tile[2] < zoom or zoom != int(zoom)):
        raise InvalidZoomError(
            "zoom must be an integer and less than that of the input tile"
        )

    x, y, z = tile
    if x != int(x) or y != int(y) or z != int(z):
        raise ParentTileError("the parent of a non-integer tile is undefined")

    target_zoom = z - 1 if zoom is None else zoom

    # Algorithm heavily inspired by https://github.com/mapbox/tilebelt.
    return_tile = tile
    while return_tile[2] > target_zoom:
        xtile, ytile, ztile = return_tile
        if xtile % 2 == 0 and ytile % 2 == 0:
            return_tile = Tile(xtile // 2, ytile // 2, ztile - 1)
        elif xtile % 2 == 0:
github mapbox / mercantile / mercantile / __init__.py View on Github external
Tile(x=0, y=0, z=0)

    """
    tile = _parse_tile_arg(*tile)

    # zoom is a keyword-only argument.
    zoom = kwargs.get("zoom", None)

    if zoom is not None and (tile[2] < zoom or zoom != int(zoom)):
        raise InvalidZoomError(
            "zoom must be an integer and less than that of the input tile"
        )

    x, y, z = tile
    if x != int(x) or y != int(y) or z != int(z):
        raise ParentTileError("the parent of a non-integer tile is undefined")

    target_zoom = z - 1 if zoom is None else zoom

    # Algorithm heavily inspired by https://github.com/mapbox/tilebelt.
    return_tile = tile
    while return_tile[2] > target_zoom:
        xtile, ytile, ztile = return_tile
        if xtile % 2 == 0 and ytile % 2 == 0:
            return_tile = Tile(xtile // 2, ytile // 2, ztile - 1)
        elif xtile % 2 == 0:
            return_tile = Tile(xtile // 2, (ytile - 1) // 2, ztile - 1)
        elif not xtile % 2 == 0 and ytile % 2 == 0:
            return_tile = Tile((xtile - 1) // 2, ytile // 2, ztile - 1)
        else:
            return_tile = Tile((xtile - 1) // 2, (ytile - 1) // 2, ztile - 1)
    return return_tile
github mapbox / mercantile / tests / test_funcs.py View on Github external
def test__xy_north_of_limit(lat):
    x, y = mercantile._xy(0.0, lat)
    assert x == 0.5
    assert y < 0
github mapbox / mercantile / tests / test_funcs.py View on Github external
def test__xy_south_of_limit(lat):
    x, y = mercantile._xy(0.0, lat)
    assert x == 0.5
    assert y > 1
github mapbox / mercantile / tests / test_funcs.py View on Github external
        mercantile.Tile(10, 10, 10),
    ],
)
def test_tiles_roundtrip(t):
    """tiles(bounds(tile)) gives the tile"""
    res = list(mercantile.tiles(*mercantile.bounds(t), zooms=[t.z]))
    assert len(res) == 1
    val = res.pop()
    assert val.x == t.x
    assert val.y == t.y
    assert val.z == t.z
github mapbox / mercantile / tests / test_funcs.py View on Github external
def test_empty_quadkey_to_tile():
    qk = ""
    expected = mercantile.Tile(0, 0, 0)
    assert mercantile.quadkey_to_tile(qk) == expected
github mapbox / robosat / tests / test_tiles.py View on Github external
def test_slippy_map_directory(self):
        root = "tests/fixtures/images"
        tiles = [tile for tile in tiles_from_slippy_map(root)]
        self.assertEqual(len(tiles), 3)

        tile, path = tiles[0]
        self.assertEqual(type(tile), mercantile.Tile)
        self.assertEqual(path, "tests/fixtures/images/18/69105/105093.jpg")
github cogeotiff / rio-cogeo / tests / test_web.py View on Github external
arr = numpy.transpose(arr, [2, 0, 1])

                    tbounds = mercantile.xy_bounds(leftTile)
                    data, mask = tile_read(
                        "cogeo.tif", tbounds, 256, resampling_method="nearest"
                    )
                    numpy.testing.assert_array_equal(data, arr)

                    # Bottom right tile
                    mime_type, tile = cog.get_tile(4, 3, 0)
                    tile_length = 256 * 256 * 3
                    t = struct.unpack_from("{}b".format(tile_length), tile)
                    arr = numpy.array(t).reshape(256, 256, 3).astype(numpy.uint8)
                    arr = numpy.transpose(arr, [2, 0, 1])

                    tbounds = mercantile.xy_bounds(rightTile)
                    data, mask = tile_read(
                        "cogeo.tif", tbounds, 256, resampling_method="nearest"
                    )
                    numpy.testing.assert_array_equal(data, arr)

                    # Low resolution (overview 1)
                    # Top Left tile
                    # NOTE: overview internal tile size is 128px
                    # We need to stack two internal tiles to compare with
                    # the 256px mercator tile fetched by rio-tiler
                    # ref: https://github.com/cogeotiff/rio-cogeo/issues/60
                    mime_type, tile = cog.get_tile(1, 0, 1)
                    tile_length = 128 * 128 * 3
                    t = struct.unpack_from("{}b".format(tile_length), tile)
                    arr1 = numpy.array(t).reshape(128, 128, 3).astype(numpy.uint8)
                    arr1 = numpy.transpose(arr1, [2, 0, 1])
github mapbox / rio-glui / tests / test_server.py View on Github external
def read_tile(self, z, x, y):
        """Read raster tile data and mask."""
        mercator_tile = mercantile.Tile(x=x, y=y, z=z)
        tile_bounds = mercantile.xy_bounds(mercator_tile)

        data, mask = tile_read(
            self.path,
            tile_bounds,
            self.tiles_size,
            indexes=self.indexes,
            nodata=self.nodata,
        )
        data = (data[0] + data[1]) / 2
        return data.astype(numpy.uint8), mask