All files / lib/generate-renderer index.js

100% Statements 43/43
100% Branches 17/17
100% Functions 13/13
100% Lines 42/42

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 1246x 6x 6x 6x 6x   6x     6x 6x 6x   6x 2x     4x 3x     1x       2x 2x 8x   2x   2x 2x 4x   2x       3x       3x 3x   2x   2x 2x 4x     2x 1x       1x       3x                   3x 9x                     1x 1x                                         1x 3x   3x                     3x 3x    
const Winnow = require('winnow')
const { getGeometryTypeFromGeojson } = require('../helpers')
const validateClassificationDefinition = require('./validate-classification-definition')
const { createColorRamp } = require('./color-ramp')
const { createSymbol } = require('./create-symbol')
 
module.exports = generateRenderer
 
function generateRenderer (data = {}, options = {}) {
  const { statistics = {}, features } = data
  const geometryType = getGeometryTypeFromGeojson(data)
  const { classificationDef = {} } = options
 
  if (statistics.classBreaks) {
    return generateRendererFromPrecalculatedStatistics(statistics, { classificationDef, geometryType })
  }
 
  if (features) {
    return generateRendererFromFeatures(data, { ...options, geometryType })
  }
 
  return {}
}
 
function generateRendererFromPrecalculatedStatistics (statistics, options) {
  const { classificationDef, geometryType = 'esriGeometryPoint' } = options
  const { colorRamp: colorRampConfig = {}, baseSymbol } = classificationDef
  const classification = statistics.classBreaks.sort((a, b) => a[0] - b[0])
 
  validateClassificationDefinition(classificationDef, geometryType, classification)
 
  const colorRamp = createColorRamp({ classification, ...colorRampConfig })
  const symbolCollection = colorRamp.map((color) => {
    return createSymbol(baseSymbol, color, geometryType)
  })
  return renderClassBreaks(classification, classificationDef, symbolCollection)
}
 
function generateRendererFromFeatures (data, params) {
  const { classificationDef, geometryType } = params
 
  // TODO: this seems weird; the winnow method is "query" but it's really a very specialized transform (aggregation)
  // consider changes to winnow - this should maybe be a different method
  const classification = Winnow.query(data, params)
  validateClassificationDefinition(classificationDef, geometryType, classification)
 
  const { colorRamp: colorRampConfig = {}, baseSymbol } = classificationDef
 
  const colorRamp = createColorRamp({ classification, ...colorRampConfig })
  const symbolCollection = colorRamp.map((color) => {
    return createSymbol(baseSymbol, color, geometryType)
  })
 
  if (classificationDef.type === 'classBreaksDef') {
    return renderClassBreaks(classification, classificationDef, symbolCollection)
  }
 
  // if not 'classBreaksDef', then its unique-values
  return renderUniqueValue(classification, classificationDef, symbolCollection)
}
 
function renderClassBreaks (breaks, classificationDef, symbolCollection) {
  return {
    type: 'classBreaks',
    field: classificationDef.classificationField || '',
    classificationMethod: classificationDef.classificationMethod || '',
    minValue: breaks[0][0],
    classBreakInfos: createClassBreakInfos(breaks, symbolCollection)
  }
}
 
function createClassBreakInfos (breaks, symbolCollection) {
  return breaks.map((classBreak, index) => {
    return {
      classMinValue: classBreak[0],
      classMaxValue: classBreak[1],
      label: `${classBreak[0]}-${classBreak[1]}`,
      description: '',
      symbol: symbolCollection[index]
    }
  })
}
 
function renderUniqueValue (classification, classificationDef, symbolCollection) {
  const { uniqueValueFields, fieldDelimiter } = classificationDef
  return {
    type: 'uniqueValue',
    field1: uniqueValueFields[0],
    field2: '',
    field3: '',
    fieldDelimiter,
    defaultSymbol: {},
    defaultLabel: '',
    uniqueValueInfos: createUniqueValueInfos(
      classification,
      fieldDelimiter,
      symbolCollection
    )
  }
}
 
function createUniqueValueInfos (
  uniqueValueEntries,
  fieldDelimiter,
  symbolCollection
) {
  return uniqueValueEntries.map((uniqueValue, index) => {
    const value = serializeUniqueValues(uniqueValue, fieldDelimiter)
 
    return {
      value,
      count: uniqueValue.count,
      label: value,
      description: '',
      symbol: symbolCollection[index]
    }
  })
}
 
function serializeUniqueValues (uniqueValue, delimiter) {
  const { count, ...rest } = uniqueValue
  return Object.values(rest).join(delimiter)
}