[OpenLayers-Commits] r2061 - in sandbox/vector: examples img lib lib/OpenLayers lib/OpenLayers/Control lib/OpenLayers/Control/EditingMode lib/OpenLayers/Control/EditingTool

commits at openlayers.org commits at openlayers.org
Fri Dec 15 03:59:04 EST 2006


Author: pgiraud
Date: 2006-12-15 03:59:03 -0500 (Fri, 15 Dec 2006)
New Revision: 2061

Added:
   sandbox/vector/img/hole-cutter.gif
   sandbox/vector/lib/OpenLayers/Control/EditingTool/PolygonHoleCutter.js
Modified:
   sandbox/vector/examples/vector.html
   sandbox/vector/lib/OpenLayers.js
   sandbox/vector/lib/OpenLayers/Control/EditingMode/PointSnapping.js
   sandbox/vector/lib/OpenLayers/Control/EditingTool.js
   sandbox/vector/lib/OpenLayers/Control/EditingTool/MovePathPoint.js
   sandbox/vector/lib/OpenLayers/Style.js
Log:
Polygon hole cutter tool added
removed segment snapping temporarily

Modified: sandbox/vector/examples/vector.html
===================================================================
--- sandbox/vector/examples/vector.html	2006-12-15 08:39:28 UTC (rev 2060)
+++ sandbox/vector/examples/vector.html	2006-12-15 08:59:03 UTC (rev 2061)
@@ -13,6 +13,9 @@
     <script type="text/javascript">
         <!--
         var map, vector;
