Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d4ec447

Browse files
peppsacautra
authored andcommitted
feat(protocols): add spatial hashing to StaticProvider (#675)
* feat(protocols): add spatial hashing to StaticProvider Using https://github.com/mourner/flatbush speeds up the look up by a factor of 10. For instance, on my computer and the panorama demo: - before the PR selectBestImageForExtent used to take 0.5ms to 3ms - after the PR: <= 0.1ms
1 parent 7166aff commit d4ec447

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

package-lock.json

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"babel-polyfill": "^6.26.0",
3636
"babel-runtime": "^6.20.0",
3737
"earcut": "^2.1.1",
38+
"flatbush": "^1.2.0",
3839
"js-priority-queue": "^0.1.5",
3940
"jszip": "^3.1.3",
4041
"monotone-convex-hull-2d": "^1.0.1",

src/Core/Scheduler/Providers/StaticProvider.js

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,24 @@
1+
import * as FlatBush from 'flatbush';
12
import { Vector4 } from 'three';
23
import Extent from '../../Geographic/Extent';
34
import OGCWebServiceHelper from './OGCWebServiceHelper';
45
import Fetcher from './Fetcher';
56
import { l_COLOR, l_ELEVATION } from '../../../Renderer/LayeredMaterialConstants';
67

8+
function _selectImagesFromSpatialIndex(index, images, extent) {
9+
return index.search(
10+
extent.west(), extent.south(),
11+
extent.east(), extent.north()).map(i => images[i]);
12+
}
13+
714
// select the smallest image entirely covering the tile
8-
function selectBestImageForExtent(images, extent) {
15+
function selectBestImageForExtent(layer, extent) {
16+
const candidates =
17+
_selectImagesFromSpatialIndex(
18+
layer._spatialIndex, layer.images, extent.as(layer.extent.crs()));
19+
920
let selection;
10-
for (const entry of images) {
21+
for (const entry of candidates) {
1122
if (extent.isInside(entry.extent)) {
1223
if (!selection) {
1324
selection = entry;
@@ -40,7 +51,7 @@ function getTexture(tile, layer) {
4051
return Promise.reject();
4152
}
4253

43-
const selection = selectBestImageForExtent(layer.images, tile.extent);
54+
const selection = selectBestImageForExtent(layer, tile.extent);
4455

4556
if (!selection) {
4657
return Promise.reject(
@@ -103,6 +114,15 @@ export default {
103114
extent,
104115
});
105116
}
117+
layer._spatialIndex = FlatBush(layer.images.length);
118+
for (const image of layer.images) {
119+
layer._spatialIndex.add(
120+
image.extent.west(),
121+
image.extent.south(),
122+
image.extent.east(),
123+
image.extent.north());
124+
}
125+
layer._spatialIndex.finish();
106126
}).then(() => {
107127
if (!layer.format) {
108128
// fetch the first image to detect format
@@ -127,20 +147,16 @@ export default {
127147
return false;
128148
}
129149

130-
for (const entry of layer.images) {
131-
if (tile.extent.isInside(entry.extent)) {
132-
return true;
133-
}
134-
}
135-
136-
return false;
150+
return _selectImagesFromSpatialIndex(
151+
layer._spatialIndex, layer.images, tile.extent.as(layer.extent.crs())).length > 0;
137152
},
138153

139154
canTileTextureBeImproved(layer, tile) {
140155
if (!layer.images) {
141156
return false;
142157
}
143-
const s = selectBestImageForExtent(layer.images, tile.extent);
158+
const s = selectBestImageForExtent(layer, tile.extent);
159+
144160
if (!s) {
145161
return false;
146162
}

0 commit comments

Comments
 (0)