7341b74622017cbede60de0bcc3f259e3c790025
1 package net.systemeD.halcyon.connection {
2     import flash.geom.Point;
3         import net.systemeD.halcyon.Globals;
4         import net.systemeD.halcyon.connection.actions.*;
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';
14         public function Way(id:Number, version:uint, tags:Object, loaded:Boolean, nodes:Array) {
16             this.nodes = nodes;
17                         for each (var node:Node in nodes) { node.addParent(this); }
18                         calculateBbox();
19         }
21                 public function update(version:uint, tags:Object, loaded:Boolean, nodes:Array):void {
22                         var node:Node;
23                         for each (node in this.nodes) { node.removeParent(this); }
25                         for each (node in nodes) { node.addParent(this); }
26                         calculateBbox();
27                 }
29         public function get length():uint {
30             return nodes.length;
31         }
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                 }
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                 }
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   )) { return false; }
51                         return true;
52                 }
54         public function getNode(index:uint):Node {
55             return nodes[index];
56         }
58                 public function getLastNode():Node {
59                         return nodes[nodes.length-1];
60                 }
62         public function insertNode(index:uint, node:Node, performAction:Function):void {
63                         performAction(new AddNodeToWayAction(this, node, nodes, index));
64         }
66         public function appendNode(node:Node, performAction:Function):uint {
67                         performAction(new AddNodeToWayAction(this, node, nodes, -1));
68             return nodes.length + 1;
69         }
71         public function prependNode(node:Node, performAction:Function):uint {
72                         performAction(new AddNodeToWayAction(this, node, nodes, 0));
73             return nodes.length + 1;
74         }
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         }
81                 public function hasOnceOnly(node:Node):Boolean {
82                         return nodes.indexOf(node)==nodes.lastIndexOf(node);
83                 }
85                 public function removeNode(node:Node, performAction:Function):void {
86                         performAction(new RemoveNodeFromWayAction(this, node, nodes));
87                 }
89         public function removeNodeByIndex(index:uint, performAction:Function, fireEvent:Boolean=true):void {
90             performAction(new RemoveNodeByIndexAction(this, nodes, index, fireEvent));
91         }
93                 public function sliceNodes(start:int,end:int):Array {
94                         return nodes.slice(start,end);
95                 }
97         public function deleteNodesFrom(start:int, performAction:Function):void {
98             for (var i:int=nodes.length-1; i>=start; i--) {
99               performAction(new RemoveNodeByIndexAction(this, nodes, i));
100             }
101             markDirty();
102         }
104                 public function mergeWith(way:Way,topos:int,frompos:int, performAction:Function):void {
105                         performAction(new MergeWaysAction(this, way, topos, frompos));
106                 }
108                 public function addToEnd(topos:int,node:Node, performAction:Function):void {
109                         if (topos==0) {
110                                 if (nodes[0]==node) { return; }
111                                 prependNode(node, performAction);
112                         } else {
113                                 if (nodes[nodes.length-1]==node) { return; }
114                                 appendNode(node, performAction);
115                         }
116                 }
118         public function reverseNodes(performAction:Function):void {
119             performAction(new ReverseNodesAction(this, nodes));
120         }
122         /**
123          * Finds the 1st way segment which intersects the projected
124          * coordinate and adds the node to that segment. If snap is
125          * specified then the node is moved to exactly bisect the
126          * segment.
127          */
128         public function insertNodeAtClosestPosition(newNode:Node, isSnap:Boolean, performAction:Function):int {
129             var closestProportion:Number = 1;
130             var newIndex:uint = 0;
131             var nP:Point = new Point(newNode.lon, newNode.latp);
132             var snapped:Point = null;
134             for ( var i:uint; i < length - 1; i++ ) {
135                 var node1:Node = getNode(i);
136                 var node2:Node = getNode(i+1);
137                 var p1:Point = new Point(node1.lon, node1.latp);
138                 var p2:Point = new Point(node2.lon, node2.latp);
140                 var directDist:Number = Point.distance(p1, p2);
141                 var viaNewDist:Number = Point.distance(p1, nP) + Point.distance(nP, p2);
143                 var proportion:Number = Math.abs(viaNewDist/directDist - 1);
144                 if ( proportion < closestProportion ) {
145                     newIndex = i+1;
146                     closestProportion = proportion;
147                     snapped = calculateSnappedPoint(p1, p2, nP);
148                 }
149             }
151             // splice in new node
152             if ( isSnap ) {
153                 newNode.setLonLatp(snapped.x, snapped.y, performAction);
154             }
155             insertNode(newIndex, newNode, performAction);
156             return newIndex;
157         }
159         private function calculateSnappedPoint(p1:Point, p2:Point, nP:Point):Point {
160             var w:Number = p2.x - p1.x;
161             var h:Number = p2.y - p1.y;
162             var u:Number = ((nP.x-p1.x) * w + (nP.y-p1.y) * h) / (w*w + h*h);
163             return new Point(p1.x + u*w, p1.y+u*h);
164         }
166         public override function toString():String {
167             return "Way("+id+"@"+version+"): "+getTagList()+
168                      " "+nodes.map(function(item:Node,index:int, arr:Array):String {return item.id.toString();}).join(",");
169         }
171                 public function isArea():Boolean {
172                         if (nodes.length==0) { return false; }
173                         return (nodes[0].id==nodes[nodes.length-1].id && nodes.length>2);
174                 }
176                 public function endsWith(node:Node):Boolean {
177                         return (nodes[0]==node || nodes[nodes.length-1]==node);
178                 }
180                 public override function remove(performAction:Function):void {
181                         performAction(new DeleteWayAction(this, setDeletedState, nodes));
182                 }
184                 public function get clockwise():Boolean {
185                         var lowest:uint=0;
186                         var xmin:Number=-999999; var ymin:Number=-999999;
187                         for (var i:uint=0; i<nodes.length; i++) {
188                                 if      (nodes[i].latp> ymin) { lowest=i; xmin=nodes[i].lon; ymin=nodes[i].latp; }
189                                 else if (nodes[i].latp==ymin
190                                           && nodes[i].lon > xmin) { lowest=i; xmin=nodes[i].lon; ymin=nodes[i].latp; }
191                         }
192                         return (this.onLeft(lowest)>0);
193                 }
195                 private function onLeft(j:uint):Number {
196                         var left:Number=0;
197                         var i:int, k:int;
198                         if (nodes.length>=3) {
199                                 i=j-1; if (i==-1) { i=nodes.length-2; }
200                                 k=j+1; if (k==nodes.length) { k=1; }
201                                 left=((nodes[j].lon-nodes[i].lon) * (nodes[k].latp-nodes[i].latp) -
202                                           (nodes[k].lon-nodes[i].lon) * (nodes[j].latp-nodes[i].latp));
203                         }
204                         return left;
205                 }
207                 internal override function isEmpty():Boolean {
208                         return (deleted || (nodes.length==0));
209                 }
211                 public override function getType():String {
212                         return 'way';
213                 }
214     }
216 }