[英]Convert X,Y pixel to Longitude and Latitude
我有一張來自 map 的一部分的 8640x11520 像素的真實比例圖像。 我需要將我的 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 = ??
當您映射到緯度/經度時,請注意,您不能使用線性比例來執行此操作,而是必須檢查 map 應用了哪種投影,然后相應地轉換坐標。
通常地圖是WGS84 投影,因此您必須應用墨卡托投影的逆公式。
這個任務並不簡單,所以我的建議是依賴像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
摘錄: 在給定圖片上將 long/lat 轉換為像素 x/y
工作示例:
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>
注意:如果您打算將 map 用於 GPS 信號,則必須了解 map 角的緯度/經度。
這是一個圖形示例,直觀地解釋了為什么線性比例不適合:
仔細觀察(例如)格陵蘭島的大小,在墨卡托投影空間坐標上它看起來比北美大。 當然不是!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.