jjm2473 / 六只脚KML下载_纠偏版

// ==UserScript==
// @name         六只脚KML下载_纠偏版
// @author       jjm2473
// @copyright    2016, jjm2473@qq.com
// @supportURL   mailto:jjm2473@qq.com
// @icon         http://www.foooooot.com/favicon.ico
// @namespace    https://openuserjs.org/users/jjm2473/
// @version      0.3.2
// @description  六只脚轨迹免银两下载,实际是调用json接口获取数据,注意最好不要同时启用纠偏版和未纠偏版
// @include      /^https?://(www\.)?foooooot\.com/trip/\d+/#?$/
// @include      /^https?://(www\.)?foooooot\.com/map/\??.*$/
// @grant        GM_xmlhttpRequest
// @run-at       document-end
// ==/UserScript==


(function ()
{
    /**
     * Created by Wandergis on 2015/7/8.
     * 提供了百度坐标(BD09)、国测局坐标(火星坐标,GCJ02)、和WGS84坐标系之间的转换
     */

    //定义一些常量
    var x_PI = 3.14159265358979324 * 3000.0 / 180.0;
    var PI = 3.1415926535897932384626;
    var a = 6378245.0;
    var ee = 0.00669342162296594323;

    /**
     * 百度坐标系 (BD-09) 与 火星坐标系 (GCJ-02)的转换
     * 即 百度 转 谷歌、高德
     * @param bd_lon
     * @param bd_lat
     * @returns {*[]}
     */
    function bd09togcj02(bd_lon, bd_lat)
    {
        var x_pi = 3.14159265358979324 * 3000.0 / 180.0;
        var x = bd_lon - 0.0065;
        var y = bd_lat - 0.006;
        var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi);
        var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi);
        var gg_lng = z * Math.cos(theta);
        var gg_lat = z * Math.sin(theta);
        return [gg_lng, gg_lat]
    }

    /**
     * 火星坐标系 (GCJ-02) 与百度坐标系 (BD-09) 的转换
     * 即谷歌、高德 转 百度
     * @param lng
     * @param lat
     * @returns {*[]}
     */
    function gcj02tobd09(lng, lat)
    {
        var z = Math.sqrt(lng * lng + lat * lat) + 0.00002 * Math.sin(lat * x_PI);
        var theta = Math.atan2(lat, lng) + 0.000003 * Math.cos(lng * x_PI);
        var bd_lng = z * Math.cos(theta) + 0.0065;
        var bd_lat = z * Math.sin(theta) + 0.006;
        return [bd_lng, bd_lat]
    }

    /**
     * WGS84转GCj02
     * @param lng
     * @param lat
     * @returns {*[]}
     */
    function wgs84togcj02(lng, lat)
    {
        if (out_of_china(lng, lat))
        {
            return [lng, lat]
        }
        else
        {
            var dlat = transformlat(lng - 105.0, lat - 35.0);
            var dlng = transformlng(lng - 105.0, lat - 35.0);
            var radlat = lat / 180.0 * PI;
            var magic = Math.sin(radlat);
            magic = 1 - ee * magic * magic;
            var sqrtmagic = Math.sqrt(magic);
            dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
            dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
            var mglat = lat + dlat;
            var mglng = lng + dlng;
            return [mglng, mglat]
        }
    }

    /**
     * GCJ02 转换为 WGS84
     * @param lng
     * @param lat
     * @returns {*[]}
     */
    function gcj02towgs84(lng, lat)
    {
        if (out_of_china(lng, lat))
        {
            return [lng, lat]
        }
        else
        {
            var dlat = transformlat(lng - 105.0, lat - 35.0);
            var dlng = transformlng(lng - 105.0, lat - 35.0);
            var radlat = lat / 180.0 * PI;
            var magic = Math.sin(radlat);
            magic = 1 - ee * magic * magic;
            var sqrtmagic = Math.sqrt(magic);
            dlat = (dlat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * PI);
            dlng = (dlng * 180.0) / (a / sqrtmagic * Math.cos(radlat) * PI);
            mglat = lat + dlat;
            mglng = lng + dlng;
            return [lng * 2 - mglng, lat * 2 - mglat]
        }
    }

    function transformlat(lng, lat)
    {
        var ret = -100.0 + 2.0 * lng + 3.0 * lat + 0.2 * lat * lat + 0.1 * lng * lat + 0.2 * Math.sqrt(Math.abs(lng));
        ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0;
        ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0;
        return ret
    }

    function transformlng(lng, lat)
    {
        var ret = 300.0 + lng + 2.0 * lat + 0.1 * lng * lng + 0.1 * lng * lat + 0.1 * Math.sqrt(Math.abs(lng));
        ret += (20.0 * Math.sin(6.0 * lng * PI) + 20.0 * Math.sin(2.0 * lng * PI)) * 2.0 / 3.0;
        ret += (20.0 * Math.sin(lng * PI) + 40.0 * Math.sin(lng / 3.0 * PI)) * 2.0 / 3.0;
        ret += (150.0 * Math.sin(lng / 12.0 * PI) + 300.0 * Math.sin(lng / 30.0 * PI)) * 2.0 / 3.0;
        return ret
    }

    /**
     * 判断是否在国内,不在国内则不做偏移
     * @param lng
     * @param lat
     * @returns {boolean}
     */
    function out_of_china(lng, lat)
    {
        return (lng < 72.004 || lng > 137.8347) || ((lat < 0.8293 || lat > 55.8271) || false);
    }

    window.gcj02towgs84 = gcj02towgs84;
}
)();

