OpenLayers OpenLayers

Changeset 7214

Show
Ignore:
Timestamp:
05/19/08 01:08:22 (7 months ago)
Author:
tschaub
Message:

more complete gml rewrite - minus tests and envelope handling

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sandbox/vector-behavior/lib/OpenLayers/Format/GML/Base.js

    r7204 r7214  
    155155            }, 
    156156            "Point": function(node, container) { 
    157                 /** 
    158                  * Three coordinate variations to consider: 
    159                  * 1) <gml:pos>x y z</gml:pos> (GML3) 
    160                  * 2) <gml:coordinates>x, y, z</gml:coordinates> 
    161                  * 3) <gml:coord><gml:X>x</gml:X><gml:Y>y</gml:Y></gml:coord> 
    162                  */ 
    163157                var obj = {}; 
    164158                this.readChildNodes(node, obj); 
    165                 if(obj.coordinates) { 
    166                     if(obj.coordinates.length == 2) { 
    167                         obj.coordinates[2] = null; 
    168                     } 
    169                 } else { 
    170                     throw "Could not parse coordinates from node: " + node; 
    171                 } 
    172                 container.components.push( 
    173                     new OpenLayers.Geometry.Point( 
    174                         obj.coordinates[0], obj.coordinates[1] 
    175                     ) 
    176                 ); 
     159                container.components.push(obj.points[0]); 
    177160            }, 
    178161            // GML3 
    179162            "pos": function(node, obj) { 
    180                 var nodeList, coordString; 
    181                 var coords = []; 
    182      
    183                 // look for <gml:pos> 
    184163                var str = this.getChildValue(node).replace( 
    185164                    this.regExes, trimSpace, "" 
    186165                ); 
    187                 // TODO: check this.xy 
    188                 obj.coordinates = str.split(this.regExes.splitSpace); 
     166                var coords = str.split(this.regExes.splitSpace); 
     167                var point; 
     168                if(this.xy) { 
     169                    point = new OpenLayers.Geometry.Point( 
     170                        coords[0], coords[1], coords[2] 
     171                    ); 
     172                } else { 
     173                    point = new OpenLayers.Geometry.Point( 
     174                        coords[1], coords[0], coords[2] 
     175                    ); 
     176                } 
     177                obj.points = [point]; 
    189178            }, 
    190179            "coordinates": function(node, obj) { 
    191                 var str = this.getChildValue(node).replace( 
    192                     this.regExes.removeSpace, "" 
    193                 ); 
    194                 // TODO: check this.xy 
    195                 obj.coordinates = str.split(","); 
     180                var str = this.concatChildValues(node).replace( 
     181                    this.regExes.trimSpace, "" 
     182                ); 
     183                str = str.replace(this.regExes.trimComma, ","); 
     184                var pointList = str.split(this.regExes.splitSpace); 
     185                var coords; 
     186                var numPoints = pointList.length; 
     187                var points = new Array(numPoints); 
     188                for(var i=0; i<numPoints; ++i) { 
     189                    coords = pointList[i].split(","); 
     190                    if (this.xy) { 
     191                        points[i] = new OpenLayers.Geometry.Point( 
     192                            coords[0], coords[1], coords[2] 
     193                        ); 
     194                    } else { 
     195                        points[i] = new OpenLayers.Geometry.Point( 
     196                            coords[1], coords[0], coords[2] 
     197                        ); 
     198                    } 
     199                } 
     200                obj.points = points; 
    196201            }, 
    197202            "coord": function(node, obj) { 
    198203                var coord = {}; 
    199204                this.readChildNodes(node, coord); 
    200                 obj.coordinates = [coord.x, coord.y, coord.z]; 
     205                obj.points = [new OpenLayers.Geometry.Point( 
     206                    coord.x, coord.y, coord.z 
     207                )]; 
    201208            }, 
    202209            "X": function(node, coord) { 
     
    214221                container.components = [ 
    215222                    new OpenLayers.Geometry.MultiPoint(obj.components) 
     223                ]; 
     224            }, 
     225            "LineString": function(node, container) { 
     226                var obj = {}; 
     227                this.readChildNodes(node, obj); 
     228                container.components.push( 
     229                    new OpenLayers.Geometry.LineString(obj.points) 
     230                ); 
     231            }, 
     232            // GML3 
     233            "posList": function(node, obj) { 
     234                var str = this.concatChildValues(node).replace( 
     235                    this.regExes.trimSpace, "" 
     236                ); 
     237                var coords = str.split(this.regExes.splitSpace); 
     238                var dim = parseInt(node.getAttribute("dimension")); 
     239                var j, x, y, z; 
     240                var points = []; 
     241                var numPoints = coords.length / dim; 
     242                var points = new Array(numPoints); 
     243                for(var i=0; i<coords.length; i += dim) { 
     244                    x = coords[i]; 
     245                    y = coords[i+1]; 
     246                    z = (dim == 2) ? null : coords[i+2]; 
     247                    if (this.xy) { 
     248                        points[i/dim] = new OpenLayers.Geometry.Point(x, y, z); 
     249                    } else { 
     250                        points[i/dim] = new OpenLayers.Geometry.Point(y, x, z); 
     251                    } 
     252                } 
     253                obj.points = points; 
     254            }, 
     255            "MultiLineString": function(node, container) { 
     256                var obj = {}; 
     257                this.readChildNodes(node, obj); 
     258                container.components = [ 
     259                    new OpenLayers.Geometry.MultiLineString(obj.components) 
     260                ]; 
     261            }, 
     262            "Polygon": function(node, container) { 
     263                var obj = {outer: null, inner: []}; 
     264                this.readChildNodes(node, obj); 
     265                obj.inner.unshift(obj.outer); 
     266                container.components.push( 
     267                    new OpenLayers.Geometry.Polygon(obj.inner) 
     268                ); 
     269            }, 
     270            "outerBoundaryIs": function(node, container) { 
     271                var obj = {}; 
     272                this.readChildNodes(node, obj); 
     273                container.outer = obj.ring; 
     274            }, 
     275            "innerBoundaryIs": function(node, container) { 
     276                var obj = {}; 
     277                this.readChildNodes(node, obj); 
     278                container.inner.push(obj.ring); 
     279            }, 
     280            "LinearRing": function(node, obj) { 
     281                var container = {}; 
     282                this.readChildNodes(node, container); 
     283                obj.ring = new OpenLayers.Geometry.LinearRing( 
     284                    container.points 
     285                ); 
     286            }, 
     287            "MultiPolygon": function(node, container) { 
     288                var obj = {}; 
     289                this.readChildNodes(node, obj); 
     290                container.components = [ 
     291                    new OpenLayers.Geometry.MultiPolygon(obj.components) 
    216292                ]; 
    217293            } 
     
    303379            "Point": function(geometry) { 
    304380                var node = this.createElementNSPlus("gml:Point"); 
    305                 var coordinates; 
    306                 if(this.xy) { 
    307                     coordinates = [geometry.x, geometry.y]; 
    308                 } else { 
    309                     coordinates = [geometry.x, geometry.y]; 
    310                 } 
    311                 this.writeNode(node, "coordinates", coordinates); 
    312                 return node; 
    313             }, 
    314             "coordinates": function(array) { 
     381                this.writeNode(node, "coordinates", [geometry]); 
     382                return node; 
     383            }, 
     384            "coordinates": function(points) { 
     385                var numPoints = points.length; 
     386                var parts = new Array(numPoints); 
     387                var point; 
     388                for(var i=0; i<numPoints; ++i) { 
     389                    point = points[i]; 
     390                    if(this.xy) { 
     391                        parts[i] = point.x + "," + point.y; 
     392                    } else { 
     393                        parts[i] = point.y + "," + point.x; 
     394                    } 
     395                } 
    315396                return this.createElementNSPlus("gml:coordinates", { 
    316                     value: array.join(",") 
     397                    value: (numPoints == 1) ? parts[0] : parts.join(" ") 
    317398                }); 
    318399            }, 
     
    321402                for(var i=0; i<geometry.components.length; ++i) { 
    322403                    this.writeNode(node, "Point", geometry.components[i]); 
     404                } 
     405                return node; 
     406            }, 
     407            "LineString": function(geometry) { 
     408                var node = this.createElementNSPlus("gml:LineString"); 
     409                this.writeNode(node, "coordinates", geometry.components); 
     410                return node; 
     411            }, 
     412            "MultiLineString": function(geometry) { 
     413                var node = this.createElementNSPlus("gml:MultiLineString"); 
     414                for(var i=0; i<geometry.components.length; ++i) { 
     415                    this.writeNode(node, "LineString", geometry.components[i]); 
     416                } 
     417                return node; 
     418            }, 
     419            "Polygon": function(geometry) { 
     420                var node = this.createElementNSPlus("gml:Polygon"); 
     421                this.writeNode(node, "outerBoundaryIs", geometry.components[0]); 
     422                for(var i=1; i<geometry.components.length; ++i) { 
     423                    this.writeNode( 
     424                        node, "innerBoundaryIs", geometry.components[i] 
     425                    ); 
     426                } 
     427                return node; 
     428            }, 
     429            "outerBoundaryIs": function(ring) { 
     430                var node = this.createElementNSPlus("gml:outerBoundaryIs"); 
     431                this.writeNode(node, "LinearRing", ring); 
     432                return node; 
     433            }, 
     434            "innerBoundaryIs": function(ring) { 
     435                var node = this.createElementNSPlus("gml:innerBoundaryIs"); 
     436                this.writeNode(node, "LinearRing", ring); 
     437                return node; 
     438            }, 
     439            "LinearRing": function(ring) { 
     440                var node = this.createElementNSPlus("gml:LinearRing"); 
     441                this.writeNode(node, "coordinates", ring.components); 
     442                return node; 
     443            }, 
     444            "MultiPolygon": function(geometry) { 
     445                var node = this.createElementNSPlus("gml:MultiPolygon"); 
     446                for(var i=0; i<geometry.components.length; ++i) { 
     447                    this.writeNode( 
     448                        node, "Polygon", geometry.components[i] 
     449                    ); 
    323450                } 
    324451                return node;