GeoJSON format back up to spec with draft 4. (See #1028)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@4770 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
crschmidt
2007-10-03 05:32:18 +00:00
parent cb3cde8d1c
commit 8ddd13db34
2 changed files with 50 additions and 36 deletions

View File

@@ -93,9 +93,9 @@ OpenLayers.Format.GeoJSON = OpenLayers.Class(OpenLayers.Format.JSON, {
break;
case "GeometryCollection":
results = [];
for(var i=0; i<obj.members.length; ++i) {
for(var i=0; i<obj.geometries.length; ++i) {
try {
results.push(this.parseGeometry(obj.members[i]));
results.push(this.parseGeometry(obj.geometries[i]));
} catch(err) {
results = null;
OpenLayers.Console.error(err);
@@ -115,9 +115,9 @@ OpenLayers.Format.GeoJSON = OpenLayers.Class(OpenLayers.Format.JSON, {
}
break;
case "FeatureCollection":
for(var i=0; i<obj.members.length; ++i) {
for(var i=0; i<obj.features.length; ++i) {
try {
results.push(this.parseFeature(obj.members[i]));
results.push(this.parseFeature(obj.features[i]));
} catch(err) {
results = null;
OpenLayers.Console.error(err);
@@ -125,9 +125,9 @@ OpenLayers.Format.GeoJSON = OpenLayers.Class(OpenLayers.Format.JSON, {
}
break;
case "GeometryCollection":
for(var i=0; i<obj.members.length; ++i) {
for(var i=0; i<obj.geometries.length; ++i) {
try {
var geom = this.parseGeometry(obj.members[i]);
var geom = this.parseGeometry(obj.geometries[i]);
results.push(new OpenLayers.Feature.Vector(geom));
} catch(err) {
results = null;
@@ -436,28 +436,24 @@ OpenLayers.Format.GeoJSON = OpenLayers.Class(OpenLayers.Format.JSON, {
"type": null
};
if(obj instanceof Array) {
geojson.members = [];
if(obj[0] instanceof OpenLayers.Feature.Vector) {
geojson.features = [];
} else if (obj[0].CLASS_NAME.search("OpenLayers.Geometry") == 0) {
geojson.geometries = [];
}
for(var i=0; i<obj.length; ++i) {
var element = obj[i];
if(element instanceof OpenLayers.Feature.Vector) {
if(geojson.type == null) {
geojson.type = "FeatureCollection";
if(element.layer && element.layer.projection) {
var proj = element.layer.projection;
if(proj.match(/epsg:/i)) {
geojson.crs = {
"type": "EPSG",
"properties": {
"code": parseInt(proj.substring(proj.indexOf(":") + 1))
}
};
}
this.createCRSObject(element);
}
} else if(geojson.type != "FeatureCollection") {
OpenLayers.Console.error("FeatureCollection only supports collections of features: " + element);
break;
}
geojson.members.push(this.extract.feature.apply(this, [element]));
geojson.features.push(this.extract.feature.apply(this, [element]));
} else if (element.CLASS_NAME.search("OpenLayers.Geometry") == 0) {
if(geojson.type == null) {
geojson.type = "GeometryCollection";
@@ -465,7 +461,7 @@ OpenLayers.Format.GeoJSON = OpenLayers.Class(OpenLayers.Format.JSON, {
OpenLayers.Console.error("GeometryCollection only supports collections of geometries: " + element);
break;
}
geojson.members.push(this.extract.geometry.apply(this, [element]));
geojson.geometries.push(this.extract.geometry.apply(this, [element]));
}
}
} else if (obj.CLASS_NAME.search("OpenLayers.Geometry") == 0) {
@@ -473,20 +469,38 @@ 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) {
var proj = obj.layer.projection;
if(proj.match(/epsg:/i)) {
geojson.crs = {
"type": "EPSG",
"properties": {
"code": parseInt(proj.substring(proj.indexOf(":") + 1))
}
};
}
this.createCRSObject(obj);
}
}
return OpenLayers.Format.JSON.prototype.write.apply(this,
[geojson, pretty]);
},
/**
* Method: createCRSObject
* Create the CRS object for an object.
*/
createCRSObject: function(object) {
var proj = object.layer.projection;
if (proj.match(/epsg:/i)) {
var code = parseInt(proj.substring(proj.indexOf(":") + 1));
if (code == 4326) {
geojson.crs = {
"type": "OGC",
"properties": {
"urn": "urn:ogc:def:crs:OGC:1.3:CRS84"
}
};
} else {
geojson.crs = {
"type": "EPSG",
"properties": {
"code": code
}
};
}
}
},
/**
* Property: extract