How to use the mercantile.ul function in mercantile

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 / tests / test_funcs.py View on Github external
def test_xy_tile():
    """x, y for the 486-332-10 tile is correctly calculated"""
    ul = mercantile.ul(486, 332, 10)
    xy = mercantile.xy(*ul)
    expected = (-1017529.7205322663, 7044436.526761846)
    for a, b in zip(expected, xy):
        assert round(a - b, 7) == 0
github mapbox / supermercado / supermercado / uniontiles.py View on Github external
def union(inputtiles, parsenames):

    tiles = sutils.tile_parser(inputtiles, parsenames)

    xmin, xmax, ymin, ymax = sutils.get_range(tiles)

    zoom = sutils.get_zoom(tiles)

    # make an array of shape (xrange + 3, yrange + 3)
    burn = sutils.burnXYZs(tiles, xmin, xmax, ymin, ymax, 0)

    nw = mercantile.xy(*mercantile.ul(xmin, ymin, zoom))

    se = mercantile.xy(*mercantile.ul(xmax + 1, ymax + 1, zoom))

    aff = Affine(
        ((se[0] - nw[0]) / float(xmax - xmin + 1)),
        0.0,
        nw[0],
        0.0,
        -((nw[1] - se[1]) / float(ymax - ymin + 1)),
        nw[1],
    )

    unprojecter = sutils.Unprojecter()

    unionedTiles = [
        {
            "geometry": unprojecter.unproject(feature),
            "properties": {},
github mapbox / rio-rgbify / rio_rgbify / mbtiler.py View on Github external
tile: list
        [x, y, z] indices of tile

    Returns
    --------
    tile, buffer
        tuple with the input tile, and a bytearray with the data encoded into
        the format created in the `writer_func`

    """
    x, y, z = tile

    bounds = [
        c
        for i in (
            mercantile.xy(*mercantile.ul(x, y + 1, z)),
            mercantile.xy(*mercantile.ul(x + 1, y, z)),
        )
        for c in i
    ]

    toaffine = transform.from_bounds(*bounds + [512, 512])

    out = np.empty((512, 512), dtype=src.meta["dtype"])

    reproject(
        rasterio.band(src, 1),
        out,
        dst_transform=toaffine,
        dst_crs="epsg:3857",
        resampling=Resampling.bilinear,
    )
github mapbox / supermercado / supermercado / burntiles.py View on Github external
def make_transform(tilerange, zoom):
    ulx, uly = mercantile.xy(
        *mercantile.ul(tilerange["x"]["min"], tilerange["y"]["min"], zoom)
    )
    lrx, lry = mercantile.xy(
        *mercantile.ul(tilerange["x"]["max"], tilerange["y"]["max"], zoom)
    )
    xcell = (lrx - ulx) / float(tilerange["x"]["max"] - tilerange["x"]["min"])
    ycell = (uly - lry) / float(tilerange["y"]["max"] - tilerange["y"]["min"])
    return Affine(xcell, 0, ulx, 0, -ycell, uly)
github GeoNode / geonode / geonode / geoserver / helpers.py View on Github external
bounds_ll = mercantile.bounds(t_ll)
        bounds_ur = mercantile.bounds(t_ur)

        lat_res = abs(256 / (bounds_ur.north - bounds_ur.south))
        lng_res = abs(256 / (bounds_ll.east - bounds_ll.west))
        top = round(abs(bounds_ur.north - bounds[3]) * -lat_res)
        left = round(abs(bounds_ll.west - bounds[0]) * -lng_res)

        tmp_tile = mercantile.tile(bounds[0], bounds[3], zoom)
        width_acc = 256 + int(left)
        first_row = [tmp_tile]
        # Add tiles to fill image width
        _n_step = 0
        while int(width) > int(width_acc):
            c = mercantile.ul(tmp_tile.x + 1, tmp_tile.y, zoom)
            lng = _v(c.lng, x=True, target_srid=4326)
            if lng == 180.0:
                lng = -180.0
            tmp_tile = mercantile.tile(lng, bounds[3], zoom)
            first_row.append(tmp_tile)
            width_acc += 256
            _n_step = _n_step + 1
        # Build Image Request Template
        _img_request_template = "<div style="height:{height}px; width:{width}px;">\
            <div style="position: absolute; top:{top}px; left:{left}px; z-index: 749; \
            transform: translate3d(0px, 0px, 0px) scale3d(1, 1, 1);"> \
            \n".format(height=height, width=width, top=top, left=left)

        for row in range(0, numberOfRows):
            for col in range(0, len(first_row)):
                box = [col * 256, row * 256]</div></div>
github mapbox / rio-mbtiles / mbtiles / __init__.py View on Github external
Returns
    -------

    tile : mercantile.Tile
        The input tile.
    bytes : bytearray
        Image bytes corresponding to the tile.

    """
    global base_kwds, resampling, src

    # Get the bounds of the tile.
    ulx, uly = mercantile.xy(
        *mercantile.ul(tile.x, tile.y, tile.z))
    lrx, lry = mercantile.xy(
        *mercantile.ul(tile.x + 1, tile.y + 1, tile.z))

    kwds = base_kwds.copy()
    kwds['transform'] = transform_from_bounds(ulx, lry, lrx, uly,
                                              kwds['width'], kwds['height'])
    src_nodata = kwds.pop('src_nodata', None)
    dst_nodata = kwds.pop('dst_nodata', None)

    warnings.simplefilter('ignore')

    with MemoryFile() as memfile:

        with memfile.open(**kwds) as tmp:

            # determine window of source raster corresponding to the tile
            # image, with small buffer at edges
            try:
github mapbox / rio-mbtiles / mbtiles / __init__.py View on Github external
tile : mercantile.Tile

    Returns
    -------

    tile : mercantile.Tile
        The input tile.
    bytes : bytearray
        Image bytes corresponding to the tile.

    """
    global base_kwds, resampling, src

    # Get the bounds of the tile.
    ulx, uly = mercantile.xy(
        *mercantile.ul(tile.x, tile.y, tile.z))
    lrx, lry = mercantile.xy(
        *mercantile.ul(tile.x + 1, tile.y + 1, tile.z))

    kwds = base_kwds.copy()
    kwds['transform'] = transform_from_bounds(ulx, lry, lrx, uly,
                                              kwds['width'], kwds['height'])
    src_nodata = kwds.pop('src_nodata', None)
    dst_nodata = kwds.pop('dst_nodata', None)

    warnings.simplefilter('ignore')

    with MemoryFile() as memfile:

        with memfile.open(**kwds) as tmp:

            # determine window of source raster corresponding to the tile