Adding support for reading and writing spatial filters with Within, Contains, and Intersects elements. Thanks pvalsecc for the original patch. r=ahocevar (closes #1959)

git-svn-id: http://svn.openlayers.org/trunk/openlayers@8921 dc9f47b5-9b13-0410-9fdd-eb0c1a62fdaf
This commit is contained in:
Tim Schaub
2009-03-01 05:40:21 +00:00
parent 9359cf0dc1
commit 97e55039ea
8 changed files with 356 additions and 17 deletions

View File

@@ -24,6 +24,8 @@ OpenLayers.Filter.Spatial = OpenLayers.Class(OpenLayers.Filter, {
* - OpenLayers.Filter.Spatial.BBOX
* - OpenLayers.Filter.Spatial.INTERSECTS
* - OpenLayers.Filter.Spatial.DWITHIN
* - OpenLayers.Filter.Spatial.WITHIN
* - OpenLayers.Filter.Spatial.CONTAINS
*/
type: null,
@@ -107,3 +109,5 @@ OpenLayers.Filter.Spatial = OpenLayers.Class(OpenLayers.Filter, {
OpenLayers.Filter.Spatial.BBOX = "BBOX";
OpenLayers.Filter.Spatial.INTERSECTS = "INTERSECTS";
OpenLayers.Filter.Spatial.DWITHIN = "DWITHIN";
OpenLayers.Filter.Spatial.WITHIN = "WITHIN";
OpenLayers.Filter.Spatial.CONTAINS = "CONTAINS";

View File

@@ -178,11 +178,49 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
},
"UpperBoundary": function(node, filter) {
filter.upperBoundary = this.readOgcExpression(node);
},
"Intersects": function(node, obj) {
this.readSpatial(node, obj, OpenLayers.Filter.Spatial.INTERSECTS);
},
"Within": function(node, obj) {
this.readSpatial(node, obj, OpenLayers.Filter.Spatial.WITHIN);
},
"Contains": function(node, obj) {
this.readSpatial(node, obj, OpenLayers.Filter.Spatial.CONTAINS);
},
"DWithin": function(node, obj) {
this.readSpatial(node, obj, OpenLayers.Filter.Spatial.DWITHIN);
},
"Distance": function(node, obj) {
obj.distance = parseInt(this.getChildValue(node));
obj.distanceUnits = node.getAttribute("units");
}
}
},
/**
* Method: readSpatial
*
* Read a {<OpenLayers.Filter.Spatial>} filter.
*
* Parameters:
* node - {DOMElement} A DOM element that contains an ogc:expression.
* obj - {Object} The target object.
* type - {String} One of the OpenLayers.Filter.Spatial.* constants.
*
* Returns:
* {<OpenLayers.Filter.Spatial>} The created filter.
*/
readSpatial: function(node, obj, type) {
var filter = new OpenLayers.Filter.Spatial({
type: type
});
this.readChildNodes(node, filter);
filter.value = filter.components[0];
delete filter.components;
obj.filters.push(filter);
},
/**
* Method: readOgcExpression
* Limited support for OGC expressions.
@@ -343,29 +381,31 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
this.writeNode("Literal", filter.upperBoundary, node);
return node;
},
"INTERSECTS": function(filter) {
return this.writeSpatial(filter, "Intersects");
},
"WITHIN": function(filter) {
return this.writeSpatial(filter, "Within");
},
"CONTAINS": function(filter) {
return this.writeSpatial(filter, "Contains");
},
"DWITHIN": function(filter) {
var node = this.createElementNSPlus("ogc:DWithin");
this.writeNode("PropertyName", filter, node);
var child = this.writeNode("feature:_geometry", filter.value);
node.appendChild(child.firstChild);
var node = this.writeSpatial(filter, "DWithin");
this.writeNode("Distance", filter, node);
return node;
},
"INTERSECTS": function(filter) {
var node = this.createElementNSPlus("ogc:Intersects");
this.writeNode("PropertyName", filter, node);
var child = this.writeNode("feature:_geometry", filter.value);
node.appendChild(child.firstChild);
return node;
},
"Distance": function(filter) {
return this.createElementNSPlus("ogc:Distance",
{attributes: {units: filter.distanceUnits},
value: filter.distance});
return this.createElementNSPlus("ogc:Distance", {
attributes: {
units: filter.distanceUnits
},
value: filter.distance
});
}
}
},
/**
* Method: getFilterType
*/
@@ -396,6 +436,8 @@ OpenLayers.Format.Filter.v1 = OpenLayers.Class(OpenLayers.Format.XML, {
"~": "PropertyIsLike",
"BBOX": "BBOX",
"DWITHIN": "DWITHIN",
"WITHIN": "WITHIN",
"CONTAINS": "CONTAINS",
"INTERSECTS": "INTERSECTS"
},

View File

@@ -111,6 +111,35 @@ OpenLayers.Format.Filter.v1_0_0 = OpenLayers.Class(
},
/**
* Method: writeSpatial
*
* Read a {<OpenLayers.Filter.Spatial>} filter and converts it into XML.
*
* Parameters:
* filter - {<OpenLayers.Filter.Spatial>} The filter.
* name - {String} Name of the generated XML element.
*
* Returns:
* {DOMElement} The created XML element.
*/
writeSpatial: function(filter, name) {
var node = this.createElementNSPlus("ogc:"+name);
this.writeNode("PropertyName", filter, node);
var child;
if(filter.value instanceof OpenLayers.Geometry) {
child = this.writeNode("feature:_geometry", filter.value).firstChild;
} else {
child = this.writeNode("gml:Box", filter.value);
}
if(filter.projection) {
child.setAttribute("srsName", filter.projection);
}
node.appendChild(child);
return node;
},
CLASS_NAME: "OpenLayers.Format.Filter.v1_0_0"
});

View File

@@ -125,6 +125,34 @@ OpenLayers.Format.Filter.v1_1_0 = OpenLayers.Class(
"feature": OpenLayers.Format.GML.v3.prototype.writers["feature"]
},
/**
* Method: writeSpatial
*
* Read a {<OpenLayers.Filter.Spatial>} filter and converts it into XML.
*
* Parameters:
* filter - {<OpenLayers.Filter.Spatial>} The filter.
* name - {String} Name of the generated XML element.
*
* Returns:
* {DOMElement} The created XML element.
*/
writeSpatial: function(filter, name) {
var node = this.createElementNSPlus("ogc:"+name);
this.writeNode("PropertyName", filter, node);
var child;
if(filter.value instanceof OpenLayers.Geometry) {
child = this.writeNode("feature:_geometry", filter.value).firstChild;
} else {
child = this.writeNode("gml:Envelope", filter.value);
}
if(filter.projection) {
child.setAttribute("srsName", filter.projection);
}
node.appendChild(child);
return node;
},
CLASS_NAME: "OpenLayers.Format.Filter.v1_1_0"
});