/*
 * 欢迎交流改进
 * @author:jjm2473@qq.com
 **/
function XMLNode(tag)
{
    this.tagName = tag;
    this.childNodes = [];
    this.innerText = null;
}

XMLNode.prototype =
{
    toString : function (indent)
    {
        indent = indent || 0;
        var cindent = "";
        var i;
        for (i = 0; i < indent; ++i)
            cindent += '\t';
        //var nindent=cindent+'\t';
        var result = "\n" + cindent + "<" + this.tagName + ">";
        if (this.innerText)
            result += this.innerText;
        ++indent;
        for (i = 0; i < this.childNodes.length; ++i)
        {
            result += this.childNodes[i].toString(indent);
        }
        if (i)
            result += ("\n" + cindent);
        result += ("</" + this.tagName + ">");
        return result;
    },
    appendChild : function (node)
    {
        this.childNodes.push(node);
        return this;
    },
    setText : function (text)
    {
        this.innerText = text;
        return this;
    }
};

(
    function ()
{
    var TIMEZONE = 8 * 60 * 60 * 1000;

    var KMLHeader = '<?xml version="1.0" encoding="UTF-8"?>\n' +
        '<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">';
    var KMLTail = '\n</kml>';

    var makeTrack = function (name, data)
    {
        var pm = new XMLNode("Placemark");
        pm.appendChild(new XMLNode("name").setText(name))
        .appendChild(new XMLNode("styleUrl").setText("#msn_track"));
        var tr = new XMLNode('gx:Track');
        for (var i = 0; i < data.length; ++i)
        {
            tr.appendChild(new XMLNode('when').setText(data[i][3]));
            tr.appendChild(new XMLNode('gx:coord').setText(data[i][0] + ' ' + data[i][1] + ' ' + data[i][2]));
        }
        pm.appendChild(tr);
        return pm;
    };

    var makePath = function (name, data)
    {
        var coords = [];
        for (var i = 0; i < data.length; ++i)
        {
            coords.push(data[i][0] + "," + data[i][1] + "," + data[i][2]);
        }
        var pm = new XMLNode("Placemark");
        pm.appendChild(new XMLNode("name").setText(name))
        .appendChild(new XMLNode("LineString")
            .appendChild(new XMLNode("coordinates")
                .setText(coords.join(" "))));
        return pm;
    };

    var getTracks = function (id, cb)
    {
        var url = "/trip/" + id + "/trackjson/";
        $.getJSON(url, function (data)
        {
            var wpts = [];
            var tz = new Date().getTimezoneOffset() * 60 * 1000;
            $.each(data, function (i, val)
            {
                var wgs84 = gcj02towgs84(val[2], val[1]);
                wpts.push([wgs84[0], wgs84[1], val[3], new Date(parseInt(val[0]) * 1000 - tz).toJSON()]);
            }
            );

            var fl = new XMLNode("Folder");
            fl.appendChild(new XMLNode("name").setText("Tracks"));
            fl.appendChild(makeTrack("Track", wpts))
            .appendChild(makePath("Path", wpts));
            cb(fl);
        }
        );
    };
    var FOOTPRINT_OPT =
    {
        TIME : 0,
        LAT : 1,
        LNG : 2,
        ELEAVTION : 3,
        TITLE : 4,
        THUMBNAIL : 5,
        GALLERY : 6,
        DESCRIPTION : 7,
        TYPE : 8,
        REPLY_TIMES : 9,
        EDIT_URL : 10,
        COMMENT_URL : 11
    };
    var getFootPrint = function (id, cb)
    {
        var url = '/trip/' + id + '/footprintsjson/';
        $.getJSON(url, function (data)
        {
            var fl = new XMLNode("Folder");
            fl.appendChild(new XMLNode("name").setText("Points"));
            $.each(data, function (i, val)
            {
                var pm = new XMLNode("Placemark")
                    .appendChild(new XMLNode("name").setText(val[FOOTPRINT_OPT.TITLE]));
                if (val[FOOTPRINT_OPT.GALLERY])
                {
                    var desc = new XMLNode("description")
                        .setText('<![CDATA[<img src="' + val[FOOTPRINT_OPT.GALLERY] + '">]]>');
                    pm.appendChild(desc);
                }
                var wgs84 = gcj02towgs84(val[FOOTPRINT_OPT.LNG], val[FOOTPRINT_OPT.LAT]);
                pm.appendChild(new XMLNode("Point")
                    .appendChild(new XMLNode("coordinates")
                        .setText(wgs84[0] + "," + wgs84[1] + "," + val[FOOTPRINT_OPT.ELEAVTION])));
                fl.appendChild(pm);
            }
            );
            cb(fl);
        }
        );
    };
    var getMarks = function (id, cb)
    {
        var url = '/trip/' + id + '/marksjson/';
        $.getJSON(url, function (data)
        {
            var fl = new XMLNode("Folder");
            fl.appendChild(new XMLNode("name").setText("Marks"));
            $.each(data, function (i, val)
            {
                var pm = new XMLNode("Placemark")
                    .appendChild(new XMLNode("name").setText(val.title));
                if (val.content)
                {
                    var desc = new XMLNode("description")
                        .setText('<![CDATA[<pre>' + val.content + '</pre>]]>');
                    pm.appendChild(desc);
                }
                var wgs84 = gcj02towgs84(val.lon, val.lat);
                pm.appendChild(new XMLNode("Point")
                    .appendChild(new XMLNode("coordinates")
                        .setText(wgs84[0] + "," + wgs84[1])));
                fl.appendChild(pm);
            }
            );
            cb(fl);
        }
        );
    };
    var getInfo = function (id, cb)
    {
        if (id)
        {
            var url = '/trip/' + id + '/info/';
            $.getJSON(url, function (data)
            {
                var sec = data.duration;
                var hour = parseInt(sec / 3600);
                var minute = parseInt(sec % 3600 / 60);

                var str = '于 ' + data.occurtime + ' 出发,历时 ' + hour + ' 小时, ' + minute + ' 分钟\n' +
                    data.start_place_name + ' - ' + data.end_place_name + ' | ' + data.activity + ',全程 ' + (parseInt(data.distance * 1000) / 1000) + ' 公里\n' +
                    '难度级别:' + data.difficulty + '\n' +
                    '累计上升:' + data.accum_uphill + '米, 累计下降:' + data.accum_downhill + '米\n' +
                    '海拔最低:' + data.elevation_min + '米, 最高:' + data.elevation_max + '米\n' +
                    '最高速度:'+data.speed_max+'公里每小时\n\n'+data.story;
                cb(data.name, str);
            }
            );
        }
        else
        {
            cb(document.getElementsByClassName("trip_box_title")[0].children[0].innerText,
                document.getElementsByClassName('trip_box')[1].innerText);
        }
    };
    var wrapKML = function (id, track, footpoints, marks, name, info)
    {
        var doc = new XMLNode("Document").appendChild(new XMLNode("name").setText(name + ".kml"))
            .appendChild(new XMLNode("description").setText('<![CDATA[<a href="http://www.foooooot.com/trip/' + id + '/">http://www.foooooot.com/trip/' + id + '/</a><br/><pre>' + info + '</pre>]]>'))
            .setText('\n\t<atom:author><atom:name>export by jjm2473\'s script</atom:name></atom:author><atom:link href="https://openuserjs.org/scripts/jjm2473/%E5%85%AD%E5%8F%AA%E8%84%9AKML%E4%B8%8B%E8%BD%BD"></atom:link>\n' +
                '\t<StyleMap id="msn_track"><Pair><key>normal</key><styleUrl>#sn_track</styleUrl></Pair><Pair><key>highlight</key><styleUrl>#sh_track</styleUrl></Pair></StyleMap><Style id="sn_track"><IconStyle><scale>1.2</scale><Icon><href>http://maps.google.com/mapfiles/kml/shapes/track.png</href></Icon><hotSpot x="32" y="1" xunits="pixels" yunits="pixels"/></IconStyle><ListStyle></ListStyle><LineStyle><color>ffffaa55</color><width>3</width></LineStyle></Style><Style id="sh_track"><IconStyle><scale>1.4</scale><Icon><href>http://maps.google.com/mapfiles/kml/shapes/track.png</href></Icon><hotSpot x="32" y="32" xunits="pixels" yunits="pixels"/></IconStyle><ListStyle></ListStyle><LineStyle><color>ffffaa55</color><width>3</width></LineStyle></Style>');
        doc.appendChild(track).appendChild(footpoints).appendChild(marks);
        return KMLHeader + doc.toString() + KMLTail;
    };
    var exportString2File = function (output, filename)
    {
        var blob = new Blob([output],
            {
                type : 'text/plain'
            }
            );
        var objectURL = URL.createObjectURL(blob);
        var a = document.createElement('a');
        a.href = objectURL;
        a.download = filename || 'Untitled';
        a.click();
    };
    var onClickListener = function ()
    {
        var tripid = this.parentNode.id;
        getTracks(tripid, function (track)
        {
            getFootPrint(tripid, function (footprints)
            {
                getMarks(tripid, function (marks)
                {
                    getInfo(tripid, function (TripName, info)
                    {
                        exportString2File(wrapKML(tripid, track, footprints, marks, TripName, info), tripid+"_"+TripName+".kml");
                    }
                    );
                }
                );
            }
            );
        }
        );
    };
    var createDownloadLink = function ()
    {
        var aa = document.createElement("a");
        aa.addEventListener("click", onClickListener);
        aa.href = "javascript:void(0);";
        //aa.classList.add("inlineIcon");
        //aa.classList.add("btnDownload");
        aa.innerHTML = '&nbsp;下载KML&nbsp;';
        return aa;
    };

    if (location.pathname.startsWith('/trip/'))
    {
        //trip view
        console.log('trip view');
        var tripid = document.getElementById("trip-id");
        if (tripid === null)
            return;

        var div = document.createElement("div");
        div.id = tripid.value;
        div.appendChild(createDownloadLink());
        tripid.parentElement.insertBefore(div, tripid);
    }
    else if (location.pathname.startsWith('/map/'))
    {
        //map view
        console.log('map view');
        var listAddLink = function ()
        {
            var list = document.getElementsByClassName('resultList')[0].getElementsByClassName('tripItem');
            $.each(list, function (i, val)
            {
                val.appendChild(createDownloadLink());
            }
            );
        };
        var funcInject = function ()
        {
            if (typeof unsafeWindow.Utils === 'object')
            {
                unsafeWindow.Utils.successCallbackProxy = function (fn)
                {
                    return function (result)
                    {
                        var action = result.action;
                        if (!action)
                        {
                            alert("Server Error:ActionNotDefined");
                            throw new Error('Server Error:ActionNotDefined');
                            return;
                        }
                        var data = action.data;
                        var actionName = action.name;
                        if (actionName == 'redirect')
                        {
                            window.location.href = data.url;
                            return;
                        }
                        fn && $.isFunction(fn) && fn(actionName, data);
                        listAddLink();
                    };
                };
                listAddLink();
            }
            else
            {
                console.error("cannot find var Utils!");
                setTimeout(funcInject, 200);
            }
        };
        funcInject();
    }
    console.error("jjm2473,I am here!"); //just for debug
}
)();