zhanmingkan
2022-05-14 0fc43fe898d14895c97427801293edfb3a0c5bf1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
function LonLatProjection(width, height) {
    var imageSize = { width: width, height: height };
    function getBoundingRect(regions) {
        var LIMIT = Number.MAX_VALUE
        var min, max
        var boundingRect = { xMin: LIMIT, yMin: LIMIT, xMax: -LIMIT, yMax: -LIMIT }
 
        for (var i = 0, L = regions.length; i < L; i++) {
            var rect = regions[i].getBoundingRect()
 
            min = { x: rect.xMin, y: rect.yMin };
            max = { x: rect.xMax, y: rect.yMax }
 
            boundingRect.xMin = boundingRect.xMin < min.x ? boundingRect.xMin : min.x
            boundingRect.yMin = boundingRect.yMin < min.y ? boundingRect.yMin : min.y
            boundingRect.xMax = boundingRect.xMax > max.x ? boundingRect.xMax : max.x
            boundingRect.yMax = boundingRect.yMax > max.y ? boundingRect.yMax : max.y
        }
 
        return boundingRect
    }
 
    function project(coordinate, boundingRect) {
        var width = boundingRect.xMax - boundingRect.xMin;
        var height = boundingRect.yMin - boundingRect.yMax;
        var distanceX = Math.abs(coordinate[0] - boundingRect.xMin);
        var distanceY = coordinate[1] - boundingRect.yMax;
 
        var percentX = (distanceX / width);
        var percentY = (distanceY / height);
         
        var px = percentX * imageSize.width,
              py = percentY * imageSize.height;
        return { x: px, y: py };
    }
 
    function unproject(pt, boundingRect) {
        var width = boundingRect.xMax - boundingRect.xMin;
        var height = boundingRect.yMin - boundingRect.yMax;
        var lon = (pt.x / imageSize.width) * width,
              lat = (pt.y / imageSize.height) * height;
        return [lon, lat];
    }
 
    this.project = project;
    this.unproject = unproject;
    this.getBoundingRect = getBoundingRect;
}
if (typeof module === "undefined") {
    this.LonLatProjection = LonLatProjection;
} else {
    module.exports = LonLatProjection;
}
if (typeof define === "function") {
    define(function () { return LonLatProjection; });
}