Answers:
在Python中转换坐标的最简单方法是pyproj,即PROJ.4库的Python接口。事实上:
from pyproj import Proj, transform
inProj = Proj(init='epsg:3857')
outProj = Proj(init='epsg:4326')
x1,y1 = -11705274.6374,4826473.6922
x2,y2 = transform(inProj,outProj,x1,y1)
print x2,y2
退货 -105.150271116 39.7278572773
默认情况下,您链接到的站点使用空间参考系统EPSG 3857(WGS84 Web Mercator)。我在这里找到此信息。
您可以通过在下面的表单中输入所需的EPSG来指定另一个空间参考系统,Spatial Reference
也可以使用Python转换返回的坐标。
例如,您可以使用GDAL Python绑定将该点从投影坐标系(EPSG 3857)转换为地理坐标系(EPSG 4326)。
import ogr, osr
pointX = -11705274.6374
pointY = 4826473.6922
# Spatial Reference System
inputEPSG = 3857
outputEPSG = 4326
# create a geometry from coordinates
point = ogr.Geometry(ogr.wkbPoint)
point.AddPoint(pointX, pointY)
# create coordinate transformation
inSpatialRef = osr.SpatialReference()
inSpatialRef.ImportFromEPSG(inputEPSG)
outSpatialRef = osr.SpatialReference()
outSpatialRef.ImportFromEPSG(outputEPSG)
coordTransform = osr.CoordinateTransformation(inSpatialRef, outSpatialRef)
# transform point
point.Transform(coordTransform)
# print point in EPSG 4326
print point.GetX(), point.GetY()
这将为您返回点的坐标-105.150271116 39.7278572773
。
afalciano的答案正确,但希望包含pyproj的其他用法。
它确实需要您知道proj4字符串,并且速度要快一点。
import pyproj
p = pyproj.Proj("+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +a=6378137 +b=6378137 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs")
lon, lat = p(x, y, inverse=True)
print lat, lon
p = pyproj.Proj(init='epsg:3857')
,结果是相同的。
尝试了Marcel Wilson建议的代码,并且速度更快:
from pyproj import Proj, transform
import time
import pyproj
# Test 1 - 0.0006158 s
start=time.time()
inProj = Proj(init='epsg:3857')
outProj = Proj(init='epsg:4326')
x1,y1 = -11705274.6374,4826473.6922
x2,y2 = transform(inProj,outProj,x1,y1)
end=time.time()
print(y2,x2)
print('%.7f' % (end-start))
# Test 2 - 0.0000517 s --- factor 11,9
start=time.time()
x,y = -11705274.6374,4826473.6922
p = pyproj.Proj("+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +a=6378137 +b=6378137 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs")
lon, lat = p(x, y, inverse=True)
end=time.time()
print(lat, lon)
print('%.7f' % (end-start))
-----------------
39.72785727727918 -105.15027111593008
0.0006158
39.72785727727918 -105.15027111593008
0.0000517
我在寻找在QGIS中执行此操作的方法时找到了这篇文章。如上所述这里,该方法使用看起来像这样:
def convertProjection(self,x,y,from_crs,to_crs):
crsSrc = QgsCoordinateReferenceSystem(from_crs)
crsDest = QgsCoordinateReferenceSystem(to_crs)
xform = QgsCoordinateTransform(crsSrc, crsDest)
pt = xform.transform(QgsPoint(x,y))
return pt.x, pt.y
# Remove the "EPSG:" part
from_crs = 3857
to_crs = 4326
x = -11705274.6374
y = 4826473.6922
lon, lat = self.convertProjection(x,y,from_crs, to_crs)
v3.x
需要使用xform = QgsCoordinateTransform(crsSrc, crsDest, QgsProject.instance())
请注意,accepts 的transform
功能pyproj
也arrays
很重要,它在涉及数据帧时非常有用
import pandas as pd
from pyproj import Proj, transform
df = pd.DataFrame({'x': [-11705274.6374]*100,
'y': [4826473.6922]*100})
inProj, outProj = Proj(init='epsg:3857'), Proj(init='epsg:4326')
df['x2'], df['y2'] = transform(inProj, outProj, df['x'].tolist(), df['y'].tolist())
import cv2
import numpy as np
def onMouse(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
# draw circle here (etc...)
print('x = %f, y = %f'%((x*2/100),(y*2/100)))
a=float(input("enter length of original dimension in cm"))
b=float(input("enter width of original dimension in cm"))
print("origional image coordinates")
im=cv2.imread('mask1.jpg',0)
re_im=cv2.resize(im,(round(a*100/2),round(b*100/2)))
cv2.imshow('image',re_im)
cv2.setMouseCallback('image', onMouse)
cv2.waitKey(0)
cv2.destroyAllWindows()