# All files / components/geo/processing NativeGeoTable.ts

46.15% Statements 30/65
33.33% Branches 11/33
55.55% Functions 5/9
46.77% Lines 29/62

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    2x

    2x

    2x

    2x

    2x

    3x
    3x
    3x
    3x
    3x

    2x

    2x
    2x
    2x

    2x

    2x

    2x

    2x

    3x

    2x
    1x
    1x
    1x

    2x

    2x

    3x

    3x
    2x

    1x

     // Types
    import {CoordinateEncoding} from './GeoTable'
    import {Table} from '../../../types'
    import {GeoTable, Track} from './GeoTable'

    // Constants
    import {
      GEO_HASH_COLUMN,
      LAT_COLUMN,
      LON_COLUMN,
      START_COLUMN,
      STOP_COLUMN,
      TABLE_COLUMN,
      TIME_COLUMN,
    } from './tableProcessing'
    import {getLatLonMixin} from './mixins'
    import {LatLonColumns} from '../../../types/geo'
    import {timestampToString} from '../../../utils/geo'

    export class NativeGeoTable implements GeoTable {
      coordinateEncoding: CoordinateEncoding
      table: Table
      maxRows: number
      s2Column: string
      latLonColumns: LatLonColumns

      constructor(
        table: Table,
        maxRows: number,
        latLonColumns: LatLonColumns,
        s2Column: string
      ) {
        this.coordinateEncoding = getDataEncoding(table, latLonColumns, s2Column)
        this.table = table
        this.maxRows = maxRows
        this.s2Column = s2Column
        this.latLonColumns = latLonColumns
      }

      getRowCount() {
        return Math.min(this.table.length, this.maxRows)
      }

      getValue(index: number, field: string): number {
        const column = this.table.getColumn(field)
        Iif (!column) {
          return null
        }
        return column[
          index * Math.max(1, Math.floor(this.table.length / this.maxRows))
        ] as number
      }

      getS2CellID(index: number): string {
        const column = this.table.getColumn(this.s2Column || GEO_HASH_COLUMN)
        Iif (!column) {
          return null
        }
        const value = column[index]
        Iif (typeof value !== 'string') {
          return null
        }
        // this happens for geo hash only
        return value as string
      }

      isTruncated(): boolean {
        return this.table.length > this.maxRows
      }

      mapTracks<T, U>(
        mapper: (track: Track, options: U, index: number) => T,
        options: U
      ): T[] {
        const {table, maxRows} = this
        const tableColumn = table.getColumn(TABLE_COLUMN)
        const lonColumn = table.getColumn(LON_COLUMN, 'number')
        const latColumn = table.getColumn(LAT_COLUMN, 'number')
        // TODO: Palak: remove the hardcoded columns for tracks

        const result = []
        let track: Track = []
        let index = 0
        let trackNumber = 0
        while (index < table.length && index < maxRows) {
          do {
            track.push([latColumn[index], lonColumn[index]])
            index++
          } while (
            index < maxRows &&
            index < table.length &&
            (!tableColumn ||
              tableColumn.length === index + 1 ||
              tableColumn[index - 1] === tableColumn[index])
          )
          result.push(mapper(track, options, trackNumber++))
          track = []
        }
        return result
      }

      getLatLon = getLatLonMixin.bind(this)

      getTimeString(index: number): string {
        const timeValue = this.getValue(index, TIME_COLUMN)
        if (timeValue) {
          return timestampToString(timeValue)
        }
        const startValue = this.getValue(index, START_COLUMN)
        const stopValue = this.getValue(index, STOP_COLUMN)
        Iif (startValue && stopValue) {
          return `${timestampToString(startValue)} - ${timestampToString(
            stopValue
          )}`
        }
        const value = startValue || stopValue
        Iif (value) {
          return timestampToString(value)
        }
      }
    }

    const getDataEncoding = (
      table: Table,
      latLonColumns: LatLonColumns,
      s2Column: string
    ): CoordinateEncoding => {
      Iif (
        typeof latLonColumns?.lat !== 'undefined' &&
        typeof latLonColumns?.lon !== 'undefined' &&
        table.getColumn(latLonColumns.lat.column) !== null &&
        table.getColumn(latLonColumns.lon.column) !== null
      ) {
        return CoordinateEncoding.FIELDS
      }
      if (table.getColumn(s2Column) !== null) {
        return CoordinateEncoding.GEO_HASH
      }
      return CoordinateEncoding.GEO_HASH
    }