All files / lib/generate-renderer index.js

100% Statements 48/48
95.23% Branches 20/21
100% Functions 13/13
100% Lines 47/47

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 1307x 7x 7x 7x 7x 7x   7x     7x 7x 7x   7x 2x     5x 4x     1x       2x 2x 8x   2x   2x 2x 4x   2x       4x       4x 4x   4x   4x 4x 8x     3x 1x     2x 1x     1x       3x                   3x 9x                     1x 1x                                         1x 3x   3x                     3x 3x 3x 3x    
const _ = require('lodash');
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 breaks = statistics.classBreaks.sort((a, b) => a[0] - b[0]);
 
  validateClassificationDefinition(classificationDef, geometryType, breaks);
  
  const colorRamp = createColorRamp({ breaks, ...colorRampConfig });
  const symbolCollection = colorRamp.map((color) => {
    return createSymbol(baseSymbol, color, geometryType);
  });
  return renderClassBreaks(breaks, 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 breaks = Winnow.query(data, params);
  validateClassificationDefinition(classificationDef, geometryType, breaks);
 
  const { colorRamp: colorRampConfig = {}, baseSymbol } = classificationDef;
 
  const colorRamp = createColorRamp({ breaks, ...colorRampConfig });
  const symbolCollection = colorRamp.map((color) => {
    return createSymbol(baseSymbol, color, geometryType);
  });
 
  if (classificationDef.type === 'classBreaksDef') {
    return renderClassBreaks(breaks, classificationDef, symbolCollection);
  }
 
  if (classificationDef.type === 'uniqueValueDef') {
    return renderUniqueValue(breaks, classificationDef, symbolCollection);
  }
 
  throw new Error('invalid classification type: ', classificationDef.type);
}
 
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(breaks, classificationDef, symbolCollection) {
  const { uniqueValueFields, fieldDelimiter } = classificationDef;
  return {
    type: 'uniqueValue',
    field1: uniqueValueFields[0],
    field2: '',
    field3: '',
    fieldDelimiter,
    defaultSymbol: {},
    defaultLabel: '',
    uniqueValueInfos: createUniqueValueInfos(
      breaks,
      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 thisBreak = _.cloneDeep(uniqueValue);
  delete thisBreak.count;
  const { count, ...rest } = uniqueValue;
  return Object.values(rest).join(delimiter);
}