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:
@@ -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}
|
||||||
|
|||||||
@@ -178,15 +178,19 @@ 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) {
|
||||||
// deal with bad geometries
|
// deal with bad geometries
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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>
|
||||||
|
|||||||
Reference in New Issue
Block a user