不同的坐标系单张

时间:2014-01-17 12:01:57

标签: javascript gis leaflet

这是我左下角显示的坐标图。它给出了我鼠标位置的坐标。

http://i.stack.imgur.com/AT3tO.png

问题在于它在全球坐标系(ESPG 4326)中,但我需要它在EPSG 3301(爱沙尼亚局部坐标系)中。

这是给出坐标的代码。

    L.Control.MousePosition = L.Control.extend({
  options: {
    position: 'bottomleft',
    separator: ' : ',
    emptyString: 'Unavailable',
    lngFirst: false,
    numDigits: 5,
    lngFormatter: undefined,
    latFormatter: undefined,
    prefix: ""
  },

  onAdd: function (map) {
    this._container = L.DomUtil.create('div', 'leaflet-control-mouseposition');
    L.DomEvent.disableClickPropagation(this._container);
    map.on('mousemove', this._onMouseMove, this);
    this._container.innerHTML=this.options.emptyString;
    return this._container;
  },

  onRemove: function (map) {
    map.off('mousemove', this._onMouseMove)
  },

  _onMouseMove: function (e) {
    var lng = this.options.lngFormatter ? this.options.lngFormatter(e.latlng.lng) : L.Util.formatNum(e.latlng.lng, this.options.numDigits);
    var lat = this.options.latFormatter ? this.options.latFormatter(e.latlng.lat) : L.Util.formatNum(e.latlng.lat, this.options.numDigits);
    var value = this.options.lngFirst ? lng + this.options.separator + lat : lat + this.options.separator + lng;
    var prefixAndValue = this.options.prefix + ' ' + value;
    this._container.innerHTML = prefixAndValue;
  }

});

L.Map.mergeOptions({
    positionControl: false
});

L.Map.addInitHook(function () {
    if (this.options.positionControl) {
        this.positionControl = new L.Control.MousePosition();
        this.addControl(this.positionControl);
    }
});

L.control.mousePosition = function (options) {
    return new L.Control.MousePosition(options);
};

谢谢, 克里斯蒂安

1 个答案:

答案 0 :(得分:0)

你必须使用Lambert Conic Conformal(2P)方法转换它们,这个方法well described但实施起来有些冗长,但这并没有阻止我。这是一个仅从经度和纬度转换为EPSG的示例实现,反之亦然:

function EPSG(lat1, lat2, latF, lonF, EF, NF) {

    this.sqr = function(x) {
        return x*x;
    }

    this.rad = function(x) {
        return Math.PI * x / 180.0;
    }

    lat1 = this.rad(lat1);
    lat2 = this.rad(lat2);
    latF = this.rad(latF);
    lonF = this.rad(lonF);

    this.a = 6378206.400;
    this.e = 0.08227185;
    this.lat1 = lat1;
    this.lat2 = lat2;
    this.latF = latF;
    this.lonF = lonF;
    this.EF = EF;
    this.NF = NF;

    var e = this.e;
    var a = this.a;

    var m1 = Math.cos(lat1) / Math.sqrt(1 - this.sqr(e * Math.sin(lat1)));
    var m2 = Math.cos(lat2) / Math.sqrt(1 - this.sqr(e * Math.sin(lat2)));
    var t1 = Math.tan(0.25 * Math.PI - 0.5 * lat1) /
        Math.pow((1 - e * Math.sin(lat1)) / 
            (1 + e * Math.sin(lat1)), 0.5*e);
    var t2 = Math.tan(0.25 * Math.PI - 0.5 * lat2) /
        Math.pow((1 - e * Math.sin(lat2)) / 
            (1 + e * Math.sin(lat2)), 0.5*e);
    var tF = Math.tan(0.25 * Math.PI - 0.5 * latF) /
        Math.pow((1 - e * Math.sin(latF)) / 
            (1 + e * Math.sin(latF)), 0.5*e);
    var n = (Math.log(m1) - Math.log(m2)) / 
        (Math.log(t1) - Math.log(t2));
    var F = m1 / (n * Math.pow(t1, n));
    var rF = a * F * Math.pow(tF, n);

    this.rF = rF;
    this.n = n;

    this.deg2epsg = function(lon, lat) {
        var e = this.e;
        var a = this.a;

        lon = this.rad(lon);
        lat = this.rad(lat);

        var t = Math.tan(0.25 * Math.PI - 0.5 * lat) /
            Math.pow((1 - e * Math.sin(lat)) /
                (1 + e * Math.sin(lat)), 0.5*e);

        var r = a * F * Math.pow(t, n);
        var theta = n * (lon - lonF);

        var E = this.EF + r * Math.sin(theta);
        var N = this.NF + this.rF - r * Math.cos(theta);

        return [E, N];
    }
}

现在,您可以使用data for EPSG 3301创建EPSG的实例并运行其deg2epsg方法:

var epsg3301 = new EPSG(59.33333333333334, 58, 
    57.51755393055556, 24, 500000, 6375000);

var tallinn = epsg3301.deg2epsg(24.7453, 59.4372);

// tallinn[0] == 542293.7844303187
// tallinn[1] == 6589057.033686307

请注意,大多数数据仅初始化一次,因此deg2epsg应该相当快。