Tilebelt

repository·main·Indexed 43 days ago

https://github.com/mapbox/tilebelt

A collection of simple utilities for working with slippy map tiles, providing conversions between tiles, GeoJSON, bounding boxes, quadkeys, and geographic coordinates. Version 2.0.3 includes functions for tile hierarchy navigation (getParent, getChildren, getSiblings), point-to-tile conversion, and quadkey transformations.

Tokens
1.4K
Snippets
10
Records
11
Agent score
5%

What's inside Tilebelt

  1. Basic usage of tilebelt

    main

    Import the library to perform tile-to-coordinate conversions. A tile is represented as an array of three integers: [x, y, z].

    import * as tilebelt from '@mapbox/tilebelt';
    
    const tile = [10, 15, 8]; // x, y, z
    
    // Get GeoJSON representation
    tilebelt.tileToGeoJSON(tile);
    
    // Get the parent tile (one zoom level lower)
    tilebelt.getParent(tile);
  2. Reference of tilebelt utility functions

    main

    The following functions are available in the @mapbox/tilebelt API:

    FunctionDescription
    tileToGeoJSON(tile)Get a GeoJSON representation of a tile
    tileToBBOX(tile)Get the bounding box (bbox) of a tile
    bboxToTile(bbox)Get the smallest tile to cover a bbox
    getChildren(tile)Get the 4 tiles one zoom level higher
    getParent(tile)Get the tile one zoom level lower
    getSiblings(tile)Get the 4 sibling tiles for a tile
    hasSiblings(tiles, tile)Check to see if an array of tiles contains a tile's siblings
    hasTile(tiles, tile)Check to see if an array of tiles contains a particular tile
    tilesEqual(tile1, tile2)Check to see if two tiles are the same
    tileToQuadkey(tile)Get the quadkey for a tile
    quadkeyToTile(quadkey)Get the tile for a quadkey
    pointToTile(lon, lat, zoom)Get the tile for a point at a specified zoom level
    pointToTileFraction(lon, lat, zoom)Get the precise fractional tile location for a point at a zoom level
  3. Convert a Point to a Tile

    main

    Convert geographic coordinates (longitude and latitude) to a tile coordinate at a specific zoom level.

    • pointToTile(lon, lat, z): Returns the integer tile coordinates [x, y, z] that contain the point.
    • pointToTileFraction(lon, lat, z): Returns the precise fractional tile location [x, y, z]. This is useful for determining exactly where within a tile a point lies.
    // Get integer tile
    const tile = pointToTile(1, 1, 20);
    
    // Get fractional tile
    const tileFraction = pointToTileFraction(30.5, 50.5, 15);
  4. Convert a Tile to GeoJSON Geometry

    main

    Use tileToGeoJSON to get a GeoJSON Polygon representation of a tile's boundaries. This is useful for visualizing tile boundaries on a map.

    const poly = tileToGeoJSON([5, 10, 10]);
    // returns a GeoJSON Polygon object
  5. Convert a Tile to a Bounding Box (BBox)

    main

    Use tileToBBOX to get the geographic bounding box (longitude/latitude) of a specific tile. The returned BBox follows the [minX, minY, maxX, maxY] format (West, South, East, North).

    const bbox = tileToBBOX([5, 10, 10]);
    // returns [west, south, east, north]
  6. Find the Smallest Tile to Cover a Bounding Box

    main

    Use bboxToTile(bboxCoords) to find the single largest tile that completely encompasses the provided bounding box. This is useful for determining the appropriate zoom level and tile for a specific geographic area.

    const tile = bboxToTile([-178, 84, -177, 85]);
  7. Navigate Tile Hierarchies (Parent, Children, Siblings)

    main

    Traverse the tile tree using these functions:

    • getChildren(tile): Returns an array of the 4 tiles at the next zoom level (z + 1) that reside within the current tile.
    • getParent(tile): Returns the single tile at the previous zoom level (z - 1) that contains the current tile.
    • getSiblings(tile): Returns the 4 tiles (including the input tile) that share the same parent.
    const tile = [5, 10, 10];
    
    const children = getChildren(tile);
    const parent = getParent(tile);
    const siblings = getSiblings(tile);
  8. Convert Tiles to and from Quadkeys

    main

    Quadkeys are string representations of tile hierarchies used by some spatial indexing systems.

    • tileToQuadkey(tile): Converts a Tile [x, y, z] to a quadkey string.
    • quadkeyToTile(quadkey): Converts a quadkey string back into a Tile [x, y, z].
    const quadkey = tileToQuadkey([0, 1, 5]);
    const tile = quadkeyToTile('00001033');
  9. Compare and Search Tiles

    main

    Utility functions for managing collections of tiles:

    • tilesEqual(tile1, tile2): Checks if two Tile objects are identical.
    • hasTile(tilesArray, tile): Checks if a specific tile exists within an array of tiles.
    • hasSiblings(tile, tilesArray): Checks if all sibling tiles of the given tile are present in the provided array.
    const tiles = [[0, 0, 5], [0, 1, 5]];
    const target = [0, 0, 5];
    
    const exists = hasTile(tiles, target);
    const isSame = tilesEqual(tiles[0], target);