kylebarron / viewshed.js

Implementation of viewshed algorithm in JS
MIT License
1 stars 1 forks source link

How to load cached terrain data #9

Open kylebarron opened 4 years ago

kylebarron commented 4 years ago

For the record, you can get the existing already-loaded tiles from a map with:

map.style.sourceCaches["terrain-rgb"]._tiles[205673580].dem.data

An example value of the data array is 4279282433. That in hex is FF10AB01. I'm not sure why but this is backwards: ABGR instead of RGBA?? so A=FF, B=10, G=AB, R=01, so in decimal, R=1, G=171, B=16, so:

height = -10000 + ((R * 256 * 256 + G * 256 + B) * 0.1)
height = 932.8 meters or 3060 feet

That matches up with the contour lines in the area.

So a general process for getting the DEM of the entire current viewport is:

  1. Determine which xyz tiles intersect the current viewport at the desired zoom level. I.e. even when zoomed out, you could theoretically request the terrain tiles at zoom 12, the max zoom, but that would probably take a lot of requests. If you request the tiles for the existing zoom level, it's more likely that they'll be in the existing cache.
  2. Find the tileID for the desired xyz tiles to check if they're in the cache.
  3. If not in cache, request those xyz tiles specifically.
  4. Use loaded DEM