The createCRSObject stuff never really worked. Unfortunately, the tests didn't

catch it because they were all minimal unit tests -- so they never hooked
up features to layers with projections. This fixes the createCRSObject
function, including updated documentation, and includes tests for regression.
(Pullup #1062)


git-svn-id: http://svn.openlayers.org/trunk/openlayers@4819 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
crschmidt
2007-10-04 18:11:15 +00:00
parent 71c338c3c6
commit 84ab6d9a2f
2 changed files with 30 additions and 4 deletions

View File

@@ -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 - {<OpenLayers.Feature.Vector>}
*
* 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;
},
/**

View File

@@ -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);