Skip to content

Commit

Permalink
replaced geojson-rbush with rbush
Browse files Browse the repository at this point in the history
  • Loading branch information
kpwebb committed May 21, 2019
1 parent d9b553e commit 6bf50d2
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 22 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,13 @@
"chalk": "^2.4.2",
"cli-progress": "^2.1.1",
"const": "^1.0.0",
"geojson-rbush": "^3.1.1",
"global-modules": "^2.0.0",
"jkstra": "^0.0.6",
"leveldown": "^5.0.0",
"levelup": "^4.0.1",
"node-fetch": "^2.3.0",
"osrm": "^5.22.0",
"rbush": "^3.0.0",
"sharedstreets-pbf": "^0.8.0",
"sharedstreets-types": "^1.3.1",
"simple-statistics": "^7.0.2",
Expand Down
6 changes: 6 additions & 0 deletions src/geom.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import destination from '@turf/destination';
import envelope from '@turf/envelope';
import * as turfHelpers from '@turf/helpers';
import bbox from '@turf/bbox';


export function envelopeBufferFromPoint(point, radius):turfHelpers.Feature<turfHelpers.Polygon> {
Expand All @@ -9,6 +10,11 @@ export function envelopeBufferFromPoint(point, radius):turfHelpers.Feature<turfH
return envelope(turfHelpers.featureCollection([nwPoint, sePoint]));
}

export function bboxFromPolygon(polygon) {
var bboxCoords = bbox(polygon)
return {"minX": bboxCoords[0], "minY": bboxCoords[1], "maxX":bboxCoords[2], "maxY":bboxCoords[3]}
}

