Merge branch 'master' into history
[potlatch2.git] / net / systemeD / halcyon / connection / Way.as
index 0f97958..11d3214 100644 (file)
@@ -11,17 +11,17 @@ package net.systemeD.halcyon.connection {
                private var edge_b:Number;
                public static var entity_type:String = 'way';
 
-        public function Way(connection:Connection, id:Number, version:uint, tags:Object, loaded:Boolean, nodes:Array, uid:Number = NaN, timestamp:String = null) {
-            super(connection, id, version, tags, loaded, uid, timestamp);
+        public function Way(connection:Connection, id:Number, version:uint, tags:Object, loaded:Boolean, nodes:Array, uid:Number = NaN, timestamp:String = null, user:String = null) {
+            super(connection, id, version, tags, loaded, uid, timestamp, user);
             this.nodes = nodes;
                        for each (var node:Node in nodes) { node.addParent(this); }
                        calculateBbox();
         }
 
-               public function update(version:uint, tags:Object, loaded:Boolean, parentsLoaded:Boolean, nodes:Array, uid:Number = NaN, timestamp:String = null):void {
+               public function update(version:uint, tags:Object, loaded:Boolean, parentsLoaded:Boolean, nodes:Array, uid:Number = NaN, timestamp:String = null, user:String = null):void {
                        var node:Node;
                        for each (node in this.nodes) { node.removeParent(this); }
-                       updateEntityProperties(version,tags,loaded,parentsLoaded,uid,timestamp); this.nodes=nodes;
+                       updateEntityProperties(version,tags,loaded,parentsLoaded,uid,timestamp,user); this.nodes=nodes;
                        for each (node in nodes) { node.addParent(this); }
                        calculateBbox();
                }
@@ -90,16 +90,16 @@ package net.systemeD.halcyon.connection {
         public function insertNode(index:uint, node:Node, performAction:Function):void {
                        if (index>0 && getNode(index-1)==node) return;
                        if (index<nodes.length-1 && getNode(index)==node) return;
-                       performAction(new AddNodeToWayAction(this, node, nodes, index));
+                       performAction(new AddNodeToWayAction(this, node, nodes, index, false));
         }
 
         public function appendNode(node:Node, performAction:Function):uint {
-                       insertNode(nodes.length, node, performAction);
+                       if (node!=getLastNode()) performAction(new AddNodeToWayAction(this, node, nodes, -1));
             return nodes.length + 1;
         }
         
         public function prependNode(node:Node, performAction:Function):uint {
-                       insertNode(0, node, performAction);
+                       if (node!=getFirstNode()) performAction(new AddNodeToWayAction(this, node, nodes, 0));
             return nodes.length + 1;
         }
         
@@ -157,6 +157,39 @@ package net.systemeD.halcyon.connection {
             performAction(new ReverseNodesAction(this, nodes));
         }
 
+               
+               /** Is a point within this way?
+               * From http://as3.miguelmoraleda.com/2009/10/28/point-in-polygon-with-actionscript-3punto-dentro-de-un-poligono-con-actionscript-3/
+               */
+
+               public function pointWithin(lon:Number,lat:Number):Boolean {
+                       if (!isArea()) return false;
+                       
+                       var counter:uint = 0;
+                       var p1x:Number = nodes[0].lon;
+                       var p1y:Number = nodes[0].lat;
+                       var p2x:Number, p2y:Number;
+                       for (var i:uint = 1; i <= length; i++) {
+                               p2x = nodes[i % length].lon;
+                               p2y = nodes[i % length].lat;
+                               if (lat > Math.min(p1y, p2y)) {
+                                       if (lat <= Math.max(p1y, p2y)) {
+                                               if (lon <= Math.max(p1x, p2x)) {
+                                                       if (p1y != p2y) {
+                                                               var xinters:Number = (lat - p1y) * (p2x - p1x) / (p2y - p1y) + p1x;
+                                                               if (p1x == p2x || lon <= xinters) counter++;
+                                                       }
+                                               }
+                                       }
+                               }
+                               p1x = p2x;
+                               p1y = p2y;
+                       }
+                       if (counter % 2 == 0) { return false; }
+                       else { return true; }
+               }
+
         /**
          * Finds the 1st way segment which intersects the projected
          * coordinate and adds the node to that segment. If snap is