Skip to content

Commit

Permalink
added tms support (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
Modif93 authored Mar 25, 2024
1 parent 13ed25d commit 41574ff
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,19 @@ import {TerrainRGB} from '@watergis/terrain-rgb';

const url = 'https://wasac.github.io/rw-terrain/tiles/{z}/{x}/{y}.png';
const trgb = new TerrainRGB(url, 512);

const elevation = await trgb.getElevation([30.0529622, -1.9575129], 15);
console.log(elevation);
```

TMS(Tile Map Service) tiles are also supported with

```ts
const trgb = new TerrainRGB(url, 512, 5, 15, true);
```


If it can't find tile, it will return 404 error.

If its terrain RGB tilesets was resampled by gdal2tiles, the result of elevation might not be the same with original DEM image.

13 changes: 10 additions & 3 deletions src/tile/base.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios from 'axios';
import { WebpMachine, loadBinaryData } from 'webp-hero';
import { lngLatToGoogle } from 'global-mercator';
import { lngLatToGoogle, lngLatToTile } from 'global-mercator';

import PNG from '../png';

/**
Expand All @@ -11,6 +12,8 @@ abstract class BaseTile {

protected tileSize: number;

protected tms: boolean;

protected minzoom: number;

protected maxzoom: number;
Expand All @@ -19,14 +22,18 @@ abstract class BaseTile {
* Constructor
* @param url URL for terrain RGB raster tilesets
* @param tileSize size of tile. 256 or 512
* @param tms whether it is Tile Map Service
* @param minzoom minzoom for terrain RGB raster tilesets
* @param maxzoom maxzoom for terrain RGB raster tilesets
* @param tms whether it is Tile Map Service
*/
constructor(url: string, tileSize: number, minzoom: number, maxzoom: number) {
constructor(url: string, tileSize: number, minzoom: number, maxzoom: number, tms: boolean) {
this.url = url;
this.tileSize = tileSize;
this.tms = tms;
this.minzoom = minzoom;
this.maxzoom = maxzoom;
this.tms = tms;
}

/**
Expand All @@ -46,7 +53,7 @@ abstract class BaseTile {
} else if (z < this.minzoom) {
zoom = this.minzoom;
}
const tile = lngLatToGoogle([lng, lat], zoom);
const tile = this.tms ? lngLatToTile([lng, lat], zoom) : lngLatToGoogle([lng, lat], zoom);
const url: string = this.url
.replace(/{x}/g, tile[0].toString())
.replace(/{y}/g, tile[1].toString())
Expand Down
5 changes: 3 additions & 2 deletions src/tile/terrainrgb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ class TerrainRGB extends BaseTile {
* Constructor
* @param url URL for terrain RGB raster tilesets
* @param tileSize size of tile. 256 or 512
* @param tms whether it is Tile Map Service
* @param minzoom minzoom for terrain RGB raster tilesets. default is 5
* @param maxzoom maxzoom for terrain RGB raster tilesets. default is 15
*/
constructor(url: string, tileSize: number, minzoom = 5, maxzoom = 15) {
super(url, tileSize, minzoom, maxzoom);
constructor(url: string, tileSize: number, minzoom = 5, maxzoom = 15, tms = false) {
super(url, tileSize, minzoom, maxzoom, tms);
}

/**
Expand Down

0 comments on commit 41574ff

Please sign in to comment.