diff --git a/lib/OpenLayers/Feature/Vector.js b/lib/OpenLayers/Feature/Vector.js index 59f2f0880b..6d1444f114 100644 --- a/lib/OpenLayers/Feature/Vector.js +++ b/lib/OpenLayers/Feature/Vector.js @@ -47,6 +47,15 @@ OpenLayers.Feature.Vector = OpenLayers.Class(OpenLayers.Feature, { */ attributes: null, + /** + * Property: bounds + * {} The box bounding that feature's geometry, that + * property can be set by an object when + * deserializing the feature, so in most cases it represents an + * information set by the server. + */ + bounds: null, + /** * Property: state * {String} diff --git a/lib/OpenLayers/Format/GeoJSON.js b/lib/OpenLayers/Format/GeoJSON.js index a91c374c6f..d227413d31 100644 --- a/lib/OpenLayers/Format/GeoJSON.js +++ b/lib/OpenLayers/Format/GeoJSON.js @@ -178,15 +178,19 @@ OpenLayers.Format.GeoJSON = OpenLayers.Class(OpenLayers.Format.JSON, { * {} A feature. */ parseFeature: function(obj) { - var feature, geometry, attributes; + var feature, geometry, attributes, bbox; attributes = (obj.properties) ? obj.properties : {}; + bbox = (obj.geometry && obj.geometry.bbox) || obj.bbox; try { - geometry = this.parseGeometry(obj.geometry); + geometry = this.parseGeometry(obj.geometry); } catch(err) { // deal with bad geometries throw err; } feature = new OpenLayers.Feature.Vector(geometry, attributes); + if(bbox) { + feature.bounds = OpenLayers.Bounds.fromArray(bbox); + } if(obj.id) { feature.fid = obj.id; } diff --git a/tests/Format/GeoJSON.html b/tests/Format/GeoJSON.html index c5160c408e..ee76b9bf4c 100644 --- a/tests/Format/GeoJSON.html +++ b/tests/Format/GeoJSON.html @@ -386,6 +386,29 @@ 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"); + } +