Example #1
0
VectorEncoder.prototype.encodeVectorStyleFill = function(symbolizer, fillStyle) {
  let fillColor = /** @type {import('ol/color.js').Color} */(fillStyle.getColor());
  if (fillColor !== null) {
    console.assert(typeof fillColor === 'string' || Array.isArray(fillColor));
    fillColor = asColorArray(fillColor);
    console.assert(Array.isArray(fillColor), 'only supporting fill colors');
    symbolizer.fillColor = rgbArrayToHex(fillColor);
    symbolizer.fillOpacity = fillColor[3];
  }
};
Example #2
0
  /**
   * Style for search results.
   * @param {null|import("ol/Feature.js").default|import("ol/render/Feature.js").default} feature
   *    The searched feature.
   * @param {number} resolution The current resolution of the map.
   * @return {import("ol/style/Style.js").default} A style for this kind of features.
   * @private
   */
  getSearchStyle_(feature, resolution) {
    if (!feature) {
      throw new Error('Missing feature');
    }
    const style = this.styles_[feature.get('layer_name')] || this.styles_['default'];
    if (this.color) {
      const color = asColorArray(this.color);

      const strokeColor = color.slice();
      // 100% opacity for the stroke color
      strokeColor[3] = 1;

      const fillColor = color.slice();
      // 50% opacity for the fill color
      fillColor[3] = 0.5;

      const strokeStyle = style.getStroke();
      if (strokeStyle) {
        strokeStyle.setColor(strokeColor);
      }
      const fillStyle = style.getFill();
      if (fillStyle) {
        fillStyle.setColor(fillColor);
      }
      // the image style can't be changed in place, the colors are updated on a clone.
      let imageStyle = style.getImage();
      if (imageStyle) {
        imageStyle = imageStyle.clone();
        const imageStrokeStyle = imageStyle.getStroke();
        if (imageStrokeStyle) {
          imageStrokeStyle.setColor(strokeColor);
        }
        const imageFillStyle = imageStyle.getFill();
        if (imageFillStyle) {
          imageFillStyle.setColor(fillColor);
        }
        style.setImage(imageStyle);
      }
    }
    return style;
  }
Example #3
0
VectorEncoder.prototype.encodeVectorStyleStroke = function(symbolizer, strokeStyle) {
  const strokeColor = /** @type {import('ol/color.js').Color} */(strokeStyle.getColor());
  if (strokeColor !== null) {
    console.assert(typeof strokeColor === 'string' || Array.isArray(strokeColor));
    const strokeColorRgba = asColorArray(strokeColor);
    console.assert(Array.isArray(strokeColorRgba), 'only supporting stroke colors');
    symbolizer.strokeColor = rgbArrayToHex(strokeColorRgba);
    symbolizer.strokeOpacity = strokeColorRgba[3];
  }
  const strokeDashstyle = strokeStyle.getLineDash();
  if (strokeDashstyle !== null) {
    /** @type {import('ngeo/print/mapfish-print-v3.js').MapFishPrintSymbolizerLine} */(symbolizer)
      .strokeDashstyle = strokeDashstyle.join(' ');
  }
  const strokeWidth = strokeStyle.getWidth();
  if (strokeWidth !== undefined) {
    symbolizer.strokeWidth = strokeWidth;
  }
  const strokeLineCap = strokeStyle.getLineCap();
  if (strokeLineCap) {
    /** @type {import('ngeo/print/mapfish-print-v3.js').MapFishPrintSymbolizerLine} */(symbolizer)
      .strokeLinecap = strokeLineCap;
  }
};
Example #4
0
VectorEncoder.prototype.encodeTextStyle = function(symbolizers, textStyle) {
  const symbolizer = /** @type {import('ngeo/print/mapfish-print-v3.js').MapFishPrintSymbolizerText} */ ({
    type: 'Text'
  });
  const label = textStyle.getText();
  if (label !== undefined) {
    symbolizer.label = label;
    let xAlign = 'c';
    let yAlign = 'm';

    const olTextAlign = textStyle.getTextAlign();
    // 'left', 'right', 'center', 'end' or 'start'.
    if (olTextAlign === 'left' || olTextAlign === 'start') {
      xAlign = 'l';
    } else if (olTextAlign === 'right' || olTextAlign === 'end') {
      xAlign = 'r';
    }

    const olTextBaseline = textStyle.getTextBaseline();
    // 'bottom', 'top', 'middle', 'alphabetic', 'hanging' or 'ideographic'
    if (olTextBaseline === 'bottom') {
      yAlign = 'l';
    } else if (olTextBaseline === 'top') {
      yAlign = 't';
    }
    symbolizer.labelAlign = `${xAlign}${yAlign}`;

    const labelRotation = textStyle.getRotation();
    if (labelRotation !== undefined) {
      // Mapfish Print expects a string, not a number to rotate text
      symbolizer.labelRotation = (labelRotation * 180 / Math.PI).toString();
      // rotate around the vertical/horizontal center
      symbolizer.labelAlign = 'cm';
    }

    const fontStyle = textStyle.getFont();
    if (fontStyle !== undefined) {
      const font = fontStyle.split(' ');
      if (font.length >= 3) {
        symbolizer.fontWeight = font[0];
        symbolizer.fontSize = font[1];
        symbolizer.fontFamily = font.splice(2).join(' ');
      }
    }

    const strokeStyle = textStyle.getStroke();
    if (strokeStyle !== null) {
      const strokeColor = /** @type {import('ol/color.js').Color} */(strokeStyle.getColor());
      console.assert(typeof strokeColor === 'string' || Array.isArray(strokeColor));
      const strokeColorRgba = asColorArray(strokeColor);
      console.assert(Array.isArray(strokeColorRgba), 'only supporting stroke colors');
      symbolizer.haloColor = rgbArrayToHex(strokeColorRgba);
      symbolizer.haloOpacity = strokeColorRgba[3];
      const width = strokeStyle.getWidth();
      if (width !== undefined) {
        // Width is a stroke, radius a radius, so divide by 2
        symbolizer.haloRadius = width / 2;
      }
    }

    const fillStyle = textStyle.getFill();
    if (fillStyle !== null) {
      const fillColor = /** @type {import('ol/color.js').Color} */(fillStyle.getColor());
      console.assert(typeof fillColor === 'string' || Array.isArray(fillColor));
      const fillColorRgba = asColorArray(fillColor);
      console.assert(Array.isArray(fillColorRgba), 'only supporting fill colors');
      symbolizer.fontColor = rgbArrayToHex(fillColorRgba);
    }

    // Mapfish Print allows offset only if labelAlign is defined.
    if (symbolizer.labelAlign !== undefined) {
      symbolizer.labelXOffset = textStyle.getOffsetX();
      // Mapfish uses the opposite direction of OpenLayers for y axis, so the
      // minus sign is required for the y offset to be identical.
      symbolizer.labelYOffset = -textStyle.getOffsetY();
    }

    symbolizers.push(symbolizer);
  }
};
Example #5
0
FeatureHelper.prototype.getRGBAColorProperty = function(feature) {
  const color = this.getColorProperty(feature);
  return colorFromString(color);
};