OpenLayers OpenLayers

Ticket #1062: geojson.3.patch

File geojson.3.patch, 3.3 kB (added by crschmidt, 1 year ago)

patch not dependant on test ordering

  • tests/Format/test_GeoJSON.html

    old new  
    184184        t.eq(types, {'Point':15, 'Polygon': 2, 'LineString':2}, "Correct number of each type");  
    185185    } 
    186186 
     187    function test_Format_GeoJSON_writeWithCRS(t) { 
     188        t.plan(2) 
     189        var feature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(1,2)); 
     190        feature.fid = 0; 
     191        var output = '{"type":"Feature","id":0,"properties":{},"geometry":{"type":"Point","coordinates":[1,2]},"crs":{"type":"OGC","properties":{"urn":"urn:ogc:def:crs:OGC:1.3:CRS84"}}}'; 
     192        layer = new OpenLayers.Layer.Vector(); 
     193        layer.projection = "EPSG:4326"; 
     194        feature.layer = layer; 
     195        var parser = new OpenLayers.Format.GeoJSON(); 
     196        test_out = parser.write(feature); 
     197        t.eq(test_out, output, "Output is equal for vector with layer in EPSG:4326 "); 
     198        feature.layer.projection = "EPSG:2805"; 
     199        var output = '{"type":"Feature","id":0,"properties":{},"geometry":{"type":"Point","coordinates":[1,2]},"crs":{"type":"EPSG","properties":{"code":2805}}}'; 
     200        test_out = parser.write(feature); 
     201        t.eq(test_out, output, "Output is equal for vector with point"); 
     202    }     
     203         
    187204    function test_Format_GeoJSON_write(t) { 
    188205        t.plan(10); 
    189206 
  • lib/OpenLayers/Format/GeoJSON.js

    old new  
    447447                    if(geojson.type == null) { 
    448448                        geojson.type = "FeatureCollection"; 
    449449                        if(element.layer && element.layer.projection) { 
    450                             this.createCRSObject(element); 
     450                            geojson.crs = this.createCRSObject(element); 
    451451                        } 
    452452                    } else if(geojson.type != "FeatureCollection") { 
    453453                        OpenLayers.Console.error("FeatureCollection only supports collections of features: " + element); 
     
    469469        } else if (obj instanceof OpenLayers.Feature.Vector) { 
    470470            geojson = this.extract.feature.apply(this, [obj]); 
    471471            if(obj.layer && obj.layer.projection) { 
    472                 this.createCRSObject(obj); 
     472                geojson.crs = this.createCRSObject(obj); 
    473473            } 
    474474        } 
    475475        return OpenLayers.Format.JSON.prototype.write.apply(this, 
     
    482482     */ 
    483483    createCRSObject: function(object) { 
    484484       var proj = object.layer.projection; 
     485       var crs = {} 
    485486       if (proj.match(/epsg:/i)) { 
    486487           var code = parseInt(proj.substring(proj.indexOf(":") + 1)); 
    487488           if (code == 4326) { 
    488                geojson.crs = { 
     489               crs = { 
    489490                   "type": "OGC", 
    490491                   "properties": { 
    491492                       "urn": "urn:ogc:def:crs:OGC:1.3:CRS84" 
    492493                   } 
    493494               }; 
    494495           } else {     
    495                geojson.crs = { 
     496               crs = { 
    496497                   "type": "EPSG", 
    497498                   "properties": { 
    498499                       "code": code  
     
    500501               }; 
    501502           }     
    502503       } 
     504       return crs; 
    503505    }, 
    504506     
    505507    /**