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