From a4c5c1347da4e969f73c5b98531b6f4386bc8b23 Mon Sep 17 00:00:00 2001 From: Jessica McInchak Date: Thu, 30 Sep 2021 15:04:58 +0200 Subject: [PATCH] add property to specify fill opacity of geojson polygon --- src/my-map.ts | 33 ++++++++++++++++++++++----------- src/utils.ts | 14 +++++++++++++- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/my-map.ts b/src/my-map.ts index 3fe4460a..4b0b2a15 100644 --- a/src/my-map.ts +++ b/src/my-map.ts @@ -7,19 +7,19 @@ import { Vector as VectorLayer } from "ol/layer"; import Map from "ol/Map"; import { fromLonLat, transformExtent } from "ol/proj"; import { Vector as VectorSource } from "ol/source"; -import { Stroke, Style } from "ol/style"; +import { Fill, Stroke, Style } from "ol/style"; import View from "ol/View"; import { last } from "rambda"; import { draw, drawingLayer, drawingSource, modify, snap } from "./draw"; -import { scaleControl } from "./scale-line" +import { scaleControl } from "./scale-line"; import { makeFeatureLayer, outlineSource, getFeaturesAtPoint, } from "./os-features"; import { makeOsVectorTileBaseMap, makeRasterBaseMap } from "./os-layers"; -import { AreaUnitEnum, fitToData, formatArea } from "./utils"; +import { AreaUnitEnum, fitToData, formatArea, hexToRgba } from "./utils"; @customElement("my-map") export class MyMap extends LitElement { @@ -93,6 +93,9 @@ export class MyMap extends LitElement { @property({ type: String }) geojsonColor = "#ff0000"; + @property({ type: Boolean }) + geojsonFill = false; + @property({ type: Number }) geojsonBuffer = 12; @@ -112,11 +115,11 @@ export class MyMap extends LitElement { staticMode = false; @property({ type: String }) - areaUnit: AreaUnitEnum = "m2" + areaUnit: AreaUnitEnum = "m2"; @property({ type: String }) ariaLabel = "Interactive map"; - + @property({ type: Boolean }) showScale = false; @@ -228,6 +231,11 @@ export class MyMap extends LitElement { color: this.geojsonColor, width: 3, }), + fill: new Fill({ + color: this.geojsonFill + ? hexToRgba(this.geojsonColor, 0.2) + : hexToRgba(this.geojsonColor, 0), + }), }), }); @@ -265,7 +273,10 @@ export class MyMap extends LitElement { }) ); - this.dispatch("areaChange", formatArea(lastSketchGeom, this.areaUnit)); + this.dispatch( + "areaChange", + formatArea(lastSketchGeom, this.areaUnit) + ); // limit to drawing a single polygon, only allow modifications to existing shape map.removeInteraction(draw); @@ -300,7 +311,10 @@ export class MyMap extends LitElement { // log total area of feature or merged features const data = outlineSource.getFeatures()[0].getGeometry(); - console.log("feature(s) total area:", formatArea(data, this.areaUnit)); + console.log( + "feature(s) total area:", + formatArea(data, this.areaUnit) + ); } }); } @@ -317,10 +331,7 @@ export class MyMap extends LitElement { render() { return html` -
`; +
`; } /** diff --git a/src/utils.ts b/src/utils.ts index f108e8a3..2324365c 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,10 +1,11 @@ +import { asArray, asString } from "ol/color"; import { buffer } from "ol/extent"; import Geometry from "ol/geom/Geometry"; import Map from "ol/Map"; import { Vector } from "ol/source"; import { getArea } from "ol/sphere"; -export type AreaUnitEnum = "m2" | "ha"; +export type AreaUnitEnum = "m2" | "ha"; /** * Calculate & format the area of a polygon @@ -43,3 +44,14 @@ export function fitToData( const extent = olSource.getExtent(); return olMap.getView().fit(buffer(extent, bufferValue)); } + +/** + * Translate a hex color to an rgba string with opacity + * @param hexColor - a hex color string + * @param alpha - a decimal to represent opacity + * @returns - a 'rgba(r,g,b,a)' string + */ +export function hexToRgba(hexColor: string, alpha: number) { + const [r, g, b] = Array.from(asArray(hexColor)); + return asString([r, g, b, alpha]); +}