在给定图片上将long / lat转换为像素x / y [英] Convert long/lat to pixel x/y on a given picture

查看:127
本文介绍了在给定图片上将long / lat转换为像素x / y的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张莫斯科城市地图。我们用一些艺术元素修改了Google Maps图像,但GPS坐标和像素之间的关系保持不变。



问题:我如何将GPS坐标从我们拥有的各种数据点转换成图像中的像素坐标?

理想情况下,我可以在Javascript中执行此操作,但PHP会正常运行。






我知道在小尺度上(例如在城市尺度上)它足够简单了(有必要了解哪些地理坐标有一个,然后分别在OX和OY轴上的图片上的地理坐标中学习一个像素的价格)。然而,在大规模(国家规模)一个像素的价格将不是一个常数,而且会有很大的变化,并且上述方法无法应用。

如何解决国家尺度上的问题?






更新:

我不使用API​​ Google M aps,我只有:对象的地理坐标(它们来自谷歌地图),我仍然在我的网站有一个简单的图片*。 gif,其中我必须绘制一个相应的地理坐标点。

解决方案

http://en.wikipedia.org/wiki/Map_projection =noreferrer>地图预测。正如其他人所指出的那样,失真的原因是球面(或更精确的椭球体)地球投射到一个平面上的事实。



为了实现您的目标,您首先必须了解有关您数据的两件事:


  1. 投影您的地图。如果它们纯粹来自 Google地图,那么它们很可能是使用球形墨卡托投影

  2. 您的纬度/经度坐标正在使用的地理坐标系。这可能会有所不同,因为在全球定位经纬度有不同的方式。用于大多数网络地图应用和GPS的最常见的GCS是 WGS84 。 li>

我假定您的数据在这些坐标系中。



球形墨卡托投影为地球表面定义了一个以米为单位的坐标对。这意味着,对于每个纬度/长度坐标都有一个匹配的米/米坐标。这使您可以使用以下步骤进行转换:


  1. 找到图像角落的WGS84 lat / long。
  2. >
  3. 将WGS lat / longs转换为球形墨卡托投影。在那里有转换工具,我最喜欢使用属于 PROJ4项目的cs2cs工具。

  4. 您可以安全地进行简单的线性转换,以便在图像上的点之间转换,并在球面墨卡托投影中转换为地球上的点,然后再返回。

为了从WGS84点转到图像上的一个像素,程序现在是:


  1. 将经纬度投影到球形墨卡托。这可以使用 proj4js库完成。

  2. 将球形墨卡托坐标转换为图像像素坐标使用上面发现的线性关系。

您可以像这样使用proj4js库:

  //包含库
< script src =lib / proj4js-combined.js>< / script> //调整服务器的路径
//或者使用压缩版本
//创建源和目标Proj4js对象
//一旦初始化,它们可以重复使用需要
var source = new Proj4js.Proj('EPSG:4326'); //源坐标将位于经度/纬度,WGS84
var dest = new Proj4js.Proj('EPSG:3785'); //以米为单位的目标坐标,全球球面投影机投影,请参阅http://spatialreference.org/ref/epsg/3785/


//转换点坐标
var p = new Proj4js.Point(-76.0,45.0); //只要有'x'和'y'属性,任何对象都会执行
Proj4js.transform(source,dest,p); //做转换。 x和y被修改

//p.x和p.y现在是EPSG:3785以米为单位


I have a city map of Moscow. We modified a Google Maps image with some artistic elements, but the relation between GPS coordinates and pixels remains the same.

Problem: How do I convert GPS coordinates from various data points we have into pixel coordinates in the image?

Ideally I can do this in Javascript, but PHP would be OK.


I know that on small scales (for example on city scales) it to make simply enough (it is necessary to learn what geographic coordinates has one of picture corners, then to learn "price" of one pixel in geographic coordinates on a picture on axes OX and OY separately).

But on the big scales (country scale) "price" of one pixel will be not a constant, and will vary strongly enough and the method described above cannot be applied.

How to solve a problem on country scales?


Update:

I do not use API Google Maps, I have only: geographic coordinates of the object (they are from google maps), I still have at my site a simple picture *. gif, in which I must draw a point corresponding geographic coordinates.

解决方案

The key to all of this is understanding map projections. As others have pointed out, the cause of the distortion is the fact that the spherical (or more accurately ellipsoidal) earth is projected onto a plane.

In order to achieve your goal, you first must know two things about your data:

  1. The projection your maps are in. If they are purely derived from Google Maps, then chances are they are using a spherical Mercator projection.
  2. The geographic coordinate system your latitude/longitude coordinates are using. This can vary, because there are different ways of locating lat/longs on the globe. The most common GCS, used in most web-mapping applications and for GPS's, is WGS84.

I'm assuming your data is in these coordinate systems.

The spherical Mercator projection defines a coordinate pair in meters, for the surface of the earth. This means, for every lat/long coordinate there is a matching meter/meter coordinate. This enables you to do the conversion using the following procedure:

  1. Find the WGS84 lat/long of the corners of the image.
  2. Convert the WGS lat/longs to the spherical Mercator projection. There conversion tools out there, my favorite is to use the cs2cs tool that is part of the PROJ4 project.
  3. You can safely do a simple linear transform to convert between points on the image, and points on the earth in the spherical Mercator projection, and back again.

In order to go from a WGS84 point to a pixel on the image, the procedure is now:

  1. Project lat/lon to spherical Mercator. This can be done using the proj4js library.
  2. Transform spherical Mercator coordinate into image pixel coordinate using the linear relationship discovered above.

You can use the proj4js library like this:

// 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