-
Notifications
You must be signed in to change notification settings - Fork 7
/
algorithm.py
370 lines (299 loc) · 14.2 KB
/
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# -*- coding: utf-8 -*-
"""
Contains both change detection and report generation algorithms.
.. note:: 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 3 of the License, or
(at your option) any later version.
"""
__author__ = 'Damián Silvani'
__date__ = '2018-06-26'
__copyright__ = '(C) 2018 by Dymaxion Labs'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'
from PyQt4.QtCore import QSettings, QCoreApplication, QTranslator
from qgis.core import QgsVectorFileWriter, QgsMessageLog, QgsMapLayerRegistry
from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.ProcessingLog import ProcessingLog
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterRaster, ParameterVector, ParameterBoolean, ParameterNumber, ParameterSelection, ParameterTableField
from processing.core.outputs import OutputRaster, OutputVector, OutputTable
from processing.tools import dataobjects, vector
from osgeo import gdal
from osgeo.gdalconst import *
import numpy as np
import cv2
import rasterio
import rasterio.mask
import fiona
import os
from shapely.geometry import shape, box
class Algorithm(GeoAlgorithm):
def __init__(self):
GeoAlgorithm.__init__(self)
self.pluginDir = os.path.dirname(os.path.realpath(__file__))
self._load_translations()
def _load_translations(self):
full_locale = QSettings().value("locale/userLocale")
if not full_locale:
return
locale = full_locale[0:2]
localePath = os.path.join(self.pluginDir, 'i18n', '{}.qm'.format(locale))
if os.path.exists(localePath):
translator = QTranslator()
translator.load(localePath)
QCoreApplication.installTranslator(translator)
class MultibandDifferenceAlgorithm(Algorithm):
"""
This algorithm applies the image difference algorithm over each band in
the raster.
"""
INPUT_A_LAYER = 'INPUT_A_LAYER'
INPUT_B_LAYER = 'INPUT_B_LAYER'
OUTPUT_RASTER_LAYER = 'OUTPUT_RASTER_LAYER'
AUTO_THRESHOLD = 'AUTO_THRESHOLD'
THRESHOLD = 'THRESHOLD'
FILTER = 'FILTER'
FILTER_TYPES = ['NONE', 'MEDIAN', 'GAUSSIAN']
FILTER_KERNEL_SIZE = 'FILTER_KERNEL_SIZE'
def defineCharacteristics(self):
self.name = self.tr('Multiband difference')
self.group = self.tr('Pixel-based algorithms')
# Main parameters
self.addParameter(ParameterRaster(self.INPUT_A_LAYER,
self.tr('Input old layer'), [ParameterRaster], False))
self.addParameter(ParameterRaster(self.INPUT_B_LAYER,
self.tr('Input new layer'), [ParameterRaster], False))
# Threshold parameters
self.addParameter(ParameterBoolean(
self.AUTO_THRESHOLD,
self.tr('Use automatic thresholding'),
True))
self.addParameter(ParameterNumber(
self.THRESHOLD,
self.tr('Threshold value (if not automatic)'),
0.0, 1.0, 0.5))
# Filter parameters
self.addParameter(ParameterSelection(
self.FILTER,
self.tr('Filter type'),
self.FILTER_TYPES, 1))
self.addParameter(ParameterNumber(self.FILTER_KERNEL_SIZE,
self.tr('Filter kernel size'),
2.0, None, 3.0))
self.addOutput(OutputRaster(self.OUTPUT_RASTER_LAYER,
self.tr('CD raster')))
def processAlgorithm(self, progress):
self.progress = progress
# The first thing to do is retrieve the values of the parameters
# entered by the user
inputAFilename = self.getParameterValue(self.INPUT_A_LAYER)
inputBFilename = self.getParameterValue(self.INPUT_B_LAYER)
outputFilename = self.getOutputValue(self.OUTPUT_RASTER_LAYER)
if inputAFilename == inputBFilename:
raise GeoAlgorithmExecutionException(
self.tr('You must use two different raster images for inputs A and B'))
threshold = self.getParameterValue(self.THRESHOLD)
autoThreshold = self.getParameterValue(self.AUTO_THRESHOLD)
if autoThreshold:
threshold = None
filterType = self.FILTER_TYPES[self.getParameterValue(self.FILTER)]
if filterType == 'NONE':
filterType = None
kernelSize = self.getParameterValue(self.FILTER_KERNEL_SIZE)
# Open and assign the contents of the raster file to a dataset
datasetA = gdal.Open(inputAFilename, GA_ReadOnly)
datasetB = gdal.Open(inputBFilename, GA_ReadOnly)
progress.setInfo(self.tr('Reading rasters into arrays'))
arrayA = self._readIntoArray(datasetA)
arrayB = self._readIntoArray(datasetB)
# Calculate image difference on each band
cds = []
bandCount = arrayA.shape[0]
for i in range(bandCount):
progress.setInfo(self.tr('Calculate image difference on band {}').format(i+1))
cd = self._detectChanges(arrayA[i], arrayB[i],
threshold=threshold,
filterType=filterType,
kernelSize=kernelSize)
cds.append(cd)
cds = np.array(cds)
# Generate new change detection raster based on results on each band
out = (np.any(cds > 0, axis=0) * 255).astype(np.uint8)
if not np.any(out):
raise GeoAlgorithmExecutionException(
self.tr('No changed detected. Try to use a lower threshold value or different images'))
# Create output raster dataset
driver = gdal.GetDriverByName('GTiff')
outDataset = driver.Create(outputFilename,
datasetA.RasterXSize,
datasetA.RasterYSize,
1,
gdal.GDT_Byte)
# Write output band
outband = outDataset.GetRasterBand(1)
outband.WriteArray(out)
outband.SetNoDataValue(0)
outband.FlushCache()
# Check if there is geotransformation or geoprojection
# in the input raster and set them in the resulting dataset
if datasetA.GetGeoTransform() != None:
outDataset.SetGeoTransform(datasetA.GetGeoTransform())
if datasetA.GetProjection() != None:
outDataset.SetProjection(datasetA.GetProjection())
# Clean resources
datasetA = datasetB = None
outDataset = None
def _readIntoArray(self, dataset):
"""Return a numpy array from a GDAL dataset"""
bands = []
for i in xrange(dataset.RasterCount):
band = dataset.GetRasterBand(i+1).ReadAsArray(0, 0,
dataset.RasterXSize,
dataset.RasterYSize)
bands.append(band)
return np.array(bands)
def _normalize(self, img):
vmin, vmax = img.min(), img.max()
norm_img = (img - vmin) / (vmax - vmin)
return norm_img
def _difference(self, a, b):
a = a.astype(np.int32)
b = b.astype(np.int32)
mean_a, mean_b = a.mean(), b.mean()
std_a, std_b = a.std(), b.std()
b_norm = ((std_a / std_b) * (b - np.ones(b.shape) * mean_b)) + mean_a
return np.abs(a - b_norm)
def _threshold(self, src, tau):
return (((src > 0) * (src >= tau)) * 255).astype(np.uint8)
def _otsuThreshold(self, src):
src = (src * 255).astype(np.uint8)
_, dst = cv2.threshold(src, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
return dst
def _medianFilter(self, src, kernel_size=3):
if kernel_size % 2 == 0:
raise GeoAlgorithmExecutionException(
self.tr('Kernel size for median filter must be an odd number'))
return cv2.medianBlur(src, kernel_size)
def _gaussFilter(self, src, kernel_size=3):
return cv2.GaussianBlur(src, (kernel_size, kernel_size), 1, 1)
def _detectChanges(self, img1, img2, threshold=None, filterType=None, kernelSize=3):
res = self._difference(img1, img2)
res = self._normalize(res)
if threshold:
res = self._threshold(res, threshold)
self.progress.setInfo(
self.tr('Applied manual threshold of value {}'.format(threshold)))
else:
res = self._otsuThreshold(res)
self.progress.setInfo('Applied Otsu threshold')
if filterType == 'GAUSSIAN':
res = self._gaussFilter(res, kernelSize)
elif filterType == 'MEDIAN':
res = self._medianFilter(res, kernelSize)
else:
raise GeoAlgorithmExecutionException(self.tr('Unhandled filter type: {}').format(filterType))
if filterType:
self.progress.setInfo('Applied {} filter with kernel size {}'.format(filterType, kernelSize))
return res
class GenerateVectorAlgorithm(Algorithm):
INPUT_LOTS_LAYER = 'INPUT_LOTS_LAYER'
INPUT_LOT_ID_FIELD = 'INPUT_LOT_ID_FIELD'
INPUT_CD_LAYER = 'INPUT_CD_LAYER'
INPUT_IMG_LAYER = 'INPUT_IMG_LAYER'
OUTPUT_VECTOR_LAYER = 'OUTPUT_VECTOR_LAYER'
OUTPUT_TABLE_LAYER = 'OUTPUT_TABLE_LAYER'
SELECTION_THRESHOLD = 'SELECTION_THRESHOLD'
def defineCharacteristics(self):
self.name = self.tr('Generate changed lots data')
self.group = self.tr('Report')
self.addParameter(ParameterRaster(self.INPUT_CD_LAYER,
self.tr('Input change detection layer'), [ParameterRaster], False))
self.addParameter(ParameterRaster(self.INPUT_IMG_LAYER,
self.tr('Input image layer'), [ParameterRaster], False))
self.addParameter(ParameterVector(self.INPUT_LOTS_LAYER,
self.tr('Input Lots vector layer'), [ParameterVector.VECTOR_TYPE_ANY], False))
self.addParameter(ParameterTableField(self.INPUT_LOT_ID_FIELD,
self.tr('Lot id field'), self.INPUT_LOTS_LAYER))
self.addParameter(ParameterNumber(
self.SELECTION_THRESHOLD,
self.tr('Lot selection threshold value'),
0.0, 1.0, 0.5))
self.addOutput(OutputVector(self.OUTPUT_VECTOR_LAYER,
self.tr('CD vector')))
self.addOutput(OutputTable(self.OUTPUT_TABLE_LAYER,
self.tr('CD table')))
def processAlgorithm(self, progress):
cdFilename = self.getParameterValue(self.INPUT_CD_LAYER)
imgFilename = self.getParameterValue(self.INPUT_IMG_LAYER)
lotsFilename = self.getParameterValue(self.INPUT_LOTS_LAYER)
lotIdFieldName = self.getParameterValue(self.INPUT_LOT_ID_FIELD)
selectionThreshold = self.getParameterValue(self.SELECTION_THRESHOLD)
outputTable = self.getOutputFromName(self.OUTPUT_TABLE_LAYER)
columns = ['lot_id', 'change', 'area', 'changed_area', 'change_perc']
writer = outputTable.getTableWriter(columns)
outputVector = self.getOutputValue(self.OUTPUT_VECTOR_LAYER)
with fiona.open(lotsFilename) as lotsDs, rasterio.open(cdFilename) as cdDs, rasterio.open(imgFilename) as imgDs:
if lotsDs.crs != cdDs.crs:
raise GeoAlgorithmExecutionException(self.tr('Lots vector file has different CRS than rasters: {} != {}').format(lotsDs.crs, cdDs.crs))
total = 100.0 / len(lotsDs) if len(lotsDs) > 0 else 1
progress.setInfo(self.tr('Processing lot features...'))
invalidGeomCount = 0
newSchema = lotsDs.schema.copy()
newSchema['properties']['changed_area'] = 'float'
newSchema['properties']['change_perc'] = 'float'
kwargs = dict(driver=lotsDs.driver,
crs=lotsDs.crs,
schema=newSchema)
bbox = box(*cdDs.bounds)
with fiona.open(outputVector, 'w', **kwargs) as dst:
for i, feat in enumerate(lotsDs):
progress.setPercentage(int(i * total))
lotId = feat['properties'][lotIdFieldName]
# Skip features with invalid geometries
if not feat['geometry']:
continue
# Skip features that are not inside rasters bounds
poly = shape(feat['geometry'])
if not bbox.contains(poly):
continue
# Calculate change percentage
try:
cdImg, _ = rasterio.mask.mask(cdDs, [feat['geometry']], crop=True)
img, _ = rasterio.mask.mask(imgDs, [feat['geometry']], crop=True)
except ValueError as err:
progress.setText(
self.tr('Error on lot id {}: {}. Skipping').format(lotId, err))
continue
# Skip features with no pixels in raster (too low resolution?)
totalPixels = np.sum(img[0] > 0)
if totalPixels == 0:
progress.setText(
self.tr('Lot {} has no pixels? Skipping...').format(lotId))
continue
count = np.sum(cdImg[0] > 0)
perc = count / float(totalPixels)
changeDetected = perc >= selectionThreshold
# Calculate areas
poly = shape(feat['geometry'])
area = poly.area
changedArea = poly.area * perc
# Build row for table
row = {}
row['lot_id'] = lotId
if changeDetected:
row['change'] = 'Y'
else:
row['change'] = 'N'
row['area'] = float(area)
row['changed_area'] = float(changedArea)
row['change_perc'] = float(perc)
writer.addRecord([row[k] for k in columns])
# Add feature in output vector file on change
if changeDetected:
newFeat = feat.copy()
newFeat['properties'] = newFeat['properties'].copy()
newFeat['properties']['changed_area'] = float(changedArea)
newFeat['properties']['change_perc'] = float(perc)
dst.write(newFeat)
del writer