/
functions.py
203 lines (185 loc) · 8.07 KB
/
functions.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
"""
/***************************************************************************
Name : IBAMA expressions
Description : Set of expressions for QGIS
Date : April, 2015. February, 2018 Migrated for QGIS 3
copyright : (C) 2015 by Luiz Motta
email : motta.luiz@gmail.com
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
Updates/New:
- 2020-04-01:
- Refactored using global
- 2020-03-23:
- Rename: getDateSentinel to getDateSentinel1
- New: getDateSentinel2
- 2020-02-12:
- New: getUTM, getCRSLayer
- Update: getCRSLayer, area_crs
- Remove(exists in default expressions): is_selected, dms_x, dms_y, existsFile, getFileName
"""
__author__ = 'Luiz Motta'
__date__ = '2015-01-01'
__copyright__ = '(C) 2015, Luiz Motta'
__revision__ = '$Format:%H$'
from qgis.PyQt.QtCore import QDate
from qgis.core import (
QgsProject,
QgsCoordinateReferenceSystem, QgsCoordinateTransform, QgsCoordinateTransformContext,
QgsGeometry,
QgsExpression
)
from qgis.utils import qgsfunction
def getCRSLayer(layer_id):
project = QgsProject.instance()
layer = project.mapLayer( layer_id )
return layer.crs()
@qgsfunction(args='auto', group='Ibama', register=False, usesgeometry=True, referenced_columns=[])
def getUTMZone(feature, parent, context):
"""
<h4>Return</h4>UTM zone: Number(longitudinal) and Letter(latitudinal)
<p> Point: The coordinate itself</p>
<p> Line: The coordinate of center</p>
<p> Polygon: The coordinate of centroid</p>
<p><h4>Syntax</h4>getUTMZone()</p>
<p><h4>Example</h4>getUTMZone() --> 20M</p>
"""
geom = feature.geometry().centroid()
layer_id = context.variable('layer_id')
crsLayer = getCRSLayer( layer_id )
crsDest = QgsCoordinateReferenceSystem('EPSG:4326')
if not crsLayer == crsDest:
ct = QgsCoordinateTransform( crsLayer, crsDest, QgsCoordinateTransformContext() )
geom.transform( ct )
point = geom.asPoint()
vlong, vlat = point.x(), point.y()
zone = int( vlong / 6 + 31 )
# Letter
wordLat = 'CDEFGHJKLMNPQRSTUVWX'
lettersLat = { n-10: wordLat[ n ] for n in range( len( wordLat ) ) }
value = int( vlat/8 ) - int( vlat < 0 )
letter = lettersLat[ value ]
return f"{zone}{letter}"
@qgsfunction(args='auto', group='Ibama', register=False, usesgeometry=False, referenced_columns=[])
def getDateLandsat(name, feature, parent):
"""
<h4>Return</h4>QDate from file name of Landsat
<p><h4>Syntax</h4>getDateLandsat(name_landsat)</p>
<p><h4>Argument</h4>File name of Landsat</p>
<p><h4>Example</h4>getDateLandsat('LC81390452014295LGN00.tif')</p>
"""
msgError = f"Enter with landsat name (ex. 'LC81390452014295LGN00'). Value = '{name}'."
if not name[3:16].isdigit():
raise Exception( msgError )
try:
julianYear = QDate( int( name[9:13] ), 1, 1 ).toJulianDay() - 1
julianDays = julianYear + int( name[13:16] )
v_date = QDate.fromJulianDay ( julianDays )
except:
raise Exception( msgError )
if not v_date.isValid():
raise Exception( msgError )
return v_date
@qgsfunction(args='auto', group='Ibama', register=False, usesgeometry=False, referenced_columns=[])
def getDateRapideye(name, feature, parent):
"""
<h4>Return</h4>QDate from file name of Rapideye
<p><h4>Syntax</h4>getDateRapideye(name_rapideye)</p>
<p><h4>Argument</h4>File name of Rapideye</p>
<p><h4>Example</h4>getDateRapideye('2227625_2012-12-26T142009_RE1_3A-NAC_14473192_171826.tif')</p>
"""
msgError = f"Enter with Rapideye name (ex. '2227625_2012-12-26T142009_RE1_3A-NAC_14473192_171826'). Value = '{name}'."
try:
v_date = QDate.fromString( name.split('_')[1][:10], "yyyy-MM-dd" )
except:
raise Exception( msgError )
if not v_date.isValid():
raise Exception( msgError )
return v_date
@qgsfunction(args='auto', group='Ibama', register=False, usesgeometry=False, referenced_columns=[])
def getDateSentinel1(name, feature, parent):
"""
<h4>Return</h4>QDate from file name of Sentinel1
<p><h4>Syntax</h4>getDateSentinel1(name_sentinel)</p>
<p><h4>Argument</h4>File name of Sentinel1</p>
<p><h4>Example</h4>getDateSentinel1('s1a-ew-grd-hh-20141031t223708-20141031t223811-003079-003869-001.tif')</p>
"""
msgError = f"Enter with Sentinel1 name (ex. 's1a-ew-grd-hh-20141031t223708-20141031t223811-003079-003869-001'). Value = '{name}'."
try:
v_date = QDate.fromString( name.split('-')[5][:8], "yyyyMMdd" )
except:
raise Exception( msgError )
if not v_date.isValid():
raise Exception( msgError )
return v_date
@qgsfunction(args='auto', group='Ibama', register=False, usesgeometry=False, referenced_columns=[])
def getDateSentinel2(name, feature, parent):
"""
<h4>Return</h4>QDate from file name of Sentinel2
<p><h4>Syntax</h4>getDateSentinel2(name_sentinel)</p>
<p><h4>Argument</h4>File name of Sentinel2</p>
<p><h4>Example</h4>getDateSentinel2('S2A_MSIL1C_20170105T013442_N0204_R031_T53NMJ_20170105T013443.tif')</p>
"""
msgError = f"Enter with Sentinel2 name (ex. 'S2A_MSIL1C_20170105T013442_N0204_R031_T53NMJ_20170105T013443'). Value = '{name}'."
try:
v_date = QDate.fromString( name.split('_')[2][:8], "yyyyMMdd" )
except:
raise Exception( msgError )
if not v_date.isValid():
raise Exception( msgError )
return v_date
@qgsfunction(args='auto', group='Ibama', register=False, usesgeometry=False, referenced_columns=[])
def getDatePlanetScope(name, feature, parent):
"""
<h4>Return</h4>QDate from file name of PlanetScope
<p><h4>Syntax</h4>getDatePlanetScope(name_planetscope)</p>
<p><h4>Argument</h4>File name of PlanetScope</p>
<p><h4>Example</h4>getDatePlanetScope('PSScene4Band_20190310_131656_0f4a.tif')</p>
"""
msgError = f"Enter with PlanetScope name (ex. 'PSScene4Band_20190310_131656_0f4a'). Value = '{name}'."
try:
v_date = QDate.fromString( name.split('_')[1], "yyyyMMdd" )
except:
raise Exception( msgError )
if not v_date.isValid():
raise Exception( msgError )
return v_date
@qgsfunction(args='auto', group='Ibama', register=False, usesgeometry=True, referenced_columns=[])
def area_crs(crs_id, feature, parent, context):
"""
<h4>Return</h4>Area using the CRS ID
<p><h4>Syntax</h4>area_crs(CRS ID)</p>
<p><h4>Argument</h4>CRS ID</p>
<p><h4>Example</h4>area_crs('EPSG:5671')</p>
"""
geom = QgsGeometry( feature.geometry() )
if not isinstance( crs_id, str):
raise Exception("Enter with ID CRS with string type(Ex.: 'EPSG:4326')")
crsDest = QgsCoordinateReferenceSystem( crs_id )
if not crsDest.isValid():
msg = "ID EPSG '{}' is not valid".format( crs_id )
raise Exception(msg)
if crsDest.isGeographic():
msg = "ID CRS '{}' is Geographic".format( crs_id )
raise Exception(msg)
layer_id = context.variable('layer_id')
crsLayer = getCRSLayer( layer_id )
if not crsDest == crsLayer:
ct = QgsCoordinateTransform( crsLayer, crsDest, QgsCoordinateTransformContext() )
geom.transform( ct )
return geom.area()
def registerFunctions(isRegister=True):
t_register = ( QgsExpression.registerFunction, lambda f: f )
u_register = ( QgsExpression.unregisterFunction, lambda f: f.name() )
( funcReg, funcArg ) = t_register if isRegister else u_register
g = globals()
l_func = ( g[v] for v in g if hasattr(g[v], 'usesGeometry') )
for f in l_func:
funcReg( funcArg( f ) )