1 package net.systemeD.halcyon {
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;
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
16 public class VectorLayer extends Object {
19 public var paint:MapPaint; // sprites
20 public var name:String;
21 public var url:String;
22 public var style:String='';
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=[]; // |
29 private var markers:Object=new Object(); // markers
30 private var negativeID:Number = -1;
32 /** Create a new VectorLayer
33 * @param n The name of the VectorLayer (eg 'GPS tracks')
34 * @param m The map. You probably have a global reference to this
35 * @param s The style you wish to use (eg 'gpx.css')
37 public function VectorLayer(n:String,m:Map,s:String) {
41 paint=new MapPaint(m,0,0);
45 public function redrawFromCSS(style:String):void {
46 paint.ruleset=new RuleSet(map.MINSCALE,map.MAXSCALE,paint.redraw,paint.redrawPOIs);
47 paint.ruleset.loadFromCSS(style);
50 /** Create a new node on the vector layer. Note that the node won't show up until on the map
51 * until the the relevant nodeUI is created, so you will need to instruct the paint to create one
53 * e.g. layer.paint.updateEntityUIs(layer.getObjectsByBbox(...)...);
55 public function createNode(tags:Object,lat:Number,lon:Number):Node {
56 var node:Node = new Node(negativeID, 0, tags, true, lat, lon);
57 nodes[negativeID]=node; negativeID--;
62 * @param tags The tags for the new Way
63 * @param nodes An array of Node objects
65 public function createWay(tags:Object,nodes:Array):Way {
66 var way:Way = new Way(negativeID, 0, tags, true, nodes.concat());
67 ways[negativeID]=way; negativeID--;
72 * @param tags The tags for the new relation
73 * @param members An array of RelationMember objects
75 public function createRelation(tags:Object,members:Array):Relation {
76 var relation:Relation = new Relation(negativeID, 0, tags, true, members.concat());
77 relations[negativeID]=relation; negativeID--;
81 public function createMarker(tags:Object,lat:Number,lon:Number):Marker {
82 var marker:Marker = new Marker(negativeID, 0, tags, true, lat, lon);
83 markers[negativeID]=marker; negativeID--;
87 public function registerPOI(node:Node):void {
88 if (pois.indexOf(node)<0) { pois.push(node); }
90 public function unregisterPOI(node:Node):void {
91 var index:uint = pois.indexOf(node);
92 if ( index >= 0 ) { pois.splice(index,1); }
95 public function getObjectsByBbox(left:Number, right:Number, top:Number, bottom:Number):Object {
96 // ** FIXME: this is just copied-and-pasted from Connection.as, which really isn't very
97 // good practice. Is there a more elegant way of doing it?
98 var o:Object = { poisInside: [], poisOutside: [], waysInside: [], waysOutside: [] };
99 for each (var way:Way in ways) {
100 if (way.within(left,right,top,bottom)) { o.waysInside.push(way); }
101 else { o.waysOutside.push(way); }
103 for each (var poi:Node in pois) {
104 if (poi.within(left,right,top,bottom)) { o.poisInside.push(poi); }
105 else { o.poisOutside.push(poi); }
111 * Transfers an entity from the VectorLayer into the main layer
112 * @param entity The entity from the VectorLayer that you want to transfer.
113 * @param connection The Connection instance to transfer to (eg Connection.getConnection() )
115 * @return either the newly created entity, or null
117 public function pullThrough(entity:Entity,connection:Connection):Entity {
119 var oldNode:Node, newNode:Node;
121 // copy way through to main layer
122 // ** shouldn't do this if the nodes are already in the main layer
123 // (or maybe we should just match on lat/long to avoid ways in background having nodes in foreground)
124 var oldWay:Way=Way(entity);
125 var newWay:Way=connection.createWay(oldWay.getTagsCopy(), [], MainUndoStack.getGlobalStack().addAction);
126 var nodemap:Object={};
127 for (i=0; i<oldWay.length; i++) {
128 oldNode = oldWay.getNode(i);
129 newNode = nodemap[oldNode.id] ? nodemap[oldNode.id] : connection.createNode(
130 oldNode.getTagsCopy(), oldNode.lat, oldNode.lon,
131 MainUndoStack.getGlobalStack().addAction);
132 newWay.appendNode(newNode, MainUndoStack.getGlobalStack().addAction);
133 nodemap[oldNode.id]=newNode;
136 while (oldWay.length) {
137 var id:Number=oldWay.getNode(0).id;
138 oldWay.removeNodeByIndex(0,MainUndoStack.getGlobalStack().addAction,false);
141 paint.wayuis[oldWay.id].redraw();
142 delete ways[oldWay.id];
143 map.paint.createWayUI(newWay);
146 } else if (entity is Node && !entity.hasParentWays) {
147 // copy node through to main layer
148 // ** should be properly undoable
149 oldNode=Node(entity);
150 unregisterPOI(oldNode);
151 var newPoiAction:CreatePOIAction = new CreatePOIAction(
152 oldNode.getTagsCopy(), oldNode.lat, oldNode.lon);
153 MainUndoStack.getGlobalStack().addAction(newPoiAction);
154 paint.deleteNodeUI(oldNode);
155 delete nodes[oldNode.id];
156 return newPoiAction.getNode();
162 * Remove all the nodes, ways, and relations from the VectorLayer.
163 * Also removes the associated NodeUIs, WayUIs and POIs
165 public function blank():void {
166 for each (var node:Node in nodes) { paint.deleteNodeUI(node); }
167 for each (var way:Way in ways) { paint.deleteWayUI(way); }
168 relations={}; nodes={}; ways={}; pois=[];