-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfixedge.py
165 lines (149 loc) · 4.69 KB
/
fixedge.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
"""Docstring"""
import numpy as np
import scipy.ndimage
from osgeo import gdal
def make_raster(in_ds, fn, data, data_type, nodata=None):
"""
Export raster using GDAL.
Code borrowed from excellent book 'Geoprocessing with Python'
(Manning Publishing) by Chris Garrard
"""
driver = gdal.GetDriverByName('GTiff')
out_ds = driver.Create(fn, in_ds.RasterXSize, in_ds.RasterYSize, 1, data_type)
out_ds.SetProjection(in_ds.GetProjection())
out_ds.SetGeoTransform(in_ds.GetGeoTransform())
out_band = out_ds.GetRasterBand(1)
if nodata is not None:
out_band.SetNoDataValue(nodata)
out_band.WriteArray(data)
out_band.FlushCache()
out_band.ComputeStatistics(False)
return out_ds
def remove_outside_cells(data, rep_value, edge_id, nodatain):
"""Removes outside cells of raster"""
#Skip if cell is no data
if data[4] == nodatain:
return nodatain
#Skip if cell has been assigned as already removed
elif data[4] == -1:
return -1
else:
edge_pixel = False
for i in data:
if i == edge_id:
edge_pixel = True
if edge_pixel:
return rep_value
else:
return data[4]
def fill_cells1(data, reps):
"""Fills cells back in 1 (filter instructions)"""
if data[4] == -2.0:
if reps > 10:
return max(data)
else:
count = 0
total = 0
for i in data:
if i > -1 and i != 0:
count = count + 1
total = total + i
if count < 2:
return -2.0
else:
try:
av = total/count
except:
print 'av fail'
av = -2
return av
else:
return data[4]
def fill_cells2(data, reps):
"""Fills cells back in 2 (filter instructions)"""
#print 'data[4]=',data[4]
#print 'fill_cells2 reps:',reps
if reps > 15:
if max(data) < 0:
return 0.01
else:
return data[4]
if data[4] == -1.0:
count = 0
total = 0
for i in data:
if i > -1 and i != 0:
count = count + 1
total = total + i
if count < 2:
return -1
else:
try:
av = total/count
except:
av = -1
return av
else:
return data[4]
def fill1(data, number):
"""
Fills cells back in 1
Can run up to to 20 times to make sure no cells are left empty
"""
#print 'min:',scipy.ndimage.minimum(data)
#print 'found?:',str(scipy.ndimage.find_objects([-1]))
reps = 0
while scipy.ndimage.minimum(data) == number:
if reps < 20:
data = scipy.ndimage.filters.generic_filter(
data, fill_cells1, size=3, mode='nearest', extra_arguments=(reps,))
reps += 1
else:
break
return data
def fill2(data, number):
"""Fills cells back in 2"""
#print 'min:',scipy.ndimage.minimum(data)
#print 'found?:',str(scipy.ndimage.find_objects([-1]))
reps = 0
while scipy.ndimage.minimum(data) == number:
if reps < 20:
data = scipy.ndimage.filters.generic_filter(
data, fill_cells2, size=3, mode='nearest', extra_arguments=(reps,))
reps += 1
else:
break
return data
def change_nodata(data, nodatain, nodataout):
"""Set nodata value to whatever"""
if data[4] == nodatain:
return nodataout
else:
return data[4]
def fixedge(in_, out, nodatain=0, nodataout=0):
"""
Main fixedge function.
Deletes outside cells and replaces with new values calculated from neighbours
"""
in_ds = gdal.Open(in_)
in_band = in_ds.GetRasterBand(1)
in_data = in_band.ReadAsArray().astype(np.float32)
#remove single row of outside cells and replace with -1
rep_value = -1.0
edge_id = nodatain
data_ = scipy.ndimage.filters.generic_filter(
in_data, remove_outside_cells, size=3, mode='nearest',
extra_arguments=(rep_value, edge_id, nodatain))
#remove second row of outside cells and replace with -2
rep_value = -2.0
edge_id = -1
data = scipy.ndimage.filters.generic_filter(
data_, remove_outside_cells, size=3, mode='nearest',
extra_arguments=(rep_value, edge_id, nodatain))
d1 = fill1(data,-2)
d2 = fill2(d1,-1)
d3 = scipy.ndimage.filters.generic_filter(
d2, change_nodata, size=3, mode='nearest',
extra_arguments=(nodatain, nodataout))
make_raster(in_ds, out, d3, gdal.GDT_Float32, nodata=nodataout)
del in_ds