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

79.45% Statements 58/73
73.77% Branches 45/61
54.54% Functions 6/11
77.94% Lines 53/68

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
    142
    143
    144
    145
    146
    147
    148
    149
    150
    2x

    2x
    2x

    2x

    2x

    2x

    2x
    2x
    10x
    2x
    2x

    2x
    2x

    2x
    2x

    2x

    2x
    2x
    35928x
    179640x
    35928x
    35928x
    1x

    35928x
    1x

    35928x
    35928x
    35928x

    35928x
    17986x

    17942x

    2x

    2x
    2x
    2x
    2x
    2x

    2x

    2x

    2x
    3x

    3x
    3x
    2x

    1x

    2x

    2x

    2x

    2x
    1x

    1x
    1x

    2x
     // Types
    import {
      FIELD_COLUMN,
      filterMetaColumns,
      GEO_HASH_COLUMN,
      TIME_COLUMN,
      VALUE_COLUMN,
    } from './tableProcessing'
    import {Table} from '../../../types'
    import {getLatLonMixin} from './mixins'
    import {CoordinateEncoding, GeoTable, Track} from './GeoTable'
    import {LatLonColumns} from '../../../types/geo'
    import {timestampToString} from '../../../utils/geo'

    interface GeoRow {
      [key: string]: number | string
    }

    export const isPivotSensible = table => {
      const fieldColumn = table.getColumn(FIELD_COLUMN, 'string')
      const valueColumn = table.getColumn(VALUE_COLUMN)
      return fieldColumn && valueColumn
    }

    export class PivotedGeoTable implements GeoTable {
      coordinateEncoding: CoordinateEncoding
      table: Table
      data: GeoRow[]
      truncated: boolean
      maxRows: number
      latLonColumns: LatLonColumns

      constructor(table: Table, maxRows, latLonColumns) {
        this.table = table
        const seriesKeyNames = filterMetaColumns(table.columnKeys)
        const seriesKeyColumns = seriesKeyNames.map(key => table.getColumn(key))
        const fieldColumn = table.getColumn(FIELD_COLUMN, 'string')
        const valueColumn = table.getColumn(VALUE_COLUMN)

        this.latLonColumns = latLonColumns
        Iif (!latLonColumns) {
          const latLonObj = {
            lat: {
              key: 'field',
              column: 'lat',
            },
            lon: {
              key: 'field',
              column: 'lon',
            },
          }
          this.latLonColumns = latLonObj
        }

        let lonFound = false,
          latFound = false
        // TODO: Palak: do we need this check here?
        // If user selects lat/lon as fields:
        // Should we move it out to UI and fail gracefully ?
        Iif (!fieldColumn || !valueColumn) {
          this.coordinateEncoding = CoordinateEncoding.GEO_HASH
          this.data = []
          return
        }
        const entriesCount = fieldColumn.length
        const mapData: {[seriesKey: string]: GeoRow} = {}
        for (let i = 0; i < entriesCount; i++) {
          const seriesKey = seriesKeyColumns.map(column => column[i]).join()
          const fieldName = fieldColumn[i]
          if (!lonFound && fieldName === this.latLonColumns?.lon?.column) {
            lonFound = true
          }
          if (!latFound && fieldName === this.latLonColumns?.lat?.column) {
            latFound = true
          }
          const point = mapData[seriesKey]
          const value = valueColumn[i]
          if (
            typeof value === 'number' ||
            (typeof value === 'string' && fieldName === GEO_HASH_COLUMN) // TODO: Palak: when s2 is provided as a field name
          ) {
            if (point) {
              point[fieldName] = value
            } else {
              mapData[seriesKey] = {
                [fieldName]: value,
              }
            }
          }
        }
        this.coordinateEncoding =
          lonFound && latFound
            ? CoordinateEncoding.FIELDS
            : CoordinateEncoding.GEO_HASH
        this.maxRows = maxRows
        const rows = Object.values(mapData)
        if (rows.length > maxRows) {
          this.data = rows.slice(0, maxRows)
          this.truncated = true
        } else E{
          this.data = rows
          this.truncated = false
        }
      }
      mapTracks<T, U>(
        _mapper: (track: Track, options: U, index: number) => T,
        _options: U
      ): T[] {
        return []
      }

      getRowCount(): number {
        return Math.min(this.data.length, this.maxRows)
      }

      getValue(index: number, field: string): number {
        Iif (
          field === this.latLonColumns?.lat?.column ||
          field === this.latLonColumns?.lon?.column
        ) {
          return this.data[index][field] as 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 {
        return this.data[index][GEO_HASH_COLUMN] as string
      }

      isTruncated(): boolean {
        return this.data.length <= this.maxRows
      }

      getLatLon = getLatLonMixin.bind(this)

      getTimeString(index: number): string {
        const timeValue = this.getValue(index, TIME_COLUMN)

        if (timeValue) {
          return timestampToString(timeValue)
        }
      }
    }