Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added tms support #19

Merged
merged 10 commits into from
Mar 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 25 additions & 21 deletions src/tile/base.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import axios from 'axios';
import { WebpMachine, loadBinaryData } from 'webp-hero';
import { lngLatToGoogle } from 'global-mercator';
import { lngLatToGoogle,lngLatToTile } from 'global-mercator';

Check failure on line 3 in src/tile/base.ts

View workflow job for this annotation

GitHub Actions / build

A space is required after ','
import PNG from '../png';

/**
Expand All @@ -11,6 +11,8 @@

protected tileSize: number;

protected tms: boolean;

protected minzoom: number;

protected maxzoom: number;
Expand All @@ -19,12 +21,14 @@
* 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
*/
constructor(url: string, tileSize: number, minzoom: number, maxzoom: number) {
constructor(url: string, tileSize: number, tms: boolean, minzoom: number, maxzoom: number) {
JinIgarashi marked this conversation as resolved.
Show resolved Hide resolved
this.url = url;
this.tileSize = tileSize;
this.tms = tms;
this.minzoom = minzoom;
this.maxzoom = maxzoom;
}
Expand All @@ -46,11 +50,11 @@
} 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);

Check failure on line 53 in src/tile/base.ts

View workflow job for this annotation

GitHub Actions / build

Operator '?' must be spaced

Check failure on line 53 in src/tile/base.ts

View workflow job for this annotation

GitHub Actions / build

Operator ':' must be spaced
const url: string = this.url
.replace(/{x}/g, tile[0].toString())
.replace(/{y}/g, tile[1].toString())
.replace(/{z}/g, tile[2].toString());
.replace(/{x}/g, tile[0].toString())

Check failure on line 55 in src/tile/base.ts

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 8 spaces but found 10
.replace(/{y}/g, tile[1].toString())

Check failure on line 56 in src/tile/base.ts

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 8 spaces but found 10
.replace(/{z}/g, tile[2].toString());

Check failure on line 57 in src/tile/base.ts

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 8 spaces but found 10
let ext = this.getUrlExtension(url);
// console.log(ext)
if (!ext) {
Expand All @@ -61,20 +65,20 @@
axios.get(url, {
responseType: 'arraybuffer',
})
.then((res) => {
const binary = Buffer.from(res.data, 'binary');
const value = this.getValueFromPNG(binary, tile, lng, lat);
resolve(value);
})
.catch((err) => reject(err));
.then((res) => {

Check failure on line 68 in src/tile/base.ts

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 12 spaces but found 14
const binary = Buffer.from(res.data, 'binary');

Check failure on line 69 in src/tile/base.ts

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 14 spaces but found 16
const value = this.getValueFromPNG(binary, tile, lng, lat);

Check failure on line 70 in src/tile/base.ts

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 14 spaces but found 16
resolve(value);

Check failure on line 71 in src/tile/base.ts

View workflow job for this annotation

GitHub Actions / build

Expected indentation of 14 spaces but found 16
})
.catch((err) => reject(err));
break;
case 'webp':
loadBinaryData(url)
.then((binary) => {
this.getValueFromWEBP(binary, tile, lng, lat).then((value: number) => {
resolve(value);
.then((binary) => {
this.getValueFromWEBP(binary, tile, lng, lat).then((value: number) => {
resolve(value);
}).catch((err) => reject(err));
}).catch((err) => reject(err));
}).catch((err) => reject(err));
break;
default:
reject(new Error(`Invalid file extension: ${ext}`));
Expand Down Expand Up @@ -109,10 +113,10 @@
* @returns the value calculated from coordinates
*/
private getValueFromWEBP(
binary: Uint8Array,
tile: number[],
lng: number,
lat: number,
binary: Uint8Array,
tile: number[],
lng: number,
lat: number,
): Promise<number> {
// eslint-disable-next-line no-unused-vars
return new Promise((resolve: (value:number)=>void, reject: (reason?: any) => void) => {
Expand All @@ -122,7 +126,7 @@
const height = this.getValueFromPNG(buffer, tile, lng, lat);
resolve(height);
})
.catch((err) => reject(err));
.catch((err) => reject(err));
});
}

Expand All @@ -134,7 +138,7 @@
* @param b blue
* @param a alfa
*/
protected abstract calc(r: number, g: number, b: number, a: number): number;

Check warning on line 141 in src/tile/base.ts

View workflow job for this annotation

GitHub Actions / build

'r' is defined but never used

Check warning on line 141 in src/tile/base.ts

View workflow job for this annotation

GitHub Actions / build

'g' is defined but never used

Check warning on line 141 in src/tile/base.ts

View workflow job for this annotation

GitHub Actions / build

'b' is defined but never used

Check warning on line 141 in src/tile/base.ts

View workflow job for this annotation

GitHub Actions / build

'a' is defined but never used

/**
* Get RGBA values from coordinates information
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, tms:boolean = false, minzoom = 5, maxzoom = 15) {
super(url, tileSize, tms, minzoom, maxzoom);
}

/**
Expand Down
Loading