delete objects (possibly not complete)
[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
5     public class Way extends Entity {
6         private var nodes:Array;
7                 public static var entity_type:String = 'way';
8
9         public function Way(id:Number, version:uint, tags:Object, loaded:Boolean, nodes:Array) {
10             super(id, version, tags, loaded);
11             this.nodes = nodes;
12                         for each (var node:Node in nodes) { node.addParent(this); }
13         }
14
15                 public function update(version:uint, tags:Object, loaded:Boolean, nodes:Array):void {
16                         var node:Node;
17                         for each (node in this.nodes) { node.removeParent(this); }
18                         updateEntityProperties(version,tags,loaded); this.nodes=nodes;
19                         for each (node in nodes) { node.addParent(this); }
20                 }
21                 
22         public function get length():uint {
23             return nodes.length;
24         }
25         
26         public function getNode(index:uint):Node {
27             return nodes[index];
28         }
29
30                 public function getLastNode():Node {
31                         return nodes[nodes.length-1];
32                 }
33
34         public function insertNode(index:uint, node:Node):void {
35                         node.addParent(this);
36             nodes.splice(index, 0, node);
37             markDirty();
38             dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_ADDED, node, this, index));
39         }
40
41         public function appendNode(node:Node):uint {
42                         node.addParent(this);
43             nodes.push(node);
44             markDirty();
45             dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_ADDED, node, this, nodes.length - 1));
46             return nodes.length;
47         }
48         
49         public function prependNode(node:Node):uint {
50                         node.addParent(this);
51             nodes.unshift(node);
52             markDirty();
53             dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_ADDED, node, this, 0));
54             return nodes.length;
55         }
56         
57         public function indexOfNode(node:Node):uint {
58             return nodes.indexOf(node);
59         }
60
61                 public function removeNode(node:Node):void {
62                         var i:int;
63                         while ((i=nodes.indexOf(i))>-1) {
64                                 nodes.splice(i,1);
65                 dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_REMOVED, node, this, i));
66                         }
67                         node.removeParent(this);
68                         markDirty();
69                 }
70
71         public function removeNodeByIndex(index:uint):void {
72             var removed:Array=nodes.splice(index, 1);
73                         if (nodes.indexOf(removed[0])==-1) { removed[0].removeParent(this); }
74                         markDirty();
75             dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_REMOVED, removed[0], this, index));
76         }
77
78                 public function removeAllNodes():void {
79                         var node:Node;
80                         while (nodes.length) { 
81                                 node=nodes.pop();
82                                 dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_REMOVED, node, this, 0));
83                                 // ** the event mechanism calls redraw once per wayNodeRemoved, which isn't too efficient
84                                 //    so we should probably add a 'redraw' flag to WayNodeEvent
85                                 node.removeParent(this);
86                         }
87                         // ** we should send an event to delete the entire way
88                 }
89
90                 public function sliceNodes(start:int,end:int):Array {
91                         return nodes.slice(start,end);
92                 }
93
94                 public function deleteNodesFrom(start:int):void {
95                         nodes.splice(start);
96                 }
97
98                 public function mergeWith(way:Way,topos:int,frompos:int):void {
99                         var i:int;
100                         Globals.vars.root.addDebug("way "+id+", merging with "+way.id+", adding to "+topos+" , from "+frompos);
101
102                         // merge relations
103                         for each (var r:Relation in way.parentRelations) {
104                                 // ** needs to copy roles as well
105                                 if (r.findEntityMemberIndex(this)==-1) {
106                                         r.appendMember(new RelationMember(this, ''));
107                                 }
108                         }
109
110                         // merge tags
111                         var t:Object=way.getTagsHash();
112                         for (var k:String in t) {
113                                 if (getTag(k) && getTag(k)!=t[k]) {
114                                         setTag(k,getTag(k)+"; "+t[k]);
115                                         // ** send a warning about tags not matching
116                                 } else {
117                                         setTag(k,t[k]);
118                                 }
119                         }
120
121                         // merge nodes
122                         if (frompos==0) { for (i=0; i<way.length;    i++) { addToEnd(topos,way.getNode(i)); } }
123                                            else { for (i=way.length-1; i>=0; i--) { addToEnd(topos,way.getNode(i)); } }
124
125                         // delete way
126                         
127                 }
128                 
129                 private function addToEnd(topos:int,node:Node):void {
130                         Globals.vars.root.addDebug("adding "+node.id+" at "+topos);
131                         if (topos==0) {
132                                 if (nodes[0]==node) { return; }
133                                 prependNode(node);
134                         } else {
135                                 if (nodes[nodes.length-1]==node) { return; }
136                                 appendNode(node);
137                         }
138                 }
139
140
141
142         /**
143          * Finds the 1st way segment which intersects the projected
144          * coordinate and adds the node to that segment. If snap is
145          * specified then the node is moved to exactly bisect the
146          * segment.
147          */
148         public function insertNodeAtClosestPosition(newNode:Node, isSnap:Boolean):int {
149             var closestProportion:Number = 1;
150             var newIndex:uint = 0;
151             var nP:Point = new Point(newNode.lon, newNode.latp);
152             var snapped:Point = null;
153             
154             for ( var i:uint; i < length - 1; i++ ) {
155                 var node1:Node = getNode(i);
156                 var node2:Node = getNode(i+1);
157                 var p1:Point = new Point(node1.lon, node1.latp);
158                 var p2:Point = new Point(node2.lon, node2.latp);
159                 
160                 var directDist:Number = Point.distance(p1, p2);
161                 var viaNewDist:Number = Point.distance(p1, nP) + Point.distance(nP, p2);
162                         
163                 var proportion:Number = Math.abs(viaNewDist/directDist - 1);
164                 if ( proportion < closestProportion ) {
165                     newIndex = i+1;
166                     closestProportion = proportion;
167                     snapped = calculateSnappedPoint(p1, p2, nP);
168                 }
169             }
170             
171             // splice in new node
172             if ( isSnap ) {
173                 newNode.latp = snapped.y;
174                 newNode.lon = snapped.x;
175             }
176             insertNode(newIndex, newNode);
177             return newIndex;
178         }
179         
180         private function calculateSnappedPoint(p1:Point, p2:Point, nP:Point):Point {
181             var w:Number = p2.x - p1.x;
182             var h:Number = p2.y - p1.y;
183             var u:Number = ((nP.x-p1.x) * w + (nP.y-p1.y) * h) / (w*w + h*h);
184             return new Point(p1.x + u*w, p1.y+u*h);
185         }
186         
187         public override function toString():String {
188             return "Way("+id+"@"+version+"): "+getTagList()+
189                      " "+nodes.map(function(item:Node,index:int, arr:Array):String {return item.id.toString();}).join(",");
190         }
191
192                 public function isArea():Boolean {
193                         return (nodes[0].id==nodes[nodes.length-1].id && nodes.length>2);
194                 }
195                 
196                 public override function remove():void {
197                         removeFromParents();
198                         for each (var node:Node in nodes) {
199                                 node.removeParent(this);
200                                 if (!node.hasParentWays) { node.remove(); }
201                         }
202                         nodes=[];
203                         deleted=true;
204             dispatchEvent(new EntityEvent(Connection.WAY_DELETED, this));
205                 }
206
207                 internal override function isEmpty():Boolean {
208                         return (deleted || (nodes.length==0));
209                 }
210
211                 public override function getType():String {
212                         return 'way';
213                 }
214     }
215
216 }