1 package net.systemeD.halcyon.connection {
2 import flash.geom.Point;
3 import net.systemeD.halcyon.Globals;
5 public class Way extends Entity {
6 private var nodes:Array;
7 public static var entity_type:String = 'way';
9 public function Way(id:Number, version:uint, tags:Object, loaded:Boolean, nodes:Array) {
10 super(id, version, tags, loaded);
12 for each (var node:Node in nodes) { node.addParent(this); }
15 public function update(version:uint, tags:Object, loaded:Boolean, nodes:Array):void {
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); }
22 public function get length():uint {
26 public function getNode(index:uint):Node {
30 public function getLastNode():Node {
31 return nodes[nodes.length-1];
34 public function insertNode(index:uint, node:Node):void {
36 nodes.splice(index, 0, node);
38 dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_ADDED, node, this, index));
41 public function appendNode(node:Node):uint {
45 dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_ADDED, node, this, nodes.length - 1));
49 public function prependNode(node:Node):uint {
53 dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_ADDED, node, this, 0));
57 public function indexOfNode(node:Node):uint {
58 return nodes.indexOf(node);
61 public function removeNode(node:Node):void {
63 while ((i=nodes.indexOf(node))>-1) {
65 dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_REMOVED, node, this, i));
67 node.removeParent(this);
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); }
75 dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_REMOVED, removed[0], this, index));
78 public function sliceNodes(start:int,end:int):Array {
79 return nodes.slice(start,end);
82 public function deleteNodesFrom(start:int):void {
86 public function mergeWith(way:Way,topos:int,frompos:int):void {
91 for each (var r:Relation in way.parentRelations) {
92 // ** needs to copy roles as well
93 if (r.findEntityMemberIndex(this)==-1) {
94 r.appendMember(new RelationMember(this, ''));
99 var t:Object=way.getTagsHash();
100 for (var k:String in t) {
101 if (getTag(k) && getTag(k)!=t[k]) {
102 setTag(k,getTag(k)+"; "+t[k]);
103 // ** send a warning about tags not matching
110 if (frompos==0) { for (i=0; i<way.length; i++) { addToEnd(topos,way.getNode(i)); } }
111 else { for (i=way.length-1; i>=0; i--) { addToEnd(topos,way.getNode(i)); } }
118 private function addToEnd(topos:int,node:Node):void {
120 if (nodes[0]==node) { return; }
123 if (nodes[nodes.length-1]==node) { return; }
131 * Finds the 1st way segment which intersects the projected
132 * coordinate and adds the node to that segment. If snap is
133 * specified then the node is moved to exactly bisect the
136 public function insertNodeAtClosestPosition(newNode:Node, isSnap:Boolean):int {
137 var closestProportion:Number = 1;
138 var newIndex:uint = 0;
139 var nP:Point = new Point(newNode.lon, newNode.latp);
140 var snapped:Point = null;
142 for ( var i:uint; i < length - 1; i++ ) {
143 var node1:Node = getNode(i);
144 var node2:Node = getNode(i+1);
145 var p1:Point = new Point(node1.lon, node1.latp);
146 var p2:Point = new Point(node2.lon, node2.latp);
148 var directDist:Number = Point.distance(p1, p2);
149 var viaNewDist:Number = Point.distance(p1, nP) + Point.distance(nP, p2);
151 var proportion:Number = Math.abs(viaNewDist/directDist - 1);
152 if ( proportion < closestProportion ) {
154 closestProportion = proportion;
155 snapped = calculateSnappedPoint(p1, p2, nP);
159 // splice in new node
161 newNode.latp = snapped.y;
162 newNode.lon = snapped.x;
164 insertNode(newIndex, newNode);
168 private function calculateSnappedPoint(p1:Point, p2:Point, nP:Point):Point {
169 var w:Number = p2.x - p1.x;
170 var h:Number = p2.y - p1.y;
171 var u:Number = ((nP.x-p1.x) * w + (nP.y-p1.y) * h) / (w*w + h*h);
172 return new Point(p1.x + u*w, p1.y+u*h);
175 public override function toString():String {
176 return "Way("+id+"@"+version+"): "+getTagList()+
177 " "+nodes.map(function(item:Node,index:int, arr:Array):String {return item.id.toString();}).join(",");
180 public function isArea():Boolean {
181 return (nodes[0].id==nodes[nodes.length-1].id && nodes.length>2);
184 public override function remove():void {
188 while (nodes.length) {
190 dispatchEvent(new WayNodeEvent(Connection.WAY_NODE_REMOVED, node, this, 0));
191 node.removeParent(this);
192 if (!node.hasParents) { node.remove(); }
195 dispatchEvent(new EntityEvent(Connection.WAY_DELETED, this));
199 internal override function isEmpty():Boolean {
200 return (deleted || (nodes.length==0));
203 public override function getType():String {