OpenLayers OpenLayers

Ticket #1028: geojson_draft4.patch

File geojson_draft4.patch, 19.2 kB (added by crschmidt, 1 year ago)

update geojson to follow latest spec

  • tests/Format/test_GeoJSON.html

    old new  
    33    <script src="../../lib/OpenLayers.js"></script>  
    44    <script type="text/javascript"> 
    55 
    6     var poly_content = '{"type": "FeatureCollection", "members": [{"geometry": {"type": "Polygon", "coordinates": [[[-131.484375, -5.9765625], [-112.5, -58.0078125], [-32.34375, -50.2734375], [-114.609375, 52.3828125], [-167.34375, -35.5078125], [-146.953125, -57.3046875], [-139.921875, -34.1015625], [-131.484375, -5.9765625]]]}, "type": "Feature", "id": 562, "properties": {"strokeColor": "red", "title": "Feature 2", "author": "Your Name Here"}}]}';  
     6    var poly_content = '{"type": "FeatureCollection", "features": [{"geometry": {"type": "Polygon", "coordinates": [[[-131.484375, -5.9765625], [-112.5, -58.0078125], [-32.34375, -50.2734375], [-114.609375, 52.3828125], [-167.34375, -35.5078125], [-146.953125, -57.3046875], [-139.921875, -34.1015625], [-131.484375, -5.9765625]]]}, "type": "Feature", "id": 562, "properties": {"strokeColor": "red", "title": "Feature 2", "author": "Your Name Here"}}]}';  
    77    var point_feature = '{"geometry": {"type": "Point", "coordinates": [94.21875, 72.94921875]}, "type": "Feature", "id": 573, "properties": {"strokeColor": "blue", "title": "Feature 5", "author": "Your Name Here"}}' 
    8     var line_feature = '{"type": "FeatureCollection", "members": [{"geometry": {"type": "LineString", "coordinates": [[-27.0703125, 59.4140625], [-77.6953125, 20.7421875], [30.5859375, -36.2109375], [67.1484375, 34.8046875]]}, "type": "Feature", "id": 559, "properties": {"strokeColor": "red", "title": "Feature 1", "author": "Your Name Here"}}]}'; 
    9     var multiple_features = '{"type": "FeatureCollection", "members": [{"geometry": {"type": "Point", "coordinates": [-91.0546875, 43.9453125]}, "type": "Feature", "id": 577, "properties": {"strokeColor": "red", "title": "Feature 2", "image": "foo.gif", "author": "Your Name Here"}}, {"geometry": {"type": "LineString", "coordinates": [[91.40625, -1.40625], [116.015625, -42.890625], [153.28125, -28.125], [108.984375, 11.25], [75.234375, 8.4375], [76.640625, 9.140625], [67.5, -36.5625], [67.5, -35.859375]]}, "type": "Feature", "id": 576, "properties": {"strokeColor": "red", "title": "Feature 1", "author": "Your Name Here"}}, {"geometry": {"type": "Point", "coordinates": [139.5703125, 57.48046875]}, "type": "Feature", "id": 575, "properties": {"strokeColor": "blue", "title": "Feature 7", "author": "Your Name Here"}}, {"geometry": {"type": "Point", "coordinates": [107.2265625, 82.44140625]}, "type": "Feature", "id": 574, "properties": {"strokeColor": "blue", "title": "Feature 6", "author": "Your Name Here"}}, {"geometry": {"type": "Point", "coordinates": [94.21875, 72.94921875]}, "type": "Feature", "id": 573, "properties": {"strokeColor": "blue", "title": "Feature 5", "author": "Your Name Here"}}, {"geometry": {"type": "Point", "coordinates": [116.3671875, 61.69921875]}, "type": "Feature", "id": 572, "properties": {"strokeColor": "blue", "title": "Feature 4", "author": "Your Name Here"}}, {"geometry": {"type": "Point", "coordinates": [145.8984375, 73.65234375]}, "type": "Feature", "id": 571, "properties": {"strokeColor": "blue", "title": "Feature 3", "author": "Your Name Here"}}, {"geometry": {"type": "Polygon", "coordinates": [[[32.34375, 52.20703125], [87.1875, 70.13671875], [122.6953125, 37.44140625], [75.234375, 42.36328125], [40.078125, 42.36328125], [28.828125, 48.33984375], [18.6328125, 56.77734375], [23.203125, 65.56640625], [32.34375, 52.20703125]]]}, "type": "Feature", "id": 570, "properties": {"strokeColor": "blue", "title": "Feature 2", "author": "Your Name Here"}}, {"geometry": {"type": "Point", "coordinates": [62.578125, -53.4375]}, "type": "Feature", "id": 569, "properties": {"strokeColor": "red", "title": "Feature 3", "author": "Your Name Here"}}, {"geometry": {"type": "Point", "coordinates": [121.640625, 16.875]}, "type": "Feature", "id": 568, "properties": {"strokeColor": "red", "title": "Feature 6", "author": "Your Name Here"}}, {"geometry": {"type": "Point", "coordinates": [135.703125, 8.4375]}, "type": "Feature", "id": 567, "properties": {"strokeColor": "red", "title": "Feature 4", "author": "Your Name Here"}}, {"geometry": {"type": "Point", "coordinates": [137.109375, 48.515625]}, "type": "Feature", "id": 566, "properties": {"strokeColor": "red", "title": "Feature 274", "author": "Your Name Here"}}, {"geometry": {"type": "Point", "coordinates": [0, 5]}, "type": "Feature", "id": 565, "properties": {}}, {"geometry": {"type": "Point", "coordinates": [0, 5]}, "type": "Feature", "id": 564, "properties": {}}, {"geometry": {"type": "Point", "coordinates": [0, 5]}, "type": "Feature", "id": 563, "properties": {}}, {"geometry": {"type": "Polygon", "coordinates": [[[-131.484375, -5.9765625], [-112.5, -58.0078125], [-32.34375, -50.2734375], [-114.609375, 52.3828125], [-167.34375, -35.5078125], [-146.953125, -57.3046875], [-139.921875, -34.1015625], [-131.484375, -5.9765625]]]}, "type": "Feature", "id": 562, "properties": {"strokeColor": "red", "title": "Feature 2", "author": "Your Name Here"}}, {"geometry": {"type": "Point", "coordinates": [48.8671875, -15.8203125]}, "type": "Feature", "id": 560, "properties": {"strokeColor": "red", "title": "Feature 2", "author": "Your Name Here"}}, {"geometry": {"type": "LineString", "coordinates": [[-27.0703125, 59.4140625], [-77.6953125, 20.7421875], [30.5859375, -36.2109375], [67.1484375, 34.8046875]]}, "type": "Feature", "id": 559, "properties": {"strokeColor": "red", "title": "Feature 1", "author": "Your Name Here"}}, {"geometry": {"type": "Point", "coordinates": [12.65625, 16.5234375]}, "type": "Feature", "id": 558, "properties": {"styleUrl": "#allstyle", "title": "Feature 1", "strokeColor": "red", "author": "Your Name Here"}}]}'; 
     8    var line_feature = '{"type": "FeatureCollection", "features": [{"geometry": {"type": "LineString", "coordinates": [[-27.0703125, 59.4140625], [-77.6953125, 20.7421875], [30.5859375, -36.2109375], [67.1484375, 34.8046875]]}, "type": "Feature", "id": 559, "properties": {"strokeColor": "red", "title": "Feature 1", "author": "Your Name Here"}}]}'; 
     9    var multiple_features = '{"type": "FeatureCollection", "features": [{"geometry": {"type": "Point", "coordinates": [-91.0546875, 43.9453125]}, "type": "Feature", "id": 577, "properties": {"strokeColor": "red", "title": "Feature 2", "image": "foo.gif", "author": "Your Name Here"}}, {"geometry": {"type": "LineString", "coordinates": [[91.40625, -1.40625], [116.015625, -42.890625], [153.28125, -28.125], [108.984375, 11.25], [75.234375, 8.4375], [76.640625, 9.140625], [67.5, -36.5625], [67.5, -35.859375]]}, "type": "Feature", "id": 576, "properties": {"strokeColor": "red", "title": "Feature 1", "author": "Your Name Here"}}, {"geometry": {"type": "Point", "coordinates": [139.5703125, 57.48046875]}, "type": "Feature", "id": 575, "properties": {"strokeColor": "blue", "title": "Feature 7", "author": "Your Name Here"}}, {"geometry": {"type": "Point", "coordinates": [107.2265625, 82.44140625]}, "type": "Feature", "id": 574, "properties": {"strokeColor": "blue", "title": "Feature 6", "author": "Your Name Here"}}, {"geometry": {"type": "Point", "coordinates": [94.21875, 72.94921875]}, "type": "Feature", "id": 573, "properties": {"strokeColor": "blue", "title": "Feature 5", "author": "Your Name Here"}}, {"geometry": {"type": "Point", "coordinates": [116.3671875, 61.69921875]}, "type": "Feature", "id": 572, "properties": {"strokeColor": "blue", "title": "Feature 4", "author": "Your Name Here"}}, {"geometry": {"type": "Point", "coordinates": [145.8984375, 73.65234375]}, "type": "Feature", "id": 571, "properties": {"strokeColor": "blue", "title": "Feature 3", "author": "Your Name Here"}}, {"geometry": {"type": "Polygon", "coordinates": [[[32.34375, 52.20703125], [87.1875, 70.13671875], [122.6953125, 37.44140625], [75.234375, 42.36328125], [40.078125, 42.36328125], [28.828125, 48.33984375], [18.6328125, 56.77734375], [23.203125, 65.56640625], [32.34375, 52.20703125]]]}, "type": "Feature", "id": 570, "properties": {"strokeColor": "blue", "title": "Feature 2", "author": "Your Name Here"}}, {"geometry": {"type": "Point", "coordinates": [62.578125, -53.4375]}, "type": "Feature", "id": 569, "properties": {"strokeColor": "red", "title": "Feature 3", "author": "Your Name Here"}}, {"geometry": {"type": "Point", "coordinates": [121.640625, 16.875]}, "type": "Feature", "id": 568, "properties": {"strokeColor": "red", "title": "Feature 6", "author": "Your Name Here"}}, {"geometry": {"type": "Point", "coordinates": [135.703125, 8.4375]}, "type": "Feature", "id": 567, "properties": {"strokeColor": "red", "title": "Feature 4", "author": "Your Name Here"}}, {"geometry": {"type": "Point", "coordinates": [137.109375, 48.515625]}, "type": "Feature", "id": 566, "properties": {"strokeColor": "red", "title": "Feature 274", "author": "Your Name Here"}}, {"geometry": {"type": "Point", "coordinates": [0, 5]}, "type": "Feature", "id": 565, "properties": {}}, {"geometry": {"type": "Point", "coordinates": [0, 5]}, "type": "Feature", "id": 564, "properties": {}}, {"geometry": {"type": "Point", "coordinates": [0, 5]}, "type": "Feature", "id": 563, "properties": {}}, {"geometry": {"type": "Polygon", "coordinates": [[[-131.484375, -5.9765625], [-112.5, -58.0078125], [-32.34375, -50.2734375], [-114.609375, 52.3828125], [-167.34375, -35.5078125], [-146.953125, -57.3046875], [-139.921875, -34.1015625], [-131.484375, -5.9765625]]]}, "type": "Feature", "id": 562, "properties": {"strokeColor": "red", "title": "Feature 2", "author": "Your Name Here"}}, {"geometry": {"type": "Point", "coordinates": [48.8671875, -15.8203125]}, "type": "Feature", "id": 560, "properties": {"strokeColor": "red", "title": "Feature 2", "author": "Your Name Here"}}, {"geometry": {"type": "LineString", "coordinates": [[-27.0703125, 59.4140625], [-77.6953125, 20.7421875], [30.5859375, -36.2109375], [67.1484375, 34.8046875]]}, "type": "Feature", "id": 559, "properties": {"strokeColor": "red", "title": "Feature 1", "author": "Your Name Here"}}, {"geometry": {"type": "Point", "coordinates": [12.65625, 16.5234375]}, "type": "Feature", "id": 558, "properties": {"styleUrl": "#allstyle", "title": "Feature 1", "strokeColor": "red", "author": "Your Name Here"}}]}'; 
    1010    var parser = new OpenLayers.Format.GeoJSON(); 
    1111 
    1212    function test_Format_GeoJSON_constructor(t) {  
     
    145145 
    146146       var geomcol = { 
    147147           "type": "GeometryCollection", 
    148            "members": [ 
     148           "geometries": [ 
    149149               { 
    150150                   "type": "Point", 
    151151                   "coordinates": [100.0, 0.0] 
     
    189189 
    190190        var line_object = { 
    191191            "type": "FeatureCollection",  
    192             "members": [ 
     192            "features": [ 
    193193                { 
    194194                    "geometry": { 
    195195                        "type": "LineString",  
     
    212212        }; 
    213213        data = parser.read(line_object); 
    214214        out = parser.write(data); 
    215         serialized = '{"type":"FeatureCollection","members":[{"type":"Feature","id":559,"properties":{"strokeColor":"red","title":"Feature 1","author":"Your Name Here"},"geometry":{"type":"LineString","coordinates":[[-27.0703125,59.4140625],[-77.6953125,20.7421875],[30.5859375,-36.2109375],[67.1484375,34.8046875]]}}]}'; 
     215        serialized = '{"type":"FeatureCollection","features":[{"type":"Feature","id":559,"properties":{"strokeColor":"red","title":"Feature 1","author":"Your Name Here"},"geometry":{"type":"LineString","coordinates":[[-27.0703125,59.4140625],[-77.6953125,20.7421875],[30.5859375,-36.2109375],[67.1484375,34.8046875]]}}]}'; 
    216216        t.eq(out, serialized, "input and output on line collections are the same"); 
    217217         
    218218        var serialize_tests = [ 
     
    253253        serialize_tests.push([multiline, '{"type":"MultiLineString","coordinates":[[[1,2],[3,4]],[[1,2],[3,4]]]}']); 
    254254        multipolygon = new OpenLayers.Geometry.MultiPolygon([serialize_tests[4][0],  serialize_tests[4][0]]); 
    255255        serialize_tests.push([multipolygon, '{"type":"MultiPolygon","coordinates":[[[[1,2],[3,4],[5,6],[1,2]]],[[[1,2],[3,4],[5,6],[1,2]]]]}']); 
    256         serialize_tests.push([ [ serialize_tests[0][0] ], '{"type":"FeatureCollection","members":[{"type":"Feature","id":0,"properties":{},"geometry":{"type":"Point","coordinates":[1,2]}}]}' ]); 
    257         serialize_tests.push([ [ serialize_tests[1][0], serialize_tests[2][0] ], '{"type":"GeometryCollection","members":[{"type":"Point","coordinates":[1,2]},{"type":"MultiPoint","coordinates":[[1,2]]}]}' ]); 
     256        serialize_tests.push([ [ serialize_tests[0][0] ], '{"type":"FeatureCollection","features":[{"type":"Feature","id":0,"properties":{},"geometry":{"type":"Point","coordinates":[1,2]}}]}' ]); 
     257        serialize_tests.push([ [ serialize_tests[1][0], serialize_tests[2][0] ], '{"type":"GeometryCollection","geometries":[{"type":"Point","coordinates":[1,2]},{"type":"MultiPoint","coordinates":[[1,2]]}]}' ]); 
    258258        for (var i = 0; i < serialize_tests.length; i++) { 
    259259            var input = serialize_tests[i][0]; 
    260260            var output = serialize_tests[i][1]; 
     
    268268 
    269269        var line_object = { 
    270270            "type": "FeatureCollection",  
    271             "members": [ 
     271            "features": [ 
    272272                { 
    273273                    "geometry": { 
    274274                        "type": "LineString",  
  • lib/OpenLayers/Format/GeoJSON.js

    old new  
    9393                    break; 
    9494                case "GeometryCollection": 
    9595                    results = []; 
    96                     for(var i=0; i<obj.members.length; ++i) { 
     96                    for(var i=0; i<obj.geometries.length; ++i) { 
    9797                        try { 
    98                             results.push(this.parseGeometry(obj.members[i])); 
     98                            results.push(this.parseGeometry(obj.geometries[i])); 
    9999                        } catch(err) { 
    100100                            results = null; 
    101101                            OpenLayers.Console.error(err); 
     
    115115                            } 
    116116                            break; 
    117117                        case "FeatureCollection": 
    118                             for(var i=0; i<obj.members.length; ++i) { 
     118                            for(var i=0; i<obj.features.length; ++i) { 
    119119                                try { 
    120                                     results.push(this.parseFeature(obj.members[i])); 
     120                                    results.push(this.parseFeature(obj.features[i])); 
    121121                                } catch(err) { 
    122122                                    results = null; 
    123123                                    OpenLayers.Console.error(err); 
     
    125125                            } 
    126126                            break; 
    127127                        case "GeometryCollection": 
    128                             for(var i=0; i<obj.members.length; ++i) { 
     128                            for(var i=0; i<obj.geometries.length; ++i) { 
    129129                                try { 
    130                                     var geom = this.parseGeometry(obj.members[i]); 
     130                                    var geom = this.parseGeometry(obj.geometries[i]); 
    131131                                    results.push(new OpenLayers.Feature.Vector(geom)); 
    132132                                } catch(err) { 
    133133                                    results = null; 
     
    436436            "type": null 
    437437        }; 
    438438        if(obj instanceof Array) { 
    439             geojson.members = []; 
     439            if(obj[0] instanceof OpenLayers.Feature.Vector) { 
     440                geojson.features = []; 
     441            } else if (obj[0].CLASS_NAME.search("OpenLayers.Geometry") == 0) { 
     442                geojson.geometries = []; 
     443            }     
    440444            for(var i=0; i<obj.length; ++i) { 
    441445                var element = obj[i]; 
    442446                if(element instanceof OpenLayers.Feature.Vector) { 
    443447                    if(geojson.type == null) { 
    444448                        geojson.type = "FeatureCollection"; 
    445449                        if(element.layer && element.layer.projection) { 
    446                             var proj = element.layer.projection; 
    447                             if(proj.match(/epsg:/i)) { 
    448                                 geojson.crs = { 
    449                                     "type": "EPSG", 
    450                                     "properties": { 
    451                                         "code": parseInt(proj.substring(proj.indexOf(":") + 1)) 
    452                                     } 
    453                                 }; 
    454                             } 
     450                            this.createCRSObject(element); 
    455451                        } 
    456452                    } else if(geojson.type != "FeatureCollection") { 
    457453                        OpenLayers.Console.error("FeatureCollection only supports collections of features: " + element); 
    458454                        break; 
    459455                    } 
    460                     geojson.members.push(this.extract.feature.apply(this, [element])); 
     456                    geojson.features.push(this.extract.feature.apply(this, [element])); 
    461457                } else if (element.CLASS_NAME.search("OpenLayers.Geometry") == 0) { 
    462458                    if(geojson.type == null) { 
    463459                        geojson.type = "GeometryCollection"; 
     
    465461                        OpenLayers.Console.error("GeometryCollection only supports collections of geometries: " + element); 
    466462                        break; 
    467463                    } 
    468                     geojson.members.push(this.extract.geometry.apply(this, [element])); 
     464                    geojson.geometries.push(this.extract.geometry.apply(this, [element])); 
    469465                } 
    470466            } 
    471467        } else if (obj.CLASS_NAME.search("OpenLayers.Geometry") == 0) { 
     
    473469        } else if (obj instanceof OpenLayers.Feature.Vector) { 
    474470            geojson = this.extract.feature.apply(this, [obj]); 
    475471            if(obj.layer && obj.layer.projection) { 
    476                 var proj = obj.layer.projection; 
    477                 if(proj.match(/epsg:/i)) { 
    478                     geojson.crs = { 
    479                         "type": "EPSG", 
    480                         "properties": { 
    481                             "code": parseInt(proj.substring(proj.indexOf(":") + 1)) 
    482                         } 
    483                     }; 
    484                 } 
     472                this.createCRSObject(obj); 
    485473            } 
    486474        } 
    487475        return OpenLayers.Format.JSON.prototype.write.apply(this, 
    488476                                                            [geojson, pretty]); 
    489477    }, 
     478 
     479    /** 
     480     * Method: createCRSObject 
     481     * Create the CRS object for an object. 
     482     */ 
     483    createCRSObject: function(object) { 
     484       var proj = object.layer.projection; 
     485       if (proj.match(/epsg:/i)) { 
     486           var code = parseInt(proj.substring(proj.indexOf(":") + 1)); 
     487           if (code == 4326) { 
     488               geojson.crs = { 
     489                   "type": "OGC", 
     490                   "properties": { 
     491                       "urn": "urn:ogc:def:crs:OGC:1.3:CRS84" 
     492                   } 
     493               }; 
     494           } else {     
     495               geojson.crs = { 
     496                   "type": "EPSG", 
     497                   "properties": { 
     498                       "code": code  
     499                   } 
     500               }; 
     501           }     
     502       } 
     503    }, 
    490504     
    491505    /** 
    492506     * Property: extract