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

fix(landing): Support Terrain for NZTM and add default LINZ-Terrain into debug #3307

Merged
merged 8 commits into from
Jul 15, 2024
11 changes: 11 additions & 0 deletions packages/config/src/config/vector.style.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { GoogleTms, Nztm2000QuadTms } from '@basemaps/geo';

import { ConfigBase } from './base.js';

/**
* Default Terrain exaggeration settings for different projection
blacha marked this conversation as resolved.
Show resolved Hide resolved
* NZTM terrain is too flat, it offsets the zoom level by 2 which means everything is approx 4x smaller than Google.
*/
export const DefaultExaggeration = {
[Nztm2000QuadTms.identifier]: 4.4,
[GoogleTms.identifier]: 1.2,
};

export interface SourceVector {
type: 'vector';
url: string;
Expand Down
11 changes: 6 additions & 5 deletions packages/lambda-tiler/src/routes/tile.style.json.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ConfigId, ConfigPrefix, ConfigTileSetRaster, Layer, Sources, StyleJson, TileSetType } from '@basemaps/config';
import { DefaultExaggeration } from '@basemaps/config/build/config/vector.style.js';
import { GoogleTms, TileMatrixSet, TileMatrixSets } from '@basemaps/geo';
import { Env, toQueryString } from '@basemaps/shared';
import { HttpHeader, LambdaHttpRequest, LambdaHttpResponse } from '@linzjs/lambda';
Expand Down Expand Up @@ -81,12 +82,12 @@ export interface StyleGet {
};
}

