add the ability to lock entities against purging
[potlatch2.git] / net / systemeD / halcyon / connection / Way.as
1 package net.systemeD.halcyon.connection {
2     import flash.geom.Point;
3         import net.systemeD.halcyon.Globals;
4         import net.systemeD.halcyon.connection.actions.*;
5
6     public class Way extends Entity {
7         private var nodes:Array;
8                 private var edge_l:Number;
9                 private var edge_r:Number;
10                 private var edge_t:Number;
11                 private var edge_b:Number;
12                 public static var entity_type:String = 'way';
13
14         public function Way(id:Number, version:uint, tags:Object, loaded:Boolean, nodes:Array, uid:Number = NaN, timestamp:String = null) {
15             super(id, version, tags, loaded, uid, timestamp);
16             this.nodes = nodes;
17                         for each (var node:Node in nodes) { node.addParent(this); }
18                         calculateBbox();
19         }
20
21                 public function update(version:uint, tags:Object, loaded:Boolean, nodes:Array, uid:Number = NaN, timestamp:String = null):void {
22                         var node:Node;
23                         for each (node in this.nodes) { node.removeParent(this); }
24                         updateEntityProperties(version,tags,loaded,uid,timestamp); this.nodes=nodes;
25                         for each (node in nodes) { node.addParent(this); }
26                         calculateBbox();
27                 }
28                 
29         public function get length():uint {
30             return nodes.length;
31         }
32
33                 private function calculateBbox():void {
34                         edge_l=999999; edge_r=-999999;
35                         edge_b=999999; edge_t=-999999;
36                         for each (var node:Node in nodes) { expandBbox(node); }
37                 }
38
39                 public function expandBbox(node:Node):void {
40                         edge_l=Math.min(edge_l,node.lon);
41                         edge_r=Math.max(edge_r,node.lon);
42                         edge_b=Math.min(edge_b,node.lat);
43                         edge_t=Math.max(edge_t,node.lat);
44                 }
45                 
46                 public override function within(left:Number,right:Number,top:Number,bottom:Number):Boolean {
47                         if ((edge_l<left   && edge_r<left  ) ||
48                             (edge_l>right  && edge_r>right ) ||
49                             (edge_b<bottom && edge_t<bottom) ||
50                             (edge_b>top    && edge_b>top   ) || deleted) { return false; }
51                         return true;
52                 }
53         
54         public function getNode(index:uint):Node {
55             return nodes[index];
56         }
57
58                 public function getLastNode():Node {
59                         return nodes[nodes.length-1];
60                 }
61
62         public function insertNode(index:uint, node:Node, performAction:Function):void {
63                         performAction(new AddNodeToWayAction(this, node, nodes, index));
64         }
65
66         public function appendNode(node:Node, performAction:Function):uint {
67                         performAction(new AddNodeToWayAction(this, node, nodes, -1));
68             return nodes.length + 1;
69         }
70         
71         public function prependNode(node:Node, performAction:Function):uint {
72                         performAction(new AddNodeToWayAction(this, node, nodes, 0));
73             return nodes.length + 1;
74         }
75         
76         // return the index of the Node, or -1 if not found
77         public function indexOfNode(node:Node):int {
78             return nodes.indexOf(node);
79         }
80
81                 public function hasOnceOnly(node:Node):Boolean {
82                         return nodes.indexOf(node)==nodes.lastIndexOf(node);
83                 }
84                 
85                 public function hasLockedNodes():Boolean {
86                         for each (var node:Node in nodes) {
87                                 if (node.locked) { return true; }
88                         }
89                         return false;
90                 }
91
92                 public function removeNode(node:Node, performAction:Function):void {
93                         performAction(new RemoveNodeFromWayAction(this, node, nodes));
94                 }
95
96         public function removeNodeByIndex(index:uint, performAction:Function, fireEvent:Boolean=true):void {
97             performAction(new RemoveNodeByIndexAction(this, nodes, index, fireEvent));
98         }
99
100                 public function sliceNodes(start:int,end:int):Array {
101                         return nodes.slice(start,end);
102                 }
103
104         public function deleteNodesFrom(start:int, performAction:Function):void {
105             for (var i:int=nodes.length-1; i>=start; i--) {
106               performAction(new RemoveNodeByIndexAction(this, nodes, i));
107             }
108             markDirty();
109         }
110
111                 public function mergeWith(way:Way,topos:int,frompos:int, performAction:Function):void {
112                         performAction(new MergeWaysAction(this, way, topos, frompos));
113                 }
114                 
115                 public function addToEnd(topos:int,node:Node, performAction:Function):void {
116                         if (topos==0) {
117                                 if (nodes[0]==node) { return; }
118                                 prependNode(node, performAction);
119                         } else {
120                                 if (nodes[nodes.length-1]==node) { return; }
121                                 appendNode(node, performAction);
122                         }
123                 }
124
125         public function reverseNodes(performAction:Function):void {
126             performAction(new ReverseNodesAction(this, nodes));
127         }
128
129         /**
130          * Finds the 1st way segment which intersects the projected
131          * coordinate and adds the node to that segment. If snap is
132          * specified then the node is moved to exactly bisect the
133          * segment.
134          */
135         public function insertNodeAtClosestPosition(newNode:Node, isSnap:Boolean, performAction:Function):int {
136             var closestProportion:Number = 1;
137             var newIndex:uint = 0;
138             var nP:Point = new Point(newNode.lon, newNode.latp);
139             var snapped:Point = null;
140             
141             for ( var i:uint; i < length - 1; i++ ) {
142                 var node1:Node = getNode(i);
143                 var node2:Node = getNode(i+1);
144                 var p1:Point = new Point(node1.lon, node1.latp);
145                 var p2:Point = new Point(node2.lon, node2.latp);
146                 
147                 var directDist:Number = Point.distance(p1, p2);
148                 var viaNewDist:Number = Point.distance(p1, nP) + Point.distance(nP, p2);
149                         
150                 var proportion:Number = Math.abs(viaNewDist/directDist - 1);
151                 if ( proportion < closestProportion ) {
152                     newIndex = i+1;
153                     closestProportion = proportion;
154                     snapped = calculateSnappedPoint(p1, p2, nP);
155                 }
156             }
157             
158             // splice in new node
159             if ( isSnap ) {
160                 newNode.setLonLatp(snapped.x, snapped.y, performAction);
161             }
162             insertNode(newIndex, newNode, performAction);
163             return newIndex;
164         }
165         
166         private function calculateSnappedPoint(p1:Point, p2:Point, nP:Point):Point {
167             var w:Number = p2.x - p1.x;
168             var h:Number = p2.y - p1.y;
169             var u:Number = ((nP.x-p1.x) * w + (nP.y-p1.y) * h) / (w*w + h*h);
170             return new Point(p1.x + u*w, p1.y+u*h);
171         }
172         
173         public override function toString():String {
174             return "Way("+id+"@"+version+"): "+getTagList()+
175                      " "+nodes.map(function(item:Node,index:int, arr:Array):String {return item.id.toString();}).join(",");
176         }
177
178                 public function isArea():Boolean {
179                         if (nodes.length==0) { return false; }
180                         return (nodes[0].id==nodes[nodes.length-1].id && nodes.length>2);
181                 }
182                 
183                 public function endsWith(node:Node):Boolean {
184                         return (nodes[0]==node || nodes[nodes.length-1]==node);
185                 }
186                 
187                 public override function remove(performAction:Function):void {
188                         performAction(new DeleteWayAction(this, setDeletedState, nodes));
189                 }
190
191                 public override function nullify():void {
192                         nullifyEntity();
193                         nodes=[];
194                 }
195                 
196                 public function get clockwise():Boolean {
197                         var lowest:uint=0;
198                         var xmin:Number=-999999; var ymin:Number=-999999;
199                         for (var i:uint=0; i<nodes.length; i++) {
200                                 if      (nodes[i].latp> ymin) { lowest=i; xmin=nodes[i].lon; ymin=nodes[i].latp; }
201                                 else if (nodes[i].latp==ymin
202                                           && nodes[i].lon > xmin) { lowest=i; xmin=nodes[i].lon; ymin=nodes[i].latp; }
203                         }
204                         return (this.onLeft(lowest)>0);
205                 }
206                 
207                 private function onLeft(j:uint):Number {
208                         var left:Number=0;
209                         var i:int, k:int;
210                         if (nodes.length>=3) {
211                                 i=j-1; if (i==-1) { i=nodes.length-2; }
212                                 k=j+1; if (k==nodes.length) { k=1; }
213                                 left=((nodes[j].lon-nodes[i].lon) * (nodes[k].latp-nodes[i].latp) -
214                                           (nodes[k].lon-nodes[i].lon) * (nodes[j].latp-nodes[i].latp));
215                         }
216                         return left;
217                 }
218
219                 internal override function isEmpty():Boolean {
220                         return (deleted || (nodes.length==0));
221                 }
222
223                 public override function getType():String {
224                         return 'way';
225                 }
226     }
227
228 }