openlayersopenlayers-7

Is it possible to generate extent from VectorTiles source?


I'm building an app where users can upload any map in the format of vector tiles. These maps are often small and I'd like to limit the zoom and pan, so users view will be always within the map bounds.

I don't know the extent of the uploaded maps. I tried multiple methods in layer/VectorTile, source/VectorTile and View, but as far as I know I need to first define extent to make it work.

Is there any way to generate extent based on edge coordinates of features in vector source? I'm using OpenLayers v7.2.2


Solution

  • Short answer, no.

    Features in vector tiles use coordinates relative to the origin of the vector tile, usually in the [4096, 4096] range. The vector tile itself does not know its coordinates. If you do not know the extent of your tileset, you cannot decode the information.

    There are three elements that define the extent of a vector tileset: the leftmost longitude, the topmost latitude and the distance between the leftmost longitude and rightmost longitude. As all vector tiles are square and they use power of 2 zoom levels, this should allow you to calculate everything else.

    If you are generating your vector tiles yourself, I suggest that you always use the classical world extent for Web Mercator 3857:

    tile_dimension_zoom_0: 20037508.34 * 2,
    tile_origin_upper_left_x: -20037508.34,
    tile_origin_upper_left_y: 20037508.34
    

    You will then have tiles that go from 500 to 505 instead of 0 to 5.

    And if you are using someone else's tiles and you do not know those three numbers, you cannot georeference these tiles.