Merge branch 'master' into history
[potlatch2.git] / net / systemeD / potlatch2 / utils / KmlImporter.as
1 package net.systemeD.potlatch2.utils {
2
3     import net.systemeD.halcyon.Map;
4     import net.systemeD.halcyon.connection.Connection;
5     import net.systemeD.halcyon.connection.Node;
6     import net.systemeD.halcyon.connection.Way;
7     import net.systemeD.halcyon.connection.Relation;
8     import net.systemeD.halcyon.connection.RelationMember;
9     import net.systemeD.potlatch2.tools.Simplify;
10
11     /**
12      * Implements parsing and loading of KML files.
13      */
14     public class KmlImporter extends Importer {
15
16         public function KmlImporter(connection:Connection, map:Map, filenames:Array, callback:Function=null, simplify:Boolean=false) {
17             super(connection, map, filenames, callback, simplify);
18         }
19
20         override protected function doImport(push:Function): void {
21             var kml:XML = new XML(files[0]);
22
23             for each (var ns:Namespace in kml.namespaceDeclarations()) {
24                 if
25                 (ns.uri.match(/^http:\/\/earth\.google\.com\/kml\/[0-9]+\.[0-9]+$/) ||
26                  ns.uri.match(/^http:\/\/www\.opengis\.net\/kml\/[0-9]+\.[0-9]+$/)) {
27                     default xml namespace = ns;
28                 }
29             }
30
31             for each (var placemark:XML in kml..Placemark) {
32                 var tags:Object = {};
33
34                 if (placemark.name.length() > 0) {
35                     tags["name"] = placemark.name.substr(0,255);
36                 }
37
38                 if (placemark.description.length() > 0) {
39                     tags["description"] = placemark.description.substr(0,255);
40                 }
41
42                 for each (var point:XML in placemark.Point) {
43                     importNode(point.coordinates, tags, push);
44                 }
45
46                 for each (var linestring:XML in placemark.LineString) {
47                     importWay(linestring.coordinates, tags, false, push);
48                 }
49
50                 for each (var linearring:XML in placemark.LinearRing) {
51                     importWay(linearring.coordinates, tags, true, push);
52                 }
53
54                 for each (var polygon:XML in placemark.Polygon) {
55                     if (polygon.innerBoundaryIs.length() > 0) {
56                         var members:Array = [];
57                         var way:Way;
58
59                         way = importWay(polygon.outerBoundaryIs.LinearRing.coordinates, {}, true, push);
60                         members.push(new RelationMember(way, "outer"));
61
62                         for each (var inner:XML in polygon.innerBoundaryIs) {
63                             way = importWay(inner.LinearRing.coordinates, {}, true, push);
64                             members.push(new RelationMember(way, "inner"));
65                         }
66
67                         tags["type"] = "multipolygon";
68
69                         connection.createRelation(tags, members, push);
70                     } else {
71                         importWay(polygon.outerBoundaryIs.LinearRing.coordinates, tags, true, push);
72                     }
73                 }
74             }
75                         default xml namespace = new Namespace("");
76         }
77
78         private function importNode(coordinates:String, tags:Object, push:Function): Node {
79             var coords:Array = coordinates.split(",");
80             var lon:Number = coords[0];
81             var lat:Number = coords[1];
82             //var ele:Number = coords[2];
83
84             var node:Node = connection.createNode(tags, lat, lon, push);
85
86             connection.registerPOI(node);
87
88             return node;
89         }
90
91         private function importWay(coordinates:String, tags:Object, polygon:Boolean, push:Function): Way {
92             var way:Way;
93             var nodestring:Array = [];
94
95             if (polygon) {
96                 coordinates = coordinates.slice(0, coordinates.lastIndexOf(" "));
97             }
98
99             for each (var tuple:String in coordinates.split(" ")) {
100                 var coords:Array = tuple.split(",");
101                 var lon:Number = coords[0];
102                 var lat:Number = coords[1];
103                 //var ele:Number = coords[2];
104
105                 nodestring.push(connection.createNode({}, lat, lon, push));
106             }
107
108             if (polygon) {
109                 nodestring.push(nodestring[0]);
110             }
111
112             if (nodestring.length > 0) {
113                 way = connection.createWay(tags, nodestring, push);
114                 if (simplify) { Simplify.simplify(way, map, false); }
115             }
116
117             return way;
118         }
119     }
120 }