Last active
November 3, 2015 19:11
-
-
Save bjornharrtell/de53b7fe604704368ac8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function consolidateImageWMSLayers (layers) { | |
let i, j | |
const layersProcessed = [] // need to record processed layers to only process once | |
const layersToConsolidate = [] // intended to contain arrays of layer that are to be consolidated | |
const otherIndices = [] // indices of unconsolidated layers, will be used when assembling new layers | |
const isImageWMS = layer => layer.type === 'Image' && layer.source.type === 'ImageWMS' | |
const isProcessed = layer => layersProcessed.indexOf(layer) !== -1 | |
const isCandidate = (l1, l2) => l1.source.url === l2.source.url && !(l1.metadata.unconsolidated === true || l2.metadata.unconsolidated === true) | |
// find all ImageWMS layers with same URL to fill above arrays | |
for (i = 0; i < layers.length; i++) { | |
if (!isImageWMS(layers[i])) { | |
otherIndices.push(i) | |
} else if (!isProcessed(layers[i])) { | |
const layersMatched = [] | |
let layerIndex = 0 | |
for (j = 0; j < layers.length; j++) { | |
if (isImageWMS(layers[j]) && !isProcessed(layers[j]) && isCandidate(layers[i], layers[j]) && (layersMatched.length === 0 || layerIndex === j - 1)) { | |
layersMatched.push(layers[j]) | |
layerIndex = j | |
layersProcessed.push(layers[j]) | |
} | |
} | |
if (layersMatched.length > 1) { | |
layersToConsolidate.push(layersMatched) | |
} else { | |
otherIndices.push(i) | |
} | |
layersProcessed.push(layers[i]) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment