e8335739fd21d2cfd85a3e1cd18bb5d133bb8954
[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,paint.redrawPOIs);
45                         paint.ruleset.loadFromCSS(style);
46                 }
47
48         /** Create a new node on the vector layer. Note that the node won't show up until on the map
49         * until the the relevant nodeUI is created, so you will need to instruct the paint to create one
50         *
51         * e.g. layer.paint.updateEntityUIs(layer.getObjectsByBbox(...)...);
52         */
53                 public function createNode(tags:Object,lat:Number,lon:Number):Node {
54                         var node:Node = new Node(negativeID, 0, tags, true, lat, lon);
55                         nodes[negativeID]=node; negativeID--;
56                         return node;
57                 }
58
59         /**
60         * @param tags The tags for the new Way
61         * @param nodes An array of Node objects
62         */
63                 public function createWay(tags:Object,nodes:Array):Way {
64                         var way:Way = new Way(negativeID, 0, tags, true, nodes.concat());
65                         ways[negativeID]=way; negativeID--;
66                         return way;
67                 }
68
69         /**
70         * @param tags The tags for the new relation
71         * @param members An array of RelationMember objects
72         */
73                 public function createRelation(tags:Object,members:Array):Relation {
74             var relation:Relation = new Relation(negativeID, 0, tags, true, members.concat());
75                         relations[negativeID]=relation; negativeID--;
76             return relation;
77                 }
78         public function registerPOI(node:Node):void {
79             if (pois.indexOf(node)<0) { pois.push(node); }
80         }
81         public function unregisterPOI(node:Node):void {
82                         var index:uint = pois.indexOf(node);
83                         if ( index >= 0 ) { pois.splice(index,1); }
84         }
85
86                 public function getObjectsByBbox(left:Number, right:Number, top:Number, bottom:Number):Object {
87                         // ** FIXME: this is just copied-and-pasted from Connection.as, which really isn't very
88                         // good practice. Is there a more elegant way of doing it?
89                         var o:Object = { poisInside: [], poisOutside: [], waysInside: [], waysOutside: [] };
90                         for each (var way:Way in ways) {
91                                 if (way.within(left,right,top,bottom)) { o.waysInside.push(way); }
92                                                                   else { o.waysOutside.push(way); }
93                         }
94                         for each (var poi:Node in pois) {
95                                 if (poi.within(left,right,top,bottom)) { o.poisInside.push(poi); }
96                                                                   else { o.poisOutside.push(poi); }
97                         }
98                         return o;
99                 }
100
101         /**
102         * Transfers an entity from the VectorLayer into the main layer
103         * @param entity The entity from the VectorLayer that you want to transfer.
104         * @param connection The Connection instance to transfer to (eg Connection.getConnection() )
105         *
106         * @return either the newly created entity, or null
107         */
108                 public function pullThrough(entity:Entity,connection:Connection):Entity {
109                         var i:uint=0;
110                         var oldNode:Node, newNode:Node;
111                         if (entity is Way) {
112                                 // copy way through to main layer
113                                 // ** shouldn't do this if the nodes are already in the main layer
114                                 //    (or maybe we should just match on lat/long to avoid ways in background having nodes in foreground)
115                                 var oldWay:Way=Way(entity);
116                                 var newWay:Way=connection.createWay(oldWay.getTagsCopy(), [], MainUndoStack.getGlobalStack().addAction);
117                                 var nodemap:Object={};
118                                 for (i=0; i<oldWay.length; i++) {
119                                         oldNode = oldWay.getNode(i);
120                                         newNode = nodemap[oldNode.id] ? nodemap[oldNode.id] : connection.createNode(
121                                                 oldNode.getTagsCopy(), oldNode.lat, oldNode.lon, 
122                                                 MainUndoStack.getGlobalStack().addAction);
123                                         newWay.appendNode(newNode, MainUndoStack.getGlobalStack().addAction);
124                                         nodemap[oldNode.id]=newNode;
125                                 }
126                                 // delete this way
127                                 while (oldWay.length) { 
128                                         var id:Number=oldWay.getNode(0).id;
129                                         oldWay.removeNodeByIndex(0,MainUndoStack.getGlobalStack().addAction,false);
130                                         delete nodes[id];
131                                 }
132                                 paint.wayuis[oldWay.id].redraw();
133                                 delete ways[oldWay.id];
134                                 map.paint.createWayUI(newWay);
135                                 return newWay;
136
137                         } else if (entity is Node && !entity.hasParentWays) {
138                                 // copy node through to main layer
139                                 // ** should be properly undoable
140                                 oldNode=Node(entity);
141                                 unregisterPOI(oldNode);
142                                 var newPoiAction:CreatePOIAction = new CreatePOIAction(
143                                         oldNode.getTagsCopy(), oldNode.lat, oldNode.lon);
144                                 MainUndoStack.getGlobalStack().addAction(newPoiAction);
145                                 paint.deleteNodeUI(oldNode);
146                                 delete nodes[oldNode.id];
147                                 return newPoiAction.getNode();
148                         }
149                         return null;
150                 }
151
152         /**
153         * Remove all the nodes, ways, and relations from the VectorLayer.
154         * Also removes the associated NodeUIs, WayUIs and POIs
155         */
156                 public function blank():void {
157                         for each (var node:Node in nodes) { paint.deleteNodeUI(node); }
158                         for each (var way:Way in ways) { paint.deleteWayUI(way); }
159                         relations={}; nodes={}; ways={}; pois=[];
160                 }
161
162         }
163 }