# All files / components GeoLayer.tsx

15.52% Statements 9/58
0% Branches 0/23
0% Functions 0/9
17.31% Lines 9/52

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
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    1x
    1x

    1x

    1x

    1x

    1x

    1x

    1x

    1x
     // Libraries
    import React, {FunctionComponent} from 'react'
    import {AutoSizer} from 'react-virtualized'

    // Components
    import Geo from './Geo'

    // Types
    import {Config, Table} from '../types'
    import {GeoLayerConfig, GeoQueryVariables} from '../types/geo'

    interface OwnProps {
      table: Table
      config: GeoLayerConfig
      plotConfig: Config
    }

    const calculateVariableAssignment = (
      width: number,
      height: number,
      lon: number,
      lat: number,
      zoom: number
    ): GeoQueryVariables => {
      const pixelRadius = Math.sqrt(width * width + height * height) / 2
      // circumference of earth = 40075016.686m
      const metersPerPixel =
        (40075016.686 * Math.abs(Math.cos((lat * Math.PI) / 180))) /
        Math.pow(2, zoom + 8)
      return {lon, lat, radius: (pixelRadius * metersPerPixel) / 1000}
    }

    interface LastRenderProperties {
      latOnLastRender?: number
      lonOnLastRender?: number
      zoomOnLastRender?: number
      widthOnLastRender?: number
      heightOnLastRender?: number
    }

    const onViewportChange = (
      props: OwnProps,
      lastRenderProperties: LastRenderProperties
    ) => (lat: number, lon: number, zoom: number) => {
      const {config} = props
      const {allowPanAndZoom, onUpdateViewport} = config
      if (allowPanAndZoom && onUpdateViewport) {
        onUpdateViewport(lat, lon, zoom)
      }
      const {widthOnLastRender, heightOnLastRender} = lastRenderProperties
      updateQuery(
        props,
        lastRenderProperties,
        widthOnLastRender,
        heightOnLastRender,
        lat,
        lon,
        zoom
      )
    }

    const updateQuery = (
      props: OwnProps,
      lastRenderProperties: LastRenderProperties,
      width: number,
      height: number,
      lat: number,
      lon: number,
      zoom: number
    ) => {
      const {onUpdateQuery = () => {}} = props.config
      const {
        widthOnLastRender,
        heightOnLastRender,
        latOnLastRender,
        lonOnLastRender,
        zoomOnLastRender,
      } = lastRenderProperties
      if (
        width &&
        height &&
        (widthOnLastRender !== width ||
          heightOnLastRender !== height ||
          latOnLastRender !== lat ||
          lonOnLastRender !== lon ||
          zoomOnLastRender !== zoom)
      ) {
        const variableAssignment = calculateVariableAssignment(
          width,
          height,
          lon,
          lat,
          zoom
        )
        onUpdateQuery(variableAssignment)
        lastRenderProperties.latOnLastRender = lat
        lastRenderProperties.lonOnLastRender = lon
        lastRenderProperties.zoomOnLastRender = zoom
        lastRenderProperties.widthOnLastRender = width
        lastRenderProperties.heightOnLastRender = height
      }
    }

    const onAutoResize = (
      props: OwnProps,
      lastRenderProperties: LastRenderProperties,
      width,
      height
    ) => {
      const {config, plotConfig, table} = props
      const {
        layers,
        lat,
        lon,
        zoom,
        allowPanAndZoom,
        detectCoordinateFields,
        mapStyle,
      } = config
      const {
        latOnLastRender,
        lonOnLastRender,
        zoomOnLastRender,
      } = lastRenderProperties

      updateQuery(
        props,
        lastRenderProperties,
        width,
        height,
        latOnLastRender === null ? lat : latOnLastRender,
        lonOnLastRender === null ? lon : lonOnLastRender,
        zoomOnLastRender === null ? zoom : zoomOnLastRender
      )

      return (
        <div className="geo">
          <Geo
            width={width}
            height={height}
            table={table}
            lat={lat}
            lon={lon}
            zoom={zoom}
            mapStyle={mapStyle}
            detectCoordinateFields={detectCoordinateFields}
            layers={layers}
            stylingConfig={plotConfig}
            onViewportChange={onViewportChange(props, lastRenderProperties)}
            allowPanAndZoom={allowPanAndZoom}
            tileServerConfiguration={config.tileServerConfiguration}
          />
        </div>
      )
    }

    const GeoLayer: FunctionComponent<OwnProps> = React.memo(props => {
      if (props.config.tileServerConfiguration) {
        return (
          <AutoSizer>
            {(() => {
              const lastRenderProperties: LastRenderProperties = {}
              return ({width, height}) =>
                onAutoResize(props, lastRenderProperties, width, height)
            })()}
          </AutoSizer>
        )
      }
      return null
    })

    export default GeoLayer