change relation.appendMember to use the undo system, and update various places as...
[potlatch2.git] / net / systemeD / halcyon / connection / Node.as
1 package net.systemeD.halcyon.connection {
2
3     import net.systemeD.halcyon.connection.actions.*;
4
5     public class Node extends Entity {
6         private var _lat:Number;
7         private var _latproj:Number;
8         private var _lon:Number;
9
10         public function Node(id:Number, version:uint, tags:Object, loaded:Boolean, lat:Number, lon:Number, uid:Number = NaN, timestamp:String = null) {
11             super(id, version, tags, loaded, uid, timestamp);
12             this._lat = lat;
13             this._latproj = lat2latp(lat);
14             this._lon = lon;
15         }
16
17                 public function update(version:uint, tags:Object, loaded:Boolean, lat:Number, lon:Number, uid:Number = NaN, timestamp:String = null):void {
18                         updateEntityProperties(version,tags,loaded,uid,timestamp); setLatLonImmediate(lat,lon);
19                 }
20
21         public function get lat():Number {
22             return _lat;
23         }
24
25         public function get latp():Number {
26             return _latproj;
27         }
28
29         public function get lon():Number {
30             return _lon;
31         }
32
33         private function setLatLonImmediate(lat:Number, lon:Number):void {
34             var connection:Connection = Connection.getConnection();
35             connection.removeDupe(this);
36             this._lat = lat;
37             this._latproj = lat2latp(lat);
38             this._lon = lon;
39             connection.addDupe(this);
40                         for each (var way:Way in this.parentWays) {
41                                 way.expandBbox(this);
42                         }
43         }
44         
45         public function set lat(lat:Number):void {
46             MainUndoStack.getGlobalStack().addAction(new MoveNodeAction(this, lat, _lon, setLatLonImmediate));
47         }
48
49         public function set latp(latproj:Number):void {
50             MainUndoStack.getGlobalStack().addAction(new MoveNodeAction(this, latp2lat(latproj), _lon, setLatLonImmediate));
51         }
52
53         public function set lon(lon:Number):void {
54             MainUndoStack.getGlobalStack().addAction(new MoveNodeAction(this, _lat, lon, setLatLonImmediate));
55         }
56         
57         public function setLatLon(lat:Number, lon:Number, performAction:Function):void {
58             performAction(new MoveNodeAction(this, lat, lon, setLatLonImmediate));
59         } 
60
61                 public function setLonLatp(lon:Number,latproj:Number, performAction:Function):void {
62                     performAction(new MoveNodeAction(this, latp2lat(latproj), lon, setLatLonImmediate));
63                 }
64
65         public override function toString():String {
66             return "Node("+id+"@"+version+"): "+lat+","+lon+" "+getTagList();
67         }
68
69                 public override function remove(performAction:Function):void {
70                         performAction(new DeleteNodeAction(this, setDeletedState));
71                 }
72
73                 public override function within(left:Number,right:Number,top:Number,bottom:Number):Boolean {
74                         if (_lon<left || _lon>right || _lat<bottom || _lat>top || deleted) { return false; }
75                         return true;
76                 }
77
78         public function unjoin(selectedWay:Way, performAction:Function):void {
79             if (parentWays.length > 1) {
80               performAction(new UnjoinNodeAction(this, selectedWay));
81             } else {
82               trace("not enough ways");
83             }
84         }
85
86         public function isDupe():Boolean {
87             var connection:Connection = Connection.getConnection();
88             if (connection.getNode(this.id) == this // node could be part of a vector layer
89                 && connection.nodesAtPosition(lat, lon) > 1) {
90               return true;
91             }
92             return false;
93         }
94
95                 internal override function isEmpty():Boolean {
96                         return deleted;
97                 }
98
99         public static function lat2latp(lat:Number):Number {
100             return 180/Math.PI * Math.log(Math.tan(Math.PI/4+lat*(Math.PI/180)/2));
101         }
102
103                 public static function latp2lat(a:Number):Number {
104                     return 180/Math.PI * (2 * Math.atan(Math.exp(a*Math.PI/180)) - Math.PI/2);
105                 }
106                 
107                 public override function getType():String {
108                         return 'node';
109                 }
110     }
111
112 }