88f7f6461db51157e0f3a58ebcd6013fe2183293
[potlatch2.git] / net / systemeD / halcyon / connection / Node.as
1 package net.systemeD.halcyon.connection {
2
3     public class Node extends Entity {
4         private var _lat:Number;
5         private var _latproj:Number;
6         private var _lon:Number;
7
8         public function Node(id:Number, version:uint, tags:Object, loaded:Boolean, lat:Number, lon:Number) {
9             super(id, version, tags, loaded);
10             this._lat = lat;
11             this._latproj = lat2latp(lat);
12             this._lon = lon;
13         }
14
15                 public function update(version:uint, tags:Object, loaded:Boolean, lat:Number, lon:Number):void {
16                         updateEntityProperties(version,tags,loaded); this.lat=lat; this.lon=lon;
17                 }
18
19         public function get lat():Number {
20             return _lat;
21         }
22
23         public function get latp():Number {
24             return _latproj;
25         }
26
27         public function get lon():Number {
28             return _lon;
29         }
30
31         public function set lat(lat:Number):void {
32             var oldLat:Number = this._lat;
33             this._lat = lat;
34             this._latproj = lat2latp(lat);
35             markDirty();
36             dispatchEvent(new NodeMovedEvent(Connection.NODE_MOVED, this, oldLat, _lon));
37         }
38
39         public function set latp(latproj:Number):void {
40             var oldLat:Number = this._lat;
41             this._latproj = latproj;
42             this._lat = latp2lat(latproj);
43             markDirty();
44             dispatchEvent(new NodeMovedEvent(Connection.NODE_MOVED, this, oldLat, _lon));
45          }
46
47         public function set lon(lon:Number):void {
48             var oldLon:Number = this._lon;
49             this._lon = lon;
50             markDirty();
51             dispatchEvent(new NodeMovedEvent(Connection.NODE_MOVED, this, _lat, oldLon));
52          }
53
54                 public function setLonLatp(lon:Number,latproj:Number):void {
55                         // move both lon and latp but only fire one event
56                         var oldLon:Number = this._lon;
57                         var oldLat:Number = this._lat;
58                         this._latproj = latproj;
59                         this._lat = latp2lat(latproj);
60                         this._lon = lon;
61                         markDirty();
62             dispatchEvent(new NodeMovedEvent(Connection.NODE_MOVED, this, oldLat, oldLon));
63                 }
64
65         public override function toString():String {
66             return "Node("+id+"@"+version+"): "+lat+","+lon+" "+getTagList();
67         }
68
69                 public override function remove():void {
70                         removeFromParents();
71                         deleted=true;
72             dispatchEvent(new EntityEvent(Connection.NODE_DELETED, this));
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 }