-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
51 lines (43 loc) · 2.37 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
import math
from qgis.core import QgsUnitTypes, QgsPointXY, QgsCoordinateReferenceSystem, QgsDistanceArea
from qgis.PyQt.QtCore import QCoreApplication
def tr(string):
return QCoreApplication.translate('@default', string)
DISTANCE_LABELS = [tr("Kilometers"), tr("Meters"), tr("Centimeters"), tr("Miles"), tr('Yards'), tr("Feet"), tr("Inches"), tr("Nautical Miles")]
def conversionToMeters(units):
if units == 0: # Kilometers
measureFactor = 1000.0
elif units == 1: # Meters
measureFactor = 1.0
elif units == 2: # Centimeters
measureFactor = QgsUnitTypes.fromUnitToUnitFactor(QgsUnitTypes.DistanceCentimeters, QgsUnitTypes.DistanceMeters)
elif units == 3: # Miles
measureFactor = QgsUnitTypes.fromUnitToUnitFactor(QgsUnitTypes.DistanceMiles, QgsUnitTypes.DistanceMeters)
elif units == 4: # Yards
measureFactor = QgsUnitTypes.fromUnitToUnitFactor(QgsUnitTypes.DistanceYards, QgsUnitTypes.DistanceMeters)
elif units == 5: # Feet
measureFactor = QgsUnitTypes.fromUnitToUnitFactor(QgsUnitTypes.DistanceFeet, QgsUnitTypes.DistanceMeters)
elif units == 6: # Inches
measureFactor = QgsUnitTypes.fromUnitToUnitFactor(QgsUnitTypes.DistanceFeet, QgsUnitTypes.DistanceMeters) / 12.0
elif units == 7: # Nautical Miles
measureFactor = QgsUnitTypes.fromUnitToUnitFactor(QgsUnitTypes.DistanceNauticalMiles, QgsUnitTypes.DistanceMeters)
return measureFactor
epsg4326 = QgsCoordinateReferenceSystem("EPSG:4326")
nmToMeters = QgsUnitTypes.fromUnitToUnitFactor(QgsUnitTypes.DistanceNauticalMiles, QgsUnitTypes.DistanceMeters)
metersToFeet = QgsUnitTypes.fromUnitToUnitFactor(QgsUnitTypes.DistanceMeters, QgsUnitTypes.DistanceFeet)
degToMeters = nmToMeters * 60
metersToDeg = 1 / degToMeters
# vector addition of two XY points
def addPoints(a, b):
return QgsPointXY(a.x() + b.x(), a.y() + b.y())
# vector difference of two XY points
def diffPoints(a, b):
return QgsPointXY(a.x() - b.x(), a.y() - b.y())
# Return a vector having the given distance along a bearing, originating at the point `p`.
# The resulting vector is expressed in degree units, in a spherical projection
# and is adjusted for the latitude of its origin.
def projectBearing(p, distance, bearing):
r = math.radians(bearing)
d = distance * metersToDeg
n = QgsPointXY(d * math.sin(r) / math.cos(math.radians(p.y())), d * math.cos(r))
return n