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

76.19% Statements 48/63
71.11% Branches 32/45
40% Functions 4/10
74.14% Lines 43/58

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

    1x
    1x

    1x

    1x

    1x
    5x
    1x
    1x

    1x
    1x

    1x
    1x

    1x

    1x
    1x
    17964x
    89820x
    17964x
    17964x
    1x

    17964x
    1x

    17964x
    17964x
    17964x

    17964x
    8993x

    8971x

    1x

    1x
    1x
    1x
    1x
    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x
    1x
     // Types
    import {
      FIELD_COLUMN,
      filterMetaColumns,
      GEO_HASH_COLUMN,
      VALUE_COLUMN,
    } from './tableProcessing'
    import {Table} from '../../../types'
    import {getLatLonMixin, getTimeStringMixin} from './mixins'
    import {CoordinateEncoding, GeoTable, Track} from './GeoTable'
    import {LatLonColumns} from '../../../types/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
      data: GeoRow[]
      truncated: boolean
      maxRows: number
      latLonColumns: LatLonColumns

      constructor(table: Table, maxRows, latLonColumns) {
        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]
          Eif (
            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)
        Eif (rows.length > maxRows) {
          this.data = rows.slice(0, maxRows)
          this.truncated = true
        } else {
          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 {
        return this.data[index][field] 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 = getTimeStringMixin.bind(this)
    }