X-Git-Url: http://git.cyclocoop.org/?p=lhc%2Fweb%2Fclavette_www.git;a=blobdiff_plain;f=www%2Fplugins%2Fgis%2Flib%2Fleaflet%2Fplugins%2FKML.js;fp=www%2Fplugins%2Fgis%2Flib%2Fleaflet%2Fplugins%2FKML.js;h=d521a308a63266a216f4d1da6dc14a8d76ced850;hp=2a7839d47797709e6bb017b12ab269b7a39eba43;hb=ebeac5db7a6b2678204355fcf4837d4986f2b2a3;hpb=0048daaa36f2abca86d683a21af54cf62f47a102 diff --git a/www/plugins/gis/lib/leaflet/plugins/KML.js b/www/plugins/gis/lib/leaflet/plugins/KML.js index 2a7839d..d521a30 100755 --- a/www/plugins/gis/lib/leaflet/plugins/KML.js +++ b/www/plugins/gis/lib/leaflet/plugins/KML.js @@ -69,6 +69,11 @@ L.Util.extend(L.KML, { l = this.parsePlacemark(el[j], xml, style); if (l) { layers.push(l); } } + el = xml.getElementsByTagName('GroundOverlay'); + for (var k = 0; k < el.length; k++) { + l = this.parseGroundOverlay(el[k]); + if (l) { layers.push(l); } + } return layers; }, @@ -87,9 +92,7 @@ L.Util.extend(L.KML, { var style = {}; var sl = xml.getElementsByTagName('Style'); - //for (var i = 0; i < sl.length; i++) { - var attributes = {color: true, width: true, Icon: true, href: true, - hotSpot: true}; + var attributes = { color: true, width: true, Icon: true, href: true, hotSpot: true }; function _parse(xml) { var options = {}; @@ -180,13 +183,28 @@ L.Util.extend(L.KML, { l = this.parsePlacemark(el[j], xml, style); if (l) { layers.push(l); } } + el = xml.getElementsByTagName('GroundOverlay'); + for (var k = 0; k < el.length; k++) { + if (!this._check_folder(el[k], xml)) { continue; } + l = this.parseGroundOverlay(el[k]); + if (l) { layers.push(l); } + } if (!layers.length) { return; } if (layers.length === 1) { return layers[0]; } return new L.FeatureGroup(layers); }, parsePlacemark: function (place, xml, style) { - var i, j, el, options = {}; + var h, i, j, el, options = {}; + + var multi = ['MultiGeometry', 'MultiTrack', 'gx:MultiTrack']; + for (h in multi) { + el = place.getElementsByTagName(multi[h]); + for (i = 0; i < el.length; i++) { + return this.parsePlacemark(el[i], xml, style); + } + } + el = place.getElementsByTagName('styleUrl'); for (i = 0; i < el.length; i++) { var url = el[i].childNodes[0].nodeValue; @@ -196,17 +214,13 @@ L.Util.extend(L.KML, { } var layers = []; - var parse = ['LineString', 'Polygon', 'Point']; + var parse = ['LineString', 'Polygon', 'Point', 'Track', 'gx:Track']; for (j in parse) { - // for jshint - if (true) - { - var tag = parse[j]; - el = place.getElementsByTagName(tag); - for (i = 0; i < el.length; i++) { - var l = this['parse' + tag](el[i], xml, options); - if (l) { layers.push(l); } - } + var tag = parse[j]; + el = place.getElementsByTagName(tag); + for (i = 0; i < el.length; i++) { + var l = this['parse' + tag.replace(/gx:/, '')](el[i], xml, options); + if (l) { layers.push(l); } } } @@ -248,6 +262,17 @@ L.Util.extend(L.KML, { return new L.Polyline(coords, options); }, + parseTrack: function (line, xml, options) { + var el = xml.getElementsByTagName('gx:coord'); + if (el.length === 0) { el = xml.getElementsByTagName('coord'); } + var coords = []; + for (var j = 0; j < el.length; j++) { + coords = coords.concat(this._read_gxcoords(el[j])); + } + if (!coords.length) { return; } + return new L.Polyline(coords, options); + }, + parsePoint: function (line, xml, options) { var el = line.getElementsByTagName('coordinates'); if (!el.length) { @@ -309,6 +334,54 @@ L.Util.extend(L.KML, { coords.push(new L.LatLng(ll[1], ll[0])); } return coords; + }, + + _read_gxcoords: function (el) { + var text = '', coords = []; + text = el.firstChild.nodeValue.split(' '); + coords.push(new L.LatLng(text[1], text[0])); + return coords; + }, + + parseGroundOverlay: function (xml) { + var latlonbox = xml.getElementsByTagName('LatLonBox')[0]; + var bounds = new L.LatLngBounds( + [ + latlonbox.getElementsByTagName('south')[0].childNodes[0].nodeValue, + latlonbox.getElementsByTagName('west')[0].childNodes[0].nodeValue + ], + [ + latlonbox.getElementsByTagName('north')[0].childNodes[0].nodeValue, + latlonbox.getElementsByTagName('east')[0].childNodes[0].nodeValue + ] + ); + var attributes = {Icon: true, href: true, color: true}; + function _parse(xml) { + var options = {}, ioptions = {}; + for (var i = 0; i < xml.childNodes.length; i++) { + var e = xml.childNodes[i]; + var key = e.tagName; + if (!attributes[key]) { continue; } + var value = e.childNodes[0].nodeValue; + if (key === 'Icon') { + ioptions = _parse(e); + if (ioptions.href) { options.href = ioptions.href; } + } else if (key === 'href') { + options.href = value; + } else if (key === 'color') { + options.opacity = parseInt(value.substring(0, 2), 16) / 255.0; + options.color = '#' + value.substring(6, 8) + value.substring(4, 6) + value.substring(2, 4); + } + } + return options; + } + var options = {}; + options = _parse(xml); + if (latlonbox.getElementsByTagName('rotation')[0] !== undefined) { + var rotation = latlonbox.getElementsByTagName('rotation')[0].childNodes[0].nodeValue; + options.rotation = parseFloat(rotation); + } + return new L.RotatedImageOverlay(options.href, bounds, {opacity: options.opacity, angle: options.rotation}); } }); @@ -322,12 +395,14 @@ L.KMLIcon = L.Icon.extend({ this.style.width = i.width + 'px'; this.style.height = i.height + 'px'; - if (this.anchorType.x === 'UNITS_FRACTION' || this.anchorType.x === 'fraction') { + if (this.anchorType.x === 'fraction' && this.anchorType.y === 'fraction') { img.style.marginLeft = (-this.anchor.x * i.width) + 'px'; - } - if (this.anchorType.y === 'UNITS_FRACTION' || this.anchorType.x === 'fraction') { img.style.marginTop = (-(1 - this.anchor.y) * i.height) + 'px'; } + if (this.anchorType.x === 'pixels' && this.anchorType.y === 'pixels') { + img.style.marginLeft = (-this.anchor.x) + 'px'; + img.style.marginTop = (this.anchor.y - i.height + 1) + 'px'; + } this.style.display = ''; }; return img; @@ -348,3 +423,34 @@ L.KMLMarker = L.Marker.extend({ } }); +// Inspired by https://github.com/bbecquet/Leaflet.PolylineDecorator/tree/master/src +L.RotatedImageOverlay = L.ImageOverlay.extend({ + options: { + angle: 0 + }, + _reset: function () { + L.ImageOverlay.prototype._reset.call(this); + this._rotate(); + }, + _animateZoom: function (e) { + L.ImageOverlay.prototype._animateZoom.call(this, e); + this._rotate(); + }, + _rotate: function () { + if (L.DomUtil.TRANSFORM) { + // use the CSS transform rule if available + this._image.style[L.DomUtil.TRANSFORM] += ' rotate(' + this.options.angle + 'deg)'; + } else if(L.Browser.ie) { + // fallback for IE6, IE7, IE8 + var rad = this.options.angle * (Math.PI / 180), + costheta = Math.cos(rad), + sintheta = Math.sin(rad); + this._image.style.filter += ' progid:DXImageTransform.Microsoft.Matrix(sizingMethod=\'auto expand\', M11=' + + costheta + ', M12=' + (-sintheta) + ', M21=' + sintheta + ', M22=' + costheta + ')'; + } + }, + getBounds: function() { + return this._bounds; + } +}); +