Bienvenue sur PostGIS.fr

Bienvenue sur PostGIS.fr , le site de la communauté des utilisateurs francophones de PostGIS.

PostGIS ajoute le support d'objets géographique à la base de données PostgreSQL. En effet, PostGIS "spatialise" le serverur PostgreSQL, ce qui permet de l'utiliser comme une base de données SIG.

Maintenu à jour, en fonction de nos disponibilités et des diverses sorties des outils que nous testons, nous vous proposons l'ensemble de nos travaux publiés en langue française.

source: trunk/workshop-routing-foss4g/web/OpenLayers/lib/OpenLayers/Format/GML/Base.js @ 76

Revision 76, 21.4 KB checked in by djay, 12 years ago (diff)

Ajout du répertoire web

  • Property svn:executable set to *
Line 
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/Format/GML.js
9 */
10
11/**
12 * Though required in the full build, if the GML format is excluded, we set
13 * the namespace here.
14 */
15if(!OpenLayers.Format.GML) {
16    OpenLayers.Format.GML = {};
17}
18
19/**
20 * Class: OpenLayers.Format.GML.Base
21 * Superclass for GML parsers.
22 *
23 * Inherits from:
24 *  - <OpenLayers.Format.XML>
25 */
26OpenLayers.Format.GML.Base = OpenLayers.Class(OpenLayers.Format.XML, {
27   
28    /**
29     * Property: namespaces
30     * {Object} Mapping of namespace aliases to namespace URIs.
31     */
32    namespaces: {
33        gml: "http://www.opengis.net/gml",
34        xlink: "http://www.w3.org/1999/xlink",
35        xsi: "http://www.w3.org/2001/XMLSchema-instance",
36        wfs: "http://www.opengis.net/wfs" // this is a convenience for reading wfs:FeatureCollection
37    },
38   
39    /**
40     * Property: defaultPrefix
41     */
42    defaultPrefix: "gml",
43
44    /**
45     * Property: schemaLocation
46     * {String} Schema location for a particular minor version.
47     */
48    schemaLocation: null,
49   
50    /**
51     * APIProperty: featureType
52     * {Array(String) or String} The local (without prefix) feature typeName(s).
53     */
54    featureType: null,
55   
56    /**
57     * APIProperty: featureNS
58     * {String} The feature namespace.  Must be set in the options at
59     *     construction.
60     */
61    featureNS: null,
62
63    /**
64     * APIProperty: geometry
65     * {String} Name of geometry element.  Defaults to "geometry".
66     */
67    geometryName: "geometry",
68
69    /**
70     * APIProperty: extractAttributes
71     * {Boolean} Extract attributes from GML.  Default is true.
72     */
73    extractAttributes: true,
74   
75    /**
76     * APIProperty: srsName
77     * {String} URI for spatial reference system.  This is optional for
78     *     single part geometries and mandatory for collections and multis.
79     *     If set, the srsName attribute will be written for all geometries.
80     *     Default is null.
81     */
82    srsName: null,
83
84    /**
85     * APIProperty: xy
86     * {Boolean} Order of the GML coordinate true:(x,y) or false:(y,x)
87     * Changing is not recommended, a new Format should be instantiated.
88     */ 
89    xy: true,
90
91    /**
92     * Property: geometryTypes
93     * {Object} Maps OpenLayers geometry class names to GML element names.
94     *     Use <setGeometryTypes> before accessing this property.
95     */
96    geometryTypes: null,
97
98    /**
99     * Property: singleFeatureType
100     * {Boolean} True if there is only 1 featureType, and not an array
101     *     of featuretypes.
102     */
103    singleFeatureType: null,
104
105    /**
106     * Property: regExes
107     * Compiled regular expressions for manipulating strings.
108     */
109    regExes: {
110        trimSpace: (/^\s*|\s*$/g),
111        removeSpace: (/\s*/g),
112        splitSpace: (/\s+/),
113        trimComma: (/\s*,\s*/g)
114    },
115
116    /**
117     * Constructor: OpenLayers.Format.GML.Base
118     * Instances of this class are not created directly.  Use the
119     *     <OpenLayers.Format.GML.v2> or <OpenLayers.Format.GML.v3> constructor
120     *     instead.
121     *
122     * Parameters:
123     * options - {Object} An optional object whose properties will be set on
124     *     this instance.
125     *
126     * Valid options properties:
127     * featureType - {Array(String) or String} Local (without prefix) feature
128     *     typeName(s) (required).
129     * featureNS - {String} Feature namespace (required).
130     * geometryName - {String} Geometry element name.
131     */
132    initialize: function(options) {
133        OpenLayers.Format.XML.prototype.initialize.apply(this, [options]);
134        this.setGeometryTypes();
135        if(options && options.featureNS) {
136            this.setNamespace("feature", options.featureNS);
137        }
138        this.singleFeatureType = !options || (typeof options.featureType === "string");
139    },
140   
141    /**
142     * Method: read
143     *
144     * Parameters:
145     * data - {DOMElement} A gml:featureMember element, a gml:featureMembers
146     *     element, or an element containing either of the above at any level.
147     *
148     * Returns:
149     * {Array(<OpenLayers.Feature.Vector>)} An array of features.
150     */
151    read: function(data) {
152        if(typeof data == "string") { 
153            data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
154        }
155        if(data && data.nodeType == 9) {
156            data = data.documentElement;
157        }
158        var features = [];
159        this.readNode(data, {features: features});
160        if(features.length == 0) {
161            // look for gml:featureMember elements
162            var elements = this.getElementsByTagNameNS(
163                data, this.namespaces.gml, "featureMember"
164            );
165            if(elements.length) {
166                for(var i=0, len=elements.length; i<len; ++i) {
167                    this.readNode(elements[i], {features: features});
168                }
169            } else {
170                // look for gml:featureMembers elements (this is v3, but does no harm here)
171                var elements = this.getElementsByTagNameNS(
172                    data, this.namespaces.gml, "featureMembers"
173                );
174                if(elements.length) {
175                    // there can be only one
176                    this.readNode(elements[0], {features: features});
177                }
178            }
179        }
180        return features;
181    },
182   
183    /**
184     * Property: readers
185     * Contains public functions, grouped by namespace prefix, that will
186     *     be applied when a namespaced node is found matching the function
187     *     name.  The function will be applied in the scope of this parser
188     *     with two arguments: the node being read and a context object passed
189     *     from the parent.
190     */
191    readers: {
192        "gml": {
193            "featureMember": function(node, obj) {
194                this.readChildNodes(node, obj);
195            },
196            "featureMembers": function(node, obj) {
197                this.readChildNodes(node, obj);               
198            },
199            "name": function(node, obj) {
200                obj.name = this.getChildValue(node);
201            },
202            "boundedBy": function(node, obj) {
203                var container = {};
204                this.readChildNodes(node, container);
205                if(container.components && container.components.length > 0) {
206                    obj.bounds = container.components[0];
207                }
208            },
209            "Point": function(node, container) {
210                var obj = {points: []};
211                this.readChildNodes(node, obj);
212                if(!container.components) {
213                    container.components = [];
214                }
215                container.components.push(obj.points[0]);
216            },
217            "coordinates": function(node, obj) {
218                var str = this.getChildValue(node).replace(
219                    this.regExes.trimSpace, ""
220                );
221                str = str.replace(this.regExes.trimComma, ",");
222                var pointList = str.split(this.regExes.splitSpace);
223                var coords;
224                var numPoints = pointList.length;
225                var points = new Array(numPoints);
226                for(var i=0; i<numPoints; ++i) {
227                    coords = pointList[i].split(",");
228                    if (this.xy) {
229                        points[i] = new OpenLayers.Geometry.Point(
230                            coords[0], coords[1], coords[2]
231                        );
232                    } else {
233                        points[i] = new OpenLayers.Geometry.Point(
234                            coords[1], coords[0], coords[2]
235                        );
236                    }
237                }
238                obj.points = points;
239            },
240            "coord": function(node, obj) {
241                var coord = {};
242                this.readChildNodes(node, coord);
243                if(!obj.points) {
244                    obj.points = [];
245                }
246                obj.points.push(new OpenLayers.Geometry.Point(
247                    coord.x, coord.y, coord.z
248                ));
249            },
250            "X": function(node, coord) {
251                coord.x = this.getChildValue(node);
252            },
253            "Y": function(node, coord) {
254                coord.y = this.getChildValue(node);
255            },
256            "Z": function(node, coord) {
257                coord.z = this.getChildValue(node);
258            },
259            "MultiPoint": function(node, container) {
260                var obj = {components: []};
261                this.readChildNodes(node, obj);
262                container.components = [
263                    new OpenLayers.Geometry.MultiPoint(obj.components)
264                ];
265            },
266            "pointMember": function(node, obj) {
267                this.readChildNodes(node, obj);
268            },
269            "LineString": function(node, container) {
270                var obj = {};
271                this.readChildNodes(node, obj);
272                if(!container.components) {
273                    container.components = [];
274                }
275                container.components.push(
276                    new OpenLayers.Geometry.LineString(obj.points)
277                );
278            },
279            "MultiLineString": function(node, container) {
280                var obj = {components: []};
281                this.readChildNodes(node, obj);
282                container.components = [
283                    new OpenLayers.Geometry.MultiLineString(obj.components)
284                ];
285            },
286            "lineStringMember": function(node, obj) {
287                this.readChildNodes(node, obj);
288            },
289            "Polygon": function(node, container) {
290                var obj = {outer: null, inner: []};
291                this.readChildNodes(node, obj);
292                obj.inner.unshift(obj.outer);
293                if(!container.components) {
294                    container.components = [];
295                }
296                container.components.push(
297                    new OpenLayers.Geometry.Polygon(obj.inner)
298                );
299            },
300            "LinearRing": function(node, obj) {
301                var container = {};
302                this.readChildNodes(node, container);
303                obj.components = [new OpenLayers.Geometry.LinearRing(
304                    container.points
305                )];
306            },
307            "MultiPolygon": function(node, container) {
308                var obj = {components: []};
309                this.readChildNodes(node, obj);
310                container.components = [
311                    new OpenLayers.Geometry.MultiPolygon(obj.components)
312                ];
313            },
314            "polygonMember": function(node, obj) {
315                this.readChildNodes(node, obj);
316            },
317            "GeometryCollection": function(node, container) {
318                var obj = {components: []};
319                this.readChildNodes(node, obj);
320                container.components = [
321                    new OpenLayers.Geometry.Collection(obj.components)
322                ];
323            },
324            "geometryMember": function(node, obj) {
325                this.readChildNodes(node, obj);
326            }
327        },
328        "feature": {
329            "*": function(node, obj) {
330                // The node can either be named like the featureType, or it
331                // can be a child of the feature:featureType.  Children can be
332                // geometry or attributes.
333                var name;
334                var local = node.localName || node.nodeName.split(":").pop();
335                // Since an attribute can have the same name as the feature type
336                // we only want to read the node as a feature if the parent
337                // node can have feature nodes as children.  In this case, the
338                // obj.features property is set.
339                if (obj.features) {
340                    if (!this.singleFeatureType &&
341                        (OpenLayers.Util.indexOf(this.featureType, local) !== -1)) {
342                        name = "_typeName";
343                    } else if(local === this.featureType) {
344                        name = "_typeName";
345                    }
346                } else {
347                    // Assume attribute elements have one child node and that the child
348                    // is a text node.  Otherwise assume it is a geometry node.
349                    if(node.childNodes.length == 0 ||
350                       (node.childNodes.length == 1 && node.firstChild.nodeType == 3)) {
351                        if(this.extractAttributes) {
352                            name = "_attribute";
353                        }
354                    } else {
355                        name = "_geometry";
356                    }
357                }
358                if(name) {
359                    this.readers.feature[name].apply(this, [node, obj]);
360                }
361            },
362            "_typeName": function(node, obj) {
363                var container = {components: [], attributes: {}};
364                this.readChildNodes(node, container);
365                // look for common gml namespaced elements
366                if(container.name) {
367                    container.attributes.name = container.name;
368                }
369                var feature = new OpenLayers.Feature.Vector(
370                    container.components[0], container.attributes
371                );
372                if (!this.singleFeatureType) {
373                    feature.type = node.nodeName.split(":").pop();
374                    feature.namespace = node.namespaceURI;
375                }
376                var fid = node.getAttribute("fid") ||
377                    this.getAttributeNS(node, this.namespaces["gml"], "id");
378                if(fid) {
379                    feature.fid = fid;
380                }
381                if(this.internalProjection && this.externalProjection &&
382                   feature.geometry) {
383                    feature.geometry.transform(
384                        this.externalProjection, this.internalProjection
385                    );
386                }
387                if(container.bounds) {
388                    feature.bounds = container.bounds;
389                }
390                obj.features.push(feature);
391            },
392            "_geometry": function(node, obj) {
393                this.readChildNodes(node, obj);
394            },
395            "_attribute": function(node, obj) {
396                var local = node.localName || node.nodeName.split(":").pop();
397                var value = this.getChildValue(node);
398                obj.attributes[local] = value;
399            }
400        },
401        "wfs": {
402            "FeatureCollection": function(node, obj) {
403                this.readChildNodes(node, obj);
404            }
405        }
406    },
407   
408    /**
409     * Method: write
410     *
411     * Parameters:
412     * features - {Array(<OpenLayers.Feature.Vector>) | OpenLayers.Feature.Vector}
413     *     An array of features or a single feature.
414     *
415     * Returns:
416     * {String} Given an array of features, a doc with a gml:featureMembers
417     *     element will be returned.  Given a single feature, a doc with a
418     *     gml:featureMember element will be returned.
419     */
420    write: function(features) {
421        var name;
422        if(features instanceof Array) {
423            name = "featureMembers";
424        } else {
425            name = "featureMember";
426        }
427        var root = this.writeNode("gml:" + name, features);
428        this.setAttributeNS(
429            root, this.namespaces["xsi"],
430            "xsi:schemaLocation", this.schemaLocation
431        );
432
433        return OpenLayers.Format.XML.prototype.write.apply(this, [root]);
434    },
435   
436    /**
437     * Property: writers
438     * As a compliment to the readers property, this structure contains public
439     *     writing functions grouped by namespace alias and named like the
440     *     node names they produce.
441     */
442    writers: {
443        "gml": {
444            "featureMember": function(feature) {
445                var node = this.createElementNSPlus("gml:featureMember");
446                this.writeNode("feature:_typeName", feature, node);
447                return node;
448            },
449            "MultiPoint": function(geometry) {
450                var node = this.createElementNSPlus("gml:MultiPoint");
451                for(var i=0; i<geometry.components.length; ++i) {
452                    this.writeNode("pointMember", geometry.components[i], node);
453                }
454                return node;
455            },
456            "pointMember": function(geometry) {
457                var node = this.createElementNSPlus("gml:pointMember");
458                this.writeNode("Point", geometry, node);
459                return node;
460            },
461            "MultiLineString": function(geometry) {
462                var node = this.createElementNSPlus("gml:MultiLineString");
463                for(var i=0; i<geometry.components.length; ++i) {
464                    this.writeNode("lineStringMember", geometry.components[i], node);
465                }
466                return node;
467            },
468            "lineStringMember": function(geometry) {
469                var node = this.createElementNSPlus("gml:lineStringMember");
470                this.writeNode("LineString", geometry, node);
471                return node;
472            },
473            "MultiPolygon": function(geometry) {
474                var node = this.createElementNSPlus("gml:MultiPolygon");
475                for(var i=0; i<geometry.components.length; ++i) {
476                    this.writeNode(
477                        "polygonMember", geometry.components[i], node
478                    );
479                }
480                return node;
481            },
482            "polygonMember": function(geometry) {
483                var node = this.createElementNSPlus("gml:polygonMember");
484                this.writeNode("Polygon", geometry, node);
485                return node;
486            },
487            "GeometryCollection": function(geometry) {
488                var node = this.createElementNSPlus("gml:GeometryCollection");
489                for(var i=0, len=geometry.components.length; i<len; ++i) {
490                    this.writeNode("geometryMember", geometry.components[i], node);
491                }
492                return node;
493            },
494            "geometryMember": function(geometry) {
495                var node = this.createElementNSPlus("gml:geometryMember");
496                var child = this.writeNode("feature:_geometry", geometry);
497                node.appendChild(child.firstChild);
498                return node;
499            }
500        },
501        "feature": {
502            "_typeName": function(feature) {
503                var node = this.createElementNSPlus("feature:" + this.featureType, {
504                    attributes: {fid: feature.fid}
505                });
506                if(feature.geometry) {
507                    this.writeNode("feature:_geometry", feature.geometry, node);
508                }
509                for(var name in feature.attributes) {
510                    var value = feature.attributes[name];
511                    if(value != null) {
512                        this.writeNode(
513                            "feature:_attribute",
514                            {name: name, value: value}, node
515                        );
516                    }
517                }
518                return node;
519            },
520            "_geometry": function(geometry) {
521                if(this.externalProjection && this.internalProjection) {
522                    geometry = geometry.clone().transform(
523                        this.internalProjection, this.externalProjection
524                    );
525                }   
526                var node = this.createElementNSPlus(
527                    "feature:" + this.geometryName
528                );
529                var type = this.geometryTypes[geometry.CLASS_NAME];
530                var child = this.writeNode("gml:" + type, geometry, node);
531                if(this.srsName) {
532                    child.setAttribute("srsName", this.srsName);
533                }
534                return node;
535            },
536            "_attribute": function(obj) {
537                return this.createElementNSPlus("feature:" + obj.name, {
538                    value: obj.value
539                });
540            }
541        },
542        "wfs": {
543            "FeatureCollection": function(features) {
544                /**
545                 * This is only here because GML2 only describes abstract
546                 * feature collections.  Typically, you would not be using
547                 * the GML format to write wfs elements.  This just provides
548                 * some way to write out lists of features.  GML3 defines the
549                 * featureMembers element, so that is used by default instead.
550                 */
551                var node = this.createElementNSPlus("wfs:FeatureCollection");
552                for(var i=0, len=features.length; i<len; ++i) {
553                    this.writeNode("gml:featureMember", features[i], node);
554                }
555                return node;
556            }
557        }
558    },
559   
560    /**
561     * Function: setGeometryTypes
562     * Sets the <geometryTypes> mapping.
563     */
564    setGeometryTypes: function() {
565        this.geometryTypes = {
566            "OpenLayers.Geometry.Point": "Point",
567            "OpenLayers.Geometry.MultiPoint": "MultiPoint",
568            "OpenLayers.Geometry.LineString": "LineString",
569            "OpenLayers.Geometry.MultiLineString": "MultiLineString",
570            "OpenLayers.Geometry.Polygon": "Polygon",
571            "OpenLayers.Geometry.MultiPolygon": "MultiPolygon",
572            "OpenLayers.Geometry.Collection": "GeometryCollection"
573        };
574    },
575
576    CLASS_NAME: "OpenLayers.Format.GML.Base" 
577
578});
Note: See TracBrowser for help on using the repository browser.