[OpenLayers-Commits] r5125 - in sandbox/ahocevar/sldRenderer: examples lib/OpenLayers lib/OpenLayers/Format lib/OpenLayers/Rule

commits at openlayers.org commits at openlayers.org
Tue Nov 6 11:35:50 EST 2007


Author: ahocevar
Date: 2007-11-06 11:35:48 -0500 (Tue, 06 Nov 2007)
New Revision: 5125

Modified:
   sandbox/ahocevar/sldRenderer/examples/georss-category.xml
   sandbox/ahocevar/sldRenderer/examples/georss.html
   sandbox/ahocevar/sldRenderer/lib/OpenLayers/Format/SLD.js
   sandbox/ahocevar/sldRenderer/lib/OpenLayers/Rule/Comparison.js
   sandbox/ahocevar/sldRenderer/lib/OpenLayers/Style.js
Log:
added capability to use ogc:Literal and ogc:PropertyName. This is represented using bash-style syntax in style properties or rule values, where eg. "foo ${bar}" will evaluate to "foo valueOfBar" for a specific feature, with valueOfBar being the value of the bar attribute of the feature.

Modified: sandbox/ahocevar/sldRenderer/examples/georss-category.xml
===================================================================
--- sandbox/ahocevar/sldRenderer/examples/georss-category.xml	2007-11-06 16:27:47 UTC (rev 5124)
+++ sandbox/ahocevar/sldRenderer/examples/georss-category.xml	2007-11-06 16:35:48 UTC (rev 5125)
@@ -22,6 +22,7 @@
 <link>http://platial.com/place/62200</link>
 <title>Allen Hall</title>
 <description><![CDATA[My dorm at UIUC.<br/>Address: 1301 W Gregory Dr, Urbana, IL<br/>Tags: dorm, uiuc, college<br/><a href="http://platial.com/place/62200"><img src="http://platial.comhttp://static.flickr.com/4/8576450_0d59cc2531_s.jpg"/></a><br/><br /><br /><a href="http://platial.com/place/62200">Map this on Platial</a><br /> <a href="http://platial.com/place_grab/62200">Grab this on Platial</a> ]]></description>
+<category>gold</category>
 <georss:point>40.104172 -88.220623</georss:point>
 <dc:creator>crschmidt</dc:creator>
 <dc:date>2006-04-14T08:01:01.872873+00:00</dc:date>

Modified: sandbox/ahocevar/sldRenderer/examples/georss.html
===================================================================
--- sandbox/ahocevar/sldRenderer/examples/georss.html	2007-11-06 16:27:47 UTC (rev 5124)
+++ sandbox/ahocevar/sldRenderer/examples/georss.html	2007-11-06 16:35:48 UTC (rev 5125)
@@ -12,6 +12,12 @@
         var map, layer;
 
         var style = new OpenLayers.Style();
+        
+        // by default, use the category as part of the image name for styling
+        style.defaultStyle.externalGraphic = "../img/marker-${category}.png";
+        
+        // define rules for pre-defined categories to style with specific
+        // markers
         var rule = new OpenLayers.Rule.Comparison({
                 type: OpenLayers.Rule.Comparison.Type.EQUAL_TO,
                 property: 'category'});
@@ -48,7 +54,7 @@
    GeoRSS URL: <input type="text" id="url" size="50" /><input type="submit" onclick="addUrl(); return false;" value="Load Feed" onsubmit="addUrl(); return false;" />
     </form>  
     <p>The above input box allows the input of a URL to a GeoRSS feed. This feed can be local to the HTML page -- for example, entering 'georss.xml' will work by default, because there is a local file in the directory called georss.xml -- or, with a properly set up ProxyHost variable (as is used here), it will be able to load any HTTP URL which contains GeoRSS and display it. Anything else will simply have no effect.</p>    
-    <p>Entering 'georss-category.xml' in the above input box will load a feed with a 'category' property for some items. The example is configured with style objects to render the items with different icons, depending on the category.</p>
+    <p>Entering 'georss-category.xml' in the above input box will load a feed with a 'category' property for some items. The example is configured with style rules to render the items with different icons, depending on the category. The golden marker is directly created by looking at the category of the item, the others are lookup values inside rules.</p>
     <div id="map"></div>
   </body>
 </html>

