Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix calls to parseFolder to stop processing folders multiple times; call... #3

Open
wants to merge 5 commits into from

3 participants

@djomp

...ing _check_folder more often to ensure the functions are not looking too deep (process one level at a time).

I'm not certain why folders are processed really, the Placemarks could just be iterated through; I suppose there are names associated with the folders that could be used somewhere. Didn't want to completely rewrite, just tweak so it wasn't drawing routes multiple times.

@djomp

Added fix to read long lists of co-ordinates in

@djomp

Added a way of getting the latLngs out from the kml file to pass to map.fitBounds(). I'm no javascript coder so I'm sure there's a nicer way of doing it than this...?

@shramov
Owner

I've repackaged plugins into separate repository https://github.com/shramov/leaflet-plugins
Sorry but history of your first two fixes is lost :)

@severinmenard

Hi,

Trying to contact you after a discussion with Martijn van Exel at SOTM about your fork of his Bing Analyzer. I volunteer for the humanitarian aspects of OSM and would be very interested to be able to get the resulting vector file to have a clear overview of what can be easily trace over imagery or not in the country hit by a humanitarian crisis. I would be happy if you can contact me on my email address severin.menard at gmail or on IRC (#hot on OFTC)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 22, 2012
  1. @djomp

    Fix calls to parseFolder to stop processing folders multiple times; c…

    djomp authored
    …alling _check_folder more often to ensure the functions are not looking too deep (process one level at a time).
Commits on Mar 23, 2012
  1. @djomp
  2. @djomp
  3. @djomp
Commits on Mar 27, 2012
  1. @djomp
This page is out of date. Refresh to see the latest.
Showing with 44 additions and 14 deletions.
  1. +44 −14 src/layer/KML.js
View
58 src/layer/KML.js
@@ -5,12 +5,12 @@ L.KML = L.FeatureGroup.extend({
L.Util.setOptions(this, options);
this._kml = kml;
this._layers = {};
-
+
if (kml) {
this.addKML(kml);
}
},
-
+
addKML: function(kml) {
var req = new window.XMLHttpRequest();
req.open('GET', kml, false);
@@ -19,17 +19,27 @@ L.KML = L.FeatureGroup.extend({
if (req.status != 200) return;
var layers = L.KML.parseKML(req.responseXML);
for (var i = 0; i < layers.length; i++)
+ {
+ this.fire('addlayer', {
+ layer: layers[i]
+ });
this.addLayer(layers[i]);
- }
+ }
+ this.latLngs = L.KML.getLatLngs(req.responseXML);
+ },
+
+ latLngs: Array()
});
L.Util.extend(L.KML, {
+
parseKML: function(xml) {
var style = this.parseStyle(xml);
var el = xml.getElementsByTagName("Folder");
var layers = [];
for (var i = 0; i < el.length; i++) {
- var l = this.parseFolder(xml, style);
+ if (!this._check_folder(el[i])) continue;
+ var l = this.parseFolder(el[i], style);
if (l) layers.push(l);
}
el = xml.getElementsByTagName('Placemark');
@@ -41,11 +51,13 @@ L.Util.extend(L.KML, {
return layers;
},
+ // Return false if e's first parent Folder is not [folder]
+ // - returns true if no parent Folders
_check_folder: function(e, folder) {
e = e.parentElement;
- while (e && e.tagName != "Folder" && e != folder)
+ while (e && e.tagName != "Folder")
e = e.parentElement;
- return e || e == folder;
+ return !e || e == folder;
},
parseStyle: function(xml) {
@@ -161,14 +173,7 @@ L.Util.extend(L.KML, {
parseCoords: function(xml) {
var el = xml.getElementsByTagName('coordinates');
- var coords = [];
- var text = el[0].childNodes[0].nodeValue.split(/[\s\n]+/);
- for (var i = 0; i < text.length; i++) {
- var ll = text[i].split(',');
- if (ll.length < 2) continue;
- coords.push(new L.LatLng(ll[1], ll[0]));
- }
- return coords;
+ return this._read_coords(el[0]);
},
parseLineString: function(line, xml, options) {
@@ -200,6 +205,31 @@ L.Util.extend(L.KML, {
if (options.fillColor) options.fill = true;
if (polys.length == 1) return new L.Polygon(polys.concat(inner), options);
return new L.MultiPolygon(polys, options);
+ },
+
+ getLatLngs: function(xml) {
+ var el = xml.getElementsByTagName('coordinates');
+ var coords = [];
+ for (var j = 0; j < el.length; j++) {
+ // text might span many childnodes
+ coords = coords.concat(this._read_coords(el[j]));
+ }
+ return coords;
+ },
+
+ _read_coords: function(el) {
+ var text = "";
+ var coords = [];
+ for (var i = 0; i < el.childNodes.length; i++) {
+ text = text + el.childNodes[i].nodeValue;
+ }
+ text = text.split(/[\s\n]+/);
+ for (var i = 0; i < text.length; i++) {
+ var ll = text[i].split(',');
+ if (ll.length < 2) continue;
+ coords.push(new L.LatLng(ll[1], ll[0]));
+ }
+ return coords;
}
});
Something went wrong with that request. Please try again.