forked from desertwolf7/NumericalDigitize
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reprojectCoordinates.py
102 lines (84 loc) · 4.63 KB
/
reprojectCoordinates.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# -*- coding: utf-8 -*-
"""
/***************************************************************************
Numerical digitize - sets up a Qgis actions for append and edit features
by inserting or changing numerical values of vertex's coordinates
A QGIS plugin
-------------------
begin : 2019 year
git sha : $Format:%H$
copyright (C) 2019 Igor Chumichev
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* 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 (QgsPoint, QgsCoordinateReferenceSystem, QgsCoordinateTransform,
QgsProject, QgsWkbTypes, QgsGeometry)
class ReprojectCoordinates:
def __init__(self, fromCRS, toCRS, p_hasZ, p_hasM):
self.hasZ = p_hasZ
self.hasM = p_hasM
self.transformation = QgsCoordinateTransform(fromCRS, toCRS, QgsProject.instance())
def copyCoordstoPoints(self, coords):
if coords is not None:
coordsPoint = list()
for j in range(len(coords)):
element = [coords[j][0], list()]
coordsPoint.append(element)
element = coords[j][1]
for i in range(len(element)):
if not (self.hasZ or self.hasM):
NodePoint = QgsPoint(float(element[i][0]),
float(element[i][1]), None, None, QgsWkbTypes.Point)
elif self.hasZ and not self.hasM:
NodePoint = QgsPoint(float(element[i][0]), float(element[i][1]), float(element[i][2]),
None, QgsWkbTypes.PointZ)
elif not self.hasZ and self.hasM:
NodePoint = QgsPoint(float(element[i][0]), float(element[i][1]), None,
float(element[i][2]), QgsWkbTypes.PointM)
else:
NodePoint = QgsPoint(float(element[i][0]), float(element[i][1]), float(element[i][2]),
float(element[i][3]), QgsWkbTypes.PointZM)
coordsPoint[j][1].append(NodePoint)
return coordsPoint
def copyPointstoCoords(self, coordsPoint):
if coordsPoint is not None:
coordsFloat = list()
for j in range(len(coordsPoint)):
element = [coordsPoint[j][0], list()]
coordsFloat.append(element)
element = coordsPoint[j][1]
for i in range(len(element)):
if not (self.hasZ or self.hasM):
f_tuple = [float(element[i].x()), float(element[i].y())]
elif self.hasZ and not self.hasM:
f_tuple = [float(element[i].x()), float(element[i].y()), float(element[i].z())]
elif not self.hasZ and self.hasM:
f_tuple = [float(element[i].x()), float(element[i].y()), float(element[i].m())]
else:
f_tuple = [float(element[i].x()), float(element[i].y()), float(element[i].z()),
float(element[i].m())]
coordsFloat[j][1].append(f_tuple)
return coordsFloat
def reproject(self, coords, retQgsPoints):
if coords is not None:
coordsPoint = list(self.copyCoordstoPoints(coords))
for j in range(len(coordsPoint)):
element = coordsPoint[j][1]
for i in range(len(element)):
element[i].transform(self.transformation, QgsCoordinateTransform.ForwardTransform, self.hasZ)
if retQgsPoints:
return coordsPoint
else:
return list(self.copyPointstoCoords(coordsPoint))
def reprojectGeometry(self, geom: QgsGeometry) -> QgsGeometry:
if geom is not None:
geom.transform(self.transformation, QgsCoordinateTransform.ForwardTransform, self.hasZ)
return geom