📄 v1.js
字号:
style.legend = legend; }, /** * Method: write * * Parameters: * context - {Object} An object representing the map context. * options - {Object} Optional object. * * Returns: * {String} A WMC document string. */ write: function(context, options) { var root = this.createElementDefaultNS("ViewContext"); this.setAttributes(root, { version: this.VERSION, id: (options && typeof options.id == "string") ? options.id : OpenLayers.Util.createUniqueID("OpenLayers_Context_") }); // add schemaLocation attribute this.setAttributeNS( root, this.namespaces.xsi, "xsi:schemaLocation", this.schemaLocation ); // required General element root.appendChild(this.write_wmc_General(context)); // required LayerList element root.appendChild(this.write_wmc_LayerList(context)); return OpenLayers.Format.XML.prototype.write.apply(this, [root]); }, /** * Method: createElementDefaultNS * Shorthand for createElementNS with namespace from <defaultPrefix>. * Can optionally be used to set attributes and a text child value. * * Parameters: * name - {String} The qualified node name. * childValue - {String} Optional value for text child node. * attributes - {Object} Optional object representing attributes. * * Returns: * {Element} An element node. */ createElementDefaultNS: function(name, childValue, attributes) { var node = this.createElementNS( this.namespaces[this.defaultPrefix], name ); if(childValue) { node.appendChild(this.createTextNode(childValue)); } if(attributes) { this.setAttributes(node, attributes); } return node; }, /** * Method: setAttributes * Set multiple attributes given key value pairs from an object. * * Parameters: * node - {Element} An element node. * obj - {Object} An object whose properties represent attribute names and * values represent attribute values. */ setAttributes: function(node, obj) { var value; for(var name in obj) { value = obj[name].toString(); if(value.match(/[A-Z]/)) { // safari lowercases attributes with setAttribute this.setAttributeNS(node, null, name, value); } else { node.setAttribute(name, value); } } }, /** * Method: write_wmc_General * Create a General node given an context object. * * Parameters: * context - {Object} Context object. * * Returns: * {Element} A WMC General element node. */ write_wmc_General: function(context) { var node = this.createElementDefaultNS("General"); // optional Window element if(context.size) { node.appendChild(this.createElementDefaultNS( "Window", null, { width: context.size.w, height: context.size.h } )); } // required BoundingBox element var bounds = context.bounds; node.appendChild(this.createElementDefaultNS( "BoundingBox", null, { minx: bounds.left.toPrecision(10), miny: bounds.bottom.toPrecision(10), maxx: bounds.right.toPrecision(10), maxy: bounds.top.toPrecision(10), SRS: context.projection } )); // required Title element node.appendChild(this.createElementDefaultNS( "Title", context.title )); // OpenLayers specific map properties node.appendChild(this.write_ol_MapExtension(context)); return node; }, /** * Method: write_ol_MapExtension */ write_ol_MapExtension: function(context) { var node = this.createElementDefaultNS("Extension"); var bounds = context.maxExtent; if(bounds) { var maxExtent = this.createElementNS( this.namespaces.ol, "ol:maxExtent" ); this.setAttributes(maxExtent, { minx: bounds.left.toPrecision(10), miny: bounds.bottom.toPrecision(10), maxx: bounds.right.toPrecision(10), maxy: bounds.top.toPrecision(10) }); node.appendChild(maxExtent); } return node; }, /** * Method: write_wmc_LayerList * Create a LayerList node given an context object. * * Parameters: * context - {Object} Context object. * * Returns: * {Element} A WMC LayerList element node. */ write_wmc_LayerList: function(context) { var list = this.createElementDefaultNS("LayerList"); var layer; for(var i=0; i<context.layers.length; ++i) { layer = context.layers[i]; if(layer instanceof OpenLayers.Layer.WMS) { list.appendChild(this.write_wmc_Layer(layer)); } } return list; }, /** * Method: write_wmc_Layer * Create a Layer node given a layer object. * * Parameters: * layer - {<OpenLayers.Layer.WMS>} Layer object. * * Returns: * {Element} A WMC Layer element node. */ write_wmc_Layer: function(layer) { var node = this.createElementDefaultNS( "Layer", null, { queryable: "1", hidden: layer.visibility ? "0" : "1" } ); // required Server element node.appendChild(this.write_wmc_Server(layer)); // required Name element node.appendChild(this.createElementDefaultNS( "Name", layer.params["LAYERS"] )); // required Title element node.appendChild(this.createElementDefaultNS( "Title", layer.name )); // optional FormatList element node.appendChild(this.write_wmc_FormatList(layer)); // optional StyleList element node.appendChild(this.write_wmc_StyleList(layer)); // OpenLayers specific properties go in an Extension element node.appendChild(this.write_wmc_LayerExtension(layer)); return node; }, /** * Method: write_wmc_LayerExtension * Add OpenLayers specific layer parameters to an Extension element. * * Parameters: * layer - {<OpenLayers.Layer.WMS>} A WMS layer. * * Returns: * {Element} A WMC Extension element (for a layer). */ write_wmc_LayerExtension: function(layer) { var node = this.createElementDefaultNS("Extension"); var bounds = layer.maxExtent; var maxExtent = this.createElementNS( this.namespaces.ol, "ol:maxExtent" ); this.setAttributes(maxExtent, { minx: bounds.left.toPrecision(10), miny: bounds.bottom.toPrecision(10), maxx: bounds.right.toPrecision(10), maxy: bounds.top.toPrecision(10) }); node.appendChild(maxExtent); var param = layer.params["TRANSPARENT"]; if(param) { var trans = this.createElementNS( this.namespaces.ol, "ol:transparent" ); trans.appendChild(this.createTextNode(param)); node.appendChild(trans); } var properties = [ "numZoomLevels", "units", "isBaseLayer", "opacity", "displayInLayerSwitcher", "singleTile" ]; var child; for(var i=0; i<properties.length; ++i) { child = this.createOLPropertyNode(layer, properties[i]); if(child) { node.appendChild(child); } } return node; }, /** * Method: createOLPropertyNode * Create a node representing an OpenLayers property. If the property is * null or undefined, null will be returned. * * Parameters: * object - {Object} An object. * prop - {String} A property. * * Returns: * {Element} A property node. */ createOLPropertyNode: function(obj, prop) { var node = null; if(obj[prop] != null) { node = this.createElementNS(this.namespaces.ol, "ol:" + prop); node.appendChild(this.createTextNode(obj[prop].toString())); } return node; }, /** * Method: write_wmc_Server * Create a Server node given a layer object. * * Parameters: * layer - {<OpenLayers.Layer.WMS>} Layer object. * * Returns: * {Element} A WMC Server element node. */ write_wmc_Server: function(layer) { var node = this.createElementDefaultNS("Server"); this.setAttributes(node, { service: "OGC:WMS", version: layer.params["VERSION"] }); // required OnlineResource element node.appendChild(this.write_wmc_OnlineResource(layer.url)); return node; }, /** * Method: write_wmc_FormatList * Create a FormatList node given a layer. * * Parameters: * layer - {<OpenLayers.Layer.WMS>} Layer object. * * Returns: * {Element} A WMC FormatList element node. */ write_wmc_FormatList: function(layer) { var node = this.createElementDefaultNS("FormatList"); node.appendChild(this.createElementDefaultNS( "Format", layer.params["FORMAT"], {current: "1"} )); return node; }, /** * Method: write_wmc_StyleList * Create a StyleList node given a layer. * * Parameters: * layer - {<OpenLayers.Layer.WMS>} Layer object. * * Returns: * {Element} A WMC StyleList element node. */ write_wmc_StyleList: function(layer) { var node = this.createElementDefaultNS("StyleList"); var style = this.createElementDefaultNS( "Style", null, {current: "1"} ); // Style can come from one of three places (prioritized as below): // 1) an SLD parameter // 2) and SLD_BODY parameter // 3) the STYLES parameter if(layer.params["SLD"]) { // create link from SLD parameter var sld = this.createElementDefaultNS("SLD"); var link = this.write_wmc_OnlineResource(layer.params["SLD"]); sld.appendChild(link); style.appendChild(sld); } else if(layer.params["SLD_BODY"]) { // include sld fragment from SLD_BODY parameter var sld = this.createElementDefaultNS("SLD"); var body = layer.params["SLD_BODY"]; // read in body as xml doc - assume proper namespace declarations var doc = OpenLayers.Format.XML.prototype.read.apply(this, [body]); // append to StyledLayerDescriptor node var imported = doc.documentElement; if(sld.ownerDocument && sld.ownerDocument.importNode) { imported = sld.ownerDocument.importNode(imported, true); } sld.appendChild(imported); style.appendChild(sld); } else { // use name(s) from STYLES parameter var name = layer.params["STYLES"] ? layer.params["STYLES"] : this.defaultStyleName; style.appendChild(this.createElementDefaultNS("Name", name)); style.appendChild(this.createElementDefaultNS( "Title", this.defaultStyleTitle )); } node.appendChild(style); return node; }, /** * Method: write_wmc_OnlineResource * Create an OnlineResource node given a URL. * * Parameters: * href - {String} URL for the resource. * * Returns: * {Element} A WMC OnlineResource element node. */ write_wmc_OnlineResource: function(href) { var node = this.createElementDefaultNS("OnlineResource"); this.setAttributeNS(node, this.namespaces.xlink, "xlink:type", "simple"); this.setAttributeNS(node, this.namespaces.xlink, "xlink:href", href); return node; }, CLASS_NAME: "OpenLayers.Format.WMC.v1" });
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -