[76] | 1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
| 2 | * full list of contributors). Published under the Clear BSD license. |
---|
| 3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
| 4 | * full text of the license. */ |
---|
| 5 | |
---|
| 6 | /** |
---|
| 7 | * @requires OpenLayers/Format/XML.js |
---|
| 8 | * @requires OpenLayers/Feature/Vector.js |
---|
| 9 | * @requires OpenLayers/Geometry/Point.js |
---|
| 10 | * @requires OpenLayers/Geometry/LineString.js |
---|
| 11 | * @requires OpenLayers/Geometry/Polygon.js |
---|
| 12 | */ |
---|
| 13 | |
---|
| 14 | /** |
---|
| 15 | * Class: OpenLayers.Format.GeoRSS |
---|
| 16 | * Read/write GeoRSS parser. Create a new instance with the |
---|
| 17 | * <OpenLayers.Format.GeoRSS> constructor. |
---|
| 18 | * |
---|
| 19 | * Inherits from: |
---|
| 20 | * - <OpenLayers.Format.XML> |
---|
| 21 | */ |
---|
| 22 | OpenLayers.Format.GeoRSS = OpenLayers.Class(OpenLayers.Format.XML, { |
---|
| 23 | |
---|
| 24 | /** |
---|
| 25 | * APIProperty: rssns |
---|
| 26 | * {String} RSS namespace to use. Defaults to |
---|
| 27 | * "http://backend.userland.com/rss2" |
---|
| 28 | */ |
---|
| 29 | rssns: "http://backend.userland.com/rss2", |
---|
| 30 | |
---|
| 31 | /** |
---|
| 32 | * APIProperty: featurens |
---|
| 33 | * {String} Feature Attributes namespace. Defaults to |
---|
| 34 | * "http://mapserver.gis.umn.edu/mapserver" |
---|
| 35 | */ |
---|
| 36 | featureNS: "http://mapserver.gis.umn.edu/mapserver", |
---|
| 37 | |
---|
| 38 | /** |
---|
| 39 | * APIProperty: georssns |
---|
| 40 | * {String} GeoRSS namespace to use. Defaults to |
---|
| 41 | * "http://www.georss.org/georss" |
---|
| 42 | */ |
---|
| 43 | georssns: "http://www.georss.org/georss", |
---|
| 44 | |
---|
| 45 | /** |
---|
| 46 | * APIProperty: geons |
---|
| 47 | * {String} W3C Geo namespace to use. Defaults to |
---|
| 48 | * "http://www.w3.org/2003/01/geo/wgs84_pos#" |
---|
| 49 | */ |
---|
| 50 | geons: "http://www.w3.org/2003/01/geo/wgs84_pos#", |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | * APIProperty: featureTitle |
---|
| 54 | * {String} Default title for features. Defaults to "Untitled" |
---|
| 55 | */ |
---|
| 56 | featureTitle: "Untitled", |
---|
| 57 | |
---|
| 58 | /** |
---|
| 59 | * APIProperty: featureDescription |
---|
| 60 | * {String} Default description for features. Defaults to "No Description" |
---|
| 61 | */ |
---|
| 62 | featureDescription: "No Description", |
---|
| 63 | |
---|
| 64 | /** |
---|
| 65 | * Property: gmlParse |
---|
| 66 | * {Object} GML Format object for parsing features |
---|
| 67 | * Non-API and only created if necessary |
---|
| 68 | */ |
---|
| 69 | gmlParser: null, |
---|
| 70 | |
---|
| 71 | /** |
---|
| 72 | * APIProperty: xy |
---|
| 73 | * {Boolean} Order of the GML coordinate: true:(x,y) or false:(y,x) |
---|
| 74 | * For GeoRSS the default is (y,x), therefore: false |
---|
| 75 | */ |
---|
| 76 | xy: false, |
---|
| 77 | |
---|
| 78 | /** |
---|
| 79 | * Constructor: OpenLayers.Format.GeoRSS |
---|
| 80 | * Create a new parser for GeoRSS. |
---|
| 81 | * |
---|
| 82 | * Parameters: |
---|
| 83 | * options - {Object} An optional object whose properties will be set on |
---|
| 84 | * this instance. |
---|
| 85 | */ |
---|
| 86 | initialize: function(options) { |
---|
| 87 | OpenLayers.Format.XML.prototype.initialize.apply(this, [options]); |
---|
| 88 | }, |
---|
| 89 | |
---|
| 90 | /** |
---|
| 91 | * Method: createGeometryFromItem |
---|
| 92 | * Return a geometry from a GeoRSS Item. |
---|
| 93 | * |
---|
| 94 | * Parameters: |
---|
| 95 | * item - {DOMElement} A GeoRSS item node. |
---|
| 96 | * |
---|
| 97 | * Returns: |
---|
| 98 | * {<OpenLayers.Geometry>} A geometry representing the node. |
---|
| 99 | */ |
---|
| 100 | createGeometryFromItem: function(item) { |
---|
| 101 | var point = this.getElementsByTagNameNS(item, this.georssns, "point"); |
---|
| 102 | var lat = this.getElementsByTagNameNS(item, this.geons, 'lat'); |
---|
| 103 | var lon = this.getElementsByTagNameNS(item, this.geons, 'long'); |
---|
| 104 | |
---|
| 105 | var line = this.getElementsByTagNameNS(item, |
---|
| 106 | this.georssns, |
---|
| 107 | "line"); |
---|
| 108 | var polygon = this.getElementsByTagNameNS(item, |
---|
| 109 | this.georssns, |
---|
| 110 | "polygon"); |
---|
| 111 | var where = this.getElementsByTagNameNS(item, |
---|
| 112 | this.georssns, |
---|
| 113 | "where"); |
---|
| 114 | var box = this.getElementsByTagNameNS(item, |
---|
| 115 | this.georssns, |
---|
| 116 | "box"); |
---|
| 117 | |
---|
| 118 | if (point.length > 0 || (lat.length > 0 && lon.length > 0)) { |
---|
| 119 | var location; |
---|
| 120 | if (point.length > 0) { |
---|
| 121 | location = OpenLayers.String.trim( |
---|
| 122 | point[0].firstChild.nodeValue).split(/\s+/); |
---|
| 123 | if (location.length !=2) { |
---|
| 124 | location = OpenLayers.String.trim( |
---|
| 125 | point[0].firstChild.nodeValue).split(/\s*,\s*/); |
---|
| 126 | } |
---|
| 127 | } else { |
---|
| 128 | location = [parseFloat(lat[0].firstChild.nodeValue), |
---|
| 129 | parseFloat(lon[0].firstChild.nodeValue)]; |
---|
| 130 | } |
---|
| 131 | |
---|
| 132 | var geometry = new OpenLayers.Geometry.Point(parseFloat(location[1]), |
---|
| 133 | parseFloat(location[0])); |
---|
| 134 | |
---|
| 135 | } else if (line.length > 0) { |
---|
| 136 | var coords = OpenLayers.String.trim(this.concatChildValues(line[0])).split(/\s+/); |
---|
| 137 | var components = []; |
---|
| 138 | var point; |
---|
| 139 | for (var i=0, len=coords.length; i<len; i+=2) { |
---|
| 140 | point = new OpenLayers.Geometry.Point(parseFloat(coords[i+1]), |
---|
| 141 | parseFloat(coords[i])); |
---|
| 142 | components.push(point); |
---|
| 143 | } |
---|
| 144 | geometry = new OpenLayers.Geometry.LineString(components); |
---|
| 145 | } else if (polygon.length > 0) { |
---|
| 146 | var coords = OpenLayers.String.trim(this.concatChildValues(polygon[0])).split(/\s+/); |
---|
| 147 | var components = []; |
---|
| 148 | var point; |
---|
| 149 | for (var i=0, len=coords.length; i<len; i+=2) { |
---|
| 150 | point = new OpenLayers.Geometry.Point(parseFloat(coords[i+1]), |
---|
| 151 | parseFloat(coords[i])); |
---|
| 152 | components.push(point); |
---|
| 153 | } |
---|
| 154 | geometry = new OpenLayers.Geometry.Polygon([new OpenLayers.Geometry.LinearRing(components)]); |
---|
| 155 | } else if (where.length > 0) { |
---|
| 156 | if (!this.gmlParser) { |
---|
| 157 | this.gmlParser = new OpenLayers.Format.GML({'xy': this.xy}); |
---|
| 158 | } |
---|
| 159 | var feature = this.gmlParser.parseFeature(where[0]); |
---|
| 160 | geometry = feature.geometry; |
---|
| 161 | } else if (box.length > 0) { |
---|
| 162 | var coords = OpenLayers.String.trim(box[0].firstChild.nodeValue).split(/\s+/); |
---|
| 163 | var components = []; |
---|
| 164 | var point; |
---|
| 165 | if (coords.length > 3) { |
---|
| 166 | point = new OpenLayers.Geometry.Point(parseFloat(coords[1]), |
---|
| 167 | parseFloat(coords[0])); |
---|
| 168 | components.push(point); |
---|
| 169 | point = new OpenLayers.Geometry.Point(parseFloat(coords[1]), |
---|
| 170 | parseFloat(coords[2])); |
---|
| 171 | components.push(point); |
---|
| 172 | point = new OpenLayers.Geometry.Point(parseFloat(coords[3]), |
---|
| 173 | parseFloat(coords[2])); |
---|
| 174 | components.push(point); |
---|
| 175 | point = new OpenLayers.Geometry.Point(parseFloat(coords[3]), |
---|
| 176 | parseFloat(coords[0])); |
---|
| 177 | components.push(point); |
---|
| 178 | point = new OpenLayers.Geometry.Point(parseFloat(coords[1]), |
---|
| 179 | parseFloat(coords[0])); |
---|
| 180 | components.push(point); |
---|
| 181 | } |
---|
| 182 | geometry = new OpenLayers.Geometry.Polygon([new OpenLayers.Geometry.LinearRing(components)]); |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | if (geometry && this.internalProjection && this.externalProjection) { |
---|
| 186 | geometry.transform(this.externalProjection, |
---|
| 187 | this.internalProjection); |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | return geometry; |
---|
| 191 | }, |
---|
| 192 | |
---|
| 193 | /** |
---|
| 194 | * Method: createFeatureFromItem |
---|
| 195 | * Return a feature from a GeoRSS Item. |
---|
| 196 | * |
---|
| 197 | * Parameters: |
---|
| 198 | * item - {DOMElement} A GeoRSS item node. |
---|
| 199 | * |
---|
| 200 | * Returns: |
---|
| 201 | * {<OpenLayers.Feature.Vector>} A feature representing the item. |
---|
| 202 | */ |
---|
| 203 | createFeatureFromItem: function(item) { |
---|
| 204 | var geometry = this.createGeometryFromItem(item); |
---|
| 205 | |
---|
| 206 | /* Provide defaults for title and description */ |
---|
| 207 | var title = this.getChildValue(item, "*", "title", this.featureTitle); |
---|
| 208 | |
---|
| 209 | /* First try RSS descriptions, then Atom summaries */ |
---|
| 210 | var description = this.getChildValue( |
---|
| 211 | item, "*", "description", |
---|
| 212 | this.getChildValue(item, "*", "content", |
---|
| 213 | this.getChildValue(item, "*", "summary", this.featureDescription))); |
---|
| 214 | |
---|
| 215 | /* If no link URL is found in the first child node, try the |
---|
| 216 | href attribute */ |
---|
| 217 | var link = this.getChildValue(item, "*", "link"); |
---|
| 218 | if(!link) { |
---|
| 219 | try { |
---|
| 220 | link = this.getElementsByTagNameNS(item, "*", "link")[0].getAttribute("href"); |
---|
| 221 | } catch(e) { |
---|
| 222 | link = null; |
---|
| 223 | } |
---|
| 224 | } |
---|
| 225 | |
---|
| 226 | var id = this.getChildValue(item, "*", "id", null); |
---|
| 227 | |
---|
| 228 | var data = { |
---|
| 229 | "title": title, |
---|
| 230 | "description": description, |
---|
| 231 | "link": link |
---|
| 232 | }; |
---|
| 233 | var feature = new OpenLayers.Feature.Vector(geometry, data); |
---|
| 234 | feature.fid = id; |
---|
| 235 | return feature; |
---|
| 236 | }, |
---|
| 237 | |
---|
| 238 | /** |
---|
| 239 | * Method: getChildValue |
---|
| 240 | * |
---|
| 241 | * Parameters: |
---|
| 242 | * node - {DOMElement} |
---|
| 243 | * nsuri - {String} Child node namespace uri ("*" for any). |
---|
| 244 | * name - {String} Child node name. |
---|
| 245 | * def - {String} Optional string default to return if no child found. |
---|
| 246 | * |
---|
| 247 | * Returns: |
---|
| 248 | * {String} The value of the first child with the given tag name. Returns |
---|
| 249 | * default value or empty string if none found. |
---|
| 250 | */ |
---|
| 251 | getChildValue: function(node, nsuri, name, def) { |
---|
| 252 | var value; |
---|
| 253 | var eles = this.getElementsByTagNameNS(node, nsuri, name); |
---|
| 254 | if(eles && eles[0] && eles[0].firstChild |
---|
| 255 | && eles[0].firstChild.nodeValue) { |
---|
| 256 | value = eles[0].firstChild.nodeValue; |
---|
| 257 | } else { |
---|
| 258 | value = (def == undefined) ? "" : def; |
---|
| 259 | } |
---|
| 260 | return value; |
---|
| 261 | }, |
---|
| 262 | |
---|
| 263 | /** |
---|
| 264 | * APIMethod: read |
---|
| 265 | * Return a list of features from a GeoRSS doc |
---|
| 266 | |
---|
| 267 | * Parameters: |
---|
| 268 | * data - {Element} |
---|
| 269 | * |
---|
| 270 | * Returns: |
---|
| 271 | * An Array of <OpenLayers.Feature.Vector>s |
---|
| 272 | */ |
---|
| 273 | read: function(doc) { |
---|
| 274 | if (typeof doc == "string") { |
---|
| 275 | doc = OpenLayers.Format.XML.prototype.read.apply(this, [doc]); |
---|
| 276 | } |
---|
| 277 | |
---|
| 278 | /* Try RSS items first, then Atom entries */ |
---|
| 279 | var itemlist = null; |
---|
| 280 | itemlist = this.getElementsByTagNameNS(doc, '*', 'item'); |
---|
| 281 | if (itemlist.length == 0) { |
---|
| 282 | itemlist = this.getElementsByTagNameNS(doc, '*', 'entry'); |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | var numItems = itemlist.length; |
---|
| 286 | var features = new Array(numItems); |
---|
| 287 | for(var i=0; i<numItems; i++) { |
---|
| 288 | features[i] = this.createFeatureFromItem(itemlist[i]); |
---|
| 289 | } |
---|
| 290 | return features; |
---|
| 291 | }, |
---|
| 292 | |
---|
| 293 | |
---|
| 294 | /** |
---|
| 295 | * APIMethod: write |
---|
| 296 | * Accept Feature Collection, and return a string. |
---|
| 297 | * |
---|
| 298 | * Parameters: |
---|
| 299 | * features - {Array(<OpenLayers.Feature.Vector>)} List of features to serialize into a string. |
---|
| 300 | */ |
---|
| 301 | write: function(features) { |
---|
| 302 | var georss; |
---|
| 303 | if(features instanceof Array) { |
---|
| 304 | georss = this.createElementNS(this.rssns, "rss"); |
---|
| 305 | for(var i=0, len=features.length; i<len; i++) { |
---|
| 306 | georss.appendChild(this.createFeatureXML(features[i])); |
---|
| 307 | } |
---|
| 308 | } else { |
---|
| 309 | georss = this.createFeatureXML(features); |
---|
| 310 | } |
---|
| 311 | return OpenLayers.Format.XML.prototype.write.apply(this, [georss]); |
---|
| 312 | }, |
---|
| 313 | |
---|
| 314 | /** |
---|
| 315 | * Method: createFeatureXML |
---|
| 316 | * Accept an <OpenLayers.Feature.Vector>, and build a geometry for it. |
---|
| 317 | * |
---|
| 318 | * Parameters: |
---|
| 319 | * feature - {<OpenLayers.Feature.Vector>} |
---|
| 320 | * |
---|
| 321 | * Returns: |
---|
| 322 | * {DOMElement} |
---|
| 323 | */ |
---|
| 324 | createFeatureXML: function(feature) { |
---|
| 325 | var geometryNode = this.buildGeometryNode(feature.geometry); |
---|
| 326 | var featureNode = this.createElementNS(this.rssns, "item"); |
---|
| 327 | var titleNode = this.createElementNS(this.rssns, "title"); |
---|
| 328 | titleNode.appendChild(this.createTextNode(feature.attributes.title ? feature.attributes.title : "")); |
---|
| 329 | var descNode = this.createElementNS(this.rssns, "description"); |
---|
| 330 | descNode.appendChild(this.createTextNode(feature.attributes.description ? feature.attributes.description : "")); |
---|
| 331 | featureNode.appendChild(titleNode); |
---|
| 332 | featureNode.appendChild(descNode); |
---|
| 333 | if (feature.attributes.link) { |
---|
| 334 | var linkNode = this.createElementNS(this.rssns, "link"); |
---|
| 335 | linkNode.appendChild(this.createTextNode(feature.attributes.link)); |
---|
| 336 | featureNode.appendChild(linkNode); |
---|
| 337 | } |
---|
| 338 | for(var attr in feature.attributes) { |
---|
| 339 | if (attr == "link" || attr == "title" || attr == "description") { continue; } |
---|
| 340 | var attrText = this.createTextNode(feature.attributes[attr]); |
---|
| 341 | var nodename = attr; |
---|
| 342 | if (attr.search(":") != -1) { |
---|
| 343 | nodename = attr.split(":")[1]; |
---|
| 344 | } |
---|
| 345 | var attrContainer = this.createElementNS(this.featureNS, "feature:"+nodename); |
---|
| 346 | attrContainer.appendChild(attrText); |
---|
| 347 | featureNode.appendChild(attrContainer); |
---|
| 348 | } |
---|
| 349 | featureNode.appendChild(geometryNode); |
---|
| 350 | return featureNode; |
---|
| 351 | }, |
---|
| 352 | |
---|
| 353 | /** |
---|
| 354 | * Method: buildGeometryNode |
---|
| 355 | * builds a GeoRSS node with a given geometry |
---|
| 356 | * |
---|
| 357 | * Parameters: |
---|
| 358 | * geometry - {<OpenLayers.Geometry>} |
---|
| 359 | * |
---|
| 360 | * Returns: |
---|
| 361 | * {DOMElement} A gml node. |
---|
| 362 | */ |
---|
| 363 | buildGeometryNode: function(geometry) { |
---|
| 364 | if (this.internalProjection && this.externalProjection) { |
---|
| 365 | geometry = geometry.clone(); |
---|
| 366 | geometry.transform(this.internalProjection, |
---|
| 367 | this.externalProjection); |
---|
| 368 | } |
---|
| 369 | var node; |
---|
| 370 | // match Polygon |
---|
| 371 | if (geometry.CLASS_NAME == "OpenLayers.Geometry.Polygon") { |
---|
| 372 | node = this.createElementNS(this.georssns, 'georss:polygon'); |
---|
| 373 | |
---|
| 374 | node.appendChild(this.buildCoordinatesNode(geometry.components[0])); |
---|
| 375 | } |
---|
| 376 | // match LineString |
---|
| 377 | else if (geometry.CLASS_NAME == "OpenLayers.Geometry.LineString") { |
---|
| 378 | node = this.createElementNS(this.georssns, 'georss:line'); |
---|
| 379 | |
---|
| 380 | node.appendChild(this.buildCoordinatesNode(geometry)); |
---|
| 381 | } |
---|
| 382 | // match Point |
---|
| 383 | else if (geometry.CLASS_NAME == "OpenLayers.Geometry.Point") { |
---|
| 384 | node = this.createElementNS(this.georssns, 'georss:point'); |
---|
| 385 | node.appendChild(this.buildCoordinatesNode(geometry)); |
---|
| 386 | } else { |
---|
| 387 | throw "Couldn't parse " + geometry.CLASS_NAME; |
---|
| 388 | } |
---|
| 389 | return node; |
---|
| 390 | }, |
---|
| 391 | |
---|
| 392 | /** |
---|
| 393 | * Method: buildCoordinatesNode |
---|
| 394 | * |
---|
| 395 | * Parameters: |
---|
| 396 | * geometry - {<OpenLayers.Geometry>} |
---|
| 397 | */ |
---|
| 398 | buildCoordinatesNode: function(geometry) { |
---|
| 399 | var points = null; |
---|
| 400 | |
---|
| 401 | if (geometry.components) { |
---|
| 402 | points = geometry.components; |
---|
| 403 | } |
---|
| 404 | |
---|
| 405 | var path; |
---|
| 406 | if (points) { |
---|
| 407 | var numPoints = points.length; |
---|
| 408 | var parts = new Array(numPoints); |
---|
| 409 | for (var i = 0; i < numPoints; i++) { |
---|
| 410 | parts[i] = points[i].y + " " + points[i].x; |
---|
| 411 | } |
---|
| 412 | path = parts.join(" "); |
---|
| 413 | } else { |
---|
| 414 | path = geometry.y + " " + geometry.x; |
---|
| 415 | } |
---|
| 416 | return this.createTextNode(path); |
---|
| 417 | }, |
---|
| 418 | |
---|
| 419 | CLASS_NAME: "OpenLayers.Format.GeoRSS" |
---|
| 420 | }); |
---|