+        var lon = 20;
+        var lat = 0;
+        var zoom = 4;
 
         function init() {
             //console.log(OpenLayers.Event);
@@ -30,7 +33,8 @@
             var drawLinearRing = new OpenLayers.Control.EditingTool.DrawLinearRing();
             var removePathPoint = new OpenLayers.Control.EditingTool.RemovePathPoint();
             var addPathPoint = new OpenLayers.Control.EditingTool.AddPathPoint();
-            toolbar.addTools([navigation, selection, editingAttributes, movePathPoint, drawPoint, drawLineString, drawLinearRing, addPathPoint, removePathPoint, rotate]);
+            var polygonHoleCutter = new OpenLayers.Control.EditingTool.PolygonHoleCutter();
+            toolbar.addTools([navigation, selection, editingAttributes, movePathPoint, drawPoint, drawLineString, drawLinearRing, addPathPoint, removePathPoint, polygonHoleCutter]);
 			
             map = new OpenLayers.Map('map', {controls: [
 				toolbar,
@@ -51,15 +55,25 @@
 			var linearRing = new OpenLayers.Geometry.LinearRing([
 				new OpenLayers.Geometry.Point(10,10),
 				new OpenLayers.Geometry.Point(20, 0),
-				new OpenLayers.Geometry.Point(30, 10),
+				new OpenLayers.Geometry.Point(30, -5),
 				new OpenLayers.Geometry.Point(40, 0),
 				new OpenLayers.Geometry.Point(50, 10),
+                new OpenLayers.Geometry.Point(30, 20),
 			]);
+      
+            var linearRing2 = new OpenLayers.Geometry.LinearRing([
+              new OpenLayers.Geometry.Point(26.89453125,8.26171875),
+              new OpenLayers.Geometry.Point(31.2890625,10.01953125),
+              new OpenLayers.Geometry.Point(31.81640625,5.80078125),
+              new OpenLayers.Geometry.Point(26.89453125,8.26171875),
+            ]);
+      
+            var polygon = new OpenLayers.Geometry.Polygon([linearRing, linearRing2]);
 
             var attributes=new OpenLayers.Attributes([{label: "test1", value: "test 1"}, {label: "test2", value: "test 2"}]);
 			var feature = new OpenLayers.Feature();
             feature.setAttributes(attributes);
-			feature.setGeometry(linearRing);
+			feature.setGeometry(polygon);
 
 			var linearRing2 = new OpenLayers.Geometry.LinearRing([
 				new OpenLayers.Geometry.Point(-10,-10),
@@ -72,10 +86,19 @@
             var feature2 = new OpenLayers.Feature();
             var attributes=new OpenLayers.Attributes([{label: "t3", value: "v3"}, {label: "lable4", value: "l4"}]);
             feature2.setAttributes(attributes);
+            feature2.setGeometry(linearRing2);
             
-            feature2.setGeometry(linearRing2);
+            var lineString = new OpenLayers.Geometry.LineString([
+                new OpenLayers.Geometry.Point(10,-10),
+                new OpenLayers.Geometry.Point(20, -10),
+                new OpenLayers.Geometry.Point(30, -15)
+            ]);
+            var feature3 = new OpenLayers.Feature();
+            feature3.setGeometry(lineString);
 			
-			vector.addFeatures([feature, feature2]);
+			vector.addFeatures([feature, feature2, feature3]);
+      
+            map.setCenter(new OpenLayers.LonLat(lon, lat), zoom);
         }
         // -->
     </script>

Added: sandbox/vector/img/hole-cutter.gif
===================================================================
(Binary files differ)


Property changes on: sandbox/vector/img/hole-cutter.gif
___________________________________________________________________
Name: svn:mime-type
   + application/octet-stream

Modified: sandbox/vector/lib/OpenLayers/Control/EditingMode/PointSnapping.js
===================================================================
--- sandbox/vector/lib/OpenLayers/Control/EditingMode/PointSnapping.js	2006-12-15 08:39:28 UTC (rev 2060)
+++ sandbox/vector/lib/OpenLayers/Control/EditingMode/PointSnapping.js	2006-12-15 08:59:03 UTC (rev 2061)
@@ -21,6 +21,24 @@
     calculatePoint: function(mouseCoordinates, targetGeometry, currentGeometry, layer){
         var snappingPoint = false;
         
+        if (targetGeometry && targetGeometry.components) {
+            var distance = this.tolerance;
+            for (var i = 0; i < targetGeometry.components.length; i++) {
+                var calculatePoint = this._calculatePoint(mouseCoordinates, targetGeometry.components[i], currentGeometry, layer);
+                if (calculatePoint.distance < distance && calculatePoint.snappingPoint) {
+                    snappingPoint = calculatePoint.snappingPoint;
+                }
+            }
+        } else {
+            var calculatePoint = this._calculatePoint(mouseCoordinates, targetGeometry, currentGeometry, layer);
+            snappingPoint = calculatePoint.snappingPoint;
+        }
+        return snappingPoint;
+    },
+    
+    _calculatePoint: function(mouseCoordinates, targetGeometry, currentGeometry, layer){
+        var snappingPoint = false;
+        var closestVertexIndex = null;
         if (targetGeometry && targetGeometry.CLASS_NAME == "OpenLayers.Geometry.Point" && targetGeometry.isSnappingElement) {
             snappingPoint = targetGeometry;
             
@@ -30,7 +48,7 @@
             layer.renderer.drawGeometry(this.tmpPoint, this.style);
                                    
         } else if (targetGeometry && targetGeometry.path) {
-            var distance = this.tolerance
+            var distance = this.tolerance;
             for(var i = 0; i < targetGeometry.path.length; i++) {
                                 
                 // Calculate screen pixel from segment points and mouseCoordinates
@@ -40,28 +58,25 @@
                 // Compare first segment point pixel to mouse pixel with a tolerance
                 var currentDistance = OpenLayers.Util.distance2Pts(pixel.x, pixel.y, mousePixel.x, mousePixel.y);
                 if (currentDistance < distance) {
-                    closestVertexIndex = i;
+                    var closestVertexIndex = i;
                     distance = currentDistance;
                 }
             }
-            var point = targetGeometry.path[closestVertexIndex];
-            
-            snappingPoint = point;
-            snappingPoint.isSnappingElement = true;
-            
-            // Set the snapping style an draw the geometry
-            this.tmpPoint.x = snappingPoint.x;
-            this.tmpPoint.y = snappingPoint.y;
-            layer.renderer.drawGeometry(this.tmpPoint, this.style);
-            
-        } else if (targetGeometry && targetGeometry.components) {
-            for (var i = 0; i < targetGeometry.components.length; i++) {
-                snappingPoint = this.calculatePoint(mouseCoordinates, targetGeometry.components[i], currentGeometry, layer);
+            if (closestVertexIndex != null) {
+                var point = targetGeometry.path[closestVertexIndex];
+                
+                snappingPoint = point;
+                snappingPoint.isSnappingElement = true;
+                
+                // Set the snapping style an draw the geometry
+                this.tmpPoint.x = snappingPoint.x;
+                this.tmpPoint.y = snappingPoint.y;
+                layer.renderer.drawGeometry(this.tmpPoint, this.style);
             }
         } else {
             layer.renderer.eraseGeometry(this.tmpPoint);
         }
-        return snappingPoint;
+        return {snappingPoint: snappingPoint, distance: distance};
     },
     
     setTolerance: function(tolerance) {

Modified: sandbox/vector/lib/OpenLayers/Control/EditingTool/MovePathPoint.js
===================================================================
--- sandbox/vector/lib/OpenLayers/Control/EditingTool/MovePathPoint.js	2006-12-15 08:39:28 UTC (rev 2060)
+++ sandbox/vector/lib/OpenLayers/Control/EditingTool/MovePathPoint.js	2006-12-15 08:59:03 UTC (rev 2061)
@@ -66,11 +66,7 @@
         var lonlat = this.map.getLonLatFromLayerPx(evt.xy);
         var mouseCoordinates =  new OpenLayers.LonLat(lonlat.lon, lonlat.lat);
         
-        if (evt.targetGeometry && evt.targetGeometry.components) {
-            for (var i = 0; i < evt.targetGeometry.components.length; i++) {
-                this._handleSelection(evt.targetGeometry.components[i], mouseCoordinates);
-            }
-        } else if (evt.targetGeometry && evt.targetGeometry.path) {
+        if (evt.targetGeometry) {
             this._handleSelection(evt.targetGeometry, mouseCoordinates);
         } else {
             // user clicked out of a geometry
@@ -92,31 +88,28 @@
     
     /**
      * private method
-     * handles the selection for tool and layer
+     * calculate the point for tool and layer
      * 
      * @param {OpenLayers.Geometry} geometry
      * @param {OpenLayers.LonLat} mouseCoordinates
      */
     _handleSelection: function(geometry, mouseCoordinates) {
-        var distance = this.tolerance
-        for(var i = 0; i < geometry.path.length; i++) {
-            
-            var tmpPoint = geometry.path[i];
-            
-            // Calculate screen pixel from segment points and mouseCoordinates
-            var pixel = this.map.getPixelFromLonLat(tmpPoint);
-            var mousePixel = this.map.getPixelFromLonLat(mouseCoordinates);
-         
-            // Compare first segment point pixel to mouse pixel with a tolerance
-            var currentDistance = OpenLayers.Util.distance2Pts(pixel.x, pixel.y, mousePixel.x, mousePixel.y);
-            if (currentDistance < distance) {
-                closestVertexIndex = i;
-                distance = currentDistance;
+        var point;
+        var distance = this.tolerance;
+        if (geometry && geometry.components) {
+            for (var i = 0; i < geometry.components.length; i++) {
+                calculatePoint = this._calculatePoint(geometry.components[i], mouseCoordinates);
+                if (calculatePoint.distance < distance) {
+                    point = calculatePoint.point; 
+                    distance = point.distance;
+                }
             }
+        } else if (geometry && geometry.path) {
+            var calculatePoint = this._calculatePoint(geometry, mouseCoordinates);
+            point = calculatePoint.point;
+        } else {
         }
         
-        var point = geometry.path[closestVertexIndex];
-        
         // Point Selection            
         if (point) {
             // Multiple point Selection
@@ -161,6 +154,32 @@
         }
         
     },
+    
+    _calculatePoint: function(geometry, mouseCoordinates) {
+    
+        var distance = this.tolerance;
+        var closestVertexIndex = null;
+        for(var i = 0; i < geometry.path.length; i++) {
+            
+            var tmpPoint = geometry.path[i];
+            
+            // Calculate screen pixel from segment points and mouseCoordinates
+            var pixel = this.map.getPixelFromLonLat(tmpPoint);
+            var mousePixel = this.map.getPixelFromLonLat(mouseCoordinates);
+         
+            // Compare first segment point pixel to mouse pixel with a tolerance
+            var currentDistance = OpenLayers.Util.distance2Pts(pixel.x, pixel.y, mousePixel.x, mousePixel.y);
+            if (currentDistance < distance) {
+                closestVertexIndex = i;
+                distance = currentDistance;
+            }
+        }
+        if (closestVertexIndex != null) {
+            return {point: geometry.path[closestVertexIndex], distance: distance};
+        } else {
+            return false;
+        }
+    },
 
     /**
      * @param {Event} evt

Added: sandbox/vector/lib/OpenLayers/Control/EditingTool/PolygonHoleCutter.js
===================================================================
--- sandbox/vector/lib/OpenLayers/Control/EditingTool/PolygonHoleCutter.js	                        (rev 0)
+++ sandbox/vector/lib/OpenLayers/Control/EditingTool/PolygonHoleCutter.js	2006-12-15 08:59:03 UTC (rev 2061)
@@ -0,0 +1,161 @@
+/* Copyright (c) 2006 MetaCarta, Inc., published under a modified BSD license.
+ * See http://svn.openlayers.org/trunk/openlayers/repository-license.txt 
+ * for the full text of the license. */
+
+/**
+ * @class
+ *
+ * @requires OpenLayers/Control/EditingTool.js
+ */
+OpenLayers.Control.EditingTool.PolygonHoleCutter = OpenLayers.Class.create();
+OpenLayers.Control.EditingTool.PolygonHoleCutter.prototype = OpenLayers.Class.inherit( OpenLayers.Control.EditingTool, {
+
+    /** @type String */
+    image: "hole-cutter.gif",
+
+    /** @type OpenLayers.Size */
+    size: new OpenLayers.Size(16, 16),
+
+    /**
+     * @constructor
+    ***/
+    initialize: function() {
+        OpenLayers.Control.EditingTool.prototype.initialize.apply(this, arguments);
+        this.style = OpenLayers.Style.DefaultRendererSelectionStyle;
+    },
+    
+    /** 
+     * Turn on the current tool and catch all the concerned events.
+     */    
+    turnOn: function(){
+        OpenLayers.Control.EditingTool.prototype.turnOn.apply(this, arguments);
+        this.geometry = new OpenLayers.Geometry.LinearRing();
+        this.tmpPoint = new OpenLayers.Geometry.Point();
+        this.tmpLineSting = new OpenLayers.Geometry.LineString();
+    },
+
+    /** 
+     * Turn off the current tool and catch all the concerned events.
+     */    
+    turnOff: function(){
+        OpenLayers.Control.EditingTool.prototype.turnOff.apply(this, arguments);
+        this.eraseTmpElements();
+        this.geometry = null;
+    },
+    
+    /** 
+     * Finalize the geometry from the current tool
+     */
+    finalizeGeometry: function(){
+        this.map.vectorLayer.renderer.eraseGeometry(this.Polygon);
+        //TBD this should be done ine addComponents method
+        this.geometry.feature = this.Polygon.feature;
+        this.Polygon.addComponents([this.geometry]);
+        this.map.vectorLayer.renderer.drawGeometry(this.Polygon, this.map.vectorLayer.style);
+        this.geometry = new OpenLayers.Geometry.LinearRing();
+        this.Polygon = null;
+    },
+
+
+    eraseTmpElements: function(){
+        for(var i = 0; i < this.geometry.path.length; i++) {
+            this.map.vectorLayer.renderer.eraseGeometry(this.geometry.path[i]);
+        }
+        this.map.vectorLayer.renderer.eraseGeometry(this.tmpPoint);
+        this.map.vectorLayer.renderer.eraseGeometry(this.tmpLineSting);
+        this.map.vectorLayer.renderer.eraseGeometry(this.geometry);
+    },
+
+    defaultDblClick: function (evt) {
+        OpenLayers.Control.EditingTool.prototype.defaultDblClick.apply(this, arguments);
+        
+        this.defaultMouseDown(evt);
+        this.eraseTmpElements();
+        if(this.geometry.path.length > 1){
+            this.finalizeGeometry();
+        }
+    },
+
+    defaultMouseDown: function (evt) {
+        // Double click manager
+        if (this.lastDown && this.lastDown.x  == evt.xy.x && this.lastDown.y == evt.xy.y) {
+            return;
+        }
+        this.lastDown = evt.xy;
+        
+        OpenLayers.Control.EditingTool.prototype.defaultMouseDown.apply(this, arguments);
+        
+        if (this.Polygon) {
+            if (evt.point == this.geometry.path[0]) {
+                this.eraseTmpElements();
+                this.finalizeGeometry();
+            } else {
+                this.geometry.addPoint(evt.point);
+                this.map.vectorLayer.renderer.drawGeometry(evt.point, this.map.vectorLayer.style);
+                this.map.vectorLayer.renderer.drawGeometry(this.geometry, this.map.vectorLayer.style);
+            }
+        } else if (evt.targetGeometry && evt.targetGeometry.CLASS_NAME == "OpenLayers.Geometry.Polygon") {
+            this.Polygon = evt.targetGeometry;
+            this.map.vectorLayer.renderer.drawGeometry(this.Polygon, this.style);
+        } else {
+            this.map.vectorLayer.renderer.drawGeometry(this.Polygon, this.map.vectorLayer.style);
+            this.Polygon = null;
+        }
+        
+        
+    },
+
+    defaultMouseMove: function (evt) {
+        // Double click manager
+        if (this.lastDown && this.lastDown.x  == evt.xy.x && this.lastDown.y == evt.xy.y) {
+            return;
+        }
+        
+        OpenLayers.Control.EditingTool.prototype.defaultMouseMove.apply(this, arguments);
+        
+        if (this.mouseDown) {
+            this.defaultMouseDown(evt);
+            
+        } else {
+            if (this.geometry.path.length > 0) {
+                this.tmpPoint.x = evt.point.x;
+                this.tmpPoint.y = evt.point.y;
+                this.map.vectorLayer.renderer.drawGeometry(this.tmpPoint, this.style);
+                this.tmpLineSting.path[0] = this.geometry.path[this.geometry.path.length-2];
+                this.tmpLineSting.path[1] = evt.point;
+                this.tmpLineSting.path[2] = this.geometry.path[this.geometry.path.length-1];
+                this.map.vectorLayer.renderer.drawGeometry(this.tmpLineSting, this.style);
+            }
+        }
+    },
+
+    defaultKeyDown: function(evt){
+        OpenLayers.Control.EditingTool.prototype.defaultKeyDown.apply(this, arguments);               
+        switch (evt.keyCode){
+            case OpenLayers.Event.KEY_RETURN:
+                this.eraseTmpElements();
+                if(this.geometry.path.length > 1){
+                    this.finalizeGeometry();
+                }           
+                this.geometry.path.clear();
+                break;
+            case OpenLayers.Event.KEY_BACKSPACE:
+            case OpenLayers.Event.KEY_DELETE:
+            case OpenLayers.Event.KEY_ESC:
+                this.eraseTmpElements();
+                // TBD destroy geometry
+                this.geometry = new OpenLayers.Geometry.LinearRing();
+                if (this.Polygon) {
+                    this.map.vectorLayer.renderer.eraseGeometry(this.Polygon);
+                    this.map.vectorLayer.renderer.drawGeometry(this.Polygon, this.map.vectorLayer.style);
+                    this.Polygon = null;
+                }
+                OpenLayers.Event.stop(evt);
+                break;
+        }
+    },
+
+    /** @final @type String */
+    CLASS_NAME: "OpenLayers.Control.PolygonHoleCutter"
+    
+});

Modified: sandbox/vector/lib/OpenLayers/Control/EditingTool.js
===================================================================
--- sandbox/vector/lib/OpenLayers/Control/EditingTool.js	2006-12-15 08:39:28 UTC (rev 2060)
+++ sandbox/vector/lib/OpenLayers/Control/EditingTool.js	2006-12-15 08:59:03 UTC (rev 2061)
@@ -44,8 +44,9 @@
      */
     initialize: function() {
         OpenLayers.Control.prototype.initialize.apply(this, arguments);
+        this.editingModes = [];
         this.editingModes.push(new OpenLayers.Control.EditingMode.PointSnapping());
-        this.editingModes.push(new OpenLayers.Control.EditingMode.SegmentSnapping());
+//        this.editingModes.push(new OpenLayers.Control.EditingMode.SegmentSnapping());
         this.style = OpenLayers.Style.DefaultRendererTemporaryElementStyle;
     },
     

Modified: sandbox/vector/lib/OpenLayers/Style.js
===================================================================
--- sandbox/vector/lib/OpenLayers/Style.js	2006-12-15 08:39:28 UTC (rev 2060)
+++ sandbox/vector/lib/OpenLayers/Style.js	2006-12-15 08:59:03 UTC (rev 2061)
@@ -12,7 +12,7 @@
  */
 OpenLayers.Style.prototype = {
 
-    fillColor: "white",
+    fillColor: "orange",
     fillOpacity: 0.4, 
     hoverFillColor: "white",
     hoverFillOpacity: 0.8,

Modified: sandbox/vector/lib/OpenLayers.js
===================================================================
--- sandbox/vector/lib/OpenLayers.js	2006-12-15 08:39:28 UTC (rev 2060)
+++ sandbox/vector/lib/OpenLayers.js	2006-12-15 08:59:03 UTC (rev 2061)
@@ -115,6 +115,7 @@
         "OpenLayers/Control/EditingTool/DrawLinearRing.js",
         "OpenLayers/Control/EditingTool/Rotate.js",
         "OpenLayers/Control/EditingTool/Commit.js",
+        "OpenLayers/Control/EditingTool/PolygonHoleCutter.js",
         "OpenLayers/Control/EditingMode.js",
         "OpenLayers/Control/EditingMode/PointSnapping.js",
         "OpenLayers/Control/EditingMode/SegmentSnapping.js",



More information about the Commits mailing list