Skip to content

Commit

Permalink
Extract FeatureIndex.deserialize; fill in missing types
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Jul 5, 2017
1 parent 9b49769 commit 6576121
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 42 deletions.
67 changes: 41 additions & 26 deletions src/data/feature_index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import type CollisionTile from '../symbol/collision_tile';
import type TileCoord from '../source/tile_coord';
import type {PaintPropertyStatistics} from './program_configuration';
import type StyleLayer from '../style/style_layer';
import type {SerializedStructArray} from '../util/struct_array';

const FeatureIndexArray = createStructArrayType({
members: [
Expand Down Expand Up @@ -50,46 +51,60 @@ type QueryParameters = {
}
}

export type SerializedFeatureIndex = {
coord: TileCoord,
overscaling: number,
grid: ArrayBuffer,
featureIndexArray: SerializedStructArray,
bucketLayerIDs: Array<Array<string>>,
paintPropertyStatistics: PaintPropertyStatistics
}

class FeatureIndex {
grid: Grid;
featureIndexArray: FeatureIndexArray;
rawTileData: any;
bucketLayerIDs: Array<Array<string>>;
paintPropertyStatistics: PaintPropertyStatistics;
coord: TileCoord;
overscaling: number;
x: number;
y: number;
z: number;
grid: Grid;
featureIndexArray: FeatureIndexArray;

rawTileData: ArrayBuffer;
bucketLayerIDs: Array<Array<string>>;
paintPropertyStatistics: PaintPropertyStatistics;

collisionTile: CollisionTile;
vtLayers: any;
vtLayers: {[string]: VectorTileLayer};
sourceLayerCoder: DictionaryCoder;

constructor(coord: TileCoord, overscaling: number, collisionTile?: CollisionTile) {
if (coord.grid) {
const serialized = coord;
const rawTileData = overscaling;
coord = serialized.coord;
overscaling = serialized.overscaling;
this.grid = new Grid(serialized.grid);
this.featureIndexArray = new FeatureIndexArray(serialized.featureIndexArray);
this.rawTileData = rawTileData;
this.bucketLayerIDs = serialized.bucketLayerIDs;
this.paintPropertyStatistics = serialized.paintPropertyStatistics;
} else {
this.grid = new Grid(EXTENT, 16, 0);
this.featureIndexArray = new FeatureIndexArray();
}
static deserialize(serialized: SerializedFeatureIndex,
rawTileData: ArrayBuffer,
collisionTile: CollisionTile) {
const self = new FeatureIndex(
serialized.coord,
serialized.overscaling,
new Grid(serialized.grid),
new FeatureIndexArray(serialized.featureIndexArray));

self.rawTileData = rawTileData;
self.bucketLayerIDs = serialized.bucketLayerIDs;
self.paintPropertyStatistics = serialized.paintPropertyStatistics;
self.setCollisionTile(collisionTile);

return self;
}

constructor(coord: TileCoord,
overscaling: number,
grid?: Grid,
featureIndexArray?: FeatureIndexArray) {
this.coord = coord;
this.overscaling = overscaling;
this.x = coord.x;
this.y = coord.y;
this.z = coord.z - Math.log(overscaling) / Math.LN2;

if (collisionTile) {
this.setCollisionTile(collisionTile);
}
this.grid = grid || new Grid(EXTENT, 16, 0);
this.featureIndexArray = featureIndexArray || new FeatureIndexArray();
}

insert(feature: IndexedFeature, bucketIndex: number) {
Expand Down Expand Up @@ -117,7 +132,7 @@ class FeatureIndex {
this.collisionTile = collisionTile;
}

serialize(transferables?: Array<Transferable>) {
serialize(transferables?: Array<Transferable>): SerializedFeatureIndex {
const grid = this.grid.toArrayBuffer();
if (transferables) {
transferables.push(grid);
Expand Down
17 changes: 11 additions & 6 deletions src/source/source.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ exports.setType = function (name: string, type: any) {
import type {TileCoord} from './tile_coord';
import type {Actor} from '../util/actor';
import type {StyleLayerIndex} from '../style/style_layer_index';
import type {SerializedBucket} from '../data/bucket';
import type {SerializedFeatureIndex} from '../data/feature_index';
import type {SerializedCollisionTile} from '../symbol/collision_tile';
import type {SerializedStructArray} from '../util/struct_array';

export type TileParameters = {
source: string,
Expand All @@ -136,19 +140,20 @@ export type WorkerTileParameters = TileParameters & {
} & PlacementConfig;

export type WorkerTileResult = {
buckets: any,
featureIndex: any,
collisionTile: any,
rawTileData?: any,
buckets: Array<SerializedBucket>,
featureIndex: SerializedFeatureIndex,
collisionTile: SerializedCollisionTile,
collisionBoxArray: SerializedStructArray,
rawTileData?: ArrayBuffer,
};

export type WorkerTileCallback = (error: ?Error, result: ?WorkerTileResult, transferables: ?Array<Transferable>) => void;

export type RedoPlacementParameters = TileParameters & PlacementConfig;

export type RedoPlacementResult = {
buckets: any,
collisionTile: any
buckets: Array<SerializedBucket>,
collisionTile: SerializedCollisionTile
};

export type RedoPlacementCallback = (error: ?Error, result: ?RedoPlacementResult, transferables: ?Array<Transferable>) => void;
Expand Down
24 changes: 15 additions & 9 deletions src/source/tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@ const Throttler = require('../util/throttler');

const CLOCK_SKEW_RETRY_TIMEOUT = 30000;

import type TileCoord from './tile_coord';
import type {WorkerTileResult} from './source';

/**
* A tile object is the combination of a Coordinate, which defines
* its place, as well as a unique ID and data tracking for its content
*
* @private
*/
class Tile {
coord: any;
coord: TileCoord;
uid: number;
uses: number;
tileSize: number;
sourceMaxZoom: number;
buckets: any;
buckets: {[string]: Bucket};
expirationTime: any;
expiredRequestCount: number;
state: 'loading' // Tile data is in the process of loading.
Expand All @@ -38,10 +41,10 @@ class Tile {
placementThrottler: any;
timeAdded: any;
fadeEndTime: any;
rawTileData: any;
collisionBoxArray: any;
rawTileData: ArrayBuffer;
collisionBoxArray: ?CollisionBoxArray;
collisionTile: ?CollisionTile;
featureIndex: any;
featureIndex: ?FeatureIndex;
redoWhenDone: boolean;
angle: number;
pitch: number;
Expand Down Expand Up @@ -96,7 +99,7 @@ class Tile {
* @returns {undefined}
* @private
*/
loadVectorData(data: any, painter: any) {
loadVectorData(data: WorkerTileResult, painter: any) {
if (this.hasData()) {
this.unloadVectorData();
}
Expand All @@ -115,7 +118,7 @@ class Tile {

this.collisionBoxArray = new CollisionBoxArray(data.collisionBoxArray);
this.collisionTile = CollisionTile.deserialize(data.collisionTile, this.collisionBoxArray);
this.featureIndex = new FeatureIndex(data.featureIndex, this.rawTileData, this.collisionTile);
this.featureIndex = FeatureIndex.deserialize(data.featureIndex, this.rawTileData, this.collisionTile);
this.buckets = Bucket.deserialize(data.buckets, painter.style);
}

Expand All @@ -126,11 +129,14 @@ class Tile {
* @returns {undefined}
* @private
*/
reloadSymbolData(data: any, style: any) {
reloadSymbolData(data: WorkerTileResult, style: any) {
if (this.state === 'unloaded') return;

this.collisionTile = CollisionTile.deserialize(data.collisionTile, this.collisionBoxArray);
this.featureIndex.setCollisionTile(this.collisionTile);

if (this.featureIndex) {
this.featureIndex.setCollisionTile(this.collisionTile);
}

for (const id in this.buckets) {
const bucket = this.buckets[id];
Expand Down
2 changes: 1 addition & 1 deletion src/symbol/collision_tile.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const Grid = require('grid-index');

const intersectionTests = require('../util/intersection_tests');

type SerializedCollisionTile = {|
export type SerializedCollisionTile = {|
angle: number,
pitch: number,
cameraToCenterDistance: number,
Expand Down

0 comments on commit 6576121

Please sign in to comment.