126
tests/Format/Filter/v1.html Normal file
View File

@@ -0,0 +1,126 @@
<html>
<head>
<script src="../../../lib/OpenLayers.js"></script>
<script type="text/javascript">
function test_Intersects(t) {
t.plan(4);
var str =
'<Filter xmlns="http://www.opengis.net/ogc">' +
'<Intersects>' +
'<PropertyName>Geometry</PropertyName>' +
'<gml:Polygon xmlns:gml="http://www.opengis.net/gml">' +
'<gml:outerBoundaryIs>' +
'<gml:LinearRing>' +
'<gml:coordinates decimal="." cs="," ts=" ">2488789,289552 2588789,289552 2588789,389552 2488789,389552 2488789,289552</gml:coordinates>' +
'</gml:LinearRing>' +
'</gml:outerBoundaryIs>' +
'</gml:Polygon>' +
'</Intersects>' +
'</Filter>';
var format = new OpenLayers.Format.Filter.v1_0_0();
var filter = new OpenLayers.Filter.Spatial({
type: OpenLayers.Filter.Spatial.INTERSECTS,
property: "Geometry",
value: OpenLayers.Geometry.fromWKT("POLYGON((2488789 289552, 2588789 289552, 2588789 389552, 2488789 389552, 2488789 289552))")
});
// test writing
var node = format.write(filter);
t.xml_eq(node, str, "filter correctly written");
// test reading
var doc = (new OpenLayers.Format.XML).read(str);
var got = format.read(doc.firstChild);
t.eq(got.type, filter.type, "read correct type");
t.eq(got.property, filter.property, "read correct property");
t.geom_eq(got.value, filter.value, "read correct value");
}
function test_Within(t) {
t.plan(4);
var str =
'<Filter xmlns="http://www.opengis.net/ogc">' +
'<Within>' +
'<PropertyName>Geometry</PropertyName>' +
'<gml:Polygon xmlns:gml="http://www.opengis.net/gml">' +
'<gml:outerBoundaryIs>' +
'<gml:LinearRing>' +
'<gml:coordinates decimal="." cs="," ts=" ">2488789,289552 2588789,289552 2588789,389552 2488789,389552 2488789,289552</gml:coordinates>' +
'</gml:LinearRing>' +
'</gml:outerBoundaryIs>' +
'</gml:Polygon>' +
'</Within>' +
'</Filter>';
var format = new OpenLayers.Format.Filter.v1_0_0();
var filter = new OpenLayers.Filter.Spatial({
type: OpenLayers.Filter.Spatial.WITHIN,
property: "Geometry",
value: OpenLayers.Geometry.fromWKT("POLYGON((2488789 289552, 2588789 289552, 2588789 389552, 2488789 389552, 2488789 289552))")
});
// test writing
var node = format.write(filter);
t.xml_eq(node, str, "filter correctly written");
// test reading
var doc = (new OpenLayers.Format.XML).read(str);
var got = format.read(doc.firstChild);
t.eq(got.type, filter.type, "read correct type");
t.eq(got.property, filter.property, "read correct property");
t.geom_eq(got.value, filter.value, "read correct value");
}
function test_Contains(t) {
t.plan(4);
var str =
'<Filter xmlns="http://www.opengis.net/ogc">' +
'<Contains>' +
'<PropertyName>Geometry</PropertyName>' +
'<gml:Polygon xmlns:gml="http://www.opengis.net/gml">' +
'<gml:outerBoundaryIs>' +
'<gml:LinearRing>' +
'<gml:coordinates decimal="." cs="," ts=" ">2488789,289552 2588789,289552 2588789,389552 2488789,389552 2488789,289552</gml:coordinates>' +
'</gml:LinearRing>' +
'</gml:outerBoundaryIs>' +
'</gml:Polygon>' +
'</Contains>' +
'</Filter>';
var format = new OpenLayers.Format.Filter.v1_0_0();
var filter = new OpenLayers.Filter.Spatial({
type: OpenLayers.Filter.Spatial.CONTAINS,
property: "Geometry",
value: OpenLayers.Geometry.fromWKT("POLYGON((2488789 289552, 2588789 289552, 2588789 389552, 2488789 389552, 2488789 289552))")
});
// test writing
var node = format.write(filter);
t.xml_eq(node, str, "filter correctly written");
// test reading
var doc = (new OpenLayers.Format.XML).read(str);
var got = format.read(doc.firstChild);
t.eq(got.type, filter.type, "read correct type");
t.eq(got.property, filter.property, "read correct property");
t.geom_eq(got.value, filter.value, "read correct value");
}
</script>
</head>
<body>
</body>
</html>

