import * as probuf_minimal from "protobufjs/minimal" ; const linearProto = require ( './proto/linear.js' ) ; var fs = require ( 'fs' ) ; import * as turfHelpers from '@turf/helpers' ; import lineOffset from "@turf/line-offset" ; import turfBbox from '@turf/bbox' ; import turfBboxPolygon from '@turf/bbox-polygon' ; import { quantileRankSorted } from "simple-statistics" ; const tileHierarchy = 6 ; const tileSource = 'osm' ; const tileBuild = 'planet-180430' ; export function getBinCountFromLength ( referenceLength , binSize ) { var numBins = Math . floor ( referenceLength / binSize ) + 1 ; return numBins ; } export function getBinLength ( referenceLength , binSize ) { return referenceLength / getBinCountFromLength ( referenceLength , binSize ) ; } export function getBinPositionFromLocation ( referenceLength , binSize , location ) { var bin = Math . floor ( location / getBinLength ( referenceLength , binSize ) ) + 1 ; return bin ; } export function generateBinId ( referenceId , binCount , binPosition ) { var binId = referenceId + "{" + binCount ; if ( binPosition ) binId = binId + ":" + binPosition ; return binId ; } export enum PeriodSize { OneSecond = 0 , FiveSeconds = 1 , TenSeconds = 2 , FifteenSeconds = 3 , ThirtySeconds = 4 , OneMinute = 5 , FiveMinutes = 6 , TenMinutes = 7 , FifteenMinutes = 8 , ThirtyMinutes = 9 , OneHour = 10 , OneDay = 11 , OneWeek = 12 , OneMonth = 13 , OneYear = 14 } class SharedStreetsBin { type : string ; count : number ; value : number ; } class SharedStreetsLinearBins { referenceId : string ; referenceLength : number ; numberOfBins : number ; bins : { } ; constructor ( referenceId , referenceLength , numberOfBins ) { this . referenceId = referenceId ; this . referenceLength = referenceLength ; this . numberOfBins = numberOfBins ; this . bins = { } ; } getId ( ) { return generateBinId ( this . referenceId , this . numberOfBins , null ) ; } addBin ( binPosition , type , count , value ) { var bin = new SharedStreetsBin ( ) ; bin . type = type ; bin . count = count ; bin . value = value ; this . bins [ binPosition ] = bin ; } } export class WeeklySharedStreetsLinearBins extends SharedStreetsLinearBins { periodSize : PeriodSize ; constructor ( referenceId , referenceLength , numberOfBins , periodSize ) { super ( referenceId , referenceLength , numberOfBins ) ; this . periodSize = periodSize ; } addPeriodBin ( binPosition , period , type , count , value ) { var bin = new SharedStreetsBin ( ) ; bin . type = type ; bin . count = count ; bin . value = value ; if ( ! this . bins [ binPosition ] ) { this . bins [ binPosition ] = { } ; } this . bins [ binPosition ] [ period ] = bin ; } getFilteredBins ( binPosition , typeFilter , periodFilter ) { var filteredBins = [ ] ; if ( this . bins [ binPosition ] ) { for ( var period of Object . keys ( this . bins [ binPosition ] ) ) { if ( periodFilter ) { if ( parseInt ( period ) < periodFilter [ 0 ] || parseInt ( period ) > periodFilter [ 1 ] ) continue ; } if ( typeFilter ) { if ( typeFilter !== this . bins [ binPosition ] [ period ] . type ) continue ; } filteredBins . push ( this . bins [ binPosition ] [ period ] ) ; } } return filteredBins ; } getHourOfDaySummary ( typeFilter ) { var filteredBins = new Map < number , SharedStreetsBin [ ] > ( ) ; for ( var binPosition of Object . keys ( this . bins ) ) { for ( var period of Object . keys ( this . bins [ binPosition ] ) ) { var hourOfDay = ( parseInt ( period ) % 23 ) ; if ( hourOfDay > 23 ) hourOfDay = hourOfDay - 23 ; if ( hourOfDay <= 0 ) hourOfDay = hourOfDay + 23 ; if ( typeFilter ) { if ( typeFilter !== this . bins [ binPosition ] [ period ] . type ) continue ; } if ( ! filteredBins [ hourOfDay ] ) filteredBins [ hourOfDay ] = [ ] ; filteredBins [ hourOfDay ] . push ( this . bins [ binPosition ] [ period ] ) ; } } return filteredBins ; } getValueForBin ( binPosition , typeFilter , periodFilter ) { var sum = 0 ; var filteredBins = this . getFilteredBins ( binPosition , typeFilter , periodFilter ) ; for ( var bin of filteredBins ) { sum = sum + bin . value ; } return sum ; } getCountForHoursOfDay ( typeFilter ) { var summary = this . getHourOfDaySummary ( typeFilter ) ; var hourOfDayCount = { } ; for ( var hourOfDay of Object . keys ( summary ) ) { hourOfDayCount [ hourOfDay ] = 0 ; for ( var bin of summary [ hourOfDay ] ) { hourOfDayCount [ hourOfDay ] = hourOfDayCount [ hourOfDay ] + bin . count ; } } return hourOfDayCount ; } getCountForBin ( binPosition , typeFilter , periodFilter ) { var sum = 0 ; var filteredBins = this . getFilteredBins ( binPosition , typeFilter , periodFilter ) ; for ( var bin of filteredBins ) { sum = sum + bin . count ; } return sum ; } getCountForEdge ( typeFilter , periodFilter ) { var sum = 0 ; for ( var binPosition = 0 ; binPosition < this . numberOfBins ; binPosition ++ ) { var filteredBins = this . getFilteredBins ( binPosition , typeFilter , periodFilter ) ; for ( var bin of filteredBins ) { sum = sum + bin . count ; } } return sum ; } } function processTile ( reader ) { var tileData = [ ] ; while ( reader . pos < reader . len ) { try { var result = linearProto . SharedStreetsWeeklyBinnedLinearReferences . decodeDelimited ( reader ) . toJSON ( ) ; var linearBins = new WeeklySharedStreetsLinearBins ( result . referenceId , result . referenceLength , result . numberOfBins , PeriodSize . OneHour ) ; for ( var i = 0 ; i < result . binPosition . length ; i ++ ) { var binPosition = result . binPosition [ i ] ; for ( var j = 0 ; j < result . binnedPeriodicData [ i ] . bins . length ; j ++ ) { var period = result . binnedPeriodicData [ i ] . periodOffset [ j ] ; var bin = result . binnedPeriodicData [ i ] . bins [ j ] ; for ( var h in bin . dataType ) { linearBins . addPeriodBin ( binPosition , period , bin . dataType [ h ] , parseInt ( bin . count [ h ] ) , parseInt ( bin . value [ h ] ) ) ; } } } tileData . push ( linearBins ) ; } catch ( e ) { console . log ( e ) ; } } return tileData ; } class BinReferenceData { geometry ; refLength ; }