Skip to content

Commit

Permalink
Merge bd7bfa4 into a2dc854
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisgervang committed Feb 17, 2020
2 parents a2dc854 + bd7bfa4 commit a2bd4dc
Show file tree
Hide file tree
Showing 16 changed files with 388 additions and 38 deletions.
205 changes: 205 additions & 0 deletions docs/layers/terrain-layer.md
@@ -0,0 +1,205 @@
<!-- INJECT:"TerrainLayerDemo" -->

<p class="badges">
<img src="https://img.shields.io/badge/@deck.gl/geo--layers-lightgrey.svg?style=flat-square" alt="@deck.gl/geo-layers" />
<img src="https://img.shields.io/badge/lighting-yes-blue.svg?style=flat-square" alt="lighting" />
</p>

# TerrainLayer

This TileLayer takes in a function `getTileData` that fetches tiles, and renders it in a GeoJsonLayer or with the layer returned in `renderSubLayers`.

```js
import DeckGL from '@deck.gl/react';
import {TileLayer} from '@deck.gl/geo-layers';
import {VectorTile} from '@mapbox/vector-tile';
import Protobuf from 'pbf';

export const App = ({viewport}) => {

const layer = new TileLayer({
stroked: false,

getLineColor: [192, 192, 192],
getFillColor: [140, 170, 180],

getLineWidth: f => {
if (f.properties.layer === 'transportation') {
switch (f.properties.class) {
case 'primary':
return 12;
case 'motorway':
return 16;
default:
return 6;
}
}
return 1;
},
lineWidthMinPixels: 1,

getTileData: ({x, y, z}) => {
const mapSource = `https://a.tiles.mapbox.com/v4/mapbox.mapbox-streets-v7/${z}/${x}/${y}.vector.pbf?access_token=${MapboxAccessToken}`;
return fetch(mapSource)
.then(response => response.arrayBuffer())
.then(buffer => {
const tile = new VectorTile(new Protobuf(buffer));
const features = [];
for (const layerName in tile.layers) {
const vectorTileLayer = tile.layers[layerName];
for (let i = 0; i < vectorTileLayer.length; i++) {
const vectorTileFeature = vectorTileLayer.feature(i);
const feature = vectorTileFeature.toGeoJSON(x, y, z);
features.push(feature);
}
}
return features;
});
}
});
return <DeckGL {...viewport} layers={[layer]} />;
};
```


## Installation

To install the dependencies from NPM:

```bash
npm install deck.gl
# or
npm install @deck.gl/core @deck.gl/layers @deck.gl/geo-layers
```

```js
import {TerrainLayer} from '@deck.gl/geo-layers';
new TerrainLayer({});
```

To use pre-bundled scripts:

```html
<script src="https://unpkg.com/deck.gl@^8.0.0/dist.min.js"></script>
<!-- or -->
<script src="https://unpkg.com/@deck.gl/core@^8.0.0/dist.min.js"></script>
<script src="https://unpkg.com/@deck.gl/layers@^8.0.0/dist.min.js"></script>
<script src="https://unpkg.com/@deck.gl/geo-layers@^8.0.0/dist.min.js"></script>
```

```js
new deck.TerrainLayer({});
```


## Properties

### Data Options

##### `getTileData` (Function)

`getTileData` given x, y, z indices of the tile, returns the tile data or a Promise that resolves to the tile data.

- Default: `tile => Promise.resolve(null)`

The `tile` argument contains the following fields:

- `x` (Number) - x index of the tile
- `y` (Number) - y index of the tile
- `z` (Number) - z index of the tile
- `bbox` (Object) - bounding box of the tile, see `tileToBoundingBox`.

By default, the `TileLayer` loads tiles defined by [the OSM tile index](https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames). You may override this by implementing `getTileIndices`.


##### `maxZoom` (Number|Null, optional)

Use tiles from this level when over-zoomed.

- Default: `null`


##### `minZoom` (Number, optional)

Hide tiles when under-zoomed.

- Default: 0


##### `maxCacheSize` (Number|Null, optional)

The maximum cache size for a tile layer. If not defined, it is calculated using the number of tiles in the current viewport times multiplied by `5`.

- Default: `null`


##### `strategy` (Enum, optional)

How the tile layer determines the visibility of tiles. One of the following:

* `'best-available'`: If a tile in the current viewport is waiting for its data to load, use cached content from the closest zoom level to fill the empty space. This approach minimizes the visual flashing due to missing content.
* `'no-overlap'`: Avoid showing overlapping tiles when backfilling with cached content. This is usually favorable when tiles do not have opaque backgrounds.

- Default: `'best-available'`


##### `tileToBoundingBox` (Function, optional)

**Advanced** Converts from `x, y, z` tile indices to a bounding box in the global coordinates. The default implementation converts an OSM tile index to `{west: <longitude>, north: <latitude>, east: <longitude>, south: <latitude>}`.

Receives arguments:

- `x` (Number)
- `y` (Number)
- `z` (Number)

The returned value will be available via `tile.bbox`.


##### `getTileIndices` (Function, optional)

**Advanced** This function converts a given viewport to the indices needed to fetch tiles contained in the viewport. The default implementation returns visible tiles defined by [the OSM tile index](https://wiki.openstreetmap.org/wiki/Slippy_map_tilenames).

Receives arguments:

- `viewport` (Viewport)
- `minZoom` (Number) The minimum zoom level
- `maxZoom` (Number) The maximum zoom level

Returns:

An array of objects in the shape of `{x, y, z}`.


### Render Options

##### `renderSubLayers` (Function, optional))

Renders one or an array of Layer instances with all the `TileLayer` props and the following props:

* `id`: An unique id for this sublayer
* `data`: Resolved from `getTileData`
* `tile`: An object containing tile index `x`, `y`, `z`, and `bbox` of the tile.

- Default: `props => new GeoJsonLayer(props)`


### Callbacks

##### `onViewportLoad` (Function, optional)

`onViewportLoad` is a function that is called when all tiles in the current viewport are loaded. The loaded content (as returned by `getTileData`) for each visible tile is passed as an array to this callback function.

- Default: `data => null`


##### `onTileError` (Function, optional)

`onTileError` called when a tile failed to load.

- Default: `console.error`


# Source

[modules/geo-layers/src/terrain-layer](https://github.com/uber/deck.gl/tree/master/modules/geo-layers/src/terrain-layer)
3 changes: 2 additions & 1 deletion docs/table-of-contents.json
Expand Up @@ -98,7 +98,8 @@
{"entry": "docs/layers/screen-grid-layer"},
{"entry": "docs/layers/simple-mesh-layer"},
{"entry": "docs/layers/tile-layer"},
{"entry": "docs/layers/trips-layer"}
{"entry": "docs/layers/trips-layer"},
{"entry": "docs/layers/terrain-layer"}
]
},
{
Expand Down
2 changes: 1 addition & 1 deletion examples/experimental/terrain/package.json
Expand Up @@ -12,7 +12,7 @@
"@loaders.gl/loader-utils": "^2.1.0-alpha.4"
},
"dependencies": {
"@loaders.gl/images": "2.0.2",
"@loaders.gl/images": "^2.1.0-alpha.4",
"@mapbox/martini": "^0.2.0",
"deck.gl": "^8.1.0-alpha.3",
"react": "^16.3.0",
Expand Down
2 changes: 1 addition & 1 deletion examples/experimental/terrain/src/app.js
Expand Up @@ -3,7 +3,7 @@ import React, {useState} from 'react';
import {render} from 'react-dom';
import DeckGL from '@deck.gl/react';

import TerrainLayer from './terrain-layer/terrain-layer';
import TerrainLayer from '@deck.gl/geo-layers';
import {getSurface} from './surface';
import {getViewState} from './locations';

Expand Down
4 changes: 2 additions & 2 deletions modules/core/package.json
Expand Up @@ -32,8 +32,8 @@
"prepublishOnly": "npm run build-debugger && npm run build-bundle && npm run build-bundle -- --env.dev"
},
"dependencies": {
"@loaders.gl/core": "^2.0.2",
"@loaders.gl/images": "^2.0.2",
"@loaders.gl/core": "^2.1.0-alpha.4",
"@loaders.gl/images": "^2.1.0-alpha.4",
"@luma.gl/core": "^8.1.0-alpha.2",
"@math.gl/web-mercator": "^3.1.3",
"gl-matrix": "^3.0.0",
Expand Down
5 changes: 4 additions & 1 deletion modules/geo-layers/package.json
Expand Up @@ -30,7 +30,10 @@
"prepublishOnly": "npm run build-bundle && npm run build-bundle -- --env.dev"
},
"dependencies": {
"@loaders.gl/3d-tiles": "^2.0.2",
"@loaders.gl/core": "^2.1.0-alpha.4",
"@loaders.gl/3d-tiles": "^2.1.0-alpha.4",
"@loaders.gl/images": "^2.1.0-alpha.4",
"@loaders.gl/terrain": "^2.1.0-alpha.4",
"@math.gl/web-mercator": "^3.1.3",
"h3-js": "^3.6.0",
"long": "^3.2.0",
Expand Down
1 change: 1 addition & 0 deletions modules/geo-layers/src/index.js
Expand Up @@ -26,3 +26,4 @@ export {default as TripsLayer} from './trips-layer/trips-layer';
export {default as H3ClusterLayer} from './h3-layers/h3-cluster-layer';
export {default as H3HexagonLayer} from './h3-layers/h3-hexagon-layer';
export {default as Tile3DLayer} from './tile-3d-layer/tile-3d-layer';
export {default as TerrainLayer} from './terrain-layer/terrain-layer';
@@ -1,9 +1,29 @@
// Copyright (c) 2015 - 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import {CompositeLayer} from '@deck.gl/core';
import {SimpleMeshLayer} from '@deck.gl/mesh-layers';
import {WebMercatorViewport, COORDINATE_SYSTEM} from '@deck.gl/core';
import {load} from '@loaders.gl/core';
import {TerrainLoader} from '@loaders.gl/terrain';
import {TileLayer} from '@deck.gl/geo-layers';
import TileLayer from '../tile-layer/tile-layer';

const defaultProps = {
...TileLayer.defaultProps,
Expand Down
6 changes: 3 additions & 3 deletions modules/jupyter-widget/package.json
Expand Up @@ -35,9 +35,9 @@
"@deck.gl/layers": "8.1.0-alpha.3",
"@deck.gl/mesh-layers": "8.1.0-alpha.3",
"@jupyter-widgets/base": "^2",
"@loaders.gl/3d-tiles": "^2.0.2",
"@loaders.gl/core": "^2.0.2",
"@loaders.gl/csv": "^2.0.2",
"@loaders.gl/3d-tiles": "^2.1.0-alpha.4",
"@loaders.gl/core": "^2.1.0-alpha.4",
"@loaders.gl/csv": "^2.1.0-alpha.4",
"@luma.gl/constants": "^8.1.0-alpha.2",
"mapbox-gl": "^1.2.1"
},
Expand Down
4 changes: 2 additions & 2 deletions modules/layers/package.json
Expand Up @@ -29,8 +29,8 @@
"prepublishOnly": "npm run build-bundle && npm run build-bundle -- --env.dev"
},
"dependencies": {
"@loaders.gl/core": "^2.0.2",
"@loaders.gl/images": "^2.0.2",
"@loaders.gl/core": "^2.1.0-alpha.4",
"@loaders.gl/images": "^2.1.0-alpha.4",
"@mapbox/tiny-sdf": "^1.1.0",
"earcut": "^2.0.6"
},
Expand Down
3 changes: 2 additions & 1 deletion modules/main/src/index.js
Expand Up @@ -118,7 +118,8 @@ export {
H3HexagonLayer,
TileLayer,
TripsLayer,
Tile3DLayer
Tile3DLayer,
TerrainLayer
} from '@deck.gl/geo-layers';

export {SimpleMeshLayer, ScenegraphLayer} from '@deck.gl/mesh-layers';
Expand Down
5 changes: 4 additions & 1 deletion test/modules/geo-layers/index.js
Expand Up @@ -26,7 +26,8 @@ import {
H3ClusterLayer,
S2Layer,
TileLayer,
TripsLayer
TripsLayer,
TerrainLayer
} from '@deck.gl/geo-layers';

test('Top-level imports', t => {
Expand All @@ -36,6 +37,7 @@ test('Top-level imports', t => {
t.ok(H3ClusterLayer, 'H3ClusterLayer symbol imported');
t.ok(TileLayer, 'TileLayer symbol imported');
t.ok(TripsLayer, 'TripsLayer symbol imported');
t.ok(TerrainLayer, 'TerrainLayer symbol imported');
t.end();
});

Expand All @@ -45,3 +47,4 @@ import './trips-layer.spec';
import './great-circle-layer.spec';
import './h3-layers.spec';
import './tile-3d-layer';
import './terrain-layer.spec';
33 changes: 33 additions & 0 deletions test/modules/geo-layers/terrain-layer.spec.js
@@ -0,0 +1,33 @@
// Copyright (c) 2015 - 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

import test from 'tape-catch';
import {generateLayerTests, testLayer} from '@deck.gl/test-utils';
import {TerrainLayer} from '@deck.gl/geo-layers';

test('TerrainLayer', t => {
const testCases = generateLayerTests({
Layer: TerrainLayer,
assert: t.ok,
onBeforeUpdate: ({testCase}) => t.comment(testCase.title)
});
testLayer({Layer: TerrainLayer, testCases, onError: t.notOk});
t.end();
});
4 changes: 4 additions & 0 deletions website/contents/pages.js
Expand Up @@ -544,6 +544,10 @@ export const docPages = generatePath([
{
name: 'TripsLayer',
content: getDocUrl('layers/trips-layer.md')
},
{
name: 'TerrainLayer',
content: getDocUrl('layers/terrain-layer.md')
}
]
},
Expand Down

0 comments on commit a2bd4dc

Please sign in to comment.