[OpenLayers-Commits] r2037 - in sandbox/vector/lib/OpenLayers: . Geometry

commits at openlayers.org commits at openlayers.org
Tue Dec 12 11:56:04 EST 2006


Author: pgiraud
Date: 2006-12-12 11:56:04 -0500 (Tue, 12 Dec 2006)
New Revision: 2037

Modified:
   sandbox/vector/lib/OpenLayers/Geometry.js
   sandbox/vector/lib/OpenLayers/Geometry/Aggregate.js
   sandbox/vector/lib/OpenLayers/Geometry/Curve.js
   sandbox/vector/lib/OpenLayers/Geometry/LinearRing.js
   sandbox/vector/lib/OpenLayers/Geometry/Polygon.js
   sandbox/vector/lib/OpenLayers/Util.js
Log:
getLength and getArea methods added to geometry classes

Modified: sandbox/vector/lib/OpenLayers/Geometry/Aggregate.js
===================================================================
--- sandbox/vector/lib/OpenLayers/Geometry/Aggregate.js	2006-12-12 00:03:06 UTC (rev 2036)
+++ sandbox/vector/lib/OpenLayers/Geometry/Aggregate.js	2006-12-12 16:56:04 UTC (rev 2037)
@@ -70,6 +70,28 @@
 //		}
 //		return atPoint;
 //	},
+        
+    /**
+     * Returns the length of the geometry
+     */
+    getLength: function() {
+        var length = 0.0;
+        for (var i = 0; i < this.components.length; i++) {
+            length += this.components[i].getLength();
+        }
+        return length;
+    },
+    
+    /**
+     * Returns the area of the geometry
+     */
+    getArea: function() {
+        var area = 0.0;
+        for (var i = 0; i < this.components.length; i++) {
+            area += this.components[i].getArea();
+        }
+        return area;
+    },
 
     CLASS_NAME: "OpenLayers.Geometry.Aggregate"
 });

Modified: sandbox/vector/lib/OpenLayers/Geometry/Curve.js
===================================================================
--- sandbox/vector/lib/OpenLayers/Geometry/Curve.js	2006-12-12 00:03:06 UTC (rev 2036)
+++ sandbox/vector/lib/OpenLayers/Geometry/Curve.js	2006-12-12 16:56:04 UTC (rev 2037)
@@ -96,5 +96,9 @@
         return new OpenLayers.Geometry.Curve(this.path); 
     },
     
+    getLength: function() {
+        return OpenLayers.Util.length(this.path);
+    },
+    
     CLASS_NAME: "OpenLayers.Geometry.Curve"
 });

Modified: sandbox/vector/lib/OpenLayers/Geometry/LinearRing.js
===================================================================
--- sandbox/vector/lib/OpenLayers/Geometry/LinearRing.js	2006-12-12 00:03:06 UTC (rev 2036)
+++ sandbox/vector/lib/OpenLayers/Geometry/LinearRing.js	2006-12-12 16:56:04 UTC (rev 2037)
@@ -52,6 +52,20 @@
     toString: function() {
         return this.path.toString();
     },
+    
+    /**
+     * Returns the length of the geometry
+     */
+    getLength: function() {
+        return OpenLayers.Util.length(this.path);
+    },
+    
+    /**
+     * Returns the area of the geometry
+     */
+    getArea: function() {
+        return OpenLayers.Util.signedArea(this.path);
+    },
 
     CLASS_NAME: "OpenLayers.Geometry.LinearRing"
 });

Modified: sandbox/vector/lib/OpenLayers/Geometry/Polygon.js
===================================================================
--- sandbox/vector/lib/OpenLayers/Geometry/Polygon.js	2006-12-12 00:03:06 UTC (rev 2036)
+++ sandbox/vector/lib/OpenLayers/Geometry/Polygon.js	2006-12-12 16:56:04 UTC (rev 2037)
@@ -50,6 +50,29 @@
     removeComponents: function(components) {
         OpenLayers.Geometry.Aggregate.prototype.removeComponents.apply(this, arguments);
     },
+    
+    /**
+     * Returns the length of the geometry
+     */
+    getLength: function() {
+        var length = 0.0;
+        for (var i = 0; i < this.components.length; i++) {
+            length += this.components[i].getLength();
+        }
+        return length;
+    },
+    
+    /**
+     * Returns the area of the geometry
+     */
+    getArea: function() {
+        var area = 0.0;
+        area += Math.abs(this.components[0].getArea());
+        for (var i = 1; i < this.components.length; i++) {
+            area -= Math.abs(this.components[i].getArea());
+        }
+        return area;
+    },
 
     CLASS_NAME: "OpenLayers.Geometry.Polygon"
 });

Modified: sandbox/vector/lib/OpenLayers/Geometry.js
===================================================================
--- sandbox/vector/lib/OpenLayers/Geometry.js	2006-12-12 00:03:06 UTC (rev 2036)
+++ sandbox/vector/lib/OpenLayers/Geometry.js	2006-12-12 16:56:04 UTC (rev 2037)
@@ -82,7 +82,20 @@
         }
         return atPoint;
     },
+    
+    /**
+     * Returns the length of the geometry
+     */
+    getLength: function() {
+        return 0.0;
+    },
 
+    /**
+     * Returns the area of the geometry
+     */
+    getArea: function() {
+        return 0.0;
+    },
 
     CLASS_NAME: "OpenLayers.Geometry"
 };
\ No newline at end of file

Modified: sandbox/vector/lib/OpenLayers/Util.js
===================================================================
--- sandbox/vector/lib/OpenLayers/Util.js	2006-12-12 00:03:06 UTC (rev 2036)
+++ sandbox/vector/lib/OpenLayers/Util.js	2006-12-12 16:56:04 UTC (rev 2037)
@@ -704,3 +704,36 @@
 OpenLayers.Util.distance2Pts = function( x1, y1, x2, y2 ) {
   return (Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2)));
 };
+
+/**
+ * Computes the length of a linestring specified by an array of points.
+ *
+ * @param pts the points specifying the linestring
+ * @return the length of the linestring
+ */
+OpenLayers.Util.length = function(pts) {
+    if (pts.length < 1) return 0.0;
+    var sum = 0.0;
+    for (var i = 1; i < pts.length; i++) {
+	   sum += OpenLayers.Util.distance2Pts(pts[i].lon, pts[i].lat, pts[i -1].lon, pts[i - 1].lat);
+    }
+    return sum;
+};
+
+
+/**
+ * Returns the signed area for a ring.  The area is positive if
+ * the ring is oriented CW.
+ */
+OpenLayers.Util.signedArea = function(pts) {
+    if (pts.length < 3) return 0.0;
+    var sum = 0.0;
+    for (var i = 0; i < pts.length - 1; i++) {
+        var bx = pts[i].lon;
+        var by = pts[i].lat;
+        var cx = pts[i + 1].lon;
+        var cy = pts[i + 1].lat;
+        sum += (bx + cx) * (cy - by);
+    }
+    return -sum  / 2.0;
+}



More information about the Commits mailing list