-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtargetprepare_algorithm.py
283 lines (231 loc) · 12 KB
/
targetprepare_algorithm.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# -*- coding: utf-8 -*-
"""
/***************************************************************************
ProcessingUMEP
A QGIS plugin
UMEP for processing toolbox
Generated by Plugin Builder: http://g-sherman.github.io/Qgis-Plugin-Builder/
-------------------
begin : 2020-04-02
copyright : (C) 2020 by Fredrik Lindberg
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. *
* *
***************************************************************************/
"""
__author__ = 'Fredrik Lindberg'
__date__ = '2022-02-07'
__copyright__ = '(C) 2020 by Fredrik Lindberg'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from qgis.PyQt.QtCore import QCoreApplication, QVariant
from qgis.core import (QgsProcessing,
QgsProcessingAlgorithm,
QgsProcessingParameterFile,
QgsProcessingParameterString,
QgsProcessingParameterNumber,
QgsProcessingParameterFolderDestination,
QgsProcessingParameterEnum,
QgsProcessingParameterFeatureSource,
QgsProcessingParameterField,
QgsProcessingException,
QgsProcessingParameterBoolean,
QgsVectorLayer)
from qgis.PyQt.QtGui import QIcon
from osgeo import osr
# from osgeo.gdalconst import *
import os
import numpy as np
import inspect
from pathlib import Path
import sys
import json
# from ..util.umep_uwg_export_component import create_uwgdict, get_uwg_file
class ProcessingTARGETPrepareAlgorithm(QgsProcessingAlgorithm):
"""
This algorithm is a processing version of UWG Prepare
"""
INPUT_POLYGONLAYER = 'INPUT_POLYGONLAYER'
ID_FIELD = 'ID_FIELD'
INPUT_MORPH = 'INPUT_MORPH'
INPUT_LC = 'INPUT_LC'
UMEP_LC = 'UMEP_LC'
FRAC_IRR = 'FRAC_IRR'
FRAC_CONC = 'FRAC_CONC'
SITE_NAME = 'SITE_NAME'
OUTPUT_DIR = 'OUTPUT_DIR'
def initAlgorithm(self, config):
self.addParameter(QgsProcessingParameterFeatureSource(self.INPUT_POLYGONLAYER,
self.tr('Vector polygon grid'), [QgsProcessing.TypeVectorPolygon]))
self.addParameter(QgsProcessingParameterField(self.ID_FIELD,
self.tr('ID field'),'', self.INPUT_POLYGONLAYER, QgsProcessingParameterField.Numeric))
self.addParameter(QgsProcessingParameterFile(self.INPUT_MORPH,
self.tr('Building morphology file (.txt)'), extension='txt'))
self.addParameter(QgsProcessingParameterFile(self.INPUT_LC,
self.tr('Land cover fraction file (.txt)'), extension='txt'))
self.addParameter(QgsProcessingParameterBoolean(self.UMEP_LC,
self.tr("Use standard UMEP land cover grid (fractions below is used)"), defaultValue=True))
self.addParameter(QgsProcessingParameterNumber(self.FRAC_IRR,
self.tr('Fraction Irrigated grass taken from Grass land cover class'),
QgsProcessingParameterNumber.Double,
QVariant(0.20), False, minValue=0.0, maxValue=1.0))
self.addParameter(QgsProcessingParameterNumber(self.FRAC_CONC,
self.tr('Fraction Concrete taken from Paved land cover class'),
QgsProcessingParameterNumber.Double,
QVariant(0.25), False, minValue=0.0, maxValue=1.0))
self.addParameter(QgsProcessingParameterString(self.SITE_NAME,
self.tr('Site name')))
self.addParameter(QgsProcessingParameterFolderDestination(self.OUTPUT_DIR,
self.tr('Output folder')))
self.plugin_dir = os.path.dirname(__file__)
def processAlgorithm(self, parameters, context, feedback):
# InputParameters
inputPolygonlayer = self.parameterAsVectorLayer(parameters, self.INPUT_POLYGONLAYER, context)
idField = self.parameterAsFields(parameters, self.ID_FIELD, context)
morphFile = self.parameterAsString(parameters, self.INPUT_MORPH, context)
lcFile = self.parameterAsString(parameters, self.INPUT_LC, context)
umepLC = self. parameterAsBool(parameters, self.UMEP_LC, context)
fracIrr = self.parameterAsDouble(parameters, self.FRAC_IRR, context)
fracConc = self.parameterAsDouble(parameters, self.FRAC_CONC, context)
siteName = self.parameterAsString(parameters, self.SITE_NAME, context)
outputDir = self.parameterAsString(parameters, self.OUTPUT_DIR, context)
if parameters['OUTPUT_DIR'] == 'TEMPORARY_OUTPUT':
if not (os.path.isdir(outputDir)):
os.mkdir(outputDir)
# if not (os.path.isdir(self.plugin_dir + '/tempdata')):
# os.mkdir(self.plugin_dir + '/tempdata')
# temporary fix for mac, ISSUE #15
pf = sys.platform
if pf == 'darwin' or pf == 'linux2' or pf == 'linux':
if not os.path.exists(outputDir + '/' + siteName):
os.makedirs(outputDir + '/' + siteName)
poly_field = idField
vlayer = inputPolygonlayer
nGrids = vlayer.featureCount()
index = 1
feedback.setProgressText("Number of grids to process: " + str(nGrids))
map_units = vlayer.crs().mapUnits()
if not map_units == 0 or map_units == 1 or map_units == 2:
raise QgsProcessingException("Could not identify the map units of the polygon layer CRS.")
arrmat = np.empty((1, 10))
header = 'FID,roof,road,watr,conc,Veg,dry,irr,H,W'
numformat = '%3d, %5.3f, %5.3f, %5.3f, %5.3f, %5.3f, %5.3f, %5.3f, %5.3f, %5.3f'
# Test for metre units in vector CRS
grid_crs = osr.SpatialReference()
grid_crs.ImportFromWkt(vlayer.crs().toWkt())
grid_unit = grid_crs.GetAttrValue('UNIT')
feedback.setProgressText("Length unit of vector layer: " + str(grid_unit))
possible_units_metre = ['metre', 'Metre', 'metres', 'Metres', 'meter', 'Meter', 'meters', 'Meters', 'm']
if not grid_unit in possible_units_metre:
raise QgsProcessingException('Error! Vector polygon projection is not in meters. Please reproject.')
#Get resolution of grid (res)
for feature in vlayer.getFeatures():
geom = feature.geometry()
bb = geom.boundingBox()
res = np.round(bb.xMaximum() - bb.xMinimum())
break
#Saving parameters file
with open(self.plugin_dir + '/parametersfortarget.json', "r") as jsn:
param = json.load(jsn)
param['res']['value'] = res
jsonout = json.dumps(param, indent=4)#'C:/temp/targettests/my_site/parameterstest.json'
with open(outputDir + '/' + siteName + '/parameters.json', "w") as jsn2:
# with open('C:/temp/targettests/my_site/parameterstest2.json'), "c" as jsn2:
jsn2.write(jsonout)
#Start loop of polygon grids
##land cover and morphology
index = 0
for feature in vlayer.getFeatures():
feedback.setProgress(int((index * 100) / nGrids))
if feedback.isCanceled():
feedback.setProgressText("Calculation cancelled")
break
index += 1
feat_id = int(feature.attribute(poly_field[0]))
with open(lcFile) as file:
next(file)
for line in file:
split = [float(x) for x in line.split()]
# split = line.split()
if feat_id == int(split[0]):
if umepLC:
conc = split[1] * fracConc
road = split[1] - conc
irr = split[5] * fracIrr
dry = split [6] + split[5] - irr # bare soil is classed as dry grass
else:
road = split[1]
conc = split[9]
dry = split[5] + split[6] # bare soil is classed as dry grass
irr = split[8]
roof = split[2]
veg = split[3] + split[4]
watr = split[7]
break
with open(morphFile) as file:
next(file)
for line in file:
split = [float(x) for x in line.split()]
# split = line.split()
if feat_id == int(split[0]):
H = split[3]
wai = split[8]
pai = split[1]
if pai == 0:
W = res
else:
HW = (wai*pai)/(2*pai*(1-pai))
W = H / HW
if W > res:
W = res #W cannot be larger than grid resolution
break
arr = [feat_id, roof, road, watr, conc, veg, dry, irr, H, W]
arrmat = np.vstack([arrmat, arr])
arrmatsave = arrmat[1: arrmat.shape[0], :]
#creating folders and saving
if not os.path.exists(outputDir + '/' + siteName + '/' + 'input' + '/' + 'LC'):
os.makedirs(outputDir + '/' + siteName + '/' + 'input' + '/' + 'LC')
if not os.path.exists(outputDir + '/' + siteName + '/' + 'input' + '/' + 'MET'):
os.makedirs(outputDir + '/' + siteName + '/' + 'input' + '/' + 'MET')
np.savetxt(outputDir + '/' + siteName + '/' + 'input' + '/' + 'LC' + '/lc_target.txt', arrmatsave,
fmt=numformat, header=header, comments='')
feedback.setProgressText("Process finished")
return {self.OUTPUT_DIR: outputDir}
def name(self):
return 'Urban Heat Island: TARGET Prepare'
def displayName(self):
return self.tr(self.name())
def group(self):
return self.tr(self.groupId())
def groupId(self):
return 'Pre-Processor'
def shortHelpString(self):
return self.tr('<b>THIS PLUGIN IS EXPERIMENTAL</b>'
'\n'
'<b>TARGET</b> (The Air-temperature Response to Green blue-infrastructure Evaluaition Tool) is a simple modelling framework used to examine '
'intra urban climate. It has specifically been developed as an efficient, easy-to-use model albe to investigate '
'heat mitigation effects of green and blue infrastructure within urban areas but can also be used to model the canopy urban heat island '
'<a href="https://gmd.copernicus.org/articles/12/785/2019/">(Broadbent et al. 2019)</a>. '
'Possibilities to model mutiple grids or a single location is available.\n'
'\n'
'This particular tool prepare input data used for the TARGET model found in the Processor in UMEP for processing.'
'\n'
'----------------------\n'
'Full manual is available via the <b>Help</b>-button.')
def helpUrl(self):
url = "https://umep-docs.readthedocs.io/en/latest/pre-processor/Urban%20Heat%20Island%20TARGET%20Prepare.html"
return url
def tr(self, string):
return QCoreApplication.translate('Processing', string)
def icon(self):
cmd_folder = Path(os.path.split(inspect.getfile(inspect.currentframe()))[0]).parent
icon = QIcon(str(cmd_folder) + "/icons/icon_uwg.png")
return icon
def createInstance(self):
return ProcessingTARGETPrepareAlgorithm()