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

Add zRange prop to TileLayer for use with TerrainLayer #4397

Merged
merged 9 commits into from
Mar 26, 2020
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
26 changes: 25 additions & 1 deletion modules/geo-layers/src/terrain-layer/terrain-layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ function urlTemplateToUpdateTrigger(template) {
return template;
}

// Get [min, max] elevation range of terrain mesh
function getZRange(positions) {
kylebarron marked this conversation as resolved.
Show resolved Hide resolved
let min = Infinity;
let max = -Infinity;
for (let i = 2; i < positions.length; i += 3) {
const ele = positions[i];
min = ele < min ? ele : min;
max = ele > max ? ele : max;
}
return [min, max];
}

/**
* state: {
* isTiled: True renders TileLayer of many SimpleMeshLayers, false renders one SimpleMeshLayer
Expand Down Expand Up @@ -168,6 +180,16 @@ export default class TerrainLayer extends CompositeLayer {
});
}

// Update zRange of viewport
onViewportLoad(data) {
Pessimistress marked this conversation as resolved.
Show resolved Hide resolved
const ranges = data.map(arr => {
return getZRange(arr[0].attributes.POSITION.value);
});
const minZ = Math.min(...ranges.flatMap(x => x[0]));
const maxZ = Math.max(...ranges.flatMap(x => x[1]));
this.setState({zRange: [minZ, maxZ]});
}

renderLayers() {
const {
color,
Expand Down Expand Up @@ -197,7 +219,9 @@ export default class TerrainLayer extends CompositeLayer {
meshMaxError,
elevationDecoder
}
}
},
onViewportLoad: this.onViewportLoad.bind(this),
zRange: this.state.zRange || null
}
);
}
Expand Down
7 changes: 4 additions & 3 deletions modules/geo-layers/src/tile-layer/tile-layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ const defaultProps = {
minZoom: 0,
maxCacheSize: null,
maxCacheByteSize: null,
refinementStrategy: STRATEGY_DEFAULT
refinementStrategy: STRATEGY_DEFAULT,
zRange: null
};

export default class TileLayer extends CompositeLayer {
Expand Down Expand Up @@ -85,8 +86,8 @@ export default class TileLayer extends CompositeLayer {

_updateTileset() {
const {tileset} = this.state;
const {onViewportLoad} = this.props;
const frameNumber = tileset.update(this.context.viewport);
const {onViewportLoad, zRange} = this.props;
const frameNumber = tileset.update(this.context.viewport, {zRange});
const {isLoaded} = tileset;

const loadingStateChanged = this.state.isLoaded !== isLoaded;
Expand Down
9 changes: 5 additions & 4 deletions modules/geo-layers/src/tile-layer/tileset-2d.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,14 @@ export default class Tileset2D {
* @param {*} viewport
* @param {*} onUpdate
*/
update(viewport) {
update(viewport, {zRange}) {
kylebarron marked this conversation as resolved.
Show resolved Hide resolved
if (viewport !== this._viewport) {
this._viewport = viewport;
const tileIndices = this.getTileIndices({
viewport,
maxZoom: this._maxZoom,
minZoom: this._minZoom
minZoom: this._minZoom,
zRange
});
this._selectedTiles = tileIndices.map(index => this._getTile(index, true));

Expand All @@ -130,8 +131,8 @@ export default class Tileset2D {
/* Public interface for subclassing */

// Returns array of {x, y, z}
getTileIndices({viewport, maxZoom, minZoom}) {
return getTileIndices(viewport, maxZoom, minZoom, this.opts.tileSize);
getTileIndices({viewport, maxZoom, minZoom, zRange}) {
return getTileIndices(viewport, maxZoom, minZoom, zRange, this.opts.tileSize);
}

// Add custom metadata to tiles
Expand Down
32 changes: 27 additions & 5 deletions modules/geo-layers/src/tile-layer/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,29 @@ export function getURLFromTemplate(template, properties) {
/**
* gets the bounding box of a viewport
*/
function getBoundingBox(viewport) {
function getBoundingBox(viewport, zRange) {
if (zRange && zRange.length === 2) {
const corners = [
// Lower zRange
viewport.unproject([0, 0], {targetZ: zRange[0]}),
kylebarron marked this conversation as resolved.
Show resolved Hide resolved
viewport.unproject([viewport.width, 0], {targetZ: zRange[0]}),
viewport.unproject([0, viewport.height], {targetZ: zRange[0]}),
viewport.unproject([viewport.width, viewport.height], {targetZ: zRange[0]}),

// Upper zRange
viewport.unproject([0, 0], {targetZ: zRange[1]}),
viewport.unproject([viewport.width, 0], {targetZ: zRange[1]}),
viewport.unproject([0, viewport.height], {targetZ: zRange[1]}),
viewport.unproject([viewport.width, viewport.height], {targetZ: zRange[1]})
];
return [
Math.min(...corners.flatMap(arr => arr[0])),
kylebarron marked this conversation as resolved.
Show resolved Hide resolved
Math.min(...corners.flatMap(arr => arr[1])),
Math.max(...corners.flatMap(arr => arr[0])),
Math.max(...corners.flatMap(arr => arr[1]))
];
}

const corners = [
kylebarron marked this conversation as resolved.
Show resolved Hide resolved
viewport.unproject([0, 0]),
viewport.unproject([viewport.width, 0]),
Expand Down Expand Up @@ -122,8 +144,8 @@ function getIdentityTileIndices(viewport, z, tileSize) {
return indices;
}

function getOSMTileIndices(viewport, z) {
const bbox = getBoundingBox(viewport);
function getOSMTileIndices(viewport, z, zRange) {
const bbox = getBoundingBox(viewport, zRange);
const scale = getScale(z);
/*
minX, maxX could be out of bounds if longitude is near the 180 meridian or multiple worlds
Expand Down Expand Up @@ -160,7 +182,7 @@ function getOSMTileIndices(viewport, z) {
* than minZoom, return an empty array. If the current zoom level is greater than maxZoom,
* return tiles that are on maxZoom.
*/
export function getTileIndices(viewport, maxZoom, minZoom, tileSize = TILE_SIZE) {
export function getTileIndices(viewport, maxZoom, minZoom, zRange, tileSize = TILE_SIZE) {
kylebarron marked this conversation as resolved.
Show resolved Hide resolved
let z = Math.ceil(viewport.zoom);
if (Number.isFinite(minZoom) && z < minZoom) {
return [];
Expand All @@ -170,6 +192,6 @@ export function getTileIndices(viewport, maxZoom, minZoom, tileSize = TILE_SIZE)
}

return viewport.isGeospatial
? getOSMTileIndices(viewport, z)
? getOSMTileIndices(viewport, z, zRange)
: getIdentityTileIndices(viewport, z, tileSize);
}