ab86a8c5a189fa4441117721b6a92eccb240c680
[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) {
11             super(id, version, tags, loaded);
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):void {
18                         updateEntityProperties(version,tags,loaded); 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             this._lat = lat;
35             this._latproj = lat2latp(lat);
36             this._lon = lon;
37                         for each (var way:Way in this.parentWays) {
38                                 way.expandBbox(this);
39                         }
40         }
41         
42         public function set lat(lat:Number):void {
43             MainUndoStack.getGlobalStack().addAction(new MoveNodeAction(this, lat, _lon, setLatLonImmediate));
44         }
45
46         public function set latp(latproj:Number):void {
47             MainUndoStack.getGlobalStack().addAction(new MoveNodeAction(this, latp2lat(latproj), _lon, setLatLonImmediate));
48         }
49
50         public function set lon(lon:Number):void {
51             MainUndoStack.getGlobalStack().addAction(new MoveNodeAction(this, _lat, lon, setLatLonImmediate));
52         }
53         
54         public function setLatLon(lat:Number, lon:Number, performAction:Function):void {
55             performAction(new MoveNodeAction(this, lat, lon, setLatLonImmediate));
56         } 
57
58                 public function setLonLatp(lon:Number,latproj:Number, performAction:Function):void {
59                     performAction(new MoveNodeAction(this, latp2lat(latproj), lon, setLatLonImmediate));
60                 }
61
62         public override function toString():String {
63             return "Node("+id+"@"+version+"): "+lat+","+lon+" "+getTagList();
64         }
65
66                 public override function remove(performAction:Function):void {
67                         performAction(new DeleteNodeAction(this, setDeletedState));
68                 }
69
70                 public override function within(left:Number,right:Number,top:Number,bottom:Number):Boolean {
71                         if (_lon<left || _lon>right || _lat<bottom || _lat>top) { return false; }
72                         return true;
73                 }
74                 
75                 internal override function isEmpty():Boolean {
76                         return deleted;
77                 }
78
79         public static function lat2latp(lat:Number):Number {
80             return 180/Math.PI * Math.log(Math.tan(Math.PI/4+lat*(Math.PI/180)/2));
81         }
82
83                 public static function latp2lat(a:Number):Number {
84                     return 180/Math.PI * (2 * Math.atan(Math.exp(a*Math.PI/180)) - Math.PI/2);
85                 }
86                 
87                 public override function getType():String {
88                         return 'node';
89                 }
90     }
91
92 }