Modified: sandbox/ahocevar/sldRenderer/lib/OpenLayers/Format/SLD.js
===================================================================
--- sandbox/ahocevar/sldRenderer/lib/OpenLayers/Format/SLD.js	2007-11-06 16:27:47 UTC (rev 5124)
+++ sandbox/ahocevar/sldRenderer/lib/OpenLayers/Format/SLD.js	2007-11-06 16:35:48 UTC (rev 5125)
@@ -389,8 +389,7 @@
             if (attributeName && attributeValue) {
                 propertyNode = this.getNodeWithAttribute(propertyNodeList,
                         attributeName, attributeValue);
-                result = propertyNode ?
-                        propertyNode.text || propertyNode.textContent : null;
+                result = this.parsePropertyNameAndLiteral(propertyNode);
             }
 
             // get the attribute value and use it as result
@@ -401,27 +400,19 @@
             }
             
             if (!attributeName) {
-
-                // get the property value from the textContent of the first
-                // node
-                propertyNode = propertyNodeList[0];
-                result = propertyNode.text || propertyNode.textContent;
-
-                // get the property value from an ogc:Literal at the level of
-                // the property node.
-                if (!result) {
-                    var result = this.parseProperty(
-                            propertyNode, this.ogcns, "Literal");
-                }
+              
+                // get the property value directly or from an ogc:propertyName
+                // or ogc:Literal at the level of the property node.
+                var result = propertyNode.firstChild ?
+                        propertyNode.firstChild.nodeValue :
+                        this.parsePropertyNameAndLiteral(propertyNode);
                 
-                // finally, get the property value from an ogc:Literal at the
-                // same level as the xmlNode
+                // finally, get the property value from an ogc:propertyName
+                // or ogc:Literal at the same level as the xmlNode
                 if (!result) {
-                    var result = this.parseProperty(xmlNode, this.ogcns,
-                            "Literal");
+                    var result = this.parsePropertyNameAndLiteral(xmlNode);
                 }
             }
-            
         }
         
         // adjust the result to be a trimmed string or a number
@@ -436,6 +427,38 @@
     },
     
     /**
+     * Method: parsePropertyNameAndLiteral
+     * parses a property for propertyNames and Literals and creates the
+     * according value string.
+     * 
+     * Parameters:
+     * xmlNode - {<DOMElement>}
+     * 
+     * Returns:
+     * {String} a string holding a value suitable for OpenLayers.Style.value
+     */
+    parsePropertyNameAndLiteral: function(xmlNode) {
+        if (!xmlNode) {
+            return null;
+        }
+        var childNodes = xmlNode.childNodes;
+        if (!childNodes) {
+            return null;
+        }
+
+        var value = [];
+        for (var i=0; i<childNodes.length; i++) {
+            if (childNodes[i].nodeName.indexOf("Literal") != -1) {
+                value.push(this.getChildValue(childNodes[i]));
+            } else
+            if (childNodes[i].nodeName.indexOf("propertyName") != -1) {
+                value.push("${"+this.getChildValue(childNodes[i])+"}");
+            }
+        }
+        return value.join("");
+    },
+        
+    /**
      * Method: getNodeWithAttribute
      * Walks through a list of xml nodes and returns the fist node that has an
      * attribute with the name and optional value specified.

Modified: sandbox/ahocevar/sldRenderer/lib/OpenLayers/Rule/Comparison.js
===================================================================
--- sandbox/ahocevar/sldRenderer/lib/OpenLayers/Rule/Comparison.js	2007-11-06 16:27:47 UTC (rev 5124)
+++ sandbox/ahocevar/sldRenderer/lib/OpenLayers/Rule/Comparison.js	2007-11-06 16:35:48 UTC (rev 5125)
@@ -5,6 +5,7 @@
 
 /**
  * @requires: OpenLayers/Rule.js
+ * @requires: OpenLayers/Style.js
  *
  * Class: OpenLayers.Rule.Comparison
  * 
@@ -32,21 +33,27 @@
     /**
      * APIProperty: value
      * {Number} or {String}
-     * comparison value for binary comparisons
+     * comparison value for binary comparisons. In the case of a String, this
+     * can be a combination of literals and propertyNames in the form
+     * "literal ${propertyName}"
      */
     value: null,
     
     /**
      * APIProperty: lowerBoundary
      * {Number} or {String}
-     * lower boundary for between comparisons
+     * lower boundary for between comparisons. In the case of a String, this
+     * can be a combination of literals and propertyNames in the form
+     * "literal ${propertyName}"
      */
     lowerBoundary: null,
     
     /**
      * APIProperty: upperBoundary
      * {Number} or {String}
-     * upper boundary for between comparisons
+     * upper boundary for between comparisons. In the case of a String, this
+     * can be a combination of literals and propertyNames in the form
+     * "literal ${propertyName}"
      */
     upperBoundary: null,
 
