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.js @ 76

Revision 76, 34.7 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/Feature/Vector.js
9 * @requires OpenLayers/Geometry/Point.js
10 * @requires OpenLayers/Geometry/MultiPoint.js
11 * @requires OpenLayers/Geometry/LineString.js
12 * @requires OpenLayers/Geometry/MultiLineString.js
13 * @requires OpenLayers/Geometry/Polygon.js
14 * @requires OpenLayers/Geometry/MultiPolygon.js
15 * @requires OpenLayers/Console.js
16 */
17
18/**
19 * Class: OpenLayers.Format.GML
20 * Read/Wite GML. Create a new instance with the <OpenLayers.Format.GML>
21 *     constructor.  Supports the GML simple features profile.
22 *
23 * Inherits from:
24 *  - <OpenLayers.Format>
25 */
26OpenLayers.Format.GML = OpenLayers.Class(OpenLayers.Format.XML, {
27   
28    /*
29     * APIProperty: featureNS
30     * {String} Namespace used for feature attributes.  Default is
31     *     "http://mapserver.gis.umn.edu/mapserver".
32     */
33    featureNS: "http://mapserver.gis.umn.edu/mapserver",
34   
35    /**
36     * APIProperty: featurePrefix
37     * {String} Namespace alias (or prefix) for feature nodes.  Default is
38     *     "feature".
39     */
40    featurePrefix: "feature",
41   
42    /*
43     * APIProperty: featureName
44     * {String} Element name for features. Default is "featureMember".
45     */
46    featureName: "featureMember", 
47   
48    /*
49     * APIProperty: layerName
50     * {String} Name of data layer. Default is "features".
51     */
52    layerName: "features",
53   
54    /**
55     * APIProperty: geometryName
56     * {String} Name of geometry element.  Defaults to "geometry".
57     */
58    geometryName: "geometry",
59   
60    /**
61     * APIProperty: collectionName
62     * {String} Name of featureCollection element.
63     */
64    collectionName: "FeatureCollection",
65   
66    /**
67     * APIProperty: gmlns
68     * {String} GML Namespace.
69     */
70    gmlns: "http://www.opengis.net/gml",
71
72    /**
73     * APIProperty: extractAttributes
74     * {Boolean} Extract attributes from GML.
75     */
76    extractAttributes: true,
77   
78    /**
79     * APIProperty: xy
80     * {Boolean} Order of the GML coordinate true:(x,y) or false:(y,x)
81     * Changing is not recommended, a new Format should be instantiated.
82     */ 
83    xy: true,
84   
85    /**
86     * Constructor: OpenLayers.Format.GML
87     * Create a new parser for GML.
88     *
89     * Parameters:
90     * options - {Object} An optional object whose properties will be set on
91     *     this instance.
92     */
93    initialize: function(options) {
94        // compile regular expressions once instead of every time they are used
95        this.regExes = {
96            trimSpace: (/^\s*|\s*$/g),
97            removeSpace: (/\s*/g),
98            splitSpace: (/\s+/),
99            trimComma: (/\s*,\s*/g)
100        };
101        OpenLayers.Format.XML.prototype.initialize.apply(this, [options]);
102    },
103
104    /**
105     * APIMethod: read
106     * Read data from a string, and return a list of features.
107     *
108     * Parameters:
109     * data - {String} or {DOMElement} data to read/parse.
110     *
111     * Returns:
112     * {Array(<OpenLayers.Feature.Vector>)} An array of features.
113     */
114    read: function(data) {
115        if(typeof data == "string") { 
116            data = OpenLayers.Format.XML.prototype.read.apply(this, [data]);
117        }
118        var featureNodes = this.getElementsByTagNameNS(data.documentElement,
119                                                       this.gmlns,
120                                                       this.featureName);
121        var features = [];
122        for(var i=0; i<featureNodes.length; i++) {
123            var feature = this.parseFeature(featureNodes[i]);
124            if(feature) {
125                features.push(feature);
126            }
127        }
128        return features;
129    },
130   
131    /**
132     * Method: parseFeature
133     * This function is the core of the GML parsing code in OpenLayers.
134     *    It creates the geometries that are then attached to the returned
135     *    feature, and calls parseAttributes() to get attribute data out.
136     *   
137     * Parameters:
138     * node - {DOMElement} A GML feature node.
139     */
140    parseFeature: function(node) {
141        // only accept one geometry per feature - look for highest "order"
142        var order = ["MultiPolygon", "Polygon",
143                     "MultiLineString", "LineString",
144                     "MultiPoint", "Point", "Envelope"];
145        // FIXME: In case we parse a feature with no geometry, but boundedBy an Envelope,
146        // this code creates a geometry derived from the Envelope. This is not correct.
147        var type, nodeList, geometry, parser;
148        for(var i=0; i<order.length; ++i) {
149            type = order[i];
150            nodeList = this.getElementsByTagNameNS(node, this.gmlns, type);
151            if(nodeList.length > 0) {
152                // only deal with first geometry of this type
153                parser = this.parseGeometry[type.toLowerCase()];
154                if(parser) {
155                    geometry = parser.apply(this, [nodeList[0]]);
156                    if (this.internalProjection && this.externalProjection) {
157                        geometry.transform(this.externalProjection, 
158                                           this.internalProjection); 
159                    }                       
160                } else {
161                    OpenLayers.Console.error(OpenLayers.i18n(
162                                "unsupportedGeometryType", {'geomType':type}));
163                }
164                // stop looking for different geometry types
165                break;
166            }
167        }
168
169        var bounds;
170        var boxNodes = this.getElementsByTagNameNS(node, this.gmlns, "Box");
171        for(i=0; i<boxNodes.length; ++i) {
172            var boxNode = boxNodes[i];
173            var box = this.parseGeometry["box"].apply(this, [boxNode]);
174            var parentNode = boxNode.parentNode;
175            var parentName = parentNode.localName ||
176                             parentNode.nodeName.split(":").pop();
177            if(parentName === "boundedBy") {
178                bounds = box;
179            } else {
180                geometry = box.toGeometry();
181            }
182        }
183       
184        // construct feature (optionally with attributes)
185        var attributes;
186        if(this.extractAttributes) {
187            attributes = this.parseAttributes(node);
188        }
189        var feature = new OpenLayers.Feature.Vector(geometry, attributes);
190        feature.bounds = bounds;
191       
192        feature.gml = {
193            featureType: node.firstChild.nodeName.split(":")[1],
194            featureNS: node.firstChild.namespaceURI,
195            featureNSPrefix: node.firstChild.prefix
196        };
197               
198        // assign fid - this can come from a "fid" or "id" attribute
199        var childNode = node.firstChild;
200        var fid;
201        while(childNode) {
202            if(childNode.nodeType == 1) {
203                fid = childNode.getAttribute("fid") ||
204                      childNode.getAttribute("id");
205                if(fid) {
206                    break;
207                }
208            }
209            childNode = childNode.nextSibling;
210        }
211        feature.fid = fid;
212        return feature;
213    },
214   
215    /**
216     * Property: parseGeometry
217     * Properties of this object are the functions that parse geometries based
218     *     on their type.
219     */
220    parseGeometry: {
221       
222        /**
223         * Method: parseGeometry.point
224         * Given a GML node representing a point geometry, create an OpenLayers
225         *     point geometry.
226         *
227         * Parameters:
228         * node - {DOMElement} A GML node.
229         *
230         * Returns:
231         * {<OpenLayers.Geometry.Point>} A point geometry.
232         */
233        point: function(node) {
234            /**
235             * Three coordinate variations to consider:
236             * 1) <gml:pos>x y z</gml:pos>
237             * 2) <gml:coordinates>x, y, z</gml:coordinates>
238             * 3) <gml:coord><gml:X>x</gml:X><gml:Y>y</gml:Y></gml:coord>
239             */
240            var nodeList, coordString;
241            var coords = [];
242
243            // look for <gml:pos>
244            var nodeList = this.getElementsByTagNameNS(node, this.gmlns, "pos");
245            if(nodeList.length > 0) {
246                coordString = nodeList[0].firstChild.nodeValue;
247                coordString = coordString.replace(this.regExes.trimSpace, "");
248                coords = coordString.split(this.regExes.splitSpace);
249            }
250
251            // look for <gml:coordinates>
252            if(coords.length == 0) {
253                nodeList = this.getElementsByTagNameNS(node, this.gmlns,
254                                                       "coordinates");
255                if(nodeList.length > 0) {
256                    coordString = nodeList[0].firstChild.nodeValue;
257                    coordString = coordString.replace(this.regExes.removeSpace,
258                                                      "");
259                    coords = coordString.split(",");
260                }
261            }
262
263            // look for <gml:coord>
264            if(coords.length == 0) {
265                nodeList = this.getElementsByTagNameNS(node, this.gmlns,
266                                                       "coord");
267                if(nodeList.length > 0) {
268                    var xList = this.getElementsByTagNameNS(nodeList[0],
269                                                            this.gmlns, "X");
270                    var yList = this.getElementsByTagNameNS(nodeList[0],
271                                                            this.gmlns, "Y");
272                    if(xList.length > 0 && yList.length > 0) {
273                        coords = [xList[0].firstChild.nodeValue,
274                                  yList[0].firstChild.nodeValue];
275                    }
276                }
277            }
278               
279            // preserve third dimension
280            if(coords.length == 2) {
281                coords[2] = null;
282            }
283           
284            if (this.xy) {
285                return new OpenLayers.Geometry.Point(coords[0], coords[1],
286                                                 coords[2]);
287            }
288            else{
289                return new OpenLayers.Geometry.Point(coords[1], coords[0],
290                                                 coords[2]);
291            }
292        },
293       
294        /**
295         * Method: parseGeometry.multipoint
296         * Given a GML node representing a multipoint geometry, create an
297         *     OpenLayers multipoint geometry.
298         *
299         * Parameters:
300         * node - {DOMElement} A GML node.
301         *
302         * Returns:
303         * {<OpenLayers.Geometry.MultiPoint>} A multipoint geometry.
304         */
305        multipoint: function(node) {
306            var nodeList = this.getElementsByTagNameNS(node, this.gmlns,
307                                                       "Point");
308            var components = [];
309            if(nodeList.length > 0) {
310                var point;
311                for(var i=0; i<nodeList.length; ++i) {
312                    point = this.parseGeometry.point.apply(this, [nodeList[i]]);
313                    if(point) {
314                        components.push(point);
315                    }
316                }
317            }
318            return new OpenLayers.Geometry.MultiPoint(components);
319        },
320       
321        /**
322         * Method: parseGeometry.linestring
323         * Given a GML node representing a linestring geometry, create an
324         *     OpenLayers linestring geometry.
325         *
326         * Parameters:
327         * node - {DOMElement} A GML node.
328         *
329         * Returns:
330         * {<OpenLayers.Geometry.LineString>} A linestring geometry.
331         */
332        linestring: function(node, ring) {
333            /**
334             * Two coordinate variations to consider:
335             * 1) <gml:posList dimension="d">x0 y0 z0 x1 y1 z1</gml:posList>
336             * 2) <gml:coordinates>x0, y0, z0 x1, y1, z1</gml:coordinates>
337             */
338            var nodeList, coordString;
339            var coords = [];
340            var points = [];
341
342            // look for <gml:posList>
343            nodeList = this.getElementsByTagNameNS(node, this.gmlns, "posList");
344            if(nodeList.length > 0) {
345                coordString = this.getChildValue(nodeList[0]);
346                coordString = coordString.replace(this.regExes.trimSpace, "");
347                coords = coordString.split(this.regExes.splitSpace);
348                var dim = parseInt(nodeList[0].getAttribute("dimension"));
349                var j, x, y, z;
350                for(var i=0; i<coords.length/dim; ++i) {
351                    j = i * dim;
352                    x = coords[j];
353                    y = coords[j+1];
354                    z = (dim == 2) ? null : coords[j+2];
355                    if (this.xy) {
356                        points.push(new OpenLayers.Geometry.Point(x, y, z));
357                    } else {
358                        points.push(new OpenLayers.Geometry.Point(y, x, z));
359                    }
360                }
361            }
362
363            // look for <gml:coordinates>
364            if(coords.length == 0) {
365                nodeList = this.getElementsByTagNameNS(node, this.gmlns,
366                                                       "coordinates");
367                if(nodeList.length > 0) {
368                    coordString = this.getChildValue(nodeList[0]);
369                    coordString = coordString.replace(this.regExes.trimSpace,
370                                                      "");
371                    coordString = coordString.replace(this.regExes.trimComma,
372                                                      ",");
373                    var pointList = coordString.split(this.regExes.splitSpace);
374                    for(var i=0; i<pointList.length; ++i) {
375                        coords = pointList[i].split(",");
376                        if(coords.length == 2) {
377                            coords[2] = null;
378                        }
379                        if (this.xy) {
380                            points.push(new OpenLayers.Geometry.Point(coords[0],
381                                                                  coords[1],
382                                                                  coords[2]));
383                        } else {
384                            points.push(new OpenLayers.Geometry.Point(coords[1],
385                                                                  coords[0],
386                                                                  coords[2]));
387                        }
388                    }
389                }
390            }
391
392            var line = null;
393            if(points.length != 0) {
394                if(ring) {
395                    line = new OpenLayers.Geometry.LinearRing(points);
396                } else {
397                    line = new OpenLayers.Geometry.LineString(points);
398                }
399            }
400            return line;
401        },
402       
403        /**
404         * Method: parseGeometry.multilinestring
405         * Given a GML node representing a multilinestring geometry, create an
406         *     OpenLayers multilinestring geometry.
407         *
408         * Parameters:
409         * node - {DOMElement} A GML node.
410         *
411         * Returns:
412         * {<OpenLayers.Geometry.MultiLineString>} A multilinestring geometry.
413         */
414        multilinestring: function(node) {
415            var nodeList = this.getElementsByTagNameNS(node, this.gmlns,
416                                                       "LineString");
417            var components = [];
418            if(nodeList.length > 0) {
419                var line;
420                for(var i=0; i<nodeList.length; ++i) {
421                    line = this.parseGeometry.linestring.apply(this,
422                                                               [nodeList[i]]);
423                    if(line) {
424                        components.push(line);
425                    }
426                }
427            }
428            return new OpenLayers.Geometry.MultiLineString(components);
429        },
430       
431        /**
432         * Method: parseGeometry.polygon
433         * Given a GML node representing a polygon geometry, create an
434         *     OpenLayers polygon geometry.
435         *
436         * Parameters:
437         * node - {DOMElement} A GML node.
438         *
439         * Returns:
440         * {<OpenLayers.Geometry.Polygon>} A polygon geometry.
441         */
442        polygon: function(node) {
443            var nodeList = this.getElementsByTagNameNS(node, this.gmlns,
444                                                       "LinearRing");
445            var components = [];
446            if(nodeList.length > 0) {
447                // this assumes exterior ring first, inner rings after
448                var ring;
449                for(var i=0; i<nodeList.length; ++i) {
450                    ring = this.parseGeometry.linestring.apply(this,
451                                                        [nodeList[i], true]);
452                    if(ring) {
453                        components.push(ring);
454                    }
455                }
456            }
457            return new OpenLayers.Geometry.Polygon(components);
458        },
459       
460        /**
461         * Method: parseGeometry.multipolygon
462         * Given a GML node representing a multipolygon geometry, create an
463         *     OpenLayers multipolygon geometry.
464         *
465         * Parameters:
466         * node - {DOMElement} A GML node.
467         *
468         * Returns:
469         * {<OpenLayers.Geometry.MultiPolygon>} A multipolygon geometry.
470         */
471        multipolygon: function(node) {
472            var nodeList = this.getElementsByTagNameNS(node, this.gmlns,
473                                                       "Polygon");
474            var components = [];
475            if(nodeList.length > 0) {
476                var polygon;
477                for(var i=0; i<nodeList.length; ++i) {
478                    polygon = this.parseGeometry.polygon.apply(this,
479                                                               [nodeList[i]]);
480                    if(polygon) {
481                        components.push(polygon);
482                    }
483                }
484            }
485            return new OpenLayers.Geometry.MultiPolygon(components);
486        },
487       
488        envelope: function(node) {
489            var components = [];
490            var coordString;
491            var envelope;
492           
493            var lpoint = this.getElementsByTagNameNS(node, this.gmlns, "lowerCorner");
494            if (lpoint.length > 0) {
495                var coords = [];
496               
497                if(lpoint.length > 0) {
498                    coordString = lpoint[0].firstChild.nodeValue;
499                    coordString = coordString.replace(this.regExes.trimSpace, "");
500                    coords = coordString.split(this.regExes.splitSpace);
501                }
502               
503                if(coords.length == 2) {
504                    coords[2] = null;
505                }
506                if (this.xy) {
507                    var lowerPoint = new OpenLayers.Geometry.Point(coords[0], coords[1],coords[2]);
508                } else {
509                    var lowerPoint = new OpenLayers.Geometry.Point(coords[1], coords[0],coords[2]);
510                }
511            }
512           
513            var upoint = this.getElementsByTagNameNS(node, this.gmlns, "upperCorner");
514            if (upoint.length > 0) {
515                var coords = [];
516               
517                if(upoint.length > 0) {
518                    coordString = upoint[0].firstChild.nodeValue;
519                    coordString = coordString.replace(this.regExes.trimSpace, "");
520                    coords = coordString.split(this.regExes.splitSpace);
521                }
522               
523                if(coords.length == 2) {
524                    coords[2] = null;
525                }
526                if (this.xy) {
527                    var upperPoint = new OpenLayers.Geometry.Point(coords[0], coords[1],coords[2]);
528                } else {
529                    var upperPoint = new OpenLayers.Geometry.Point(coords[1], coords[0],coords[2]);
530                }
531            }
532           
533            if (lowerPoint && upperPoint) {
534                components.push(new OpenLayers.Geometry.Point(lowerPoint.x, lowerPoint.y));
535                components.push(new OpenLayers.Geometry.Point(upperPoint.x, lowerPoint.y));
536                components.push(new OpenLayers.Geometry.Point(upperPoint.x, upperPoint.y));
537                components.push(new OpenLayers.Geometry.Point(lowerPoint.x, upperPoint.y));
538                components.push(new OpenLayers.Geometry.Point(lowerPoint.x, lowerPoint.y));
539               
540                var ring = new OpenLayers.Geometry.LinearRing(components);
541                envelope = new OpenLayers.Geometry.Polygon([ring]);
542            }
543            return envelope; 
544        },
545
546        /**
547         * Method: parseGeometry.box
548         * Given a GML node representing a box geometry, create an
549         *     OpenLayers.Bounds.
550         *
551         * Parameters:
552         * node - {DOMElement} A GML node.
553         *
554         * Returns:
555         * {<OpenLayers.Bounds>} A bounds representing the box.
556         */
557        box: function(node) {
558            var nodeList = this.getElementsByTagNameNS(node, this.gmlns,
559                                                   "coordinates");
560            var coordString;
561            var coords, beginPoint = null, endPoint = null;
562            if (nodeList.length > 0) {
563                coordString = nodeList[0].firstChild.nodeValue;
564                coords = coordString.split(" ");
565                if (coords.length == 2) {
566                    beginPoint = coords[0].split(",");
567                    endPoint = coords[1].split(",");
568                }
569            }
570            if (beginPoint !== null && endPoint !== null) {
571                return new OpenLayers.Bounds(parseFloat(beginPoint[0]),
572                    parseFloat(beginPoint[1]),
573                    parseFloat(endPoint[0]),
574                    parseFloat(endPoint[1]) );
575            }
576        }
577       
578    },
579   
580    /**
581     * Method: parseAttributes
582     *
583     * Parameters:
584     * node - {<DOMElement>}
585     *
586     * Returns:
587     * {Object} An attributes object.
588     */
589    parseAttributes: function(node) {
590        var attributes = {};
591        // assume attributes are children of the first type 1 child
592        var childNode = node.firstChild;
593        var children, i, child, grandchildren, grandchild, name, value;
594        while(childNode) {
595            if(childNode.nodeType == 1) {
596                // attributes are type 1 children with one type 3 child
597                children = childNode.childNodes;
598                for(i=0; i<children.length; ++i) {
599                    child = children[i];
600                    if(child.nodeType == 1) {
601                        grandchildren = child.childNodes;
602                        if(grandchildren.length == 1) {
603                            grandchild = grandchildren[0];
604                            if(grandchild.nodeType == 3 ||
605                               grandchild.nodeType == 4) {
606                                name = (child.prefix) ?
607                                        child.nodeName.split(":")[1] :
608                                        child.nodeName;
609                                value = grandchild.nodeValue.replace(
610                                                this.regExes.trimSpace, "");
611                                attributes[name] = value;
612                            }
613                        } else {
614                            // If child has no childNodes (grandchildren),
615                            // set an attribute with null value.
616                            // e.g. <prefix:fieldname/> becomes
617                            // {fieldname: null}
618                            attributes[child.nodeName.split(":").pop()] = null;
619                        }
620                    }
621                }
622                break;
623            }
624            childNode = childNode.nextSibling;
625        }
626        return attributes;
627    },
628   
629    /**
630     * APIMethod: write
631     * Generate a GML document string given a list of features.
632     *
633     * Parameters:
634     * features - {Array(<OpenLayers.Feature.Vector>)} List of features to
635     *     serialize into a string.
636     *
637     * Returns:
638     * {String} A string representing the GML document.
639     */
640    write: function(features) {
641        if(!(features instanceof Array)) {
642            features = [features];
643        }
644        var gml = this.createElementNS("http://www.opengis.net/wfs",
645                                       "wfs:" + this.collectionName);
646        for(var i=0; i<features.length; i++) {
647            gml.appendChild(this.createFeatureXML(features[i]));
648        }
649        return OpenLayers.Format.XML.prototype.write.apply(this, [gml]);
650    },
651
652    /**
653     * Method: createFeatureXML
654     * Accept an OpenLayers.Feature.Vector, and build a GML node for it.
655     *
656     * Parameters:
657     * feature - {<OpenLayers.Feature.Vector>} The feature to be built as GML.
658     *
659     * Returns:
660     * {DOMElement} A node reprensting the feature in GML.
661     */
662    createFeatureXML: function(feature) {
663        var geometry = feature.geometry;
664        var geometryNode = this.buildGeometryNode(geometry);
665        var geomContainer = this.createElementNS(this.featureNS,
666                                                 this.featurePrefix + ":" +
667                                                 this.geometryName);
668        geomContainer.appendChild(geometryNode);
669        var featureNode = this.createElementNS(this.gmlns,
670                                               "gml:" + this.featureName);
671        var featureContainer = this.createElementNS(this.featureNS,
672                                                    this.featurePrefix + ":" +
673                                                    this.layerName);
674        var fid = feature.fid || feature.id;
675        featureContainer.setAttribute("fid", fid);
676        featureContainer.appendChild(geomContainer);
677        for(var attr in feature.attributes) {
678            var attrText = this.createTextNode(feature.attributes[attr]); 
679            var nodename = attr.substring(attr.lastIndexOf(":") + 1);
680            var attrContainer = this.createElementNS(this.featureNS,
681                                                     this.featurePrefix + ":" +
682                                                     nodename);
683            attrContainer.appendChild(attrText);
684            featureContainer.appendChild(attrContainer);
685        }   
686        featureNode.appendChild(featureContainer);
687        return featureNode;
688    },
689   
690    /**
691     * APIMethod: buildGeometryNode
692     */
693    buildGeometryNode: function(geometry) {
694        if (this.externalProjection && this.internalProjection) {
695            geometry = geometry.clone();
696            geometry.transform(this.internalProjection, 
697                               this.externalProjection);
698        }   
699        var className = geometry.CLASS_NAME;
700        var type = className.substring(className.lastIndexOf(".") + 1);
701        var builder = this.buildGeometry[type.toLowerCase()];
702        return builder.apply(this, [geometry]);
703    },
704
705    /**
706     * Property: buildGeometry
707     * Object containing methods to do the actual geometry node building
708     *     based on geometry type.
709     */
710    buildGeometry: {
711        // TBD retrieve the srs from layer
712        // srsName is non-standard, so not including it until it's right.
713        // gml.setAttribute("srsName",
714        //                  "http://www.opengis.net/gml/srs/epsg.xml#4326");
715
716        /**
717         * Method: buildGeometry.point
718         * Given an OpenLayers point geometry, create a GML point.
719         *
720         * Parameters:
721         * geometry - {<OpenLayers.Geometry.Point>} A point geometry.
722         *
723         * Returns:
724         * {DOMElement} A GML point node.
725         */
726        point: function(geometry) {
727            var gml = this.createElementNS(this.gmlns, "gml:Point");
728            gml.appendChild(this.buildCoordinatesNode(geometry));
729            return gml;
730        },
731       
732        /**
733         * Method: buildGeometry.multipoint
734         * Given an OpenLayers multipoint geometry, create a GML multipoint.
735         *
736         * Parameters:
737         * geometry - {<OpenLayers.Geometry.MultiPoint>} A multipoint geometry.
738         *
739         * Returns:
740         * {DOMElement} A GML multipoint node.
741         */
742        multipoint: function(geometry) {
743            var gml = this.createElementNS(this.gmlns, "gml:MultiPoint");
744            var points = geometry.components;
745            var pointMember, pointGeom;
746            for(var i=0; i<points.length; i++) { 
747                pointMember = this.createElementNS(this.gmlns,
748                                                   "gml:pointMember");
749                pointGeom = this.buildGeometry.point.apply(this,
750                                                               [points[i]]);
751                pointMember.appendChild(pointGeom);
752                gml.appendChild(pointMember);
753            }
754            return gml;           
755        },
756       
757        /**
758         * Method: buildGeometry.linestring
759         * Given an OpenLayers linestring geometry, create a GML linestring.
760         *
761         * Parameters:
762         * geometry - {<OpenLayers.Geometry.LineString>} A linestring geometry.
763         *
764         * Returns:
765         * {DOMElement} A GML linestring node.
766         */
767        linestring: function(geometry) {
768            var gml = this.createElementNS(this.gmlns, "gml:LineString");
769            gml.appendChild(this.buildCoordinatesNode(geometry));
770            return gml;
771        },
772       
773        /**
774         * Method: buildGeometry.multilinestring
775         * Given an OpenLayers multilinestring geometry, create a GML
776         *     multilinestring.
777         *
778         * Parameters:
779         * geometry - {<OpenLayers.Geometry.MultiLineString>} A multilinestring
780         *     geometry.
781         *
782         * Returns:
783         * {DOMElement} A GML multilinestring node.
784         */
785        multilinestring: function(geometry) {
786            var gml = this.createElementNS(this.gmlns, "gml:MultiLineString");
787            var lines = geometry.components;
788            var lineMember, lineGeom;
789            for(var i=0; i<lines.length; ++i) {
790                lineMember = this.createElementNS(this.gmlns,
791                                                  "gml:lineStringMember");
792                lineGeom = this.buildGeometry.linestring.apply(this,
793                                                                   [lines[i]]);
794                lineMember.appendChild(lineGeom);
795                gml.appendChild(lineMember);
796            }
797            return gml;
798        },
799       
800        /**
801         * Method: buildGeometry.linearring
802         * Given an OpenLayers linearring geometry, create a GML linearring.
803         *
804         * Parameters:
805         * geometry - {<OpenLayers.Geometry.LinearRing>} A linearring geometry.
806         *
807         * Returns:
808         * {DOMElement} A GML linearring node.
809         */
810        linearring: function(geometry) {
811            var gml = this.createElementNS(this.gmlns, "gml:LinearRing");
812            gml.appendChild(this.buildCoordinatesNode(geometry));
813            return gml;
814        },
815       
816        /**
817         * Method: buildGeometry.polygon
818         * Given an OpenLayers polygon geometry, create a GML polygon.
819         *
820         * Parameters:
821         * geometry - {<OpenLayers.Geometry.Polygon>} A polygon geometry.
822         *
823         * Returns:
824         * {DOMElement} A GML polygon node.
825         */
826        polygon: function(geometry) {
827            var gml = this.createElementNS(this.gmlns, "gml:Polygon");
828            var rings = geometry.components;
829            var ringMember, ringGeom, type;
830            for(var i=0; i<rings.length; ++i) {
831                type = (i==0) ? "outerBoundaryIs" : "innerBoundaryIs";
832                ringMember = this.createElementNS(this.gmlns,
833                                                  "gml:" + type);
834                ringGeom = this.buildGeometry.linearring.apply(this,
835                                                                   [rings[i]]);
836                ringMember.appendChild(ringGeom);
837                gml.appendChild(ringMember);
838            }
839            return gml;
840        },
841       
842        /**
843         * Method: buildGeometry.multipolygon
844         * Given an OpenLayers multipolygon geometry, create a GML multipolygon.
845         *
846         * Parameters:
847         * geometry - {<OpenLayers.Geometry.MultiPolygon>} A multipolygon
848         *     geometry.
849         *
850         * Returns:
851         * {DOMElement} A GML multipolygon node.
852         */
853        multipolygon: function(geometry) {
854            var gml = this.createElementNS(this.gmlns, "gml:MultiPolygon");
855            var polys = geometry.components;
856            var polyMember, polyGeom;
857            for(var i=0; i<polys.length; ++i) {
858                polyMember = this.createElementNS(this.gmlns,
859                                                  "gml:polygonMember");
860                polyGeom = this.buildGeometry.polygon.apply(this,
861                                                                [polys[i]]);
862                polyMember.appendChild(polyGeom);
863                gml.appendChild(polyMember);
864            }
865            return gml;
866
867        },
868 
869        /**
870         * Method: buildGeometry.bounds
871         * Given an OpenLayers bounds, create a GML box.
872         *
873         * Parameters:
874         * bounds - {<OpenLayers.Geometry.Bounds>} A bounds object.
875         *
876         * Returns:
877         * {DOMElement} A GML box node.
878         */
879        bounds: function(bounds) {
880            var gml = this.createElementNS(this.gmlns, "gml:Box");
881            gml.appendChild(this.buildCoordinatesNode(bounds));
882            return gml;
883        }
884    },
885
886    /**
887     * Method: buildCoordinates
888     * builds the coordinates XmlNode
889     * (code)
890     * <gml:coordinates decimal="." cs="," ts=" ">...</gml:coordinates>
891     * (end)
892     * Parameters:
893     * geometry - {<OpenLayers.Geometry>}
894     *
895     * Returns:
896     * {XmlNode} created xmlNode
897     */
898    buildCoordinatesNode: function(geometry) {
899        var coordinatesNode = this.createElementNS(this.gmlns,
900                                                   "gml:coordinates");
901        coordinatesNode.setAttribute("decimal", ".");
902        coordinatesNode.setAttribute("cs", ",");
903        coordinatesNode.setAttribute("ts", " ");
904
905        var parts = [];
906
907        if(geometry instanceof OpenLayers.Bounds){
908            parts.push(geometry.left + "," + geometry.bottom);
909            parts.push(geometry.right + "," + geometry.top);
910        } else {
911            var points = (geometry.components) ? geometry.components : [geometry];
912            for(var i=0; i<points.length; i++) {
913                parts.push(points[i].x + "," + points[i].y);               
914            }           
915        }
916
917        var txtNode = this.createTextNode(parts.join(" "));
918        coordinatesNode.appendChild(txtNode);
919       
920        return coordinatesNode;
921    },
922
923    CLASS_NAME: "OpenLayers.Format.GML" 
924});
Note: See TracBrowser for help on using the repository browser.