Changeset 4305
- Timestamp:
- 09/14/07 18:50:30 (10 months ago)
- Files:
-
- trunk/openlayers/examples/vector-formats.html (modified) (2 diffs)
- trunk/openlayers/lib/OpenLayers/Format/GeoRSS.js (modified) (7 diffs)
- trunk/openlayers/tests/Format/test_GeoRSS.html (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/openlayers/examples/vector-formats.html
r4219 r4305 78 78 wkt: new OpenLayers.Format.WKT(), 79 79 geojson: new OpenLayers.Format.GeoJSON(), 80 georss: new OpenLayers.Format.GeoRSS(), 80 81 gml: new OpenLayers.Format.GML(), 81 82 kml: new OpenLayers.Format.KML() … … 148 149 <option value="geojson" selected="selected">GeoJSON</option> 149 150 <option value="kml">KML</option> 151 <option value="georss">GeoRSS</option> 150 152 <option value="gml">GML</option> 151 153 <option value="wkt">Well-Known Text (WKT)</option> trunk/openlayers/lib/OpenLayers/Format/GeoRSS.js
r4110 r4305 5 5 /** 6 6 * @requires OpenLayers/Format.js 7 * @requires OpenLayers/Format/XML.js 7 8 * 8 9 * Class: OpenLayers.Format.GeoRSS 9 * Write-only GeoRSS. Create a new instance with the10 * Read/write GeoRSS parser. Create a new instance with the 10 11 * <OpenLayers.Format.GeoRSS> constructor. 11 12 * 12 13 * Inherits from: 13 * - <OpenLayers.Format >14 * - <OpenLayers.Format.XML> 14 15 */ 15 OpenLayers.Format.GeoRSS = OpenLayers.Class(OpenLayers.Format , {16 OpenLayers.Format.GeoRSS = OpenLayers.Class(OpenLayers.Format.XML, { 16 17 17 18 /** 18 19 * APIProperty: rssns 19 * RSS namespace to use. 20 * {String} RSS namespace to use. Defaults to 21 * "http://backend.userland.com/rss2" 20 22 */ 21 23 rssns: "http://backend.userland.com/rss2", … … 23 25 /** 24 26 * APIProperty: featurens 25 * Feature Attributes namespace 27 * {String} Feature Attributes namespace. Defaults to 28 * "http://mapserver.gis.umn.edu/mapserver" 26 29 */ 27 30 featureNS: "http://mapserver.gis.umn.edu/mapserver", … … 29 32 /** 30 33 * APIProperty: georssns 31 * GeoRSS namespace to use. 34 * {String} GeoRSS namespace to use. Defaults to 35 * "http://www.georss.org/georss" 32 36 */ 33 37 georssns: "http://www.georss.org/georss", 34 38 39 /** 40 * APIProperty: featureTitle 41 * {String} Default title for features. Defaults to "Untitled" 42 */ 43 featureTitle: "Untitled", 44 45 /** 46 * APIProperty: featureDescription 47 * {String} Default description for features. Defaults to "No Description" 48 */ 49 featureDescription: "No Description", 35 50 36 51 /** 37 52 * Constructor: OpenLayers.Format.GeoRSS 38 * Create a new parser for GeoRSS 53 * Create a new parser for GeoRSS. 39 54 * 40 55 * Parameters: 41 56 * options - {Object} An optional object whose properties will be set on 42 * this instance.57 * this instance. 43 58 */ 44 59 initialize: function(options) { 45 OpenLayers.Format.prototype.initialize.apply(this, [options]); 46 }, 60 OpenLayers.Format.XML.prototype.initialize.apply(this, [options]); 61 }, 62 63 /** 64 * Method: createGeometryFromItem 65 * Return a geometry from a GeoRSS Item. 66 * 67 * Parameters: 68 * item - {DOMElement} A GeoRSS item node. 69 * 70 * Returns: 71 * {<OpenLayers.Geometry>} A geometry representing the node. 72 */ 73 createGeometryFromItem: function(item) { 74 var point = this.getElementsByTagNameNS(item, this.georssns, "point"); 75 var lat = this.getElementsByTagNameNS(item, this.geons, 'lat'); 76 var lon = this.getElementsByTagNameNS(item, this.geons, 'long'); 77 78 var line = this.getElementsByTagNameNS(item, 79 this.georssns, 80 "line"); 81 var polygon = this.getElementsByTagNameNS(item, 82 this.georssns, 83 "polygon"); 84 85 if (point.length > 0 || (lat.length > 0 && lon.length > 0)) { 86 if (point.length > 0) { 87 var location = OpenLayers.String.trim( 88 point[0].firstChild.nodeValue).split(/\s+/); 89 90 if (location.length !=2) { 91 var location = OpenLayers.String.trim( 92 point[0].firstChild.nodeValue).split(/\s*,\s*/); 93 } 94 } else { 95 var location = [parseFloat(lat[0].firstChild.nodeValue), 96 parseFloat(lon[0].firstChild.nodeValue)]; 97 } 98 var geometry = new OpenLayers.Geometry.Point(parseFloat(location[1]), 99 parseFloat(location[0])); 100 } else if (line.length > 0) { 101 var coords = OpenLayers.String.trim(line[0].firstChild.nodeValue).split(/\s+/); 102 var components = []; 103 for (var i=0; i < coords.length; i+=2) { 104 var point = new OpenLayers.Geometry.Point(parseFloat(coords[i+1]), parseFloat(coords[i])); 105 components.push(point); 106 } 107 geometry = new OpenLayers.Geometry.LineString(components); 108 } else if (polygon.length > 0) { 109 var coords = OpenLayers.String.trim(polygon[0].firstChild.nodeValue).split(/\s+/); 110 var components = []; 111 for (var i=0; i < coords.length; i+=2) { 112 var point = new OpenLayers.Geometry.Point(parseFloat(coords[i+1]), parseFloat(coords[i])); 113 components.push(point); 114 } 115 geometry = new OpenLayers.Geometry.Polygon([new OpenLayers.Geometry.LinearRing(components)]); 116 } 117 return geometry; 118 }, 119 120 /** 121 * Method: createGeometryFromItem 122 * Return a feature from a GeoRSS Item. 123 * 124 * Parameters: 125 * item - {DOMElement} A GeoRSS item node. 126 * 127 * Returns: 128 * {<OpenLayers.Feature.Vector>} A feature representing the item. 129 */ 130 createFeatureFromItem: function(item) { 131 var geometry = this.createGeometryFromItem(item); 132 /* Provide defaults for title and description */ 133 var title = this.getChildValue(item, "*", "title", this.featureTitle); 134 135 /* First try RSS descriptions, then Atom summaries */ 136 var description = this.getChildValue( 137 item, "*", "description", 138 this.getChildValue(item, "*", "content", this.featureDescription) 139 ); 140 141 /* If no link URL is found in the first child node, try the 142 href attribute */ 143 var link = this.getChildValue(item, "*", "link"); 144 if(!link) { 145 try { 146 link = this.getElementsByTagNameNS(item, "*", "link")[0].getAttribute("href"); 147 } catch(e) { 148 link = null; 149 } 150 } 151 152 var id = this.getChildValue(item, "*", "id", null); 153 154 var data = { 155 "title": title, 156 "description": description, 157 "link": link 158 }; 159 var feature = new OpenLayers.Feature.Vector(geometry, data); 160 feature.fid = id; 161 return feature; 162 }, 163 164 /** 165 * Method: getChildValue 166 * 167 * Parameters: 168 * node - {DOMElement} 169 * nsuri - {String} Child node namespace uri ("*" for any). 170 * name - {String} Child node name. 171 * def - {String} Optional string default to return if no child found. 172 * 173 * Returns: 174 * {String} The value of the first child with the given tag name. Returns 175 * default value or empty string if none found. 176 */ 177 getChildValue: function(node, nsuri, name, def) { 178 var value; 179 try { 180 value = this.getElementsByTagNameNS(node, nsuri, name)[0].firstChild.nodeValue; 181 } catch(e) { 182 value = (def == undefined) ? "" : def; 183 } 184 return value; 185 }, 186 187 /** 188 * APIMethod: read 189 * Return a list of features from a GeoRSS doc 190 191 * Parameters: 192 * data - {Element} 193 * 194 * Returns: 195 * An Array of <OpenLayers.Feature.Vector>s 196 */ 197 read: function(doc) { 198 if (typeof doc == "string") { 199 doc = OpenLayers.Format.XML.prototype.read.apply(this, [doc]); 200 } 201 202 /* Try RSS items first, then Atom entries */ 203 var itemlist = null; 204 itemlist = this.getElementsByTagNameNS(doc, '*', 'item'); 205 if (itemlist.length == 0) { 206 itemlist = this.getElementsByTagNameNS(doc, '*', 'entry'); 207 } 208 209 var numItems = itemlist.length; 210 var features = new Array(numItems); 211 for(var i=0; i<numItems; i++) { 212 features[i] = this.createFeatureFromItem(itemlist[i]); 213 } 214 return features; 215 }, 216 47 217 48 218 /** … … 53 223 * features - Array({<OpenLayers.Feature.Vector>}) List of features to serialize into a string. 54 224 */ 55 write: function(features) { 56 var featureCollection = document.createElementNS(this.rssns, "rss"); 57 for (var i=0; i < features.length; i++) { 58 featureCollection.appendChild(this.createFeatureXML(features[i])); 59 } 60 return featureCollection; 61 }, 62 225 write: function(features) { 226 var georss; 227 if(features instanceof Array) { 228 georss = this.createElementNS(this.rssns, "rss"); 229 for(var i=0; i < features.length; i++) { 230 georss.appendChild(this.createFeatureXML(features[i])); 231 } 232 } else { 233 georss = this.createFeatureXML(features); 234 } 235 return OpenLayers.Format.XML.prototype.write.apply(this, [georss]); 236 }, 237 63 238 /** 64 239 * Method: createFeatureXML … … 73 248 createFeatureXML: function(feature) { 74 249 var geometryNode = this.buildGeometryNode(feature.geometry); 75 var featureNode = document.createElementNS(this.rssns, "item");76 var titleNode = document.createElementNS(this.rssns, "title");77 titleNode.appendChild( document.createTextNode(feature.attributes.title ? feature.attributes.title : ""));78 var descNode = document.createElementNS(this.rssns, "description");79 descNode.appendChild( document.createTextNode(feature.attributes.description ? feature.attributes.description : ""));250 var featureNode = this.createElementNS(this.rssns, "item"); 251 var titleNode = this.createElementNS(this.rssns, "title"); 252 titleNode.appendChild(this.createTextNode(feature.attributes.title ? feature.attributes.title : "")); 253 var descNode = this.createElementNS(this.rssns, "description"); 254 descNode.appendChild(this.createTextNode(feature.attributes.description ? feature.attributes.description : "")); 80 255 featureNode.appendChild(titleNode); 81 256 featureNode.appendChild(descNode); 257 if (feature.attributes.link) { 258 var linkNode = this.createElementNS(this.rssns, "link"); 259 linkNode.appendChild(this.createTextNode(feature.attributes.link)); 260 featureNode.appendChild(linkNode); 261 } 82 262 for(var attr in feature.attributes) { 83 var attrText = document.createTextNode(feature.attributes[attr]); 263 if (attr == "link" || attr == "title" || attr == "description") { continue; } 264 var attrText = this.createTextNode(feature.attributes[attr]); 84 265 var nodename = attr; 85 266 if (attr.search(":") != -1) { 86 267 nodename = attr.split(":")[1]; 87 268 } 88 var attrContainer = document.createElementNS(this.featureNS, "feature:"+nodename);269 var attrContainer = this.createElementNS(this.featureNS, "feature:"+nodename); 89 270 attrContainer.appendChild(attrText); 90 271 featureNode.appendChild(attrContainer); … … 99 280 * 100 281 * Parameters: 101 * geometry - {<OpenLayers.Geometry>} 282 * geometry - {<OpenLayers.Geometry>} 283 * 284 * Returns: 285 * {DOMElement} A gml node. 102 286 */ 103 287 buildGeometryNode: function(geometry) { 104 var gml = "";105 // match MultiPolygon orPolygon288 var node; 289 // match Polygon 106 290 if (geometry.CLASS_NAME == "OpenLayers.Geometry.Polygon") { 107 gml = document.createElementNS(this.georssns, 'georss:polygon');108 109 gml.appendChild(this.buildCoordinatesNode(geometry.components[0]));110 }111 // match MultiLineString orLineString291 node = this.createElementNS(this.georssns, 'georss:polygon'); 292 293 node.appendChild(this.buildCoordinatesNode(geometry.components[0])); 294 } 295 // match LineString 112 296 else if (geometry.CLASS_NAME == "OpenLayers.Geometry.LineString") { 113 gml = document.createElementNS(this.georssns, 'georss:line');114 115 gml.appendChild(this.buildCoordinatesNode(geometry));116 }117 // match MultiPoint orPoint297 node = this.createElementNS(this.georssns, 'georss:line'); 298 299 node.appendChild(this.buildCoordinatesNode(geometry)); 300 } 301 // match Point 118 302 else if (geometry.CLASS_NAME == "OpenLayers.Geometry.Point") { 119 gml = document.createElementNS(this.georssns, 'georss:point');120 gml.appendChild(this.buildCoordinatesNode(geometry));121 } else { 122 alert("Couldn't parse " + geometry.CLASS_NAME);303 node = this.createElementNS(this.georssns, 'georss:point'); 304 node.appendChild(this.buildCoordinatesNode(geometry)); 305 } else { 306 throw "Couldn't parse " + geometry.CLASS_NAME; 123 307 } 124 return gml;308 return node; 125 309 }, 126 310 … … 138 322 } 139 323 140 var path = "";324 var path; 141 325 if (points) { 142 for (var i = 0; i < points.length; i++) { 143 path += points[i].y + " " + points[i].x + " "; 144 } 326 var numPoints = points.length; 327 var parts = new Array(numPoints); 328 for (var i = 0; i < numPoints; i++) { 329 parts[i] = points[i].y + " " + points[i].x; 330 } 331 path = parts.join(" "); 145 332 } else { 146 path += geometry.y + " " + geometry.x + " ";147 } 148 return document.createTextNode(path);333 path = geometry.y + " " + geometry.x; 334 } 335 return this.createTextNode(path); 149 336 }, 150 337 trunk/openlayers/tests/Format/test_GeoRSS.html
r4156 r4305 19 19 t.plan(1); 20 20 21 if (OpenLayers.Util.getBrowserName() == "msie") { 22 //serialization currently not supported in IE 23 t.ok(true, "GeoRSS serialization not currently supported in IE"); 24 } else { 25 var parser = new OpenLayers.Format.GeoRSS(); 26 var point = new OpenLayers.Geometry.Point(-111.04, 45.68); 27 var point2 = new OpenLayers.Geometry.Point(-112.04, 45.68); 28 var l = new OpenLayers.Geometry.LineString([point, point2]); 29 var f = new OpenLayers.Feature.Vector(l); 30 var data = parser.write([f]); 31 t.eq(data.firstChild.childNodes[2].firstChild.nodeValue, '45.68 -111.04 45.68 -112.04 ', 'GeoRSS serializes a line correctly'); 21 var parser = new OpenLayers.Format.GeoRSS(); 22 var point = new OpenLayers.Geometry.Point(-111.04, 45.68); 23 var point2 = new OpenLayers.Geometry.Point(-112.04, 45.68); 24 var l = new OpenLayers.Geometry.LineString([point, point2]); 25 var f = new OpenLayers.Feature.Vector(l); 26 var data = parser.write([f]); 27 t.eq(data, '<rss xmlns="http://backend.userland.com/rss2"><item><title></title><description></description><georss:line xmlns:georss="http://www.georss.org/georss">45.68 -111.04 45.68 -112.04</georss:line></item></rss>', 'GeoRSS serializes a line correctly'); 28 } 29 function test_Format_GeoRSS_roundtrip(t) { 30 t.plan(input.length); 31 var parser = new OpenLayers.Format.GeoRSS(); 32 for(var i=0; i < input.length; i++) { 33 var feed = shell_start+input[i]+shell_end; 34 var data = parser.read(feed); 35 var out = parser.write(data); 36 t.eq(out, output[i], "Output gave expected value"); 32 37 } 33 38 } 39 40 var shell_start = '<feed xmlns="http://www.w3.org/2005/Atom" \n xmlns:georss="http://www.georss.org/georss">\n <title>scribble</title>\n <id>http://featureserver.org/featureserver.cgi/scribble?format=atom</id>\n <author><name>FeatureServer</name></author>\n'; 41 var shell_end = '</feed>'; 42 var input = ['<entry><id>http://featureserver.org/featureserver.cgi/scribble/562.atom</id><link href="http://featureserver.org/featureserver.cgi/scribble/562.atom"/><title>Feature 2</title><content type="html"><b>strokeColor</b>: red<br /><b>title</b>: Feature 2<br /><b>author</b>: Your Name Here</content><georss:polygon>-5.9765625 -131.484375 -58.0078125 -112.5 -50.2734375 -32.34375 52.3828125 -114.609375 -35.5078125 -167.34375 -57.3046875 -146.953125 -34.1015625 -139.921875 -5.9765625 -131.484375</georss:polygon></entry>', 43 '<entry><id>http://featureserver.org/featureserver.cgi/scribble/796.atom</id><link href="http://featureserver.org/featureserver.cgi/scribble/796.atom"/><title>Feature 2</title><content type="html"><b>strokeColor</b>: 00ccff<br /><b>title</b>: Feature 2<br /><b>author</b>: Your Name Here</content><georss:point>75.5859375 15.46875</georss:point></entry>', 44 '<entry><id>http://featureserver.org/featureserver.cgi/scribble/794.atom</id><link href="http://featureserver.org/featureserver.cgi/scribble/794.atom"/><title>Feature 5</title><content type="html"><b>strokeColor</b>: red<br /><b>title</b>: Feature 5<br /><b>author</b>: Your Name Here</content><georss:line>28.828125 32.6953125 49.921875 34.8046875 39.375 58.0078125 39.375 58.0078125 40.078125 58.0078125 41.484375 58.0078125 43.59375 58.0078125 45.703125 58.7109375 47.8125 58.7109375 49.21875 58.7109375 51.328125 59.4140625 52.03125 59.4140625 54.140625 60.8203125 56.25 61.5234375 56.25 62.2265625 57.65625 62.2265625 57.65625 62.9296875 58.359375 63.6328125 58.359375 65.0390625 58.359375 65.7421875 59.0625 66.4453125 59.0625 67.1484375 59.0625 68.5546875 59.765625 69.9609375 59.765625 72.0703125 59.765625 73.4765625 59.765625 76.2890625 59.765625 78.3984375 59.765625 79.8046875 59.765625 81.9140625 59.765625 83.3203125 59.0625 84.7265625 59.0625 86.8359375 58.359375 87.5390625 58.359375 88.2421875 56.953125 89.6484375 56.25 91.0546875 54.84375 93.8671875 52.03125 96.6796875 51.328125 98.7890625 50.625 100.1953125 49.21875 102.3046875 48.515625 103.7109375 47.8125 104.4140625 47.109375 105.8203125 46.40625 106.5234375 46.40625 107.9296875 45.703125 109.3359375 45 110.7421875 43.59375 112.8515625 43.59375 114.2578125 43.59375 114.9609375 42.890625 117.0703125 42.890625 117.7734375 42.1875 118.4765625 42.1875 119.1796875 42.1875 119.8828125</georss:line></entry>' 45 ]; 46 var output= ['<rss xmlns="http://backend.userland.com/rss2"><item><title>Feature 2</title><description><b>strokeColor</b>: red<br /><b>title</b>: Feature 2<br /><b>author</b>: Your Name Here</description><link>http://featureserver.org/featureserver.cgi/scribble/562.atom</link><georss:polygon xmlns:georss="http://www.georss.org/georss">-5.9765625 -131.484375 -58.0078125 -112.5 -50.2734375 -32.34375 52.3828125 -114.609375 -35.5078125 -167.34375 -57.3046875 -146.953125 -34.1015625 -139.921875 -5.9765625 -131.484375</georss:polygon></item></rss>', 47 '<rss xmlns="http://backend.userland.com/rss2"><item><title>Feature 2</title><description><b>strokeColor</b>: 00ccff<br /><b>title</b>: Feature 2<br /><b>author</b>: Your Name Here</description><link>http://featureserver.org/featureserver.cgi/scribble/796.atom</link><georss:point xmlns:georss="http://www.georss.org/georss">75.5859375 15.46875</georss:point></item></rss>', 48 '<rss xmlns="http://backend.userland.com/rss2"><item><title>Feature 5</title><description><b>strokeColor</b>: red<br /><b>title</b>: Feature 5<br /><b>author</b>: Your Name Here</description><link>http://featureserver.org/featureserver.cgi/scribble/794.atom</link><georss:line xmlns:georss="http://www.georss.org/georss">28.828125 32.6953125 49.921875 34.8046875 39.375 58.0078125 39.375 58.0078125 40.078125 58.0078125 41.484375 58.0078125 43.59375 58.0078125 45.703125 58.7109375 47.8125 58.7109375 49.21875 58.7109375 51.328125 59.4140625 52.03125 59.4140625 54.140625 60.8203125 56.25 61.5234375 56.25 62.2265625 57.65625 62.2265625 57.65625 62.9296875 58.359375 63.6328125 58.359375 65.0390625 58.359375 65.7421875 59.0625 66.4453125 59.0625 67.1484375 59.0625 68.5546875 59.765625 69.9609375 59.765625 72.0703125 59.765625 73.4765625 59.765625 76.2890625 59.765625 78.3984375 59.765625 79.8046875 59.765625 81.9140625 59.765625 83.3203125 59.0625 84.7265625 59.0625 86.8359375 58.359375 87.5390625 58.359375 88.2421875 56.953125 89.6484375 56.25 91.0546875 54.84375 93.8671875 52.03125 96.6796875 51.328125 98.7890625 50.625 100.1953125 49.21875 102.3046875 48.515625 103.7109375 47.8125 104.4140625 47.109375 105.8203125 46.40625 106.5234375 46.40625 107.9296875 45.703125 109.3359375 45 110.7421875 43.59375 112.8515625 43.59375 114.2578125 43.59375 114.9609375 42.890625 117.0703125 42.890625 117.7734375 42.1875 118.4765625 42.1875 119.1796875 42.1875 119.8828125</georss:line></item></rss>']; 34 49 35 50 </script>