View File

@@ -77,7 +77,81 @@
t.xml_eq(node, out, "bbox correctly written");
}
function test_DWithin(t) {
t.plan(6);
var str =
'<Filter xmlns="http://www.opengis.net/ogc">' +
'<DWithin>' +
'<PropertyName>Geometry</PropertyName>' +
'<gml:Point xmlns:gml="http://www.opengis.net/gml">' +
'<gml:coordinates decimal="." cs="," ts=" ">2488789,289552</gml:coordinates>' +
'</gml:Point>' +
'<Distance units="m">1000</Distance>' +
'</DWithin>' +
'</Filter>';
var format = new OpenLayers.Format.Filter.v1_0_0();
var filter = new OpenLayers.Filter.Spatial({
type: OpenLayers.Filter.Spatial.DWITHIN,
property: "Geometry",
value: new OpenLayers.Geometry.Point(2488789,289552),
distance: 1000,
distanceUnits: "m"
});
// test writing
var node = format.write(filter);
t.xml_eq(node, str, "filter correctly written");
// test reading
var doc = (new OpenLayers.Format.XML).read(str);
var got = format.read(doc.firstChild);
t.eq(got.type, filter.type, "read correct type");
t.eq(got.property, filter.property, "read correct property");
t.geom_eq(got.value, filter.value, "read correct value");
t.eq(got.distance, filter.distance, "read correct distance");
t.eq(got.distanceUnits, filter.distanceUnits, "read correct distance units");
}
function test_Intersects(t) {
t.plan(4);
var str =
'<Filter xmlns="http://www.opengis.net/ogc">' +
'<Intersects>' +
'<PropertyName>Geometry</PropertyName>' +
'<gml:Box xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:4326">' +
'<gml:coordinates decimal="." cs="," ts=" ">-180,-90 180,90</gml:coordinates>' +
'</gml:Box>' +
'</Intersects>' +
'</Filter>';
var format = new OpenLayers.Format.Filter.v1_0_0();
var filter = new OpenLayers.Filter.Spatial({
type: OpenLayers.Filter.Spatial.INTERSECTS,
property: "Geometry",
value: new OpenLayers.Bounds(-180, -90, 180, 90),
projection: "EPSG:4326"
});
// test writing
var node = format.write(filter);
t.xml_eq(node, str, "filter correctly written");
// test reading
var doc = (new OpenLayers.Format.XML).read(str);
var got = format.read(doc.firstChild);
t.eq(got.type, filter.type, "read correct type");
t.eq(got.property, filter.property, "read correct property");
t.eq(got.value.toArray(), filter.value.toArray(), "read correct value");
}
</script>
</head>

