Skip to content

Commit

Permalink
feat(wms): parse layer attribution
Browse files Browse the repository at this point in the history
  • Loading branch information
jahow committed Apr 27, 2021
1 parent ee39611 commit 9680ace
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 42 deletions.
20 changes: 2 additions & 18 deletions fixtures/wms/capabilities-brgm-1-1-1.xml
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@
<BoundingBox SRS="EPSG:32621"
minx="3.93464e+06" miny="6.12146e+06" maxx="5.93425e+06" maxy="8.11543e+06" />
<Attribution>
<Title>BRGM</Title>
<OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.brgm.fr"/>
<Title>BRGM (modified attribution)</Title>
<OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.brgm.fr/"/>
<LogoURL width="158" height="82">
<Format>image/png</Format>
<OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="http://mapsref.brgm.fr/legendes/brgm_logo.png"/>
Expand Down Expand Up @@ -259,14 +259,6 @@
minx="4.23619e+06" miny="6.6238e+06" maxx="6.21236e+06" maxy="8.70077e+06" />
<BoundingBox SRS="EPSG:32621"
minx="3.9038e+06" miny="6.1856e+06" maxx="5.89555e+06" maxy="8.21306e+06" />
<Attribution>
<Title>BRGM</Title>
<OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.brgm.fr"/>
<LogoURL width="158" height="82">
<Format>image/png</Format>
<OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="http://mapsref.brgm.fr/legendes/brgm_logo.png"/>
</LogoURL>
</Attribution>
<AuthorityURL name="BRGM">
<OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="www.brgm.fr"/>
</AuthorityURL>
Expand Down Expand Up @@ -301,14 +293,6 @@
minx="3.88148e+06" miny="6.13796e+06" maxx="6.31307e+06" maxy="8.70752e+06" />
<BoundingBox SRS="EPSG:32621"
minx="3.52434e+06" miny="5.74736e+06" maxx="5.97375e+06" maxy="8.23867e+06" />
<Attribution>
<Title>BRGM</Title>
<OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.brgm.fr"/>
<LogoURL width="158" height="82">
<Format>image/png</Format>
<OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="http://mapsref.brgm.fr/legendes/brgm_logo.png"/>
</LogoURL>
</Attribution>
<AuthorityURL name="BRGM">
<OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="www.brgm.fr"/>
</AuthorityURL>
Expand Down
20 changes: 2 additions & 18 deletions fixtures/wms/capabilities-brgm-1-3-0.xml
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,8 @@
<BoundingBox CRS="EPSG:32621"
minx="3.93464e+06" miny="6.12146e+06" maxx="5.93425e+06" maxy="8.11543e+06" />
<Attribution>
<Title>BRGM</Title>
<OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.brgm.fr"/>
<Title>BRGM (modified attribution)</Title>
<OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.brgm.fr/"/>
<LogoURL width="158" height="82">
<Format>image/png</Format>
<OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="http://mapsref.brgm.fr/legendes/brgm_logo.png"/>
Expand Down Expand Up @@ -280,14 +280,6 @@
minx="4.23619e+06" miny="6.6238e+06" maxx="6.21236e+06" maxy="8.70077e+06" />
<BoundingBox CRS="EPSG:32621"
minx="3.9038e+06" miny="6.1856e+06" maxx="5.89555e+06" maxy="8.21306e+06" />
<Attribution>
<Title>BRGM</Title>
<OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.brgm.fr"/>
<LogoURL width="158" height="82">
<Format>image/png</Format>
<OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="http://mapsref.brgm.fr/legendes/brgm_logo.png"/>
</LogoURL>
</Attribution>
<AuthorityURL name="BRGM">
<OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="www.brgm.fr"/>
</AuthorityURL>
Expand Down Expand Up @@ -329,14 +321,6 @@
minx="3.88148e+06" miny="6.13796e+06" maxx="6.31307e+06" maxy="8.70752e+06" />
<BoundingBox CRS="EPSG:32621"
minx="3.52434e+06" miny="5.74736e+06" maxx="5.97375e+06" maxy="8.23867e+06" />
<Attribution>
<Title>BRGM</Title>
<OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://www.brgm.fr"/>
<LogoURL width="158" height="82">
<Format>image/png</Format>
<OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="simple" xlink:href="http://mapsref.brgm.fr/legendes/brgm_logo.png"/>
</LogoURL>
</Attribution>
<AuthorityURL name="BRGM">
<OnlineResource xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="www.brgm.fr"/>
</AuthorityURL>
Expand Down
39 changes: 33 additions & 6 deletions src/wms/capabilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,20 @@ export function readLayersFromCapabilities(capabilitiesDoc) {

/**
* @param {string[]} path
* @param {CrsCode[]} [inheritedCrs]
* @param {LayerStyle[]} [inheritedStyles]
* @param {WmsLayer} [parentLayer]
* @return {function(WmsLayer[], XmlElement): WmsLayer[]} A reducer which parses all layers recursively
*/
const recursiveParseLayer = (path, inheritedCrs, inheritedStyles) => (prev, layerEl) => {
const parsedLayer = parseLayer(path, layerEl, version, inheritedCrs, inheritedStyles);
const recursiveParseLayer = (path, parentLayer) => (prev, layerEl) => {
const parsedLayer = parseLayer(path, layerEl, version,
parentLayer && parentLayer.availableCrs,
parentLayer && parentLayer.styles,
parentLayer && parentLayer.attribution);
const children = findChildrenElement(layerEl, 'Layer');
const childrenPath = [...path, parsedLayer.name];
return [
...prev,
parsedLayer,
...children.reduce(recursiveParseLayer(childrenPath, parsedLayer.availableCrs, parsedLayer.styles), [])
...children.reduce(recursiveParseLayer(childrenPath, parsedLayer), [])
];
}

Expand Down Expand Up @@ -70,9 +72,10 @@ export function readInfoFromCapabilities(capabilitiesDoc) {
* @param {WmsVersion} version
* @param {CrsCode[]} [inheritedSrs]
* @param {LayerStyle[]} [inheritedStyles]
* @param {LayerAttribution} [inheritedAttribution]
* @return {WmsLayer}
*/
function parseLayer(path, layerEl, version, inheritedSrs = [], inheritedStyles = []) {
function parseLayer(path, layerEl, version, inheritedSrs = [], inheritedStyles = [], inheritedAttribution = null) {
const srsTag = version === '1.3.0' ? 'CRS' : 'SRS'
const srsList = findChildrenElement(layerEl, srsTag).map(getElementText)
const availableCrs = srsList.length > 0 ? srsList : inheritedSrs
Expand All @@ -86,12 +89,15 @@ function parseLayer(path, layerEl, version, inheritedSrs = [], inheritedStyles =
['minx', 'miny', 'maxx', 'maxy'];
return attrs.map(name => getElementAttribute(bboxEl, name))
}
const attributionEl = findChildElement(layerEl, 'Attribution')
const attribution = attributionEl !== null ? parseLayerAttribution(attributionEl) : inheritedAttribution
return {
name: getElementText(findChildElement(layerEl, 'Name')),
title: getElementText(findChildElement(layerEl, 'Title')),
abstract: getElementText(findChildElement(layerEl, 'Abstract')),
availableCrs,
styles,
attribution,
boundingBoxes: findChildrenElement(layerEl, 'BoundingBox').reduce((prev, bboxEl) =>
({
...prev,
Expand Down Expand Up @@ -119,3 +125,24 @@ function parseLayerStyle(styleEl) {
...legendUrl && { legendUrl }
};
}

/**
* @param {XmlElement} attributionEl
* @return {LayerAttribution}
*/
function parseLayerAttribution(attributionEl) {
const logoUrl = getElementAttribute(
findChildElement(
findChildElement(attributionEl, 'LogoURL'),
'OnlineResource'
),
'xlink:href'
);
const url = getElementAttribute(findChildElement(attributionEl, 'OnlineResource'), 'xlink:href')
const title = getElementText(findChildElement(attributionEl, 'Title'))
return {
...title && { title },
...url && { url },
...logoUrl && { logoUrl }
};
}
13 changes: 13 additions & 0 deletions src/wms/capabilities.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ describe('WMS capabilities', () => {
})

describe('readLayersFromCapabilities', () => {
const attribution = {
title: 'Brgm',
url: 'http://www.brgm.fr/',
logoUrl: 'http://mapsref.brgm.fr/legendes/brgm_logo.png'
}
const expectedLayers = [
{
name: 'GEOSERVICES_GEOLOGIE',
Expand Down Expand Up @@ -67,6 +72,7 @@ describe('WMS capabilities', () => {
"90"
]
},
attribution,
path: []
},
{
Expand All @@ -87,6 +93,7 @@ describe('WMS capabilities', () => {
}
],
boundingBoxes: {},
attribution,
path: ['GEOSERVICES_GEOLOGIE']
},
{
Expand Down Expand Up @@ -144,6 +151,10 @@ describe('WMS capabilities', () => {
},
name: "SCAN_F_GEOL1M",
title: "Carte géologique image de la France au million",
attribution: {
...attribution,
title: 'BRGM (modified attribution)'
},
path: ["GEOSERVICES_GEOLOGIE", "GEOLOGIE"]
},
{
Expand Down Expand Up @@ -195,6 +206,7 @@ describe('WMS capabilities', () => {
},
name: "SCAN_F_GEOL250",
title: "Carte géologique image de la France au 1/250000",
attribution,
path: ["GEOSERVICES_GEOLOGIE", "GEOLOGIE"]
},
{
Expand Down Expand Up @@ -246,6 +258,7 @@ describe('WMS capabilities', () => {
},
name: "SCAN_D_GEOL50",
title: "Carte géologique image de la France au 1/50 000e",
attribution,
path: ["GEOSERVICES_GEOLOGIE", "GEOLOGIE"]
}
]
Expand Down
8 changes: 8 additions & 0 deletions src/wms/endpoint.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
* @property {string} [legendUrl] May not be defined; a GetLegendGraphic operation should work in any case
*/

/**
* @typedef {Object} LayerAttribution
* @property {string} [title]
* @property {string} [url]
* @property {string} [logoUrl]
*/

/**
* @typedef {Object} WmsLayer
* @property {string} name
Expand All @@ -22,6 +29,7 @@
* @property {CrsCode[]} availableCrs
* @property {LayerStyle[]} styles
* @property {Object.<CrsCode, BoundingBox>} boundingBoxes Dict of bounding boxes where keys are CRS codes
* @property {LayerAttribution} [attribution]
* @property {string[]} path If the layer is nested in other layers, the path will contain the name of previous layers
* in order from the topmost parent (root layer) to the closest one.
*/
Expand Down

0 comments on commit 9680ace

Please sign in to comment.