Skip to content

Commit

Permalink
Fix promise getting rejected due to resolveGetPixels() erroneously …
Browse files Browse the repository at this point in the history
…entering NodeJS specific context (#25)

Since three-geo v1.4.3, when using with NodeJS, you must explicitly set the constructor option `useNodePixels` to `true` (https://github.com/w3reality/three-geo#note-nodejs)
  • Loading branch information
j-devel committed Dec 1, 2020
1 parent 524650e commit cda26f3
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 22 deletions.
13 changes: 8 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@ class ThreeGeo {
constructor(opts={}) {
this.version = __version;

Utils.Meta.consoleLog(`ThreeGeo ${__version} with THREE r${THREE.REVISION}`);
console.info(`ThreeGeo ${__version} with THREE r${THREE.REVISION}`);
console.info('Note: Since three-geo v1.4.3, when using with NodeJS, you must set the constructor option `useNodePixels` to `true` (https://github.com/w3reality/three-geo#api)');

const defaults = {
unitsSide: 1.0,
tokenMapbox: '',
useNodePixels: false, // Do enable this when using with NodeJS
apiVector: 'mapbox-terrain-vector',
apiRgb: 'mapbox-terrain-rgb',
apiSatellite: 'mapbox-satellite',
};
const actual = Object.assign({}, defaults, opts);
this.constUnitsSide = actual.unitsSide;
this.tokenMapbox = actual.tokenMapbox;
this.useNodePixels = actual.useNodePixels;
this.apiVector = actual.apiVector;
this.apiRgb = actual.apiRgb;
this.apiSatellite = actual.apiSatellite;
Expand Down Expand Up @@ -178,7 +181,7 @@ class ThreeGeo {
const unitsPerMeter = ThreeGeo._getUnitsPerMeter(_unitsSide, radius);
const projectCoord = (coord, nw, se) =>
ThreeGeo._projectCoord(_unitsSide, coord, nw, se);
const { tokenMapbox: token,
const { tokenMapbox: token, useNodePixels,
apiRgb, apiSatellite, apiVector } = this;

// callbacks
Expand All @@ -193,21 +196,21 @@ class ThreeGeo {
if (onRgbDem) {
(new RgbModel({
unitsPerMeter, projectCoord,
token, apiRgb, apiSatellite,
token, useNodePixels, apiRgb, apiSatellite,
onRgbDem, onSatelliteMat, watcher,
})).fetch(zpCovered, bbox);
}

if (onVectorDem) {
(new VectorModel({
unitsPerMeter, projectCoord,
token, apiVector,
token, useNodePixels, apiVector,
onVectorDem, watcher,
})).fetch(zpCovered, bbox, radius);
}
} catch (err) {
console.error('err:', err);
rej(null);
rej(err);
}
});
}
Expand Down
10 changes: 6 additions & 4 deletions src/models/fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,8 @@ class Fetch {
};
}

static async resolveGetPixels() {
return Utils.Meta.isNodeJS() ?
static async resolveGetPixels(useNodePixels) {
return useNodePixels ?
await Utils.Meta.nodeRequire(global, 'get-pixels/node-pixels') :
__getPixelsDom; // use the statically imported one
}
Expand All @@ -147,7 +147,7 @@ class Fetch {
.map(triplet => triplet.split(',').map(num => parseFloat(num)));
}

static fetchTile(zoompos, api, token, cb) {
static fetchTile(zoompos, api, token, useNodePixels, cb) {
// console.log('token:', token);
let isMapbox = api.startsWith('mapbox-');
let uri = isMapbox ?
Expand Down Expand Up @@ -181,7 +181,9 @@ class Fetch {
}

const _cb = (err, pixels) => cb(err ? null : pixels);
this.resolveGetPixels().then(fn => fn(uri, _cb));
this.resolveGetPixels(useNodePixels)
.then(fn => fn(uri, _cb))
.catch(err => console.error('err:', err));
} else {
console.log('nop, unsupported api:', api);
}
Expand Down
16 changes: 9 additions & 7 deletions src/models/rgb.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class RgbModel {
this.unitsPerMeter = params.unitsPerMeter;
this.projectCoord = params.projectCoord;
this.token = params.token;
this.useNodePixels = params.useNodePixels;
this.apiRgb = params.apiRgb;
this.apiSatellite = params.apiSatellite;

Expand All @@ -77,7 +78,7 @@ class RgbModel {

let count = 0;
zpEle.forEach(zoompos => {
Fetch.fetchTile(zoompos, this.apiRgb, this.token, tile => {
Fetch.fetchTile(zoompos, this.apiRgb, this.token, this.useNodePixels, tile => {
if (tile) {
this.addTile(tile, zoompos, zpCovered, bbox);
} else {
Expand Down Expand Up @@ -305,14 +306,15 @@ class RgbModel {
}

const meshes = RgbModel._build(
this.dataEleCovered, this.apiSatellite, this.token, onSatelliteMatWrapper);
this.dataEleCovered, this.apiSatellite,
this.token, this.useNodePixels, onSatelliteMatWrapper);

this.onRgbDem(meshes); // legacy API
if (!onSatelliteMatWrapper) {
this.watcher({what: 'dem-rgb', data: meshes});
}
}
static _build(dataEle, apiSatellite, token, onSatelliteMatWrapper) {
static _build(dataEle, apiSatellite, token, useNodePixels, onSatelliteMatWrapper) {
console.log('apiSatellite:', apiSatellite);

// dataEle should be sorted so that .resolveSeams() is applied
Expand Down Expand Up @@ -366,7 +368,7 @@ class RgbModel {
};
objs.push(plane);

this.resolveTex(zoompos, apiSatellite, token, tex => {
this.resolveTex(zoompos, apiSatellite, token, useNodePixels, tex => {
if (tex) {
plane.material = new THREE.MeshBasicMaterial({
side: THREE.FrontSide,
Expand All @@ -383,8 +385,9 @@ class RgbModel {
}

//==== THREE specific
static resolveTex(zoompos, apiSatellite, token, onTex) {
Fetch.fetchTile(zoompos, apiSatellite, token, pixels => {
// _buildModelThree() {} // TODO (refactor)
static resolveTex(zoompos, apiSatellite, token, useNodePixels, onTex) {
Fetch.fetchTile(zoompos, apiSatellite, token, useNodePixels, pixels => {
let tex = null;
if (pixels) {
// console.log("satellite pixels", pixels.shape.slice(0));
Expand All @@ -409,7 +412,6 @@ class RgbModel {
}
});
}
// _buildModelThree() {} // TODO !!!!!!!!
}

export default RgbModel;
3 changes: 2 additions & 1 deletion src/models/vector.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class VectorModel {
this.unitsPerMeter = params.unitsPerMeter;
this.projectCoord = params.projectCoord;
this.token = params.token;
this.useNodePixels = params.useNodePixels;
this.apiVector = params.apiVector;

// callbacks
Expand All @@ -36,7 +37,7 @@ class VectorModel {

let count = 0;
zpEle.forEach(zoompos => {
Fetch.fetchTile(zoompos, this.apiVector, this.token, tile => {
Fetch.fetchTile(zoompos, this.apiVector, this.token, this.useNodePixels, tile => {
if (tile) {
this.addTile(tile, zoompos);
} else {
Expand Down
11 changes: 6 additions & 5 deletions tests/units.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const setupApi = (tgeo, base) => {
const setupApi = (tgeo, preset, base) => {
tgeo.tokenMapbox = 'zzzz';
tgeo.useNodePixels = preset === 'node';
tgeo.setApiVector(`${base}/custom-terrain-vector`);
tgeo.setApiRgb(`${base}/custom-terrain-rgb`);
tgeo.setApiSatellite(`${base}/custom-satellite`);
Expand All @@ -22,7 +23,7 @@ units['rgb-noexist'] = async (ThreeGeo, dataDir, preset='node') => {

// The API call should return even when no rgb DEM files are fetched
const loc = {name: 'noexist', origin: [46.5763, 7.9904], radius: 5.0, zoom: 12};
setupApi(tgeo, `${dataDir}/${loc.name}`);
setupApi(tgeo, preset, `${dataDir}/${loc.name}`);

const { origin, radius, zoom } = loc;
const ret = await run(() => tgeo.getTerrainRgb(origin, radius, zoom));
Expand All @@ -38,7 +39,7 @@ units['rgb-eiger'] = async (ThreeGeo, dataDir, preset='node') => {
const tgeo = new ThreeGeo();

const loc = {name: 'eiger', origin: [46.5763, 7.9904], radius: 5.0, zoom: 12};
setupApi(tgeo, `${dataDir}/${loc.name}`);
setupApi(tgeo, preset, `${dataDir}/${loc.name}`);

const { origin, radius, zoom } = loc;
const ret = await run(() => tgeo.getTerrainRgb(origin, radius, zoom));
Expand Down Expand Up @@ -66,7 +67,7 @@ units['rgb-table'] = async (ThreeGeo, dataDir, preset='node') => {
const tgeo = new ThreeGeo();

const loc = {name: 'table', origin: [-33.9625, 18.4107], radius: 1.25, zoom: 14};
setupApi(tgeo, `${dataDir}/${loc.name}`);
setupApi(tgeo, preset, `${dataDir}/${loc.name}`);

const { origin, radius, zoom } = loc;
const ret = await run(() => tgeo.getTerrainRgb(origin, radius, zoom));
Expand Down Expand Up @@ -94,7 +95,7 @@ units['vec-table'] = async (ThreeGeo, dataDir, preset='node') => {
const tgeo = new ThreeGeo();

const loc = {name: 'table', origin: [-33.9625, 18.4107], radius: 1.25, zoom: 14};
setupApi(tgeo, `${dataDir}/${loc.name}`);
setupApi(tgeo, preset, `${dataDir}/${loc.name}`);

const { origin, radius, zoom } = loc;
const ret = await run(() => tgeo.getTerrainVector(origin, radius, zoom));
Expand Down

0 comments on commit cda26f3

Please sign in to comment.