View File

@@ -167,6 +167,41 @@
t.xml_eq(node, out, "bbox correctly written");
}
function test_Intersects(t) {
t.plan(4);
var str =
'<Filter xmlns="http://www.opengis.net/ogc">' +
'<Intersects>' +
'<PropertyName>Geometry</PropertyName>' +
'<gml:Envelope xmlns:gml="http://www.opengis.net/gml" srsName="EPSG:4326">' +
'<gml:lowerCorner>-180 -90</gml:lowerCorner>' +
'<gml:upperCorner>180 90</gml:upperCorner>' +
'</gml:Envelope>' +
'</Intersects>' +
'</Filter>';
var format = new OpenLayers.Format.Filter.v1_1_0();
var filter = new OpenLayers.Filter.Spatial({
type: OpenLayers.Filter.Spatial.INTERSECTS,
property: "Geometry",
value: new OpenLayers.Bounds(-180, -90, 180, 90),
projection: "EPSG:4326"
});
// test writing
var node = format.write(filter);
t.xml_eq(node, str, "filter correctly written");
// test reading
var doc = (new OpenLayers.Format.XML).read(str);
var got = format.read(doc.firstChild);
t.eq(got.type, filter.type, "read correct type");
t.eq(got.property, filter.property, "read correct property");
t.eq(got.value.toArray(), filter.value.toArray(), "read correct value");
}
</script>
</head>

View File

@@ -52,6 +52,7 @@
<li>Format/SLD.html</li>
<li>Format/SLD/v1_0_0.html</li>
<li>Format/Filter.html</li>
<li>Format/Filter/v1.html</li>
<li>Format/Filter/v1_0_0.html</li>
<li>Format/Filter/v1_1_0.html</li>
<li>Format/WFSDescribeFeatureType.html</li>