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

41.51% Statements 22/53
41.38% Branches 12/29
37.5% Functions 3/8
42% Lines 21/50

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
    1x

    1x

    1x

    1x

    2x
    2x
    2x
    2x

    1x

    1x

    1x

    1x

    1x

    2x

    2x
    1x

    1x

    2x

    2x
    1x

    1x

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

    // Constants
    import {
      GEO_HASH_COLUMN,
      LAT_COLUMN,
      LON_COLUMN,
      TABLE_COLUMN,
    } from './tableProcessing'
    import {getLatLonMixin, getTimeStringMixin} from './mixins'
    import {LatLonColumns} from '../../../types/geo'

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

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

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

      getValue(index: number, field: string): number {
        const column = this.table.getColumn(field)
        if (!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(GEO_HASH_COLUMN)
        if (!column) {
          return null
        }
        const value = column[index]
        if (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 = getTimeStringMixin.bind(this)
    }

    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
    }