[OpenLayers-Commits] r11756 - in trunk/openlayers: lib/OpenLayers/Filter lib/OpenLayers/Format/Filter tests/Format/Filter

commits-20090109 at openlayers.org commits-20090109 at openlayers.org
Tue Mar 29 14:49:32 EDT 2011


Author: tschaub
Date: 2011-03-29 11:49:27 -0700 (Tue, 29 Mar 2011)
New Revision: 11756

Modified:
   trunk/openlayers/lib/OpenLayers/Filter/FeatureId.js
   trunk/openlayers/lib/OpenLayers/Format/Filter/v1.js
   trunk/openlayers/tests/Format/Filter/v1.html
Log:
Extending the filter format and the feature ID filter to support writing of logical filters that contain FID filters.  r=bartvde (closes #3012)

Modified: trunk/openlayers/lib/OpenLayers/Filter/FeatureId.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Filter/FeatureId.js	2011-03-29 17:07:54 UTC (rev 11755)
+++ trunk/openlayers/lib/OpenLayers/Filter/FeatureId.js	2011-03-29 18:49:27 UTC (rev 11756)
@@ -26,6 +26,12 @@
     fids: null,
     
     /** 
+     * Property: type
+     * {String} Type to identify this filter.
+     */
+    type: "FID",
+    
+    /** 
      * Constructor: OpenLayers.Filter.FeatureId
      * Creates an ogc:FeatureId rule.
      *

Modified: trunk/openlayers/lib/OpenLayers/Format/Filter/v1.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Format/Filter/v1.js	2011-03-29 17:07:54 UTC (rev 11755)
+++ trunk/openlayers/lib/OpenLayers/Format/Filter/v1.js	2011-03-29 18:49:27 UTC (rev 11756)
@@ -273,6 +273,19 @@
     },
     
     /**
+     * Method: writeFeatureIdNodes
+     * 
+     * Parameters:
+     * filter - {<OpenLayers.Filter.FeatureId}
+     * node - {DOMElement}
+     */
+    writeFeatureIdNodes: function(filter, node) {
+        for (var i=0, ii=filter.fids.length; i<ii; ++i) {
+            this.writeNode("FeatureId", filter.fids[i], node);
+        }
+    },
+    
+    /**
      * Property: writers
      * As a compliment to the readers property, this structure contains public
      *     writing functions grouped by namespace alias and named like the
@@ -282,11 +295,8 @@
         "ogc": {
             "Filter": function(filter) {
                 var node = this.createElementNSPlus("ogc:Filter");
-                var sub = filter.CLASS_NAME.split(".").pop();
-                if(sub == "FeatureId") {
-                    for(var i=0; i<filter.fids.length; ++i) {
-                        this.writeNode("FeatureId", filter.fids[i], node);
-                    }
+                if (filter.type === "FID") {
+                    this.writeFeatureIdNodes(filter, node);
                 } else {
                     this.writeNode(this.getFilterType(filter), filter, node);
                 }
@@ -300,31 +310,43 @@
             "And": function(filter) {
                 var node = this.createElementNSPlus("ogc:And");
                 var childFilter;
-                for(var i=0; i<filter.filters.length; ++i) {
+                for (var i=0, ii=filter.filters.length; i<ii; ++i) {
                     childFilter = filter.filters[i];
+                    if (childFilter.type === "FID") {
+                        this.writeFeatureIdNodes(childFilter, node);
+                    } else {
                     this.writeNode(
                         this.getFilterType(childFilter), childFilter, node
                     );
                 }
+                }
                 return node;
             },
             "Or": function(filter) {
                 var node = this.createElementNSPlus("ogc:Or");
                 var childFilter;
-                for(var i=0; i<filter.filters.length; ++i) {
+                for (var i=0, ii=filter.filters.length; i<ii; ++i) {
                     childFilter = filter.filters[i];
+                    if (childFilter.type === "FID") {
+                        this.writeFeatureIdNodes(childFilter, node);
+                    } else {
                     this.writeNode(
                         this.getFilterType(childFilter), childFilter, node
                     );
                 }
+                }
                 return node;
             },
             "Not": function(filter) {
                 var node = this.createElementNSPlus("ogc:Not");
                 var childFilter = filter.filters[0];
+                if (childFilter.type === "FID") {
+                    this.writeFeatureIdNodes(childFilter, node);
+                } else {
                 this.writeNode(
                     this.getFilterType(childFilter), childFilter, node
                 );
+                }
                 return node;
             },
             "PropertyIsLessThan": function(filter) {
@@ -460,7 +482,8 @@
         "DWITHIN": "DWITHIN",
         "WITHIN": "WITHIN",
         "CONTAINS": "CONTAINS",
-        "INTERSECTS": "INTERSECTS"
+        "INTERSECTS": "INTERSECTS",
+        "FID": "FeatureId"
     },
 
     CLASS_NAME: "OpenLayers.Format.Filter.v1" 

Modified: trunk/openlayers/tests/Format/Filter/v1.html
===================================================================
--- trunk/openlayers/tests/Format/Filter/v1.html	2011-03-29 17:07:54 UTC (rev 11755)
+++ trunk/openlayers/tests/Format/Filter/v1.html	2011-03-29 18:49:27 UTC (rev 11756)
@@ -166,9 +166,96 @@
 
     }
 
+    function test_logical_fid(t) {
+        // the Filter Encoding spec doesn't allow for FID filters inside logical filters
+        // however, to be liberal, we will write them without complaining
+        t.plan(3);
 
+        var filter = new OpenLayers.Filter.Logical({
+            type: OpenLayers.Filter.Logical.OR,
+            filters: [
+                new OpenLayers.Filter.Comparison({
+                    type: OpenLayers.Filter.Comparison.LIKE,
+                    property: "person",
+                    value: "me"
+                }),
+                new OpenLayers.Filter.FeatureId({fids: ["foo.1", "foo.2"]})
+            ]
+        });
+        var format = new OpenLayers.Format.Filter.v1_0_0();
+        
+        var got = format.write(filter);
+        var exp = readXML("LogicalFeatureId");
+        t.xml_eq(got, exp, "wrote FID filter in logical OR without complaint");
+
+        filter = new OpenLayers.Filter.Logical({
+            type: OpenLayers.Filter.Logical.AND,
+            filters: [
+                new OpenLayers.Filter.Comparison({
+                    type: OpenLayers.Filter.Comparison.LIKE,
+                    property: "person",
+                    value: "me"
+                }),
+                new OpenLayers.Filter.FeatureId({fids: ["foo.1", "foo.2"]})
+            ]
+        });
+        got = format.write(filter);
+        exp = readXML("LogicalFeatureIdAnd");
+        t.xml_eq(got, exp, "wrote FID filter in logical AND without complaint");
+
+        filter = new OpenLayers.Filter.Logical({
+            type: OpenLayers.Filter.Logical.NOT,
+            filters: [
+                new OpenLayers.Filter.FeatureId({fids: ["foo.2"]})
+            ]
+        });
+        got = format.write(filter);
+        exp = readXML("LogicalFeatureIdNot");
+        t.xml_eq(got, exp, "wrote FID filter in logical NOT without complaint");
+    }
+
+
+    function readXML(id) {
+        var xml = document.getElementById(id).firstChild.nodeValue;
+        return new OpenLayers.Format.XML().read(xml).documentElement;
+    }
+
+
     </script> 
 </head> 
 <body>
+
+<div id="LogicalFeatureId"><!--
+<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
+    <ogc:Or>
+        <ogc:PropertyIsLike wildCard="*" singleChar="." escape="!">
+            <ogc:PropertyName>person</ogc:PropertyName>
+            <ogc:Literal>me</ogc:Literal>
+        </ogc:PropertyIsLike>
+        <ogc:FeatureId fid="foo.1"/>
+        <ogc:FeatureId fid="foo.2"/>
+    </ogc:Or>
+</ogc:Filter>
+--></div>
+<div id="LogicalFeatureIdAnd"><!--
+<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
+    <ogc:And>
+        <ogc:PropertyIsLike wildCard="*" singleChar="." escape="!">
+            <ogc:PropertyName>person</ogc:PropertyName>
+            <ogc:Literal>me</ogc:Literal>
+        </ogc:PropertyIsLike>
+        <ogc:FeatureId fid="foo.1"/>
+        <ogc:FeatureId fid="foo.2"/>
+    </ogc:And>
+</ogc:Filter>
+--></div>
+<div id="LogicalFeatureIdNot"><!--
+<ogc:Filter xmlns:ogc="http://www.opengis.net/ogc">
+    <ogc:Not>
+        <ogc:FeatureId fid="foo.2"/>
+    </ogc:Not>
+</ogc:Filter>
+--></div>
+
 </body> 
 </html> 



More information about the Commits mailing list