将X,Y像素转换为坐标

问题描述 投票:4回答:1

我从地图的一部分获得了真实比例为8640x11520像素的图像。我需要将x,y点转换为坐标,任何人都有一个想法可以找到它?

enter image description here

var mapWidth = 8640;
var mapHeight = 11520;

var mapLatitudeStart = 28.349768989955244;
var mapLongitudeStart = -81.55803680419922;

var maxLatitude = 28.349806758250104;
var maxLongitude = [![-81.541128][1]][1]15856934;

var pointNeedConversion = {'x': 4813.10 'y': 2674.84};
var pointLatitude = ??
javascript image math coordinates pixel
1个回答
0
投票

当您映射到纬度/经度时,请注意,不能使用线性比例进行此操作,相反,您必须检查对地图应用了哪种投影,然后相应地转换坐标。

通常,地图是WGS84 Projections,因此您必须对墨卡托投影应用逆公式。

该任务并非易事,所以我的建议是依赖Proj4js之类的库>

该库的用法很简单,您提供了一个要使用的参考系统,然后可以在另一个投影上变换坐标。

    // include the library
    <script src="lib/proj4js-combined.js"></script>  //adjust the path for your server
                                                     //or else use the compressed version
    // creating source and destination Proj4js objects
    // once initialized, these may be re-used as often as needed
    var source = new Proj4js.Proj('EPSG:4326');    //source coordinates will be in Longitude/Latitude, WGS84
    var dest = new Proj4js.Proj('EPSG:3785');     //destination coordinates in meters, global spherical mercators projection, see http://spatialreference.org/ref/epsg/3785/


    // transforming point coordinates
    var p = new Proj4js.Point(-76.0,45.0);   //any object will do as long as it has 'x' and 'y' properties
    Proj4js.transform(source, dest, p);      //do the transformation.  x and y are modified in place

    //p.x and p.y are now EPSG:3785 in meters

代码段的信用:Convert long/lat to pixel x/y on a given picture

工作示例:

var dest = new proj4.Proj('EPSG:4326'); //destination coordinates in meters, global spherical mercators projection, see http://spatialreference.org/ref/epsg/3785/  
var source = new proj4.Proj('EPSG:3785'); //source coordinates will be in Longitude/Latitude, WGS84     

$("#convert").on("click", function(){
    var p = new proj4.Point($("#x").val(), $("#y").val() );
    proj4.transform(source, dest, p);
    alert("lng : " +p.x + " \nlat : " + p.y);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/proj4js/2.3.3/proj4.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
x : <input type="number" id="x" />
y : <input type="number" id="y" />
<button id="convert">Convert</button>
© www.soinside.com 2019 - 2024. All rights reserved.