Add support for GeoJSON Bounding Box/Envelope, r=crschmidt (closes #1878)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@8582 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Éric Lemoine
2009-01-05 14:23:01 +00:00
parent 850d69a02f
commit 27e9f855e9
3 changed files with 38 additions and 2 deletions
+9
View File
@@ -47,6 +47,15 @@ OpenLayers.Feature.Vector = OpenLayers.Class(OpenLayers.Feature, {
*/ */
attributes: null, attributes: null,
/**
* Property: bounds
* {<OpenLayers.Bounds>} The box bounding that feature's geometry, that
* property can be set by an <OpenLayers.Format> object when
* deserializing the feature, so in most cases it represents an
* information set by the server.
*/
bounds: null,
/** /**
* Property: state * Property: state
* {String} * {String}
+5 -1
View File
@@ -178,8 +178,9 @@ OpenLayers.Format.GeoJSON = OpenLayers.Class(OpenLayers.Format.JSON, {
* {<OpenLayers.Feature.Vector>} A feature. * {<OpenLayers.Feature.Vector>} A feature.
*/ */
parseFeature: function(obj) { parseFeature: function(obj) {
var feature, geometry, attributes; var feature, geometry, attributes, bbox;
attributes = (obj.properties) ? obj.properties : {}; attributes = (obj.properties) ? obj.properties : {};
bbox = (obj.geometry && obj.geometry.bbox) || obj.bbox;
try { try {
geometry = this.parseGeometry(obj.geometry); geometry = this.parseGeometry(obj.geometry);
} catch(err) { } catch(err) {
@@ -187,6 +188,9 @@ OpenLayers.Format.GeoJSON = OpenLayers.Class(OpenLayers.Format.JSON, {
throw err; throw err;
} }
feature = new OpenLayers.Feature.Vector(geometry, attributes); feature = new OpenLayers.Feature.Vector(geometry, attributes);
if(bbox) {
feature.bounds = OpenLayers.Bounds.fromArray(bbox);
}
if(obj.id) { if(obj.id) {
feature.fid = obj.id; feature.fid = obj.id;
} }
+23
View File
@@ -386,6 +386,29 @@
t.eq(data[0].attributes['author'], 'Your Name Here', 'read author attribute properly'); t.eq(data[0].attributes['author'], 'Your Name Here', 'read author attribute properly');
} }
function test_read_bbox(t) {
t.plan(8);
var f;
parser = new OpenLayers.Format.GeoJSON();
// 4 tests
f = '{"geometry": {"type": "Point", "coordinates": [94.21875, 72.94921875], "bbox": [94.21875, 72.94921875, 94.21875, 72.94921875]}, "type": "Feature", "id": 573, "properties": {}, "bbox": [95.0, 73.0]}';
data = parser.read(f);
t.eq(data[0].bounds.left, 94.21875, "read left bound is correct");
t.eq(data[0].bounds.bottom, 72.94921875, "read bottom left bound is correct");
t.eq(data[0].bounds.right, 94.21875, "read right bound is correct");
t.eq(data[0].bounds.top, 72.94921875, "read top left bound is correct");
// 4 tests
f = '{"geometry": {"type": "Point", "coordinates": [94.21875, 72.94921875]}, "type": "Feature", "id": 573, "properties": {}, "bbox": [95.0, 73.0, 96.0, 74.0]}';
data = parser.read(f);
t.eq(data[0].bounds.left, 95.0, "read left bound is correct");
t.eq(data[0].bounds.bottom, 73.0, "read bottom left bound is correct");
t.eq(data[0].bounds.right, 96.0, "read right bound is correct");
t.eq(data[0].bounds.top, 74.0, "read top left bound is correct");
}
</script> </script>
</head> </head>
<body> <body>