-
Notifications
You must be signed in to change notification settings - Fork 17
/
Utils.py
72 lines (61 loc) · 2.36 KB
/
Utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""
***************************************************************************
Name : Geocoding
Description : Geocoding and reverse Geocoding using Google
Date : 28/May/09
copyright : (C) 2009-2013 by ItOpen
email : info@itopen.it
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
"""
from qgis.core import *
from qgis.gui import *
class ClickTool(QgsMapTool):
def __init__(self,iface, callback):
QgsMapTool.__init__(self,iface.mapCanvas())
self.iface = iface
self.callback = callback
self.canvas = iface.mapCanvas()
return None
def canvasReleaseEvent(self,e):
point = self.canvas.getCoordinateTransform().toMapPoint(e.pos().x(),e.pos().y())
self.callback(point)
return None
def pointToWGS84(point, crs):
"""
crs is the renderer crs
"""
t=QgsCoordinateReferenceSystem()
t.createFromSrid(4326)
f=crs #QgsCoordinateReferenceSystem()
#f.createFromProj4(proj4string)
try:
transformer = QgsCoordinateTransform(f,t)
except:
transformer = QgsCoordinateTransform(f, t, QgsProject.instance())
try:
pt = transformer.transform(point)
except:
pt = transformer.transform(QgsPointXY(point))
return pt
def pointFromWGS84(point, crs):
f=QgsCoordinateReferenceSystem()
f.createFromSrid(4326)
t=crs # QgsCoordinateReferenceSystem()
#t.createFromProj4(proj4string)
try:
transformer = QgsCoordinateTransform(f,t)
except:
transformer = QgsCoordinateTransform(f, t, QgsProject.instance())
try:
pt = transformer.transform(point)
except:
pt = transformer.transform(QgsPointXY(point))
return pt