function setStyleTerrain(style: StyleJson, terrain: string): void {
function setStyleTerrain(style: StyleJson, terrain: string, tileMatrix: TileMatrixSet): void {
const source = Object.keys(style.sources).find((s) => s === terrain);
if (source == null) throw new LambdaHttpResponse(400, `Terrain: ${terrain} is not exists in the style source.`);
style.terrain = {
source,
exaggeration: 1.2,
exaggeration: DefaultExaggeration[tileMatrix.identifier] ?? DefaultExaggeration[GoogleTms.identifier],
};
}

Expand Down Expand Up @@ -143,7 +144,7 @@ export async function tileSetToStyle(
await ensureTerrain(req, tileMatrix, apiKey, style);

// Add terrain in style
if (terrain) setStyleTerrain(style, terrain);
if (terrain) setStyleTerrain(style, terrain, tileMatrix);

const data = Buffer.from(JSON.stringify(style));

Expand Down Expand Up @@ -226,7 +227,7 @@ export async function tileSetOutputToStyle(
await ensureTerrain(req, tileMatrix, apiKey, style);

// Add terrain in style
if (terrain) setStyleTerrain(style, terrain);
if (terrain) setStyleTerrain(style, terrain, tileMatrix);

const data = Buffer.from(JSON.stringify(style));

Expand Down Expand Up @@ -277,7 +278,7 @@ export async function styleJsonGet(req: LambdaHttpRequest<StyleGet>): Promise<La
await ensureTerrain(req, tileMatrix, apiKey, style);

// Add terrain in style
if (terrain) setStyleTerrain(style, terrain);
if (terrain) setStyleTerrain(style, terrain, tileMatrix);

const data = Buffer.from(JSON.stringify(style));

Expand Down
17 changes: 12 additions & 5 deletions packages/landing/src/components/debug.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ConfigImagery } from '@basemaps/config/build/config/imagery.js';
import { ConfigTileSetRaster } from '@basemaps/config/build/config/tile.set.js';
import { Epsg, GoogleTms, LocationUrl } from '@basemaps/geo';
import { DefaultExaggeration } from '@basemaps/config/build/config/vector.style.js';
import { GoogleTms, LocationUrl, TileMatrixSet } from '@basemaps/geo';
import { RasterLayerSpecification, SourceSpecification } from 'maplibre-gl';
import { ChangeEventHandler, Component, FormEventHandler, Fragment, ReactNode } from 'react';

Expand Down Expand Up @@ -361,7 +362,7 @@ export class Debug extends Component<{ map: maplibregl.Map }, DebugState> {
return;
}

const target = getTerrainForSource(sourceId, Config.map.tileMatrix.projection);
const target = getTerrainForSource(sourceId, Config.map.tileMatrix);
// no changes
if (currentTerrain?.source === sourceId && currentTerrain?.exaggeration === target.exaggeration) return;

Expand Down Expand Up @@ -455,7 +456,13 @@ export class Debug extends Component<{ map: maplibregl.Map }, DebugState> {

getSourcesIds(type: string): string[] {
Wentao-Kuang marked this conversation as resolved.
Show resolved Hide resolved
const style = this.props.map.getStyle();
return Object.keys(style.sources).filter((id) => id.startsWith('basemaps') && style.sources[id].type === type);
if (type === 'raster-dem') {
return Object.keys(style.sources).filter(
(id) => !id.startsWith(HillShadePrefix) && style.sources[id].type === type,
);
} else if (type === 'raster') {
return Object.keys(style.sources).filter((id) => id.startsWith('basemaps') && style.sources[id].type === type);
} else throw new Error('Only support to get raster or raster-dem sources for debug dropdown.');
}

renderRasterSourceDropdown(): ReactNode | null {
Expand Down Expand Up @@ -657,9 +664,9 @@ export class Debug extends Component<{ map: maplibregl.Map }, DebugState> {
* @param projection current projection
* @returns
*/
function getTerrainForSource(sourceId: string, projection: Epsg): { source: string; exaggeration: number } {
function getTerrainForSource(sourceId: string, tileMatrix: TileMatrixSet): { source: string; exaggeration: number } {
return {
source: sourceId,
exaggeration: projection.code === Epsg.Nztm2000.code ? 4.4 : 1.1,
exaggeration: DefaultExaggeration[tileMatrix.identifier] ?? DefaultExaggeration[GoogleTms.identifier],
};
}
32 changes: 16 additions & 16 deletions packages/landing/src/components/map.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DefaultExaggeration } from '@basemaps/config/build/config/vector.style.js';
import { GoogleTms, LocationUrl } from '@basemaps/geo';
import maplibre, { RasterLayerSpecification } from 'maplibre-gl';
import { Component, ReactNode } from 'react';
Expand Down Expand Up @@ -96,24 +97,23 @@ export class Basemaps extends Component<unknown, { isLayerSwitcherEnabled: boole
ensureElevationControl(): void {
if (Config.map.debug['debug.screenshot']) return;
if (Config.map.isDebug) return;
if (Config.map.tileMatrix === GoogleTms) {
if (this.controlTerrain != null) return;
// Try to find terrain source and add to the control
for (const [key, source] of Object.entries(this.map.getStyle().sources)) {
if (source.type === 'raster-dem') {
this.controlTerrain = new maplibre.TerrainControl({
source: key,
exaggeration: 1.2,
});
this.map.addControl(this.controlTerrain, 'top-left');
break;
}
if (this.controlTerrain != null) return;
// Try to find terrain source and add to the control
for (const [key, source] of Object.entries(this.map.getStyle().sources)) {
if (source.type === 'raster-dem') {
this.controlTerrain = new maplibre.TerrainControl({
source: key,
exaggeration:
DefaultExaggeration[Config.map.tileMatrix.identifier] ?? DefaultExaggeration[GoogleTms.identifier],
});
this.map.addControl(this.controlTerrain, 'top-left');
return;
}
} else {
if (this.controlTerrain == null) return;
this.map.removeControl(this.controlTerrain);
this.controlTerrain = null;
}

if (this.controlTerrain == null) return;
this.map.removeControl(this.controlTerrain);
this.controlTerrain = null;
}

/**
Expand Down
1 change: 0 additions & 1 deletion packages/landing/src/config.debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export interface DebugState {
'debug.terrain': string | null;
/** What layer should be visible only */
'debug.layer': string | null;

/** Should a hillshade be shown */
'debug.hillshade': string | null;
}
Expand Down
Loading