From d5b3d27e628d1f6e487e127f14b507fe5598dad3 Mon Sep 17 00:00:00 2001 From: Jiri Lysek Date: Thu, 19 Mar 2020 13:02:53 +0100 Subject: [PATCH 1/3] parsing color from IconStyle in KML files --- src/ol/format/KML.js | 7 ++++++- test/spec/ol/format/kml.test.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/ol/format/KML.js b/src/ol/format/KML.js index d94a4c473b..e693a4e127 100644 --- a/src/ol/format/KML.js +++ b/src/ol/format/KML.js @@ -1171,6 +1171,7 @@ function readStyleMapValue(node, objectStack) { const ICON_STYLE_PARSERS = makeStructureNS( NAMESPACE_URIS, { 'Icon': makeObjectPropertySetter(readIcon), + 'color': makeObjectPropertySetter(readColor), 'heading': makeObjectPropertySetter(readDecimal), 'hotSpot': makeObjectPropertySetter(readVec2), 'scale': makeObjectPropertySetter(readScale) @@ -1252,6 +1253,9 @@ function iconStyleParser(node, objectStack) { let scale = /** @type {number|undefined} */ (object['scale']); + const color = /** @type {[number]|undefined} */ + (object['color']); + if (drawIcon) { if (src == DEFAULT_IMAGE_STYLE_SRC) { size = DEFAULT_IMAGE_STYLE_SIZE; @@ -1271,7 +1275,8 @@ function iconStyleParser(node, objectStack) { rotation: rotation, scale: scale, size: size, - src: src + src: src, + color: color }); styleObject['imageStyle'] = imageStyle; } else { diff --git a/test/spec/ol/format/kml.test.js b/test/spec/ol/format/kml.test.js index e1a248bb75..7adedfc2c8 100644 --- a/test/spec/ol/format/kml.test.js +++ b/test/spec/ol/format/kml.test.js @@ -1992,6 +1992,37 @@ describe('ol.format.KML', function() { expect(style.getZIndex()).to.be(undefined); }); + it('can read a feature\'s IconStyle and set color of image', () => { + const text = + '' + + ' ' + + ' ' + + ' ' + + ''; + const fs = format.readFeatures(text); + expect(fs).to.have.length(1); + const f = fs[0]; + expect(f).to.be.an(Feature); + const styleFunction = f.getStyleFunction(); + const styleArray = styleFunction(f, 0); + expect(styleArray).to.be.an(Array); + expect(styleArray).to.have.length(1); + const style = styleArray[0]; + expect(style).to.be.an(Style); + expect(style.getFill()).to.be(getDefaultFillStyle()); + expect(style.getStroke()).to.be(getDefaultStrokeStyle()); + const imageStyle = style.getImage(); + expect(imageStyle.getColor()).to.eql([0xFF, 0, 0, 0xFF / 255]); + }); + it('can read a feature\'s LabelStyle', function() { const text = '' + From 7bf44078e178bd5da25b31406e92edf631b7dc9e Mon Sep 17 00:00:00 2001 From: Jiri Lysek Date: Thu, 19 Mar 2020 13:32:02 +0100 Subject: [PATCH 2/3] fixed type comment --- src/ol/format/KML.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ol/format/KML.js b/src/ol/format/KML.js index e693a4e127..2833548651 100644 --- a/src/ol/format/KML.js +++ b/src/ol/format/KML.js @@ -1253,7 +1253,7 @@ function iconStyleParser(node, objectStack) { let scale = /** @type {number|undefined} */ (object['scale']); - const color = /** @type {[number]|undefined} */ + const color = /** @type {Array|undefined} */ (object['color']); if (drawIcon) { From 2b6e767840292db180d7312aae23db68497e021c Mon Sep 17 00:00:00 2001 From: Jiri Lysek Date: Mon, 23 Mar 2020 10:10:12 +0100 Subject: [PATCH 3/3] encoding color into KML --- src/ol/format/KML.js | 8 +++++++- test/spec/ol/format/kml.test.js | 4 +++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/ol/format/KML.js b/src/ol/format/KML.js index 2833548651..88b69bbc98 100644 --- a/src/ol/format/KML.js +++ b/src/ol/format/KML.js @@ -2452,7 +2452,7 @@ function writeIcon(node, icon, objectStack) { // @ts-ignore const ICON_STYLE_SEQUENCE = makeStructureNS( NAMESPACE_URIS, [ - 'scale', 'heading', 'Icon', 'hotSpot' + 'scale', 'heading', 'Icon', 'color', 'hotSpot' ]); @@ -2464,6 +2464,7 @@ const ICON_STYLE_SEQUENCE = makeStructureNS( const ICON_STYLE_SERIALIZERS = makeStructureNS( NAMESPACE_URIS, { 'Icon': makeChildAppender(writeIcon), + 'color': makeChildAppender(writeColorTextNode), 'heading': makeChildAppender(writeDecimalTextNode), 'hotSpot': makeChildAppender(writeVec2), 'scale': makeChildAppender(writeScaleTextNode) @@ -2519,6 +2520,11 @@ function writeIconStyle(node, style, objectStack) { properties['heading'] = rotation; // 0-360 } + const color = style.getColor(); + if (color) { + properties['color'] = color; + } + const parentNode = objectStack[objectStack.length - 1].node; const orderedKeys = ICON_STYLE_SEQUENCE[parentNode.namespaceURI]; const values = makeSequence(properties, orderedKeys); diff --git a/test/spec/ol/format/kml.test.js b/test/spec/ol/format/kml.test.js index 7adedfc2c8..7369705db5 100644 --- a/test/spec/ol/format/kml.test.js +++ b/test/spec/ol/format/kml.test.js @@ -2394,7 +2394,8 @@ describe('ol.format.KML', function() { rotation: 45, scale: 0.5, size: [48, 48], - src: 'http://foo.png' + src: 'http://foo.png', + color: 'rgba(255,0,0,1)' }) }); const imageStyle = style.getImage(); @@ -2420,6 +2421,7 @@ describe('ol.format.KML', function() { ' 48' + ' 48' + ' ' + + ' ff0000ff' + ' ' + ' ' +