Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Xiaoji Chen
committed
Jan 5, 2020
1 parent
28d3c4f
commit 6de7138
Showing
5 changed files
with
70 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,46 @@ | ||
import Protobuf from 'pbf'; | ||
import {VectorTile} from '@mapbox/vector-tile'; | ||
import {worldToLngLat} from 'viewport-mercator-project'; | ||
import {vectorTileFeatureToGeoJSON} from './feature'; | ||
|
||
const TILE_SIZE = 512; | ||
const PI = Math.PI; | ||
const PI_4 = PI / 4; | ||
const RADIANS_TO_DEGREES_2 = 360 / PI; | ||
|
||
export function decodeTile(x, y, z, arrayBuffer) { | ||
const tile = new VectorTile(new Protobuf(arrayBuffer)); | ||
|
||
const result = []; | ||
const xProj = x * TILE_SIZE; | ||
const yProj = y * TILE_SIZE; | ||
|
||
const scale = Math.pow(2, z); | ||
const projX = x / scale; | ||
const projY = y / scale; | ||
|
||
const projectFunc = project.bind(null, xProj, yProj, scale); | ||
const projectFunc = project.bind(null, projX, projY, scale); | ||
|
||
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 features = vectorTileFeatureToGeoJSON(vectorTileFeature, projectFunc); | ||
features.forEach(f => { | ||
for (const f of features) { | ||
f.properties.layer = layerName; | ||
result.push(f); | ||
}); | ||
} | ||
} | ||
} | ||
return result; | ||
} | ||
|
||
function project(x, y, scale, line, extent) { | ||
const sizeToPixel = extent / TILE_SIZE; | ||
const pixelToCommon = 1 / extent / scale; | ||
|
||
for (let ii = 0; ii < line.length; ii++) { | ||
const p = line[ii]; | ||
for (let i = 0; i < line.length; i++) { | ||
const p = line[i]; | ||
// common space | ||
const cx = x + p[0] * pixelToCommon; | ||
const cy = y + p[1] * pixelToCommon; | ||
// LNGLAT | ||
line[ii] = worldToLngLat([x + p[0] / sizeToPixel, y + p[1] / sizeToPixel], scale); | ||
p[0] = cx * 360 - 180; | ||
p[1] = (Math.atan(Math.exp(PI - cy * 2 * PI)) - PI_4) * RADIANS_TO_DEGREES_2; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,42 +1,33 @@ | ||
// NOTE: To use this example standalone (e.g. outside of deck.gl repo) | ||
// delete the local development overrides at the bottom of this file | ||
|
||
// avoid destructuring for older Node version support | ||
const resolve = require('path').resolve; | ||
const webpack = require('webpack'); | ||
|
||
const CONFIG = { | ||
mode: 'development', | ||
|
||
entry: { | ||
app: resolve('./app.js') | ||
app: './app.js' | ||
}, | ||
|
||
devtool: 'source-map', | ||
|
||
module: { | ||
rules: [ | ||
{ | ||
// Compile ES2015 using buble | ||
// Transpile ES6 to ES5 with babel | ||
// Remove if your app does not use JSX or you don't need to support old browsers | ||
test: /\.js$/, | ||
loader: 'buble-loader', | ||
include: [resolve('.')], | ||
loader: 'babel-loader', | ||
exclude: [/node_modules/], | ||
options: { | ||
objectAssign: 'Object.assign' | ||
presets: ['@babel/preset-react'] | ||
} | ||
} | ||
] | ||
}, | ||
|
||
resolve: { | ||
alias: { | ||
// From mapbox-gl-js README. Required for non-browserify bundlers (e.g. webpack): | ||
'mapbox-gl$': resolve('./node_modules/mapbox-gl/dist/mapbox-gl.js') | ||
} | ||
}, | ||
|
||
// Optional: Enables reading mapbox token from environment variable | ||
plugins: [new webpack.EnvironmentPlugin(['MapboxAccessToken'])] | ||
}; | ||
|
||
// This line enables bundling against src in this repo rather than installed deck.gl module | ||
module.exports = env => (env ? require('../webpack.config.local')(CONFIG)(env) : CONFIG); | ||
// This line enables bundling against src in this repo rather than installed module | ||
module.exports = env => (env ? require('..//webpack.config.local')(CONFIG)(env) : CONFIG); |