diff --git a/lib/OpenLayers/Format/GeoJSON.js b/lib/OpenLayers/Format/GeoJSON.js index 0e2d76252c..c2a1e7b3e4 100644 --- a/lib/OpenLayers/Format/GeoJSON.js +++ b/lib/OpenLayers/Format/GeoJSON.js @@ -447,7 +447,7 @@ OpenLayers.Format.GeoJSON = OpenLayers.Class(OpenLayers.Format.JSON, { if(geojson.type == null) { geojson.type = "FeatureCollection"; if(element.layer && element.layer.projection) { - this.createCRSObject(element); + geojson.crs = this.createCRSObject(element); } } else if(geojson.type != "FeatureCollection") { OpenLayers.Console.error("FeatureCollection only supports collections of features: " + element); @@ -469,7 +469,7 @@ OpenLayers.Format.GeoJSON = OpenLayers.Class(OpenLayers.Format.JSON, { } else if (obj instanceof OpenLayers.Feature.Vector) { geojson = this.extract.feature.apply(this, [obj]); if(obj.layer && obj.layer.projection) { - this.createCRSObject(obj); + geojson.crs = this.createCRSObject(obj); } } return OpenLayers.Format.JSON.prototype.write.apply(this, @@ -479,20 +479,28 @@ OpenLayers.Format.GeoJSON = OpenLayers.Class(OpenLayers.Format.JSON, { /** * Method: createCRSObject * Create the CRS object for an object. + * + * Parameters: + * object - {} + * + * Returns: + * {Object} An object which can be assigned to the crs property + * of a GeoJSON object. */ createCRSObject: function(object) { var proj = object.layer.projection; + var crs = {} if (proj.match(/epsg:/i)) { var code = parseInt(proj.substring(proj.indexOf(":") + 1)); if (code == 4326) { - geojson.crs = { + crs = { "type": "OGC", "properties": { "urn": "urn:ogc:def:crs:OGC:1.3:CRS84" } }; } else { - geojson.crs = { + crs = { "type": "EPSG", "properties": { "code": code @@ -500,6 +508,7 @@ OpenLayers.Format.GeoJSON = OpenLayers.Class(OpenLayers.Format.JSON, { }; } } + return crs; }, /** diff --git a/tests/Format/test_GeoJSON.html b/tests/Format/test_GeoJSON.html index b221391ba0..f6bd389991 100644 --- a/tests/Format/test_GeoJSON.html +++ b/tests/Format/test_GeoJSON.html @@ -184,6 +184,23 @@ t.eq(types, {'Point':15, 'Polygon': 2, 'LineString':2}, "Correct number of each type"); } + function test_Format_GeoJSON_writeWithCRS(t) { + t.plan(2) + var feature = new OpenLayers.Feature.Vector(new OpenLayers.Geometry.Point(1,2)); + feature.fid = 0; + 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"}}}'; + layer = new OpenLayers.Layer.Vector(); + layer.projection = "EPSG:4326"; + feature.layer = layer; + var parser = new OpenLayers.Format.GeoJSON(); + test_out = parser.write(feature); + t.eq(test_out, output, "Output is equal for vector with layer in EPSG:4326 "); + feature.layer.projection = "EPSG:2805"; + var output = '{"type":"Feature","id":0,"properties":{},"geometry":{"type":"Point","coordinates":[1,2]},"crs":{"type":"EPSG","properties":{"code":2805}}}'; + test_out = parser.write(feature); + t.eq(test_out, output, "Output is equal for vector with point"); + } + function test_Format_GeoJSON_write(t) { t.plan(10);