-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFilterDEM.py
195 lines (153 loc) · 4.69 KB
/
FilterDEM.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
# -*- coding: cp1251 -*-
import arcpy
import sys
import traceback
import math
import numpy
shift = []
window = []
noData = -9999
# 2017, Timofey Samsonov, Lomonosov Moscow State University
def sample_window(raster, i, j):
global window
global shift
global noData
w = 0
for k in shift:
ik = i+k
for l in shift:
jl = j+l
if raster[ik, jl] != noData:
window[w] = raster[ik, jl]
w += 1
elems = window[0:w]
return elems
# nfilt = 0
def calc_lower_quartile(raster, i, j):
elems = sample_window(raster, i, j)
elems.sort()
n = int(math.floor(len(elems)/4))
return 0.5*(elems[n]+elems[n-1])
# nfilt = 1
def calc_upper_quartile(raster, i, j):
elems = sample_window(raster, i, j)
elems.sort(reverse=True)
n = int(math.floor(len(elems)/4))
return 0.5*(elems[n]+elems[n-1])
# nfilt = 2
def calc_min(raster, i, j):
elems = sample_window(raster, i, j)
value = min(elems)
return value
# nfilt = 3
def calc_max(raster, i, j):
elems = sample_window(raster, i, j)
value = max(elems)
return value
# nfilt = 4
def calc_mean(raster, i, j):
elems = sample_window(raster, i, j)
sum = 0
for k in range(len(elems)):
sum += elems[k]
value = sum/len(elems)
return value
# nfilt = 5
def calc_median(raster, i, j):
elems = sample_window(raster, i, j)
elems.sort()
n = len(elems)
if n == 0:
return noData
elif n%2 == 0:
return elems[n//2-1]
else:
return (elems[n//2-1] + elems[n//2])*0.5
# Filter selector
filters = {0: calc_lower_quartile,
1: calc_upper_quartile,
2: calc_min,
3: calc_max,
4: calc_mean,
5: calc_median
}
def extend_array(array, nx, ny, value):
ni = array.shape[0]
nj = array.shape[1]
extarray = numpy.empty((ni + ny, nj + nx))
extarray.fill(value)
for i in range(ni):
for j in range(nj):
extarray[i, j] = array[i, j]
return extarray
def process_raster(inraster, niter, nfilt):
global noData
ni = inraster.shape[0]
nj = inraster.shape[1]
arcpy.SetProgressor("step", "Processing rows", 0, ni-1, 1)
raster = inraster
outraster = numpy.zeros((ni, nj))
for k in range(niter):
raster = extend_array(raster, len(shift) - 1, len(shift) - 1, noData)
arcpy.AddMessage("Iteration " + str(k+1))
for i in range(ni):
arcpy.SetProgressorLabel("Processing row " + str(i) + " from " + str(ni))
for j in range(nj):
if raster[i, j] == noData:
outraster[i, j] = raster[i, j]
else:
outraster[i, j] = filters[nfilt](raster, i, j)
arcpy.SetProgressorPosition(i)
raster = outraster
return outraster
def execute(inraster, outraster, wsize, niter, qtype):
global window
global shift
global noData
shift = [i for i in range(-math.trunc(wsize / 2), math.trunc(wsize / 2) + 1)]
corrected_size = 2*math.trunc(wsize / 2) + 1
window = [0 for i in range(corrected_size**2)]
# Select the appropriate filter number
nfilt = 0
if qtype == "Lower Quartile":
nfilt = 0
elif qtype == "Upper Quartile":
nfilt = 1
elif qtype == "Min":
nfilt = 2
elif qtype == "Max":
nfilt = 3
elif qtype == "Mean":
nfilt = 4
elif qtype == "Median":
nfilt = 5
else:
nfilt = 4
# Process: RasterToASCII_conversion
rasternumpy = arcpy.RasterToNumPyArray(inraster)
# Calculating quartiles
arcpy.AddMessage("Filtering raster...")
newrasternumpy = process_raster(rasternumpy, niter, nfilt)
desc = arcpy.Describe(inraster)
lowerleft = arcpy.Point(desc.extent.XMin, desc.extent.YMin)
cellsize = desc.meanCellWidth
crs = desc.spatialReference
# Convert python list to ASCII
arcpy.AddMessage("Writing output...")
outinnerraster = arcpy.NumPyArrayToRaster(newrasternumpy, lowerleft, cellsize)
arcpy.DefineProjection_management(outinnerraster, crs)
outinnerraster.save(outraster)
if __name__ == "__main__":
try:
inRaster = arcpy.GetParameterAsText(0)
outRaster = arcpy.GetParameterAsText(1)
wSize = int(arcpy.GetParameterAsText(2))
nIter = int(arcpy.GetParameterAsText(3))
qType = arcpy.GetParameterAsText(4)
execute(inRaster, outRaster, wSize, nIter, qType)
except:
tb = sys.exc_info()[2]
tbinfo = traceback.format_tb(tb)[0]
pymsg = "Traceback Info:\n" + tbinfo + "\nError Info:\n " + \
str(sys.exc_type)+ ": " + str(sys.exc_value) + "\n"
arcpy.AddError(pymsg)