function cleanProperties(og_props:{}) {
var new_props:{} = {};
for(var prop of Object.keys(og_props)) {
Expand Down
72 changes: 52 additions & 20 deletions src/tile_index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,15 @@ import lineSliceAlong from '@turf/line-slice-along';
import distance from '@turf/distance';
import lineOffset from '@turf/line-offset';

import geojsonRbush, { RBush } from 'geojson-rbush';
import RBush from 'rbush';

import { SharedStreetsIntersection, SharedStreetsGeometry, SharedStreetsReference, SharedStreetsMetadata } from 'sharedstreets-types';

import { lonlatsToCoords } from '../src/index';
import { TilePath, getTile, TileType, TilePathGroup, getTileIdsForPolygon, TilePathParams, getTileIdsForPoint } from './tiles';
import { Graph, ReferenceSideOfStreet } from './graph';
import { reverseLineString } from './geom';
import { reverseLineString, bboxFromPolygon } from './geom';
import { featureCollection } from '@turf/helpers';

const SHST_ID_API_URL = 'https://api.sharedstreets.io/v0.1.0/id/';

Expand Down Expand Up @@ -57,8 +58,8 @@ export class TileIndex {
osmWayIndex:Map<string, any>;
binIndex:Map<string, turfHelpers.Feature<turfHelpers.MultiPoint>>;

intersectionIndex:RBush<turfHelpers.Geometry, turfHelpers.Properties>;
geometryIndex:RBush<turfHelpers.Geometry, turfHelpers.Properties>;
intersectionIndex:RBush;
geometryIndex:RBush;

additionalTileTypes:TileType[] = [];

Expand All @@ -73,8 +74,8 @@ export class TileIndex {
this.osmWayIndex = new Map();
this.binIndex = new Map();

this.intersectionIndex = geojsonRbush(9);
this.geometryIndex = geojsonRbush(9);
this.intersectionIndex = new RBush(9);
this.geometryIndex = new RBush(9);
}

addTileType(tileType:TileType) {
Expand Down Expand Up @@ -111,8 +112,10 @@ export class TileIndex {
if(!this.objectIndex.has(geometry.id)) {
this.objectIndex.set(geometry.id, geometry);
var geometryFeature = createGeometry(geometry);
this.featureIndex.set(geometry.id, geometryFeature)
geometryFeatures.push(geometryFeature);
this.featureIndex.set(geometry.id, geometryFeature)
var bboxCoords = bboxFromPolygon(geometryFeature);
bboxCoords['id'] = geometry.id;
geometryFeatures.push(bboxCoords);
}
}
this.geometryIndex.load(geometryFeatures);
Expand All @@ -125,7 +128,9 @@ export class TileIndex {
var intesectionFeature = createIntersectionGeometry(intersection);
this.featureIndex.set(intersection.id, intesectionFeature);
this.osmNodeIntersectionIndex.set(intersection.nodeId, intersection);
intersectionFeatures.push(intesectionFeature);
var bboxCoords = bboxFromPolygon(intesectionFeature);
bboxCoords['id'] = intersection.id;
intersectionFeatures.push(bboxCoords);
}
}
this.intersectionIndex.load(intersectionFeatures);
Expand Down Expand Up @@ -188,12 +193,25 @@ export class TileIndex {

await this.indexTilesByPathGroup(tilePaths);

var data:turfHelpers.FeatureCollection<turfHelpers.Geometry>
var data:turfHelpers.FeatureCollection<turfHelpers.Geometry> = featureCollection([]);

if(searchType === TileType.GEOMETRY)
data = this.geometryIndex.search(polygon);
else if(searchType === TileType.INTERSECTION)
data = this.intersectionIndex.search(polygon);
if(searchType === TileType.GEOMETRY){
var bboxCoords = bboxFromPolygon(polygon);
var rbushMatches = this.geometryIndex.search(bboxCoords);

for(var rbushMatch of rbushMatches) {
var matchedGeom = this.featureIndex.get(rbushMatch.id);
data.features.push(matchedGeom);
}
}
else if(searchType === TileType.INTERSECTION) {
var bboxCoords = bboxFromPolygon(polygon);
var rbushMatches = this.intersectionIndex.search(bboxCoords);
for(var rbushMatch of rbushMatches) {
var matchedGeom = this.featureIndex.get(rbushMatch.id);
data.features.push(matchedGeom);
}
}

return data;
}
Expand All @@ -218,12 +236,26 @@ export class TileIndex {
await this.indexTilesByPathGroup(tilePaths);

var bufferedPoint:turfHelpers.Feature<turfHelpers.Polygon> = buffer(point, searchRadius, {'units':'meters'});
var data:turfHelpers.FeatureCollection<turfHelpers.Geometry>

if(searchType === TileType.GEOMETRY)
data = this.geometryIndex.search(bufferedPoint);
else if(searchType === TileType.INTERSECTION)
data = this.intersectionIndex.search(bufferedPoint);
var data:turfHelpers.FeatureCollection<turfHelpers.Geometry> = featureCollection([]);

if(searchType === TileType.GEOMETRY){
var bboxCoords = bboxFromPolygon(bufferedPoint);
var rbushMatches = this.geometryIndex.search(bboxCoords);

for(var rbushMatch of rbushMatches) {
var matchedGeom = this.featureIndex.get(rbushMatch.id);
data.features.push(matchedGeom);
}
}
else if(searchType === TileType.INTERSECTION) {
var bboxCoords = bboxFromPolygon(bufferedPoint);
var rbushMatches = this.intersectionIndex.search(bboxCoords);

for(var rbushMatch of rbushMatches) {
var matchedGeom = this.featureIndex.get(rbushMatch.id);
data.features.push(matchedGeom);
}
}

return data;

Expand Down
2 changes: 1 addition & 1 deletion test/geojson/points_1b.out.geojson
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"score":3.1799868991648736,"location":103.30809300066495,"referenceLength":133.47,"geometryId":"1d12bdab9cc895cf2ffee2f386a8fd41","referenceId":"4ef86ba3b7aedb47b4e6eb16cd082b8b","direction":"forward","bearing":90.30216185745682,"sideOfStreet":"right","interceptAngle":90.00001082771188},"geometry":{"type":"Point","coordinates":[-77.02842931440927,38.89981241618331]}},{"type":"Feature","properties":{"score":3.1799868991648736,"location":30.161906999335045,"referenceLength":133.47,"geometryId":"1d12bdab9cc895cf2ffee2f386a8fd41","referenceId":"1f621430356ba66efee4b83379a147b1","direction":"backward","bearing":270.30216185745684,"sideOfStreet":"left","interceptAngle":270.0000108277119},"geometry":{"type":"Point","coordinates":[-77.02842931440927,38.89981241618331]}},{"type":"Feature","properties":{"score":29.856311212715827,"location":162.7052754105483,"referenceLength":165.45,"geometryId":"0283824017780cbdf9febed3ec5a2450","referenceId":"0e7016db2d00589705b3af197c80040e","direction":"forward","bearing":359.99410728693755,"sideOfStreet":"left","interceptAngle":270.00101637940037},"geometry":{"type":"Point","coordinates":[-77.02808449641095,38.89978384167077]}},{"type":"Feature","properties":{"score":30.008594618988095,"location":0,"referenceLength":96.02,"geometryId":"960144327553e0f7487e8a4adf56e54c","referenceId":"11854de0a281c98d50e48d7745216aa6","direction":"forward","bearing":0,"sideOfStreet":"left","interceptAngle":264.21946709022404},"geometry":{"type":"Point","coordinates":[-77.0280845,38.899811]}},{"type":"Feature","properties":{"score":30.008594618988095,"location":0,"referenceLength":90.87,"geometryId":"32aabe41771a4a94aff41de3102debb8","referenceId":"7edae7bb25899c06ba232d2ae846dc92","direction":"forward","bearing":89.64389549319387,"sideOfStreet":"right","interceptAngle":174.57557159703018},"geometry":{"type":"Point","coordinates":[-77.0280845,38.899811]}},{"type":"Feature","properties":{"score":30.008594618988095,"location":90.87,"referenceLength":90.87,"geometryId":"32aabe41771a4a94aff41de3102debb8","referenceId":"e2ff5fedf35bd749f1db4e301f9a56b5","direction":"backward","bearing":269.6438954931939,"sideOfStreet":"left","interceptAngle":354.57557159703015},"geometry":{"type":"Point","coordinates":[-77.0280845,38.899811]}},{"type":"Feature","properties":{"score":77.055412546651,"location":78.33969936780356,"referenceLength":146.19,"geometryId":"453c30aa4cd2b9e2a65704ea94ad6798","referenceId":"34139d9c4d72d540f6ccc48d1633d202","direction":"forward","bearing":66.08274158025435,"sideOfStreet":"right","interceptAngle":89.99964467828553},"geometry":{"type":"Point","coordinates":[-77.02879050964661,38.90041728767828]}},{"type":"Feature","properties":{"score":91.98319618346457,"location":72.39665429706584,"referenceLength":145.52,"geometryId":"e373958592276406c70acc59e4d1b171","referenceId":"c3c584c377f0f635235c851688a91f7b","direction":"forward","bearing":246.67620786578303,"sideOfStreet":"left","interceptAngle":269.999633039488},"geometry":{"type":"Point","coordinates":[-77.02885035857211,38.9005434415667]}},{"type":"Feature","properties":{"score":103.33383932646024,"location":165.09727879687583,"referenceLength":167.01,"geometryId":"d047de8f7d38bb0efc54b34494a06aa0","referenceId":"b878500e619bf881c6991ea753f829b5","direction":"forward","bearing":0.8512041084756191,"sideOfStreet":"right","interceptAngle":89.9992466923876},"geometry":{"type":"Point","coordinates":[-77.02962347575352,38.899797617672725]}},{"type":"Feature","properties":{"score":103.33383932646024,"location":1.912721203124164,"referenceLength":167.01,"geometryId":"d047de8f7d38bb0efc54b34494a06aa0","referenceId":"b29c667e5cbfdcda80b7bfe4fcaa8bb1","direction":"backward","bearing":180.85120410847563,"sideOfStreet":"left","interceptAngle":269.9992466923876},"geometry":{"type":"Point","coordinates":[-77.02962347575352,38.899797617672725]}}]}
{"type":"FeatureCollection","features":[{"type":"Feature","properties":{"score":3.1799868991648736,"location":103.30809300066495,"referenceLength":133.47,"geometryId":"1d12bdab9cc895cf2ffee2f386a8fd41","referenceId":"4ef86ba3b7aedb47b4e6eb16cd082b8b","direction":"forward","bearing":90.30216185745682,"sideOfStreet":"right","interceptAngle":90.00001082771188},"geometry":{"type":"Point","coordinates":[-77.02842931440927,38.89981241618331]}},{"type":"Feature","properties":{"score":3.1799868991648736,"location":30.161906999335045,"referenceLength":133.47,"geometryId":"1d12bdab9cc895cf2ffee2f386a8fd41","referenceId":"1f621430356ba66efee4b83379a147b1","direction":"backward","bearing":270.30216185745684,"sideOfStreet":"left","interceptAngle":270.0000108277119},"geometry":{"type":"Point","coordinates":[-77.02842931440927,38.89981241618331]}},{"type":"Feature","properties":{"score":29.856311212715827,"location":162.7052754105483,"referenceLength":165.45,"geometryId":"0283824017780cbdf9febed3ec5a2450","referenceId":"0e7016db2d00589705b3af197c80040e","direction":"forward","bearing":359.99410728693755,"sideOfStreet":"left","interceptAngle":270.00101637940037},"geometry":{"type":"Point","coordinates":[-77.02808449641095,38.89978384167077]}},{"type":"Feature","properties":{"score":30.008594618988095,"location":0,"referenceLength":90.87,"geometryId":"32aabe41771a4a94aff41de3102debb8","referenceId":"7edae7bb25899c06ba232d2ae846dc92","direction":"forward","bearing":89.64389549319387,"sideOfStreet":"right","interceptAngle":174.57557159703018},"geometry":{"type":"Point","coordinates":[-77.0280845,38.899811]}},{"type":"Feature","properties":{"score":30.008594618988095,"location":90.87,"referenceLength":90.87,"geometryId":"32aabe41771a4a94aff41de3102debb8","referenceId":"e2ff5fedf35bd749f1db4e301f9a56b5","direction":"backward","bearing":269.6438954931939,"sideOfStreet":"left","interceptAngle":354.57557159703015},"geometry":{"type":"Point","coordinates":[-77.0280845,38.899811]}},{"type":"Feature","properties":{"score":30.008594618988095,"location":0,"referenceLength":96.02,"geometryId":"960144327553e0f7487e8a4adf56e54c","referenceId":"11854de0a281c98d50e48d7745216aa6","direction":"forward","bearing":0,"sideOfStreet":"left","interceptAngle":264.21946709022404},"geometry":{"type":"Point","coordinates":[-77.0280845,38.899811]}},{"type":"Feature","properties":{"score":77.055412546651,"location":78.33969936780356,"referenceLength":146.19,"geometryId":"453c30aa4cd2b9e2a65704ea94ad6798","referenceId":"34139d9c4d72d540f6ccc48d1633d202","direction":"forward","bearing":66.08274158025435,"sideOfStreet":"right","interceptAngle":89.99964467828553},"geometry":{"type":"Point","coordinates":[-77.02879050964661,38.90041728767828]}},{"type":"Feature","properties":{"score":91.98319618346457,"location":72.39665429706584,"referenceLength":145.52,"geometryId":"e373958592276406c70acc59e4d1b171","referenceId":"c3c584c377f0f635235c851688a91f7b","direction":"forward","bearing":246.67620786578303,"sideOfStreet":"left","interceptAngle":269.999633039488},"geometry":{"type":"Point","coordinates":[-77.02885035857211,38.9005434415667]}},{"type":"Feature","properties":{"score":103.33383932646024,"location":165.09727879687583,"referenceLength":167.01,"geometryId":"d047de8f7d38bb0efc54b34494a06aa0","referenceId":"b878500e619bf881c6991ea753f829b5","direction":"forward","bearing":0.8512041084756191,"sideOfStreet":"right","interceptAngle":89.9992466923876},"geometry":{"type":"Point","coordinates":[-77.02962347575352,38.899797617672725]}},{"type":"Feature","properties":{"score":103.33383932646024,"location":1.912721203124164,"referenceLength":167.01,"geometryId":"d047de8f7d38bb0efc54b34494a06aa0","referenceId":"b29c667e5cbfdcda80b7bfe4fcaa8bb1","direction":"backward","bearing":180.85120410847563,"sideOfStreet":"left","interceptAngle":269.9992466923876},"geometry":{"type":"Point","coordinates":[-77.02962347575352,38.899797617672725]}}]}

0 comments on commit 6bf50d2

Please sign in to comment.