简体   繁体   English

将 X,Y 像素转换为经度和纬度

[英]Convert X,Y pixel to Longitude and Latitude

I have an image 8640x11520 pixels from a part of the map in real scale.我有一张来自 map 的一部分的 8640x11520 像素的真实比例图像。 I need convert my x, y point to coordinate, anyone has an idea to find out it??我需要将我的 x,y 点转换为坐标,有人知道吗?

在此处输入图像描述

var mapWidth = 8640;
var mapHeight = 11520;

var mapLatitudeStart = 28.349768989955244;
var mapLongitudeStart = -81.55803680419922;

var maxLatitude = 28.349806758250104;
var maxLongitude = -81.541128;

var pointNeedConversion = {'x': 4813.10 'y': 2674.84};
var pointLatitude = ??

As you are mapping to lat/long, beware, you can't do that with a linear proportion , instead you have to check what kind of projection is applied to the map, then convert coordinates accordingly.当您映射到纬度/经度时,请注意,您不能使用线性比例来执行此操作,而是必须检查 map 应用了哪种投影,然后相应地转换坐标。

Usually maps are WGS84 Projections so you have to apply the inverse formulas for the Mercator projection.通常地图是WGS84 投影,因此您必须应用墨卡托投影的逆公式。

The task is not trivial so my advice is to rely on libraries like Proj4js这个任务并不简单,所以我的建议是依赖像Proj4js这样的库

The usage of the library is simple, you provide a reference system to work on, then you can trasform coordinates on another projection.该库的使用很简单,您提供一个参考系统来处理,然后您可以在另一个投影上转换坐标。

    // 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

Credit for the snippet: Convert long/lat to pixel x/y on a given picture摘录: 在给定图片上将 long/lat 转换为像素 x/y

Working example:工作示例:

 var dest = new proj4.Proj('EPSG:4326'); //destination coordinates coordinates will be in Longitude/Latitude, WGS84, global spherical mercators projection, see http://spatialreference.org/ref/epsg/3785/ var source = new proj4.Proj('EPSG:3785'); //source coordinates in meters $("#convert").on("click", function(){ var p = new proj4.Point($("#x").val(), $("#y").val() ); proj4.transform(source, dest, p); alert("lng: " +px + " \nlat: " + py); });
 <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>

Note: it is essential to know what is the lat/lon of the corner of your map if you intend to use it to map a GPS signal.注意:如果您打算将 map 用于 GPS 信号,则必须了解 map 角的纬度/经度。

Here a graph example that visually explains why a linear proportion is not suitable:这是一个图形示例,直观地解释了为什么线性比例不适合:

在此处输入图像描述

Take a close look to (Eg) the size of the Greenland, on mercator projection space coordinates it looks bigger than north America .仔细观察(例如)格陵兰岛的大小,在墨卡托投影空间坐标上它看起来比北美大 Of course it is not!当然不是!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM