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