@@ -87,16 +94,23 @@
             case OpenLayers.Rule.Comparison.Type.GREATER_THAN:
             case OpenLayers.Rule.Comparison.Type.LESS_THAN_OR_EQUAL_TO:
             case OpenLayers.Rule.Comparison.Type.GREATER_THAN_OR_EQUAL_TO:
-                return this.binaryCompare(feature, this.property, this.value);
+                return this.binaryCompare(feature, this.property,
+                        OpenLayers.Style.createLiteral(this.value, feature));
             
             case OpenLayers.Rule.Comparison.Type.BETWEEN:
                 var result =
-                        attributes[this.property] > this.lowerBoundary;
+                        attributes[this.property] >
+                                OpenLayers.Style.createLiteral(
+                                        this.lowerBoundary, feature);
                 result = result &&
-                        attributes[this.property] < this.upperBoundary;
+                        attributes[this.property] <
+                                OpenLayers.Style.createLiteral(
+                                        this.upperBoundary, feature);
                 return result;
             case OpenLayers.Rule.Comparison.Type.LIKE:
-                var regexp = new RegExp(this.value, "gi");
+                var regexp = new RegExp(
+                        OpenLayers.Style.createLiteral(this.value, feature),
+                                "gi");
                 return regexp.test(attributes[this.property]); 
         }
     },

Modified: sandbox/ahocevar/sldRenderer/lib/OpenLayers/Style.js
===================================================================
--- sandbox/ahocevar/sldRenderer/lib/OpenLayers/Style.js	2007-11-06 16:27:47 UTC (rev 5124)
+++ sandbox/ahocevar/sldRenderer/lib/OpenLayers/Style.js	2007-11-06 16:35:48 UTC (rev 5125)
@@ -77,8 +77,7 @@
             pointRadius: 6
         }
         
-        this.defaultSelectStyle = {};
-        OpenLayers.Util.extend(this.defaultSelectStyle,
+        this.defaultSelectStyle = OpenLayers.Util.extend({},
                 OpenLayers.Feature.Vector.style["select"]);
         
         OpenLayers.Util.extend(this, options);
@@ -104,7 +103,7 @@
      * style.
      * 
      * Parameters:
-     * feature - {<OpenLayers.Feature.Vector>} feature to evaluate rules for
+     * feature - {<OpenLayers.Feature>} feature to evaluate rules for
      * baseStyle - {Object} hash of styles feature styles to extend
      * 
      * Returns:
@@ -114,8 +113,7 @@
         if (!baseStyle) {
             baseStyle = this.defaultStyle;
         }
-        var style = {}
-        OpenLayers.Util.extend(style, baseStyle);
+        var style = OpenLayers.Util.extend({}, baseStyle);
         
         var draw = true;
 
@@ -145,6 +143,10 @@
 
         style.hidden = !draw;
 
+        for (var i in style) {
+            style[i] = OpenLayers.Style.createLiteral(style[i], feature);
+        }
+        
         return style;
     },
         
@@ -154,7 +156,7 @@
      * geometry type of the passed geometry
      * 
      * Parameters:
-     * geometry {<OpenLayers.Geometry>)
+     * geometry {<OpenLayers.Geometry>}
      * 
      * Returns:
      * {String} key of the according symbolizer
@@ -173,6 +175,33 @@
 
 
 /**
+ * APIMethod: createLiteral
+ * converts a style value holding a combination of PropertyName and Literal
+ * into a Literal, taking the property values from the passed features.
+ * 
+ * Parameters:
+ * value   {String} value to parse. If this string contains a construct like
+ *         "foo ${bar}", then "foo " will be taken as literal, and "${bar}"
+ *         will be replaced by the value of the "bar" attribute of the passed
+ *         feature.
+ * feature {<OpenLayers.Feature>} feature to take attribute values from
+ * 
+ * Returns:
+ * {String} the parsed value. In the example of the value parameter above, the
+ * result would be "foo valueOfBar", assuming that the passed feature has an
+ * attribute named "bar" with the value "valueOfBar".
+ */
+OpenLayers.Style.createLiteral = function(value, feature) {
+    if (typeof value == "string" && value.indexOf("${") != -1) {
+        var attributes = feature.attributes || feature.data;
+        for (var i in attributes) {
+            value = value.replace("${"+i+"}", attributes[i]);
+        }
+    }
+    return value;
+}
+    
+/**
  * Constant: OpenLayers.Style.SYMBOLIZER_PREFIXES
  * {Array} prefixes of the sld symbolizers. These are the
  * same as the main geometry types



More information about the Commits mailing list