Document some of the features of VectorLayer.as
[potlatch2.git] / net / systemeD / halcyon / VectorLayer.as
1 package net.systemeD.halcyon {
2
3         import net.systemeD.halcyon.Map;
4         import net.systemeD.halcyon.MapPaint;
5         import net.systemeD.halcyon.connection.*;
6     import net.systemeD.halcyon.connection.actions.*;
7         import net.systemeD.halcyon.Globals;
8         import net.systemeD.halcyon.styleparser.RuleSet;
9
10     /** The VectorLayer class is used for the concept of Vector Background Layers.
11     * It is similar in concept to the various Connection layers used for the core
12     * OpenStreetMap data, and as such it stores its own list of nodes, ways and relations.
13     * The most interesting function is pullThrough which allows moving entities out
14     * of a VectorLayer and into the main map layer
15     */
16         public class VectorLayer extends Object {
17
18                 public var map:Map;
19                 public var paint:MapPaint;                                              // sprites
20                 public var name:String;
21                 public var url:String;
22                 public var style:String='';
23
24                 public var ways:Object=new Object();                    // geodata
25                 public var nodes:Object=new Object();                   //  |
26                 public var relations:Object=new Object();               //  |
27                 private var pois:Array=[];                                              //  |
28         private var negativeID:Number = -1;
29
30         /** Create a new VectorLayer
31         * @param n The name of the VectorLayer (eg 'GPS tracks')
32         * @param m The map. You probably have a global reference to this
33         * @param s The style you wish to use (eg 'gpx.css')
34         */
35                 public function VectorLayer(n:String,m:Map,s:String) {
36                         name=n;
37                         map=m;
38                         style=s;
39                         paint=new MapPaint(m,0,0);
40                         redrawFromCSS(style);
41                 }
42
43                 public function redrawFromCSS(style:String):void {
44                         paint.ruleset=new RuleSet(map.MINSCALE,map.MAXSCALE,paint.redraw);
45                         paint.ruleset.loadFromCSS(style);
46                 }
47                 
48                 public function createNode(tags:Object,lat:Number,lon:Number):Node {
49                         var node:Node = new Node(negativeID, 0, tags, true, lat, lon);
50                         nodes[negativeID]=node; negativeID--;
51                         return node;
52                 }
53                 public function createWay(tags:Object,nodes:Array):Way {
54                         var way:Way = new Way(negativeID, 0, tags, true, nodes.concat());
55                         ways[negativeID]=way; negativeID--;
56                         return way;
57                 }
58                 public function createRelation(tags:Object,members:Array):Relation {
59             var relation:Relation = new Relation(negativeID, 0, tags, true, members.concat());
60                         relations[negativeID]=relation; negativeID--;
61             return relation;
62                 }
63         public function registerPOI(node:Node):void {
64             if (pois.indexOf(node)<0) { pois.push(node); }
65         }
66         public function unregisterPOI(node:Node):void {
67                         var index:uint = pois.indexOf(node);
68                         if ( index >= 0 ) { pois.splice(index,1); }
69         }
70
71                 public function getObjectsByBbox(left:Number, right:Number, top:Number, bottom:Number):Object {
72                         // ** FIXME: this is just copied-and-pasted from Connection.as, which really isn't very
73                         // good practice. Is there a more elegant way of doing it?
74                         var o:Object = { poisInside: [], poisOutside: [], waysInside: [], waysOutside: [] };
75                         for each (var way:Way in ways) {
76                                 if (way.within(left,right,top,bottom)) { o.waysInside.push(way); }
77                                                                   else { o.waysOutside.push(way); }
78                         }
79                         for each (var poi:Node in pois) {
80                                 if (poi.within(left,right,top,bottom)) { o.poisInside.push(poi); }
81                                                                   else { o.poisOutside.push(poi); }
82                         }
83                         return o;
84                 }
85
86         /**
87         * Transfers an entity from the VectorLayer into the main layer
88         * @param entity The entity from the VectorLayer that you want to transfer.
89         * @param connection The Connection instance to transfer to (eg Connection.getConnection() )
90         *
91         * @return either the newly created entity, or null
92         */
93                 public function pullThrough(entity:Entity,connection:Connection):Entity {
94                         var i:uint=0;
95                         var oldNode:Node, newNode:Node;
96                         if (entity is Way) {
97                                 // copy way through to main layer
98                                 // ** shouldn't do this if the nodes are already in the main layer
99                                 //    (or maybe we should just match on lat/long to avoid ways in background having nodes in foreground)
100                                 var oldWay:Way=Way(entity);
101                                 var newWay:Way=connection.createWay(oldWay.getTagsCopy(), [], MainUndoStack.getGlobalStack().addAction);
102                                 var nodemap:Object={};
103                                 for (i=0; i<oldWay.length; i++) {
104                                         oldNode = oldWay.getNode(i);
105                                         newNode = nodemap[oldNode.id] ? nodemap[oldNode.id] : connection.createNode(
106                                                 oldNode.getTagsCopy(), oldNode.lat, oldNode.lon, 
107                                                 MainUndoStack.getGlobalStack().addAction);
108                                         newWay.appendNode(newNode, MainUndoStack.getGlobalStack().addAction);
109                                         nodemap[oldNode.id]=newNode;
110                                 }
111                                 // delete this way
112                                 while (oldWay.length) { 
113                                         var id:Number=oldWay.getNode(0).id;
114                                         oldWay.removeNodeByIndex(0,MainUndoStack.getGlobalStack().addAction,false);
115                                         delete nodes[id];
116                                 }
117                                 paint.wayuis[oldWay.id].redraw();
118                                 delete ways[oldWay.id];
119                                 map.paint.createWayUI(newWay);
120                                 return newWay;
121
122                         } else if (entity is Node && !entity.hasParentWays) {
123                                 // copy node through to main layer
124                                 // ** should be properly undoable
125                                 oldNode=Node(entity);
126                                 unregisterPOI(oldNode);
127                                 var newPoiAction:CreatePOIAction = new CreatePOIAction(
128                                         oldNode.getTagsCopy(), oldNode.lat, oldNode.lon);
129                                 MainUndoStack.getGlobalStack().addAction(newPoiAction);
130                                 paint.deleteNodeUI(oldNode);
131                                 delete nodes[oldNode.id];
132                                 return newPoiAction.getNode();
133                         }
134                         return null;
135                 }
136
137         /**
138         * Remove all the nodes, ways, and relations from the VectorLayer.
139         * Also removes the associated NodeUIs, WayUIs and POIs
140         */
141                 public function blank():void {
142                         for each (var node:Node in nodes) { paint.deleteNodeUI(node); }
143                         for each (var way:Way in ways) { paint.deleteWayUI(way); }
144                         relations={}; nodes={}; ways={}; pois=[];
145                 }
146
147         }
148 }