davidj411
davidj411

Reputation: 1015

how to compute tile id to be used with HERE maps api

tile id needs to be computed from coordinates, but i am not sure how that is done.

this HERE api uses it https://smap.hereapi.com/v8/maps/attributes

in https://developer.here.com/documentation/content-map-attributes/dev_guide/topics/here-map-content.html
their docs mention

      tile size = 180° / 2^level [degree]
      tileY = trunc((latitude  +  90°) / tile size)
      tileX = trunc((longitude + 180°) / tile size)
      tileID = tileY * 2 * (2^level) + tileX

but i'd like to get a python working example:

import math
def get_tile_id(lat, long, level=8):
    latitude = math.degrees(lat)
    longitude = math.degrees(long)
    denom = math.degrees(math.pow(2, level))
    tile_size = math.degrees(180) / denom
    tileY = math.trunc(
        (latitude  +  math.degrees(90)) / tile_size
    )
    tileX = math.trunc(
        (longitude + math.degrees(180)) / tile_size
    )
    tile_id = tileY * 2 * math.pow(2, level) + tileX
    return tile_id

Upvotes: 0

Views: 902

Answers (1)

user3505695
user3505695

Reputation:

In this formula

tile size = 180° / 2^level [degree]
tileY = trunc((latitude  +  90°) / tile size)
tileX = trunc((longitude + 180°) / tile size)
tileID = tileY * 2 * (2^level) + tileX

the latitude, longitude, 90, 180 are in normal float type. They are not in radians.

You don't need to convert them to degrees.

See please below JavaScript example:

function getTileId(point, level){
    var
        degSize = 180 / Math.pow(2, level),
        tileY = Math.floor((point.lat + 90) / degSize),
        tileX = Math.floor((point.lng + 180) / degSize);
    return (tileY * 2 * Math.pow(2, level) + tileX);
}

is using in this example where main code of it there

Upvotes: 1

Related Questions