import { lineString } from "@turf/helpers" ; import length from "@turf/length" ; import * as fs from "fs" ; import * as glob from "glob" ; import * as path from "path" ; import * as sharedstreetsPbf from "sharedstreets-pbf" ; import * as sharedstreets from "./src/index" ; import * as turfHelpers from '@turf/helpers' ; import { TileIndex } from './src/index' ; import { TilePathGroup , TileType , TilePathParams , } from './src/index' ; import { CleanedPoints , CleanedLines } from "./src/geom" ; import { Graph , GraphMode } from "./src/index" ; import envelope from "@turf/envelope" ; const test = require ( 'tape' ) ; const BUILD_TEST_OUPUT = false ; test ( "match points" , async ( t ) => { const content = fs . readFileSync ( 'test/geojson/points_1.in.geojson' ) ; var pointsIn = JSON . parse ( content . toLocaleString ( ) ) ; var cleanedPoints = new CleanedPoints ( pointsIn ) ; var points = turfHelpers . featureCollection ( cleanedPoints . clean ) ; var params = new TilePathParams ( ) ; params . source = 'osm/planet-180430' ; params . tileHierarchy = 6 ; var matcher = new Graph ( null , params ) ; var matchedPoints = [ ] ; for ( let searchPoint of points . features ) { let matches = await matcher . matchPoint ( searchPoint , null , 3 ) ; for ( let match of matches ) { matchedPoints . push ( match . toFeature ( ) ) ; } } const matchedPointFeatureCollection_1a = turfHelpers . featureCollection ( matchedPoints ) ; const expected_1a_file = 'test/geojson/points_1a.out.geojson' ; if ( BUILD_TEST_OUPUT ) { var expected_1a_out = JSON . stringify ( matchedPointFeatureCollection_1a ) ; fs . writeFileSync ( expected_1a_file , expected_1a_out ) ; } const expected_1a_in = fs . readFileSync ( expected_1a_file ) ; const expected_1a = JSON . parse ( expected_1a_in . toLocaleString ( ) ) ; t . deepEqual ( expected_1a , matchedPointFeatureCollection_1a ) ; matcher . searchRadius = 1000 ; var matchedPoints = [ ] ; let matches = await matcher . matchPoint ( points . features [ 0 ] , null , 10 ) ; for ( let match of matches ) { matchedPoints . push ( match . toFeature ( ) ) ; } const matchedPointFeatureCollection_1b = turfHelpers . featureCollection ( matchedPoints ) ; const expected_1b_file = 'test/geojson/points_1b.out.geojson' ; if ( BUILD_TEST_OUPUT ) { var expected_1b_out = JSON . stringify ( matchedPointFeatureCollection_1b ) ; fs . writeFileSync ( expected_1b_file , expected_1b_out ) ; } const expected_1b_in = fs . readFileSync ( expected_1b_file ) ; const expected_1b = JSON . parse ( expected_1b_in . toLocaleString ( ) ) ; t . deepEqual ( expected_1b , matchedPointFeatureCollection_1b ) ; t . end ( ) ; } ) ; test ( "match lines 1" , async ( t ) => { const content = fs . readFileSync ( 'test/geojson/sf_centerlines.sample.geojson' ) ; var linesIn = JSON . parse ( content . toLocaleString ( ) ) ; var cleanedLines = new CleanedLines ( linesIn ) ; var lines = turfHelpers . featureCollection ( cleanedLines . clean ) ; var params = new TilePathParams ( ) ; params . source = 'osm/planet-180430' ; params . tileHierarchy = 6 ; var matcher = new Graph ( envelope ( lines ) , params ) ; await matcher . buildGraph ( ) ; var matchedLines = turfHelpers . featureCollection ( [ ] ) ; for ( var line of lines . features ) { var pathCandidate = await matcher . matchGeom ( line ) ; matchedLines . features . push ( pathCandidate . matchedPath ) ; } const expected_1a_file = 'test/geojson/sf_centerlines.sample.out.geojson' ; if ( BUILD_TEST_OUPUT ) { var expected_1a_out = JSON . stringify ( matchedLines ) ; fs . writeFileSync ( expected_1a_file , expected_1a_out ) ; } const expected_1a_in = fs . readFileSync ( expected_1a_file ) ; const expected_1a = JSON . parse ( expected_1a_in . toLocaleString ( ) ) ; t . deepEqual ( matchedLines , expected_1a ) ; t . end ( ) ; } ) ; test ( "match lines 2 -- snapping and directed edges" , async ( t ) => { const content = fs . readFileSync ( 'test/geojson/line-directed-test.in.geojson' ) ; var linesIn = JSON . parse ( content . toLocaleString ( ) ) ; var cleanedLines = new CleanedLines ( linesIn ) ; var lines = turfHelpers . featureCollection ( cleanedLines . clean ) ; var params = new TilePathParams ( ) ; params . source = 'osm/planet-180430' ; params . tileHierarchy = 6 ; var matcher = new Graph ( envelope ( lines ) , params ) ; await matcher . buildGraph ( ) ; var matchedLines = turfHelpers . featureCollection ( [ ] ) ; for ( var line of lines . features ) { var pathCandidate = await matcher . matchGeom ( line ) ; matchedLines . features . push ( pathCandidate . matchedPath ) ; } const expected_1a_file = 'test/geojson/line-directed-test-snapped.out.geojson' ; if ( BUILD_TEST_OUPUT ) { var expected_1a_out = JSON . stringify ( matchedLines ) ; fs . writeFileSync ( expected_1a_file , expected_1a_out ) ; } const expected_1a_in = fs . readFileSync ( expected_1a_file ) ; const expected_1a = JSON . parse ( expected_1a_in . toLocaleString ( ) ) ; t . deepEqual ( matchedLines , expected_1a ) ; matcher . snapIntersections = false ; var matchedLines = turfHelpers . featureCollection ( [ ] ) ; for ( var line of lines . features ) { var pathCandidate = await matcher . matchGeom ( line ) ; matchedLines . features . push ( pathCandidate . matchedPath ) ; } const expected_1b_file = 'test/geojson/line-directed-test-unsnapped.out.geojson' ; if ( BUILD_TEST_OUPUT ) { var expected_1b_out = JSON . stringify ( matchedLines ) ; fs . writeFileSync ( expected_1b_file , expected_1b_out ) ; } const expected_1b_in = fs . readFileSync ( expected_1b_file ) ; const expected_1b = JSON . parse ( expected_1b_in . toLocaleString ( ) ) ; t . deepEqual ( matchedLines , expected_1b ) ; t . end ( ) ; } ) ;