最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Convert X,Y pixel to Longitude and Latitude - Stack Overflow

programmeradmin6浏览0评论

I have an image 8640x11520 pixels from a part of the map in real scale. I need convert my x, y point to coordinate, anyone has an idea to find out it??

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 = ??

I have an image 8640x11520 pixels from a part of the map in real scale. I need convert my x, y point to coordinate, anyone has an idea to find out it??

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 = ??
Share Improve this question edited Nov 20, 2019 at 14:17 Mosè Raguzzini 15.9k1 gold badge34 silver badges46 bronze badges asked Nov 20, 2019 at 12:41 Andre GoncalvesAndre Goncalves 1592 silver badges7 bronze badges 2
  • 1 is start and end latitudes the top-left and bottom-right corners, respectively? – ibrahim mahrir Commented Nov 20, 2019 at 12:45
  • Yes the start and end latitudes the top-left and bottom-right corners – Andre Goncalves Commented Nov 20, 2019 at 13:35
Add a ment  | 

1 Answer 1

Reset to default 6

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.

Usually maps are WGS84 Projections so you have to apply the inverse formulas for the Mercator projection.

The task is not trivial so my advice is to rely on libraries like 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-bined.js"></script>  //adjust the path for your server
                                                     //or else use the pressed 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/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

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/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 : " +p.x + " \nlat : " + p.y);
});
<script src="https://cdnjs.cloudflare./ajax/libs/proj4js/2.3.3/proj4.js"></script>
<script src="https://cdnjs.cloudflare./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.

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!

发布评论

评论列表(0)

  1. 暂无评论