Ticket #100: wmc.patch
| File wmc.patch, 56.9 kB (added by tschaub, 1 year ago) |
|---|
-
tests/Format/test_WMC.html
old new 1 <html> 2 <head> 3 <script src="../../lib/OpenLayers.js"></script> 4 <script type="text/javascript"> 5 6 var v1_0_0 = '<ViewContext xmlns="http://www.opengis.net/context" version="1.0.0" id="OpenLayers_Context_233" xsi:schemaLocation="http://www.opengis.net/context http://schemas.opengis.net/context/1.0.0/context.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><General><Window width="512" height="256"/><BoundingBox minx="-109.9709708" miny="27.01451459" maxx="-80.02902918" maxy="41.98548541" SRS="EPSG:4326"/><Title/><Extension><ol:maxExtent xmlns:ol="http://openlayers.org/context" minx="-130.0000000" miny="14.00000000" maxx="-60.00000000" maxy="55.00000000"/></Extension></General><LayerList><Layer queryable="1" hidden="0"><Server service="OGC:WMS" version="1.1.1"><OnlineResource xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://t1.hypercube.telascience.org/cgi-bin/landsat7"/></Server><Name>landsat7</Name><Title>NASA Global Mosaic</Title><FormatList><Format current="1">image/jpeg</Format></FormatList><StyleList><Style current="1"><Name/><Title>Default</Title></Style></StyleList><Extension><ol:maxExtent xmlns:ol="http://openlayers.org/context" minx="-130.0000000" miny="14.00000000" maxx="-60.00000000" maxy="55.00000000"/><ol:numZoomLevels xmlns:ol="http://openlayers.org/context">4</ol:numZoomLevels><ol:units xmlns:ol="http://openlayers.org/context">degrees</ol:units><ol:isBaseLayer xmlns:ol="http://openlayers.org/context">true</ol:isBaseLayer><ol:displayInLayerSwitcher xmlns:ol="http://openlayers.org/context">true</ol:displayInLayerSwitcher><ol:singleTile xmlns:ol="http://openlayers.org/context">false</ol:singleTile></Extension></Layer><Layer queryable="1" hidden="1"><Server service="OGC:WMS" version="1.1.1"><OnlineResource xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://labs.metacarta.com/wms/vmap0"/></Server><Name>basic</Name><Title>OpenLayers WMS</Title><FormatList><Format current="1">image/jpeg</Format></FormatList><StyleList><Style current="1"><Name/><Title>Default</Title></Style></StyleList><Extension><ol:maxExtent xmlns:ol="http://openlayers.org/context" minx="-130.0000000" miny="14.00000000" maxx="-60.00000000" maxy="55.00000000"/><ol:numZoomLevels xmlns:ol="http://openlayers.org/context">4</ol:numZoomLevels><ol:units xmlns:ol="http://openlayers.org/context">degrees</ol:units><ol:isBaseLayer xmlns:ol="http://openlayers.org/context">true</ol:isBaseLayer><ol:displayInLayerSwitcher xmlns:ol="http://openlayers.org/context">true</ol:displayInLayerSwitcher><ol:singleTile xmlns:ol="http://openlayers.org/context">false</ol:singleTile></Extension></Layer><Layer queryable="1" hidden="0"><Server service="OGC:WMS" version="1.1.1"><OnlineResource xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://lioapp.lrc.gov.on.ca/cubeserv/cubeserv.pl"/></Server><Name>na_road:CCRS</Name><Title>Transportation Network</Title><FormatList><Format current="1">image/png</Format></FormatList><StyleList><Style current="1"><Name/><Title>Default</Title></Style></StyleList><Extension><ol:maxExtent xmlns:ol="http://openlayers.org/context" minx="-166.5320000" miny="4.050460000" maxx="-0.2068180000" maxy="70.28700000"/><ol:transparent xmlns:ol="http://openlayers.org/context">TRUE</ol:transparent><ol:numZoomLevels xmlns:ol="http://openlayers.org/context">4</ol:numZoomLevels><ol:units xmlns:ol="http://openlayers.org/context">degrees</ol:units><ol:isBaseLayer xmlns:ol="http://openlayers.org/context">false</ol:isBaseLayer><ol:opacity xmlns:ol="http://openlayers.org/context">0.6</ol:opacity><ol:displayInLayerSwitcher xmlns:ol="http://openlayers.org/context">false</ol:displayInLayerSwitcher><ol:singleTile xmlns:ol="http://openlayers.org/context">false</ol:singleTile></Extension></Layer><Layer queryable="1" hidden="0"><Server service="OGC:WMS" version="1.1.1"><OnlineResource xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://columbo.nrlssc.navy.mil/ogcwms/servlet/WMSServlet/AccuWeather_Maps.wms"/></Server><Name>3:1</Name><Title>Radar 3:1</Title><FormatList><Format current="1">image/png</Format></FormatList><StyleList><Style current="1"><Name/><Title>Default</Title></Style></StyleList><Extension><ol:maxExtent xmlns:ol="http://openlayers.org/context" minx="-131.0294952" miny="14.56289673" maxx="-61.02950287" maxy="54.56289673"/><ol:transparent xmlns:ol="http://openlayers.org/context">TRUE</ol:transparent><ol:numZoomLevels xmlns:ol="http://openlayers.org/context">4</ol:numZoomLevels><ol:units xmlns:ol="http://openlayers.org/context">degrees</ol:units><ol:isBaseLayer xmlns:ol="http://openlayers.org/context">false</ol:isBaseLayer><ol:opacity xmlns:ol="http://openlayers.org/context">0.8</ol:opacity><ol:displayInLayerSwitcher xmlns:ol="http://openlayers.org/context">true</ol:displayInLayerSwitcher><ol:singleTile xmlns:ol="http://openlayers.org/context">true</ol:singleTile></Extension></Layer></LayerList></ViewContext>'; 7 var v1_1_0 = '<ViewContext xmlns="http://www.opengis.net/context" version="1.1.0" id="OpenLayers_Context_232" xsi:schemaLocation="http://www.opengis.net/context http://schemas.opengis.net/context/1.1.0/context.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><General><Window width="512" height="256"/><BoundingBox minx="-109.9709708" miny="27.01451459" maxx="-80.02902918" maxy="41.98548541" SRS="EPSG:4326"/><Title/><Extension><ol:maxExtent xmlns:ol="http://openlayers.org/context" minx="-130.0000000" miny="14.00000000" maxx="-60.00000000" maxy="55.00000000"/></Extension></General><LayerList><Layer queryable="1" hidden="0"><Server service="OGC:WMS" version="1.1.1"><OnlineResource xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://t1.hypercube.telascience.org/cgi-bin/landsat7"/></Server><Name>landsat7</Name><Title>NASA Global Mosaic</Title><sld:MinScaleDenominator xmlns:sld="http://www.opengis.net/sld">6299645.760</sld:MinScaleDenominator><sld:MaxScaleDenominator xmlns:sld="http://www.opengis.net/sld">31498228.80</sld:MaxScaleDenominator><FormatList><Format current="1">image/jpeg</Format></FormatList><StyleList><Style current="1"><Name/><Title>Default</Title></Style></StyleList><Extension><ol:maxExtent xmlns:ol="http://openlayers.org/context" minx="-130.0000000" miny="14.00000000" maxx="-60.00000000" maxy="55.00000000"/><ol:numZoomLevels xmlns:ol="http://openlayers.org/context">4</ol:numZoomLevels><ol:units xmlns:ol="http://openlayers.org/context">degrees</ol:units><ol:isBaseLayer xmlns:ol="http://openlayers.org/context">true</ol:isBaseLayer><ol:displayInLayerSwitcher xmlns:ol="http://openlayers.org/context">true</ol:displayInLayerSwitcher><ol:singleTile xmlns:ol="http://openlayers.org/context">false</ol:singleTile></Extension></Layer><Layer queryable="1" hidden="1"><Server service="OGC:WMS" version="1.1.1"><OnlineResource xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://labs.metacarta.com/wms/vmap0"/></Server><Name>basic</Name><Title>OpenLayers WMS</Title><sld:MinScaleDenominator xmlns:sld="http://www.opengis.net/sld">6299645.760</sld:MinScaleDenominator><sld:MaxScaleDenominator xmlns:sld="http://www.opengis.net/sld">31498228.80</sld:MaxScaleDenominator><FormatList><Format current="1">image/jpeg</Format></FormatList><StyleList><Style current="1"><Name/><Title>Default</Title></Style></StyleList><Extension><ol:maxExtent xmlns:ol="http://openlayers.org/context" minx="-130.0000000" miny="14.00000000" maxx="-60.00000000" maxy="55.00000000"/><ol:numZoomLevels xmlns:ol="http://openlayers.org/context">4</ol:numZoomLevels><ol:units xmlns:ol="http://openlayers.org/context">degrees</ol:units><ol:isBaseLayer xmlns:ol="http://openlayers.org/context">true</ol:isBaseLayer><ol:displayInLayerSwitcher xmlns:ol="http://openlayers.org/context">true</ol:displayInLayerSwitcher><ol:singleTile xmlns:ol="http://openlayers.org/context">false</ol:singleTile></Extension></Layer><Layer queryable="1" hidden="0"><Server service="OGC:WMS" version="1.1.1"><OnlineResource xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://lioapp.lrc.gov.on.ca/cubeserv/cubeserv.pl"/></Server><Name>na_road:CCRS</Name><Title>Transportation Network</Title><sld:MinScaleDenominator xmlns:sld="http://www.opengis.net/sld">6200000.000</sld:MinScaleDenominator><sld:MaxScaleDenominator xmlns:sld="http://www.opengis.net/sld">32000000.00</sld:MaxScaleDenominator><FormatList><Format current="1">image/png</Format></FormatList><StyleList><Style current="1"><Name/><Title>Default</Title></Style></StyleList><Extension><ol:maxExtent xmlns:ol="http://openlayers.org/context" minx="-166.5320000" miny="4.050460000" maxx="-0.2068180000" maxy="70.28700000"/><ol:transparent xmlns:ol="http://openlayers.org/context">TRUE</ol:transparent><ol:numZoomLevels xmlns:ol="http://openlayers.org/context">4</ol:numZoomLevels><ol:units xmlns:ol="http://openlayers.org/context">degrees</ol:units><ol:isBaseLayer xmlns:ol="http://openlayers.org/context">false</ol:isBaseLayer><ol:opacity xmlns:ol="http://openlayers.org/context">0.6</ol:opacity><ol:displayInLayerSwitcher xmlns:ol="http://openlayers.org/context">false</ol:displayInLayerSwitcher><ol:singleTile xmlns:ol="http://openlayers.org/context">false</ol:singleTile></Extension></Layer><Layer queryable="1" hidden="0"><Server service="OGC:WMS" version="1.1.1"><OnlineResource xlink:type="simple" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="http://columbo.nrlssc.navy.mil/ogcwms/servlet/WMSServlet/AccuWeather_Maps.wms"/></Server><Name>3:1</Name><Title>Radar 3:1</Title><sld:MinScaleDenominator xmlns:sld="http://www.opengis.net/sld">6299645.760</sld:MinScaleDenominator><sld:MaxScaleDenominator xmlns:sld="http://www.opengis.net/sld">31498228.80</sld:MaxScaleDenominator><FormatList><Format current="1">image/png</Format></FormatList><StyleList><Style current="1"><Name/><Title>Default</Title></Style></StyleList><Extension><ol:maxExtent xmlns:ol="http://openlayers.org/context" minx="-131.0294952" miny="14.56289673" maxx="-61.02950287" maxy="54.56289673"/><ol:transparent xmlns:ol="http://openlayers.org/context">TRUE</ol:transparent><ol:numZoomLevels xmlns:ol="http://openlayers.org/context">4</ol:numZoomLevels><ol:units xmlns:ol="http://openlayers.org/context">degrees</ol:units><ol:isBaseLayer xmlns:ol="http://openlayers.org/context">false</ol:isBaseLayer><ol:opacity xmlns:ol="http://openlayers.org/context">0.8</ol:opacity><ol:displayInLayerSwitcher xmlns:ol="http://openlayers.org/context">true</ol:displayInLayerSwitcher><ol:singleTile xmlns:ol="http://openlayers.org/context">true</ol:singleTile></Extension></Layer></LayerList></ViewContext>'; 8 9 function test_Format_WMC_read(t) { 10 t.plan(32); 11 12 var format = new OpenLayers.Format.WMC(); 13 var map, layer; 14 15 // test v1.0.0 16 map = format.read(v1_0_0, {map: "map"}); 17 t.eq(map.center.lon.toPrecision(6), (-95).toPrecision(6), "(v1.0.0) map center correctly set"); 18 t.eq(map.center.lat.toPrecision(6), (34.5).toPrecision(6), "(v1.0.0) map center correctly set"); 19 t.eq(map.maxExtent.left.toPrecision(6), (-130).toPrecision(6), "(v1.0.0) map maxExtent correctly set"); 20 t.eq(map.layers.length, 4, "(v1.0.0) correct number of layers"); 21 t.eq(map.projection, "EPSG:4326", "(v1.0.0) map projection set correctly"); 22 // check out first base layer 23 layer = map.layers[0]; 24 t.ok(layer instanceof OpenLayers.Layer.WMS, 25 "(v1.0.0) wms layer correctly instantiated"); 26 t.eq(layer.url, "http://t1.hypercube.telascience.org/cgi-bin/landsat7", 27 "(v1.0.0) layer url correctly set"); 28 t.eq(layer.maxExtent.right.toPrecision(6), (-60).toPrecision(6), 29 "(v1.0.0) layer maxExtent correctly set"); 30 t.eq(layer.numZoomLevels, 4, 31 "(v1.0.0) layer numZoomLevels correctly set"); 32 t.eq(layer.isBaseLayer, true, 33 "(v1.0.0) layer isBaseLayer correctly set for base layer"); 34 // check out layer not in switcher 35 layer = map.layers[2]; 36 t.eq(layer.displayInLayerSwitcher, false, 37 "(v1.0.0) layer displayInLayerSwitcher correctly set"); 38 t.eq(layer.params["TRANSPARENT"], "TRUE", 39 "(v1.0.0) layer param.transparent correctly set"); 40 t.eq(layer.isBaseLayer, false, 41 "(v1.0.0) layer isBaseLayer correctly set for overlay"); 42 // check out single tile overlay 43 layer = map.layers[3]; 44 t.eq(layer.singleTile, true, 45 "(v1.0.0) layer singleTile correctly set"); 46 t.eq(layer.opacity, 0.8, 47 "(v1.0.0) layer opacity correctly set"); 48 map.destroy(); 49 50 // test v1.1.0 51 var map = format.read(v1_1_0, {map: "map"}); 52 t.eq(map.center.lon.toPrecision(6), (-95).toPrecision(6), "(v1.1.0) map center correctly set"); 53 t.eq(map.center.lat.toPrecision(6), (34.5).toPrecision(6), "(v1.1.0) map center correctly set"); 54 t.eq(map.maxExtent.left.toPrecision(6), (-130).toPrecision(6), "(v1.1.0) map maxExtent correctly set"); 55 t.eq(map.layers.length, 4, "(v1.1.0) correct number of layers"); 56 t.eq(map.projection, "EPSG:4326", "(v1.1.0) map projection set correctly"); 57 // check out first baseLayer 58 layer = map.layers[0]; 59 t.ok(layer instanceof OpenLayers.Layer.WMS, 60 "(v1.1.0) wms layer correctly instantiated"); 61 t.eq(layer.url, "http://t1.hypercube.telascience.org/cgi-bin/landsat7", 62 "(v1.1.0) layer url correctly set"); 63 t.eq(layer.maxExtent.right.toPrecision(6), (-60).toPrecision(6), 64 "(v1.1.0) layer maxExtent correctly set"); 65 t.eq(layer.numZoomLevels, 4, 66 "(v1.1.0) layer numZoomLevels correctly set"); 67 t.eq(layer.isBaseLayer, true, 68 "(v1.1.0) layer isBaseLayer correctly set for overlay"); 69 // check out layer not in switcher 70 layer = map.layers[2]; 71 t.eq(layer.displayInLayerSwitcher, false, 72 "(v1.1.0) layer displayInLayerSwitcher correctly set"); 73 t.eq(layer.params["TRANSPARENT"], "TRUE", 74 "(v1.1.0) layer param.transparent correctly set"); 75 t.eq(layer.isBaseLayer, false, 76 "(v1.1.0) layer isBaseLayer correctly set for overlay"); 77 t.eq(layer.minScale.toPrecision(6), (32000000).toPrecision(6), 78 "(v1.1.0) layer minScale correctly set"); 79 t.eq(layer.maxScale.toPrecision(6), (6200000).toPrecision(6), 80 "(v1.1.0) layer maxScale correctly set"); 81 // check out single tile overlay 82 layer = map.layers[3]; 83 t.eq(layer.singleTile, true, 84 "(v1.1.0) layer singleTile correctly set"); 85 t.eq(layer.opacity, 0.8, 86 "(v1.1.0) layer opacity correctly set"); 87 map.destroy(); 88 89 } 90 91 function test_Format_WMC_write(t) { 92 93 var format = new OpenLayers.Format.WMC(); 94 var map = format.read(v1_1_0, {map: "map"}); 95 var wmc; 96 97 function sanitize(str) { 98 // id is unique so we don't expect it to be the same 99 str = str.replace(/OpenLayers_Context_\d+/, "foo"); 100 // FF and IE differ on ordering of attribute nodes 101 // a better way to compare xml would be nice 102 str = str.replace(/\s*xsi:schemaLocation=".*?"/, ""); 103 str = str.replace(/\s*xmlns:xlink=".*?"/g, ""); 104 // and of course floating point precision is an issue 105 str = str.replace(/(-?\d+\.\d+)/g, function(m) {return parseFloat(m).toPrecision(6)}); 106 return str; 107 } 108 109 /** 110 * Version 1.0.0 doesn't make the round trip because min/max resolutions 111 * are not stored. Version 1.1.0 introduced minScaleDenominator and 112 * maxScaleDenominator. The parser still writes valid WMC v1.0.0, you 113 * just need a validator to prove it. 114 * 115 * The wmc.html example can be used to produce WMC docs for validation. 116 * Prior to validation, the Extension tags need to be removed unless someone 117 * cares to write and maintain some XSD. Both version 1.0.0 and 1.1.0 118 * validate on http://www.validome.org/xml/. 119 */ 120 121 // test v1.1.0 122 if(OpenLayers.Util.getBrowserName()== "opera") { 123 t.plan(0); 124 t.debug_print("WMC writing works but is not tested in Opera"); 125 } else { 126 t.plan(1); 127 128 map = format.read(v1_1_0, {map: "map"}); 129 wmc = format.write(map); 130 t.eq(sanitize(wmc), sanitize(v1_1_0), 131 "(v1.1.0) write gives what read got"); 132 map.destroy(); 133 134 } 135 } 136 137 </script> 138 </head> 139 <body> 140 <div id="map" style="width: 512px; height: 256px;"></div> 141 </body> 142 </html> -
tests/list-tests.html
old new 28 28 <li>Format/test_KML.html</li> 29 29 <li>Format/test_SLD.html</li> 30 30 <li>Format/test_WKT.html</li> 31 <li>Format/test_WMC.html</li> 31 32 <li>Format/test_XML.html</li> 32 33 <li>test_Icon.html</li> 33 34 <li>test_Marker.html</li> -
lib/OpenLayers/Format/WMC.js
old new 1 /** 2 * @requires OpenLayers/Format/XML.js 3 */ 4 5 /** 6 * Class: OpenLayers.Format.WMC 7 * Read and write Web Map Context documents. 8 * 9 * Inherits from: 10 * - <OpenLayers.Format.XML> 11 */ 12 OpenLayers.Format.WMC = OpenLayers.Class({ 13 14 /** 15 * APIProperty: defaultVersion 16 * {String} Version number to assume if none found. Default is "1.1.0". 17 */ 18 defaultVersion: "1.1.0", 19 20 /** 21 * APIProperty: version 22 * {String} Specify a version string if one is known. 23 */ 24 version: null, 25 26 /** 27 * Property: parser 28 * {Object} Instance of the versioned parser. Cached for multiple read and 29 * write calls of the same version. 30 */ 31 parser: null, 32 33 /** 34 * Constructor: OpenLayers.Format.WMC 35 * Create a new parser for WMC docs. 36 * 37 * Parameters: 38 * options - {Object} An optional object whose properties will be set on 39 * this instance. 40 */ 41 initialize: function(options) { 42 OpenLayers.Util.extend(this, options); 43 this.options = options; 44 }, 45 46 /** 47 * APIMethod: read 48 * Read WMC data from a string, and return an object with map properties 49 * and a list of layers. 50 * 51 * Parameters: 52 * data - {String} or {DOMElement} data to read/parse. 53 * options - {Object} The options object must contain a map property. If 54 * the map property is a string, it must be the id of a dom element 55 * where the new map will be placed. If the map property is an 56 * <OpenLayers.Map>, the layers from the context document will be added 57 * to the map. 58 * 59 * Returns: 60 * {<OpenLayers.Map>} A map based on the context. 61 */ 62 read: function(data, options) { 63 if(typeof data == "string") { 64 data = OpenLayers.Format.XML.prototype.read.apply(this, [data]); 65 } 66 var root = data.documentElement; 67 var version = this.version; 68 if(!version) { 69 version = root.getAttribute("version"); 70 if(!version) { 71 version = this.defaultVersion; 72 } 73 } 74 if(!this.parser || this.parser.VERSION != version) { 75 var format = OpenLayers.Format.WMC[ 76 "v" + version.replace(/\./g, "_") 77 ]; 78 if(!format) { 79 throw "Can't find a WMS capabilities parser for version " + 80 version; 81 } 82 this.parser = new format(this.options); 83 } 84 var context = this.parser.read(data); 85 var map; 86 if(options.map instanceof OpenLayers.Map) { 87 map = this.mergeContextToMap(context, options.map); 88 } else { 89 map = this.contextToMap(context, options.map); 90 } 91 return map; 92 }, 93 94 /** 95 * Method: contextToMap 96 * Create a map given a context object. 97 * 98 * Parameters: 99 * context - {Object} The context object. 100 * id - {String | Element} The dom element or element id that will contain 101 * the map. 102 * 103 * Returns: 104 * {<OpenLayers.Map>} A map based on the context object. 105 */ 106 contextToMap: function(context, id) { 107 var map = new OpenLayers.Map(id, { 108 maxExtent: context.maxExtent, 109 projection: context.projection 110 }); 111 map.addLayers(context.layers); 112 map.setCenter( 113 context.bounds.getCenterLonLat(), 114 map.getZoomForExtent(context.bounds, true) 115 ); 116 return map; 117 }, 118 119 /** 120 * Method: mergeContextToMap 121 * Add layers from a context object to a map. 122 * 123 * Parameters: 124 * context - {Object} The context object. 125 * map - {<OpenLayers.Map>} The map. 126 * 127 * Returns: 128 * {<OpenLayers.Map>} The same map with layers added. 129 */ 130 mergeContextToMap: function(context, map) { 131 map.addLayers(context.layers); 132 return map; 133 }, 134 135 /** 136 * APIMethod: write 137 * Write a WMC document given a map. 138 * 139 * Parameters: 140 * obj - {<OpenLayers.Map> | Object} A map or context object. 141 * options - {Object} Optional configuration object. 142 * 143 * Returns: 144 * {String} A WMC document string. 145 */ 146 write: function(obj, options) { 147 if(obj.CLASS_NAME == "OpenLayers.Map") { 148 obj = this.mapToContext(obj); 149 } 150 var version = (options && options.version) || 151 this.version || this.defaultVersion; 152 if(!this.parser || this.parser.VERSION != version) { 153 var format = OpenLayers.Format.WMC[ 154 "v" + version.replace(/\./g, "_") 155 ]; 156 if(!format) { 157 throw "Can't find a WMS capabilities parser for version " + 158 version; 159 } 160 this.parser = new format(this.options); 161 } 162 var wmc = this.parser.write(obj); 163 return wmc; 164 }, 165 166 /** 167 * Method: mapToContext 168 * Create a context object given a map. 169 * 170 * Parameters: 171 * map - {<OpenLayers.Map>} The map. 172 * 173 * Returns: 174 * {Object} A context object. 175 */ 176 mapToContext: function(map) { 177 var context = { 178 bounds: map.getExtent(), 179 maxExtent: map.maxExtent, 180 projection: map.projection, 181 layers: map.layers, 182 size: map.getSize() 183 }; 184 return context; 185 }, 186 187 CLASS_NAME: "OpenLayers.Format.WMC" 188 189 }); -
lib/OpenLayers/Format/WMC/v1_0_0.js
old new 1 /** 2 * @requires OpenLayers/Format/WMC/v1.js 3 */ 4 5 /** 6 * Class: OpenLayers.Format.WMC.v1_0_0 7 * Read and write WMC version 1.0.0. 8 * 9 * Inherits from: 10 * - <OpenLayers.Format.WMC.v1> 11 */ 12 OpenLayers.Format.WMC.v1_0_0 = OpenLayers.Class( 13 OpenLayers.Format.WMC.v1, { 14 15 /** 16 * Constant: VERSION 17 * {String} 1.0.0 18 */ 19 VERSION: "1.0.0", 20 21 /** 22 * Property: schemaLocation 23 * {String} http://www.opengis.net/context 24 * http://schemas.opengis.net/context/1.0.0/context.xsd 25 */ 26 schemaLocation: "http://www.opengis.net/context http://schemas.opengis.net/context/1.0.0/context.xsd", 27 28 /** 29 * Constructor: OpenLayers.Format.WMC.v1_0_0 30 * Instances of this class are not created directly. Use the 31 * <OpenLayers.Format.WMC> constructor instead. 32 * 33 * Parameters: 34 * options - {Object} An optional object whose properties will be set on 35 * this instance. 36 */ 37 initialize: function(options) { 38 OpenLayers.Format.WMC.v1.prototype.initialize.apply( 39 this, [options] 40 ); 41 }, 42 43 CLASS_NAME: "OpenLayers.Format.WMC.v1_0_0" 44 45 }); -
lib/OpenLayers/Format/WMC/v1_1_0.js
old new 1 /** 2 * @requires OpenLayers/Format/WMC/v1.js 3 */ 4 5 /** 6 * Class: OpenLayers.Format.WMC.v1_1_0 7 * Read and write WMC version 1.1.0. 8 * 9 * Differences between 1.1.0 and 1.0.0: 10 * - 1.1.0 Layers have optional sld:MinScaleDenominator and 11 * sld:MaxScaleDenominator 12 * 13 * Inherits from: 14 * - <OpenLayers.Format.WMC.v1> 15 */ 16 OpenLayers.Format.WMC.v1_1_0 = OpenLayers.Class( 17 OpenLayers.Format.WMC.v1, { 18 19 /** 20 * Constant: VERSION 21 * {String} 1.1.0 22 */ 23 VERSION: "1.1.0", 24 25 /** 26 * Property: schemaLocation 27 * {String} http://www.opengis.net/context 28 * http://schemas.opengis.net/context/1.1.0/context.xsd 29 */ 30 schemaLocation: "http://www.opengis.net/context http://schemas.opengis.net/context/1.1.0/context.xsd", 31 32 /** 33 * Constructor: OpenLayers.Format.WMC.v1_1_0 34 * Instances of this class are not created directly. Use the 35 * <OpenLayers.Format.WMC> constructor instead. 36 * 37 * Parameters: 38 * options - {Object} An optional object whose properties will be set on 39 * this instance. 40 */ 41 initialize: function(options) { 42 OpenLayers.Format.WMC.v1.prototype.initialize.apply( 43 this, [options] 44 ); 45 }, 46 47 /** 48 * Method: read_sld_MinScaleDenominator 49 * Read a sld:MinScaleDenominator node. 50 * 51 * Parameters: 52 * layerInfo - {Object} An object representing a layer. 53 * node - {Element} An element node. 54 */ 55 read_sld_MinScaleDenominator: function(layerInfo, node) { 56 layerInfo.options.maxScale = this.getChildValue(node); 57 }, 58 59 /** 60 * Method: read_sld_MaxScaleDenominator 61 * Read a sld:MaxScaleDenominator node. 62 * 63 * Parameters: 64 * layerInfo - {Object} An object representing a layer. 65 * node - {Element} An element node. 66 */ 67 read_sld_MaxScaleDenominator: function(layerInfo, node) { 68 layerInfo.options.minScale = this.getChildValue(node); 69 }, 70 71 /** 72 * Method: write_wmc_Layer 73 * Create a Layer node given a layer object. This method adds elements 74 * specific to version 1.1.0. 75 * 76 * Parameters: 77 * layer - {<OpenLayers.Layer.WMS>} Layer object. 78 * 79 * Returns: 80 * {Element} A WMC Layer element node. 81 */ 82 write_wmc_Layer: function(layer) { 83 var node = OpenLayers.Format.WMC.v1.prototype.write_wmc_Layer.apply( 84 this, [layer] 85 ); 86 87 // min/max scale denominator elements go before the 4th element in v1 88 var minSD = this.createElementNS( 89 this.namespaces.sld, "sld:MinScaleDenominator" 90 ); 91 minSD.appendChild(this.createTextNode(layer.maxScale.toPrecision(10))); 92 node.insertBefore(minSD, node.childNodes[3]); 93 94 var maxSD = this.createElementNS( 95 this.namespaces.sld, "sld:MaxScaleDenominator" 96 ); 97 maxSD.appendChild(this.createTextNode(layer.minScale.toPrecision(10))); 98 node.insertBefore(maxSD, node.childNodes[4]); 99 100 return node; 101 102 }, 103 104 CLASS_NAME: "OpenLayers.Format.WMC.v1_1_0" 105 106 }); -
lib/OpenLayers/Format/WMC/v1.js
old new 1 /** 2 * @requires OpenLayers/Format/XML.js 3 */ 4 5 /** 6 * Class: OpenLayers.Format.WMC.v1 7 * Superclass for WMC version 1 parsers. 8 * 9 * Inherits from: 10 * - <OpenLayers.Format.XML> 11 */ 12 OpenLayers.Format.WMC.v1 = OpenLayers.Class(OpenLayers.Format.XML, { 13 14 /** 15 * Property: namespaces 16 * {Object} Mapping of namespace aliases to namespace URIs. 17 */ 18 namespaces: { 19 ol: "http://openlayers.org/context", 20 wmc: "http://www.opengis.net/context", 21 sld: "http://www.opengis.net/sld", 22 xlink: "http://www.w3.org/1999/xlink", 23 xsi: "http://www.w3.org/2001/XMLSchema-instance" 24 }, 25 26 /** 27 * Property: schemaLocation 28 * {String} Schema location for a particular minor version. 29 */ 30 schemaLocation: "", 31 32 /** 33 * Method: getNamespacePrefix 34 * Get the namespace prefix for a given uri from the <namespaces> object. 35 * 36 * Returns: 37 * {String} A namespace prefix or null if none found. 38 */ 39 getNamespacePrefix: function(uri) { 40 var prefix = null; 41 if(uri == null) { 42 prefix = this.namespaces[this.defaultPrefix]; 43 } else { 44 for(prefix in this.namespaces) { 45 if(this.namespaces[prefix] == uri) { 46 break; 47 } 48 } 49 } 50 return prefix; 51 }, 52 53 /** 54 * Property: defaultPrefix 55 */ 56 defaultPrefix: "wmc", 57 58 /** 59 * Property: rootPrefix 60 * {String} Prefix on the root node that maps to the context namespace URI. 61 */ 62 rootPrefix: null, 63 64 /** 65 * Property: defaultStyleName 66 * {String} Style name used if layer has no style param. Default is "". 67 */ 68 defaultStyleName: "", 69 70 /** 71 * Property: defaultStyleTitle 72 * {String} Default style title. Default is "Default". 73 */ 74 defaultStyleTitle: "Default", 75 76 /** 77 * Constructor: OpenLayers.Format.WMC.v1 78 * Instances of this class are not created directly. Use the 79 * <OpenLayers.Format.WMC> constructor instead. 80 * 81 * Parameters: 82 * options - {Object} An optional object whose properties will be set on 83 * this instance. 84 */ 85 initialize: function(options) { 86 OpenLayers.Format.XML.prototype.initialize.apply(this, [options]); 87 }, 88 89 /** 90 * Method: read 91 * Read capabilities data from a string, and return a list of layers. 92 * 93 * Parameters: 94 * data - {String} or {DOMElement} data to read/parse. 95 * 96 * Returns: 97 * {Array} List of named layers. 98 */ 99 read: function(data) { 100 if(typeof data == "string") { 101 data = OpenLayers.Format.XML.prototype.read.apply(this, [data]); 102 } 103 var root = data.documentElement; 104 this.rootPrefix = root.prefix; 105 var context = { 106 version: root.getAttribute("version") 107 }; 108 this.runChildNodes(context, root); 109 return context; 110 }, 111 112 /** 113 * Method: runChildNodes 114 */ 115 runChildNodes: function(obj, node) { 116 var children = node.childNodes; 117 var childNode, processor, prefix, local; 118 for(var i=0; i<children.length; ++i) { 119 childNode = children[i]; 120 if(childNode.nodeType == 1) { 121 prefix = (childNode.prefix == this.rootPrefix) ? 122 this.defaultPrefix : 123 this.getNamespacePrefix(childNode.namespaceURI); 124 local = childNode.nodeName.split(":").pop(); 125 processor = this["read_" + prefix + "_" + local]; 126 if(processor) { 127 processor.apply(this, [obj, childNode]); 128 } 129 } 130 } 131 }, 132 133 /** 134 * Method: read_wmc_General 135 */ 136 read_wmc_General: function(context, node) { 137 this.runChildNodes(context, node); 138 }, 139 140 /** 141 * Method: read_wmc_BoundingBox 142 */ 143 read_wmc_BoundingBox: function(context, node) { 144 context.projection = node.getAttribute("SRS"); 145 context.bounds = new OpenLayers.Bounds( 146 parseFloat(node.getAttribute("minx")), 147 parseFloat(node.getAttribute("miny")), 148 parseFloat(node.getAttribute("maxx")), 149 parseFloat(node.getAttribute("maxy")) 150 ); 151 }, 152 153 /** 154 * Method: read_wmc_LayerList 155 */ 156 read_wmc_LayerList: function(context, node) { 157 context.layers = []; 158 this.runChildNodes(context, node); 159 }, 160 161 /** 162 * Method: read_wmc_Layer 163 */ 164 read_wmc_Layer: function(context, node) { 165 var layerInfo = { 166 params: {}, 167 options: { 168 visibility: (node.getAttribute("hidden") != "1") 169 }, 170 queryable: (node.getAttribute("queryable") == "1"), 171 formats: [], 172 styles: [] 173 }; 174 this.runChildNodes(layerInfo, node); 175 // set properties common to multiple objects on layer options/params 176 layerInfo.params.layers = layerInfo.name; 177 layerInfo.options.maxExtent = layerInfo.maxExtent; 178 // create the layer 179 var layer = this.getLayerFromInfo(layerInfo); 180 context.layers.push(layer); 181 }, 182 183 /** 184 * Method: getLayerFromInfo 185 * Create a WMS layer from a layerInfo object. 186 * 187 * Parameters: 188 * layerInfo - {Object} An object representing a WMS layer. 189 * 190 * Returns: 191 * {<OpenLayers.Layer.WMS>} A WMS layer. 192 */ 193 getLayerFromInfo: function(layerInfo) { 194 var layer = new OpenLayers.Layer.WMS( 195 layerInfo.title, 196 layerInfo.href, 197 layerInfo.params, 198 layerInfo.options 199 ); 200 return layer; 201 }, 202 203 /** 204 * Method: read_wmc_Extension 205 */ 206 read_wmc_Extension: function(obj, node) { 207 this.runChildNodes(obj, node); 208 }, 209 210 /** 211 * Method: read_ol_units 212 */ 213 read_ol_units: function(layerInfo, node) { 214 layerInfo.options.units = this.getChildValue(node); 215 }, 216 217 /** 218 * Method: read_ol_maxExtent 219 */ 220 read_ol_maxExtent: function(obj, node) { 221 var bounds = new OpenLayers.Bounds( 222 node.getAttribute("minx"), node.getAttribute("miny"), 223 node.getAttribute("maxx"), node.getAttribute("maxy") 224 ); 225 obj.maxExtent = bounds; 226 }, 227 228 /** 229 * Method: read_ol_transparent 230 */ 231 read_ol_transparent: function(layerInfo, node) { 232 layerInfo.params.transparent = this.getChildValue(node); 233 }, 234 235 /** 236 * Method: read_ol_numZoomLevels 237 */ 238 read_ol_numZoomLevels: function(layerInfo, node) { 239 layerInfo.options.numZoomLevels = parseInt(this.getChildValue(node)); 240 }, 241 242 /** 243 * Method: read_ol_opacity 244 */ 245 read_ol_opacity: function(layerInfo, node) { 246 layerInfo.options.opacity = parseFloat(this.getChildValue(node)); 247 }, 248 249 /** 250 * Method: read_ol_singleTile 251 */ 252 read_ol_singleTile: function(layerInfo, node) { 253 layerInfo.options.singleTile = (this.getChildValue(node) == "true"); 254 }, 255 256 /** 257 * Method: read_ol_isBaseLayer 258 */ 259 read_ol_isBaseLayer: function(layerInfo, node) { 260 layerInfo.options.isBaseLayer = (this.getChildValue(node) == "true"); 261 }, 262 263 /** 264 * Method: read_ol_displayInLayerSwitcher 265 */ 266 read_ol_displayInLayerSwitcher: function(layerInfo, node) { 267 layerInfo.options.displayInLayerSwitcher = 268 (this.getChildValue(node) == "true"); 269 }, 270 271 /** 272 * Method: read_wmc_Server 273 */ 274 read_wmc_Server: function(layerInfo, node) { 275 layerInfo.params.version = node.getAttribute("version"); 276 this.runChildNodes(layerInfo, node); 277 }, 278 279 /** 280 * Method: read_wmc_FormatList 281 */ 282 read_wmc_FormatList: function(layerInfo, node) { 283 this.runChildNodes(layerInfo, node); 284 }, 285 286 /** 287 * Method: read_wmc_Format 288 */ 289 read_wmc_Format: function(layerInfo, node) { 290 var format = this.getChildValue(node) 291 layerInfo.formats.push(format); 292 if(node.getAttribute("current") == "1") { 293 layerInfo.params.format = format; 294 } 295 }, 296 297 /** 298 * Method: read_wmc_StyleList 299 */ 300 read_wmc_StyleList: function(layerInfo, node) { 301 this.runChildNodes(layerInfo, node); 302 }, 303 304 /** 305 * Method: read_wmc_Style 306 */ 307 read_wmc_Style: function(layerInfo, node) { 308 var style = {}; 309 this.runChildNodes(style, node); 310 if(node.getAttribute("current") == "1") { 311 layerInfo.params.style = style.name; 312 } 313 layerInfo.styles.push(style); 314 }, 315 316 /** 317 * Method: read_wmc_OnlineResource 318 */ 319 read_wmc_OnlineResource: function(obj, node) { 320 obj.href = this.getAttributeNS( 321 node, this.namespaces.xlink, "href" 322 ); 323 }, 324 325 /** 326 * Method: read_wmc_Name 327 */ 328 read_wmc_Name: function(obj, node) { 329 var name = this.getChildValue(node); 330 if(name) { 331 obj.name = name; 332 } 333 }, 334 335 /** 336 * Method: read_wmc_Title 337 */ 338 read_wmc_Title: function(obj, node) { 339 var title = this.getChildValue(node); 340 if(title) { 341 obj.title = title; 342 } 343 }, 344 345 /** 346 * Method: read_wmc_Abstract 347 */ 348 read_wmc_Abstract: function(obj, node) { 349 var abst = this.getChildValue(node); 350 if(abst) { 351 obj["abstract"] = abst; 352 } 353 }, 354 355 /** 356 * Method: read_wmc_LatLonBoundingBox 357 */ 358 read_wmc_LatLonBoundingBox: function(layer, node) { 359 layer.llbbox = [ 360 parseFloat(node.getAttribute("minx")), 361 parseFloat(node.getAttribute("miny")), 362 parseFloat(node.getAttribute("maxx")), 363 parseFloat(node.getAttribute("maxy")) 364 ]; 365 }, 366 367 /** 368 * Method: read_wmc_LegendURL 369 */ 370 read_wmc_LegendURL: function(style, node) { 371 var legend = { 372 width: node.getAttribute('width'), 373 height: node.getAttribute('height') 374 }; 375 var links = node.getElementsByTagName("OnlineResource"); 376 if(links.length > 0) { 377 this.read_wmc_OnlineResource(legend, links[0]); 378 } 379 style.legend = legend; 380 }, 381 382 /** 383 * Method: write 384 * 385 * Parameters: 386 * context - {Object} An object representing the map context. 387 * options - {Object} Optional object. 388 * 389 * Returns: 390 * {String} A WMC document string. 391 */ 392 write: function(context, options) { 393 var root = this.createElementDefaultNS("ViewContext"); 394 this.setAttributes(root, { 395 version: this.VERSION, 396 id: (options && typeof options.id == "string") ? 397 options.id : 398 OpenLayers.Util.createUniqueID("OpenLayers_Context_") 399 }); 400 401 // add schemaLocation attribute 402 this.setAttributeNS( 403 root, this.namespaces.xsi, 404 "xsi:schemaLocation", this.schemaLocation 405 ); 406 407 // required General element 408 root.appendChild(this.write_wmc_General(context)); 409 410 // required LayerList element 411 root.appendChild(this.write_wmc_LayerList(context)); 412 413 return OpenLayers.Format.XML.prototype.write.apply(this, [root]); 414 }, 415 416 /** 417 * Method: createElementDefaultNS 418 * Shorthand for createElementNS with namespace from <defaultPrefix>. 419 * Can optionally be used to set attributes and a text child value. 420 * 421 * Parameters: 422 * name - {String} The qualified node name. 423 * childValue - {String} Optional value for text child node. 424 * attributes - {Object} Optional object representing attributes. 425 * 426 * Returns: 427 * {Element} An element node. 428 */ 429 createElementDefaultNS: function(name, childValue, attributes) { 430 var node = this.createElementNS( 431 this.namespaces[this.defaultPrefix], 432 name 433 ); 434 if(childValue) { 435 node.appendChild(this.createTextNode(childValue)); 436 } 437 if(attributes) { 438 this.setAttributes(node, attributes); 439 } 440 return node; 441 }, 442  
