Last active
August 29, 2015 14:10
-
-
Save oskosk/720c2d046eea2c202bca to your computer and use it in GitHub Desktop.
small module for getting WMS capabilities with jQuery
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
var capabilitiesUrl = "/geoserver/wms?request=getcapabilities&service=wms&version=1.1.1"; | |
wms.capabilities(capabilitiesUrl, function(capabilities) { | |
console.log(wms.layerBounds(capabilities, "opengeo:EL_NILO_LOTE_1")) | |
}); |
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
wms = { | |
knownServices: {}, | |
//invokes "callback" with the parsed capabilities as only parameters | |
capabilities: function (url, callback) { | |
var cached = wms.knownServices[url]; | |
if (cached) { | |
callback(cached); | |
} else { | |
wms.getCapabilitiesDocument(url, function (xml) { | |
var capabilities = wms.parseCapabilitiesDocument(xml, url); | |
// cache the parsed document | |
wms.knownServices[url]= capabilities ; | |
// call again trusting it's already cached | |
wms.capabilities(url, callback); | |
}); | |
} | |
}, | |
// Taken from http://davidwalsh.name/convert-xml-json | |
// Changes XML to JSON | |
xmlToJson: function (xml) { | |
// Create the return object | |
var obj = {}; | |
if (xml.nodeType == 1) { // element | |
// do attributes | |
if (xml.attributes.length > 0) { | |
obj["@attributes"] = {}; | |
for (var j = 0; j < xml.attributes.length; j++) { | |
var attribute = xml.attributes.item(j); | |
obj["@attributes"][attribute.nodeName] = attribute.nodeValue; | |
} | |
} | |
} else if (xml.nodeType == 3) { // text | |
obj = xml.nodeValue; | |
} | |
// do children | |
if (xml.hasChildNodes()) { | |
for (var i = 0; i < xml.childNodes.length; i++) { | |
var item = xml.childNodes.item(i); | |
var nodeName = item.nodeName; | |
if (typeof (obj[nodeName]) == "undefined") { | |
obj[nodeName] = wms.xmlToJson(item); | |
} else { | |
if (typeof (obj[nodeName].push) == "undefined") { | |
var old = obj[nodeName]; | |
obj[nodeName] = []; | |
obj[nodeName].push(old); | |
} | |
obj[nodeName].push(wms.xmlToJson(item)); | |
} | |
} | |
} | |
return obj; | |
}, | |
// Va a buscar el XML de capabilities de un WMS | |
getCapabilitiesDocument: function (url, callback) { | |
var wms_uri = url; | |
$.get(wms_uri, function (xml) { | |
callback(xml); | |
}).fail(function () { | |
$.error("Could not get capabilities"); | |
}); | |
}, | |
parseCapabilitiesDocument: function (xml, url) { | |
var _this = this, | |
capabilities = {}; | |
return wms.xmlToJson(xml); | |
}, | |
layer: function(capabilities, layerName) { | |
var layers = capabilities.WMT_MS_Capabilities[1].Capability.Layer.Layer; | |
layers = layers.filter(function(layer) {return layer.Name["#text"] == layerName }); | |
return layers[0]; | |
}, | |
layerBounds: function (capabilities, layerName) { | |
var layer = wms.layer(capabilities, layerName); | |
return layer.LatLonBoundingBox["@attributes"]; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment