First cut at styling TIGER data via an option. Needs lots of propagation of uid and...
[potlatch2.git] / net / systemeD / halcyon / connection / XMLBaseConnection.as
1 package net.systemeD.halcyon.connection {
2
3     import flash.events.*;
4
5         import flash.system.Security;
6         import flash.net.*;
7     import org.iotashan.oauth.*;
8
9         import net.systemeD.halcyon.Globals;
10
11     /**
12     * XMLBaseConnection is the common code between connecting to an OSM server
13     * (i.e. XMLConnection) and connecting to a standalone .osm file (i.e. OSMConnection)
14     * and so mainly concerns itself with /map -call-ish related matters
15     */
16         public class XMLBaseConnection extends Connection {
17
18                 public function XMLBaseConnection() {
19                 }
20                 
21         protected function loadedMap(event:Event):void {
22             dispatchEvent(new Event(LOAD_COMPLETED));
23
24             var map:XML = new XML(URLLoader(event.target).data);
25             var id:Number;
26             var version:uint;
27             var uid:Number;
28             var timestamp:String;
29             var tags:Object;
30
31             for each(var relData:XML in map.relation) {
32                 id = Number(relData.@id);
33                 version = uint(relData.@version);
34                 
35                 var rel:Relation = getRelation(id);
36                 if ( rel == null || !rel.loaded ) {
37                     tags = parseTags(relData.tag);
38                     var members:Array = [];
39                     for each(var memberXML:XML in relData.member) {
40                         var type:String = memberXML.@type.toLowerCase();
41                         var role:String = memberXML.@role;
42                         var memberID:Number = Number(memberXML.@ref);
43                         var member:Entity = null;
44                         if ( type == "node" ) {
45                             member = getNode(memberID);
46                             if ( member == null ) {
47                                 member = new Node(memberID,0,{},false,0,0);
48                                 setNode(Node(member),true);
49                             }
50                         } else if ( type == "way" ) {
51                             member = getWay(memberID);
52                             if (member == null) {
53                                 member = new Way(memberID,0,{},false,[]);
54                                 setWay(Way(member),true);
55                             }
56                         } else if ( type == "relation" ) {
57                             member = getRelation(memberID);
58                             if (member == null) {
59                                 member = new Relation(memberID,0,{},false,[]);
60                                 setRelation(Relation(member),true);
61                             }
62                         }
63                         
64                         if ( member != null )
65                             members.push(new RelationMember(member, role));
66                     }
67                     
68                     if ( rel == null )
69                         setRelation(new Relation(id, version, tags, true, members), false);
70                     else {
71                         rel.update(version,tags,true,members);
72                         sendEvent(new EntityEvent(NEW_RELATION, rel), false);
73                     }
74                 }
75             }
76             
77             for each(var nodeData:XML in map.node) {
78                 id = Number(nodeData.@id);
79                 version = uint(nodeData.@version);
80                 uid = Number(nodeData.@uid);
81                 timestamp = nodeData.@timestamp;
82
83                 var node:Node = getNode(id);
84                 if ( node == null || !node.loaded ) {
85                     var lat:Number = Number(nodeData.@lat);
86                     var lon:Number = Number(nodeData.@lon);
87                     tags = parseTags(nodeData.tag);
88                     if ( node == null )
89                         setNode(new Node(id, version, tags, true, lat, lon, uid, timestamp),false);
90                     else {
91                         node.update(version, tags, true, lat, lon, uid, timestamp);
92                         sendEvent(new EntityEvent(NEW_NODE, node), false);
93                     }
94                 }
95             }
96
97             for each(var data:XML in map.way) {
98                 id = Number(data.@id);
99                 version = uint(data.@version);
100                 uid = Number(data.@uid);
101                 timestamp = data.@timestamp;
102
103                 var way:Way = getWay(id);
104                 if ( way == null || !way.loaded ) {
105                     var nodes:Array = [];
106                     for each(var nd:XML in data.nd)
107                         nodes.push(getNode(Number(nd.@ref)));
108                     tags = parseTags(data.tag);
109                     if ( way == null )
110                         setWay(new Way(id, version, tags, true, nodes, uid, timestamp),false);
111                     else {
112                         way.update(version, tags, true, nodes, uid, timestamp);
113                         sendEvent(new EntityEvent(NEW_WAY, way), false);
114                     }
115                 }
116             }
117             
118             registerPOINodes();
119         }
120         
121         protected function registerPOINodes():void {
122             for each (var nodeID:Number in getAllNodeIDs()) {
123                 var node:Node = getNode(nodeID);
124                 if (!node.hasParentWays)
125                     registerPOI(node);
126             }
127         }
128
129         private function parseTags(tagElements:XMLList):Object {
130             var tags:Object = {};
131             for each (var tagEl:XML in tagElements)
132                 tags[tagEl.@k] = tagEl.@v;
133             return tags;
134         }
135
136         }
137 }