OpenLayers OpenLayers

Changeset 5016

Show
Ignore:
Timestamp:
10/18/07 20:09:34 (1 year ago)
Author:
elam
Message:

Added option to allow gml coordinate to be ordered xy or yx.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sandbox/elam/openlayers/lib/OpenLayers/Format/GML.js

    r4960 r5016  
    7373     
    7474    /** 
     75     * APIProperty: xy 
     76     * Order of the gml coordinate (x,y) or false:(y,x) 
     77     * Changing is not recomended, a new Format should be instantiated. 
     78     */  
     79    xy: true, 
     80     
     81    /** 
    7582     * Constructor: OpenLayers.Format.GML 
    7683     * Create a new parser for GML. 
     
    241248                coords[2] = null; 
    242249            } 
    243             return new OpenLayers.Geometry.Point(coords[0], coords[1], 
     250             
     251            if (this.xy) { 
     252                return new OpenLayers.Geometry.Point(coords[0], coords[1], 
    244253                                                 coords[2]); 
     254            } 
     255            else{ 
     256                return new OpenLayers.Geometry.Point(coords[1], coords[0], 
     257                                                 coords[2]); 
     258            } 
    245259        }, 
    246260         
     
    306320                    y = coords[j+1]; 
    307321                    z = (dim == 2) ? null : coords[j+2]; 
    308                     points.push(new OpenLayers.Geometry.Point(x, y, z)); 
     322                    if (this.xy) { 
     323                        points.push(new OpenLayers.Geometry.Point(x, y, z)); 
     324                    } else { 
     325                        points.push(new OpenLayers.Geometry.Point(y, x, z)); 
     326                    } 
    309327                } 
    310328            } 
     
    326344                            coords[2] = null; 
    327345                        } 
    328                         points.push(new OpenLayers.Geometry.Point(coords[0], 
     346                        if (this.xy) { 
     347                            points.push(new OpenLayers.Geometry.Point(coords[0], 
    329348                                                                  coords[1], 
    330349                                                                  coords[2])); 
     350                        } else { 
     351                            points.push(new OpenLayers.Geometry.Point(coords[1], 
     352                                                                  coords[0], 
     353                                                                  coords[2])); 
     354                        } 
    331355                    } 
    332356                } 
     
    445469                    coords[2] = null; 
    446470                } 
    447                 var lowerPoint = new OpenLayers.Geometry.Point(coords[0], coords[1],coords[2]); 
     471                if (this.xy) { 
     472                    var lowerPoint = new OpenLayers.Geometry.Point(coords[0], coords[1],coords[2]); 
     473                } else { 
     474                    var lowerPoint = new OpenLayers.Geometry.Point(coords[1], coords[0],coords[2]); 
     475                } 
    448476            } 
    449477             
     
    461489                    coords[2] = null; 
    462490                } 
    463                 var upperPoint = new OpenLayers.Geometry.Point(coords[0], coords[1],coords[2]); 
     491                if (this.xy) { 
     492                    var upperPoint = new OpenLayers.Geometry.Point(coords[0], coords[1],coords[2]); 
     493                } else { 
     494                    var upperPoint = new OpenLayers.Geometry.Point(coords[1], coords[0],coords[2]); 
     495                } 
    464496            } 
    465497             
  • sandbox/elam/openlayers/tests/Format/test_GML.html

    r5001 r5016  
    7474        t.eq(data[0].geometry.x, 1, "x coord correct"); 
    7575        t.eq(data[0].geometry.y, 2, "y coord correct"); 
    76     }     
     76    } 
    7777    function test_Format_GML_read_linestring_geom(t) { 
    7878        t.plan(5); 
     
    162162        t.eq(data[0].geometry.CLASS_NAME, "OpenLayers.Geometry.Polygon", "Envelope GML returns correct classname"); 
    163163        t.eq(data[0].geometry.components[0].components[0].x, 0, "first x coord correct"); 
    164         t.eq(data[0].geometry.components[0].components[0].y, 0, "first y coord correct"); 
     164        t.eq(data[0].geometry.components[0].components[0].y, 1, "first y coord correct"); 
    165165        t.eq(data[0].geometry.components[0].components[1].x, 20, "second x coord correct"); 
    166         t.eq(data[0].geometry.components[0].components[1].y, 0, "second y coord correct"); 
     166        t.eq(data[0].geometry.components[0].components[1].y, 1, "second y coord correct"); 
    167167        t.eq(data[0].geometry.components[0].components[2].x, 20, "third x coord correct"); 
    168         t.eq(data[0].geometry.components[0].components[2].y, 20, "third y coord correct"); 
     168        t.eq(data[0].geometry.components[0].components[2].y, 21, "third y coord correct"); 
    169169        t.eq(data[0].geometry.components[0].components[3].x, 0, "fouth x coord correct"); 
    170         t.eq(data[0].geometry.components[0].components[3].y, 20, "fourth y coord correct"); 
     170        t.eq(data[0].geometry.components[0].components[3].y, 21, "fourth y coord correct"); 
    171171        t.eq(data[0].geometry.components[0].components[4].x, 0, "fifth x coord correct"); 
    172         t.eq(data[0].geometry.components[0].components[4].y, 0, "fifth y coord correct"); 
     172        t.eq(data[0].geometry.components[0].components[4].y, 1, "fifth y coord correct"); 
    173173        t.eq(data[0].geometry.components[0].components.length, 5, "coords length correct"); 
    174174        t.eq(data[0].geometry.components.length, 1, "rings length correct"); 
    175175    }     
     176    /////////////////////////////////////////////////////////////// 
     177    // tests the y x order of gml point 
     178    ///////////////////////////////////////////////////////////// 
     179    function test_Format_GML_read_point_geom_yx(t) { 
     180        t.plan(3); 
     181         
     182        var point = shell_start + geoms_yx['point'] + shell_end; 
     183        var parser = new OpenLayers.Format.GML({'xy':false}); 
     184        data = parser.read(point); 
     185        t.eq(data[0].geometry.CLASS_NAME, "OpenLayers.Geometry.Point", "Point GML returns correct classname"); 
     186        t.eq(data[0].geometry.x, 1, "x coord correct"); 
     187        t.eq(data[0].geometry.y, 2, "y coord correct"); 
     188    }  
     189    function test_Format_GML_read_linestring_geom_yx(t) { 
     190        t.plan(5); 
     191         
     192        var line = shell_start + geoms_yx['linestring'] + shell_end; 
     193        var parser = new OpenLayers.Format.GML({'xy':false}); 
     194        data = parser.read(line); 
     195        t.eq(data[0].geometry.CLASS_NAME, "OpenLayers.Geometry.LineString", "LineString GML returns correct classname"); 
     196        t.eq(data[0].geometry.components[0].x, 1, "first x coord correct"); 
     197        t.eq(data[0].geometry.components[0].y, 2, "first y coord correct"); 
     198        t.eq(data[0].geometry.components[1].x, 4, "second x coord correct"); 
     199        t.eq(data[0].geometry.components[1].y, 5, "second y coord correct"); 
     200    }     
     201    function test_Format_GML_read_polygon_geom_yx(t) { 
     202        t.plan(7); 
     203         
     204        var polygon = shell_start + geoms_yx['polygon'] + shell_end; 
     205        var parser = new OpenLayers.Format.GML({'xy':false}); 
     206        data = parser.read(polygon); 
     207        t.eq(data[0].geometry.CLASS_NAME, "OpenLayers.Geometry.Polygon", "Polygon GML returns correct classname"); 
     208        t.eq(data[0].geometry.components[0].components[0].x, 1, "first x coord correct"); 
     209        t.eq(data[0].geometry.components[0].components[0].y, 2, "first y coord correct"); 
     210        t.eq(data[0].geometry.components[0].components[1].x, 4, "second x coord correct"); 
     211        t.eq(data[0].geometry.components[0].components[1].y, 5, "second y coord correct"); 
     212        t.eq(data[0].geometry.components[0].components.length, 4, "coords length correct"); 
     213        t.eq(data[0].geometry.components.length, 1, "rings length correct"); 
     214    }    
     215    function test_Format_GML_read_multipoint_geom_yx(t) { 
     216        t.plan(6); 
     217         
     218        var multipoint = shell_start + geoms_yx['multipoint'] + shell_end; 
     219        var parser = new OpenLayers.Format.GML({'xy':false}); 
     220        data = parser.read(multipoint); 
     221        t.eq(data[0].geometry.CLASS_NAME, "OpenLayers.Geometry.MultiPoint", "MultiPoint GML returns correct classname"); 
     222        t.eq(data[0].geometry.components[0].x, 1, "x coord correct"); 
     223        t.eq(data[0].geometry.components[0].y, 2, "y coord correct"); 
     224        t.eq(data[0].geometry.components.length, 2, "length correct"); 
     225        t.eq(data[0].geometry.components[1].x, 4, "x coord correct"); 
     226        t.eq(data[0].geometry.components[1].y, 5, "y coord correct"); 
     227    }     
     228    function test_Format_GML_read_multilinestring_geom_yx(t) { 
     229        t.plan(6); 
     230         
     231        var multilinestring = shell_start + geoms_yx['multilinestring'] + shell_end; 
     232        var parser = new OpenLayers.Format.GML({'xy':false}); 
     233        data = parser.read(multilinestring); 
     234        t.eq(data[0].geometry.CLASS_NAME, "OpenLayers.Geometry.MultiLineString", "MultiLineString GML returns correct classname"); 
     235        t.eq(data[0].geometry.components[0].components[0].x, 1, "x coord correct"); 
     236        t.eq(data[0].geometry.components[0].components[0].y, 2, "y coord correct"); 
     237        t.eq(data[0].geometry.components[0].components.length, 2, "length correct"); 
     238        t.eq(data[0].geometry.components[0].components[1].x, 4, "x coord correct"); 
     239        t.eq(data[0].geometry.components[0].components[1].y, 5, "y coord correct"); 
     240         
     241    }     
     242    function test_Format_GML_read_polygon_with_holes_geom_yx(t) { 
     243        t.plan(12); 
     244         
     245        var polygon_with_holes = shell_start + geoms_yx['polygon_with_holes'] + shell_end; 
     246        var parser = new OpenLayers.Format.GML({'xy':false}); 
     247        data = parser.read(polygon_with_holes); 
     248        t.eq(data[0].geometry.CLASS_NAME, "OpenLayers.Geometry.Polygon", "Polygon GML returns correct classname"); 
     249        t.eq(data[0].geometry.components[0].components[0].x, 1, "first x coord correct"); 
     250        t.eq(data[0].geometry.components[0].components[0].y, 2, "first y coord correct"); 
     251        t.eq(data[0].geometry.components[0].components[1].x, 4, "second x coord correct"); 
     252        t.eq(data[0].geometry.components[0].components[1].y, 5, "second y coord correct"); 
     253        t.eq(data[0].geometry.components[0].components.length, 4, "coords length correct"); 
     254        t.eq(data[0].geometry.components[1].components[0].x, 11, "first x coord correct"); 
     255        t.eq(data[0].geometry.components[1].components[0].y, 12, "first y coord correct"); 
     256        t.eq(data[0].geometry.components[1].components[1].x, 14, "second x coord correct"); 
     257        t.eq(data[0].geometry.components[1].components[1].y, 15, "second y coord correct"); 
     258        t.eq(data[0].geometry.components[1].components.length, 4, "coords length correct"); 
     259        t.eq(data[0].geometry.components.length, 2, "rings length correct"); 
     260    } 
     261 
     262    function test_Format_GML_read_envelope_geom_yx(t) { 
     263        t.plan(13); 
     264         
     265        var envelope = shell_start + geoms_yx['envelope'] + shell_end; 
     266        var parser = new OpenLayers.Format.GML({'xy':false}); 
     267        data = parser.read(envelope); 
     268        t.eq(data[0].geometry.CLASS_NAME, "OpenLayers.Geometry.Polygon", "Envelope GML returns correct classname"); 
     269        t.eq(data[0].geometry.components[0].components[0].x, 0, "first x coord correct"); 
     270        t.eq(data[0].geometry.components[0].components[0].y, 1, "first y coord correct"); 
     271        t.eq(data[0].geometry.components[0].components[1].x, 20, "second x coord correct"); 
     272        t.eq(data[0].geometry.components[0].components[1].y, 1, "second y coord correct"); 
     273        t.eq(data[0].geometry.components[0].components[2].x, 20, "third x coord correct"); 
     274        t.eq(data[0].geometry.components[0].components[2].y, 21, "third y coord correct"); 
     275        t.eq(data[0].geometry.components[0].components[3].x, 0, "fouth x coord correct"); 
     276        t.eq(data[0].geometry.components[0].components[3].y, 21, "fourth y coord correct"); 
     277        t.eq(data[0].geometry.components[0].components[4].x, 0, "fifth x coord correct"); 
     278        t.eq(data[0].geometry.components[0].components[4].y, 1, "fifth y coord correct"); 
     279        t.eq(data[0].geometry.components[0].components.length, 5, "coords length correct"); 
     280        t.eq(data[0].geometry.components.length, 1, "rings length correct"); 
     281    } 
     282     
     283    function test_Format_GML_write_geoms_yx(t) { 
     284        t.plan(5); 
     285        var parser = new OpenLayers.Format.GML({'xy':false}); 
     286         
     287        var point_xy = shell_start + serialize_geoms['point'] + shell_end; 
     288        var point = shell_start + serialize_geoms_yx['point'] + shell_end; 
     289        data = parser.read(point); 
     290        var output = parser.write(data); 
     291        t.eq(output, point_xy, "Point geometry round trips correctly."); 
     292         
     293        var linestring_xy = shell_start + serialize_geoms['linestring'] + shell_end; 
     294        var linestring = shell_start + serialize_geoms_yx['linestring'] + shell_end; 
     295        data = parser.read(linestring); 
     296        var output = parser.write(data); 
     297        t.eq(output, linestring_xy, "Line geometry round trips correctly."); 
     298         
     299        var polygon_xy = shell_start + serialize_geoms['polygon'] + shell_end; 
     300        var polygon = shell_start + serialize_geoms_yx['polygon'] + shell_end; 
     301        data = parser.read(polygon); 
     302        var output = parser.write(data); 
     303        t.eq(output, polygon_xy, "Poly geometry round trips correctly."); 
     304         
     305        var multipoint_xy = shell_start + serialize_geoms['multipoint'] + shell_end; 
     306        var multipoint = shell_start + serialize_geoms_yx['multipoint'] + shell_end; 
     307        data = parser.read(multipoint); 
     308        var output = parser.write(data); 
     309        t.eq(output, multipoint_xy, "MultiPoint geometry round trips correctly."); 
     310         
     311        var multilinestring_xy = shell_start + serialize_geoms['multilinestring'] + shell_end; 
     312        var multilinestring = shell_start + serialize_geoms_yx['multilinestring'] + shell_end; 
     313        data = parser.read(multilinestring); 
     314        var output = parser.write(data); 
     315        t.eq(output, multilinestring_xy, "MultiLine geometry round trips correctly."); 
     316    } 
     317     
    176318 
    177319    var test_content = ['<?xml version="1.0" encoding="utf-8" ?>\n<ogr:FeatureCollection\n     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"\n     xsi:schemaLocation="http://ogr.maptools.org/ testoutput.xsd"\n     xmlns:ogr="http://ogr.maptools.org/"\n     xmlns:gml="http://www.opengis.net/gml">\n  <gml:boundedBy>\n    <gml:Box>\n      <gml:coord><gml:X>-1254041.389711702</gml:X><gml:Y>250906.9515983529</gml:Y></gml:coord>\n      <gml:coord><gml:X>-634517.1199908922</gml:X><gml:Y>762236.2940800377</gml:Y></gml:coord>\n    </gml:Box>\n  </gml:boundedBy>                    \n  <gml:featureMember>\n    <ogr:states fid="F0">\n      <ogr:geometryProperty><gml:Polygon><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>-634517.11999089224,691849.77929356066,0 -653761.64509297756,471181.53429472551,0 -673343.60852865304,250906.9515983529,0 -1088825.734430399,299284.85108220269,0 -1254041.3897117018,324729.27754874947,0 -1235750.4212498858,434167.33911316615,0 -1190777.7803201093,704392.96327195223,0 -1181607.835811228,762236.29408003774,0 -634517.11999089224,691849.77929356066,0</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon></ogr:geometryProperty>\n      <ogr:NAME>WY</ogr:NAME>\n    <ogr:LONGNAME><![CDATA[Wyoming]]></ogr:LONGNAME>\n    </ogr:states>\n  </gml:featureMember>\n</ogr:FeatureCollection>\n', 
     
    220362        'multipoint': '<gml:MultiPoint><gml:Point><gml:coordinates>1,2,3</gml:coordinates></gml:Point><gml:Point><gml:coordinates>4,5,6</gml:coordinates></gml:Point></gml:MultiPoint>', 
    221363        'multilinestring': '<gml:MultiLineString><gml:LineString><gml:coordinates>1,2,3 4,5,6</gml:coordinates></gml:LineString><gml:LineString><gml:coordinates>11,12,13 14,15,16</gml:coordinates></gml:LineString></gml:MultiLineString>', 
    222         'envelope': '<gml:Envelope><gml:lowerCorner>0 0</gml:lowerCorner><gml:upperCorner>20 20</gml:upperCorner></gml:Envelope>' // , 
     364        'envelope': '<gml:Envelope><gml:lowerCorner>0 1</gml:lowerCorner><gml:upperCorner>20 21</gml:upperCorner></gml:Envelope>' // , 
    223365        // 'multipolygon_with_holes': '<gml:MultiPolygon><gml:Polygon><gml:outerBoundaryIs>1,2 4,5 3,6 1,2</gml:outerBoundaryIs><gml:innerBoundaryIs>11,12 14,15 13,16 11,12</gml:innerBoundaryIs></gml:Polygon><gml:Polygon><gml:outerBoundaryIs>101,102 104,105 103,106 101,102</gml:outerBoundaryIs><gml:innerBoundaryIs>111,112 114,115 113,116 111,112</gml:innerBoundaryIs></gml:Polygon></gml:MultiPolygon>' 
    224366    }; 
     367 
     368 
     369// 
     370// The following data has the (x y) reordered to (y x), in 3 dimensions it goes from (x y z) to (y x z) 
     371// 
     372     
     373    var serialize_geoms_yx = { 
     374        'point': '<gml:Point><gml:coordinates decimal="." cs="," ts=" ">2,1</gml:coordinates></gml:Point>', 
     375        'linestring': '<gml:LineString><gml:coordinates decimal="." cs="," ts=" ">2,1 5,4</gml:coordinates></gml:LineString>', 
     376        'polygon': '<gml:Polygon><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates decimal="." cs="," ts=" ">2,1 5,4 6,3 2,1</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon>', 
     377        'multipoint': '<gml:MultiPoint><gml:pointMember><gml:Point><gml:coordinates decimal="." cs="," ts=" ">2,1</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates decimal="." cs="," ts=" ">5,4</gml:coordinates></gml:Point></gml:pointMember></gml:MultiPoint>', 
     378        'multilinestring': '<gml:MultiLineString><gml:lineStringMember><gml:LineString><gml:coordinates decimal="." cs="," ts=" ">2,1 5,4</gml:coordinates></gml:LineString></gml:lineStringMember><gml:lineStringMember><gml:LineString><gml:coordinates decimal="." cs="," ts=" ">12,11 15,14</gml:coordinates></gml:LineString></gml:lineStringMember></gml:MultiLineString>' 
     379    }; 
     380    var geoms_yx = { 
     381        'point': '<gml:Point><gml:coordinates>2,1,3</gml:coordinates></gml:Point>', 
     382        'linestring': '<gml:LineString><gml:coordinates>2,1,3 5,4,6</gml:coordinates></gml:LineString>', 
     383        'polygon': '<gml:Polygon><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>2,1 5,4 6,3 2,1</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs></gml:Polygon>', 
     384        'polygon_with_holes': '<gml:Polygon><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>2,1 5,4 6,3 2,1</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs><gml:innerBoundaryIs><gml:LinearRing><gml:coordinates>12,11 15,14 16,13 12,11</gml:coordinates></gml:LinearRing></gml:innerBoundaryIs></gml:Polygon>', 
     385        'multipoint': '<gml:MultiPoint><gml:Point><gml:coordinates>2,1,3</gml:coordinates></gml:Point><gml:Point><gml:coordinates>5,4,6</gml:coordinates></gml:Point></gml:MultiPoint>', 
     386        'multilinestring': '<gml:MultiLineString><gml:LineString><gml:coordinates>2,1,3 5,4,6</gml:coordinates></gml:LineString><gml:LineString><gml:coordinates>12,11,13 15,14,16</gml:coordinates></gml:LineString></gml:MultiLineString>', 
     387        'envelope': '<gml:Envelope><gml:lowerCorner>1 0</gml:lowerCorner><gml:upperCorner>21 20</gml:upperCorner></gml:Envelope>' 
     388    }; 
     389     
    225390    </script>  
    226391</head>