-
Notifications
You must be signed in to change notification settings - Fork 0
/
apply_segments.py
369 lines (286 loc) · 13.4 KB
/
apply_segments.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
from osgeo import ogr
import numpy as np
from numpy import random, dstack
import random
import os
import sys
import csv
import subprocess
import glob
import gdal
from gdalconst import *
import dir_util as df
from apply_segments_global_var import *
'''
integrates naip image segmentation results with landsat land cover
classification. uses simple majority rule on land cover over segment boundaries.
Segmented map is mosaicked, sieved, and remapped to final labels.
input: pixel-based classification results from c5 and segments (vector)
output: segmented map with 4-digit land cover codes
'''
wd='C:/classification/segmentation/segments/'
map_file='C:/classification/mosaicking/final_mosaics/level1/statewide_level1.tif'
out_file='C:/classification/maps/statewide/level1/statewide_level1_map.tif'
remap_file='C:/classification/maps/final_footprint/level1/pr026029_1305b_20151125_remap_values.txt'
'''_____________________________________________________________DEFINE FUNCTIONS'''
def resampleMap(map_file, divisor):
'''downsample map resolution by divisor'''
outmap = map_file.split('.')[0]+'_rs.tif'
in_map = gdal.Open(map_file)
geotransform = in_map.GetGeoTransform()
x_res = str(geotransform[1] / divisor)
y_res = str(geotransform[5] / divisor)
del inmap
resample = 'gdalwarp -tr ' + xres + ' ' + yres + ' ' + map_file + ' ' + outmap
print 'resampling to x: {0:s} y:{1:s} '.format(x_res, y_res)
os.system(resample)
return outmap
def gme_isectpolyrst(polygon, raster):
'''call GME isectpolyrst to intersect segments with map'''
file_name = wd+'tmp.txt'
newFile = 'isectpolyrst(in="' + polygon + '", raster="' + raster + '", prefix="lev", thematic=TRUE, proportion=TRUE, metrics="CNT", allowpartialoverlap=TRUE, medquant=FALSE);'
newFileObj = open(file_name, 'w')
newFileObj.write(newFile)
newFileObj.close()
print "Intersecting polygons with raster..."
subprocess.call([r'C:\Program Files (x86)\SpatialEcology\GME\SEGME.exe', '-c', 'run(in="' + file_name + '");'])
os.remove(file_name)
def gme_ptinpoly(polygon, point, where):
'''call GME genpointinpoly to generate points in unlabeled polygons'''
file_name = wd+'tmp.txt'
newFile = 'genpointinpoly(in="' + polygon + '", out="' + point + '", position="LABEL", copyfields=TRUE, ' + where + ');'
newFileObj = open(file_name, 'w')
newFileObj.write(newFile)
newFileObj.close()
print "Points in polygons..."
subprocess.call([r'C:\Program Files (x86)\SpatialEcology\GME\SEGME.exe', '-c', 'run(in="' + file_name + '");'])
os.remove(file_name)
def gme_isectptrst(points, raster):
'''intersect point with raster with gme'''
file_name = wd+'tmp.txt'
command = 'isectpntrst(in="' + points + '", raster="' + raster + '", field="label", update=TRUE);'
newFileObj = open(file_name, 'w')
newFileObj.write(command)
newFileObj.close()
print "Intersect points with raster..."
subprocess.call([r'C:\Program Files (x86)\SpatialEcology\GME\SEGME.exe', '-c', 'run(in="' + file_name + '");'])
os.remove(file_name)
def nearestPoint(points, compare_pt):
'''Get nearest point'''
file_name = wd+'tmp.txt'
outf = points.split('.')[0]+'dist.csv'
nn = points.split('.')[0]+'nn.csv'
command = 'pointdistances(in="' + points + '", fld="RefID", out="'+outf+'", in2="' + compare_pt + '", fld2="RefId", nearest=1, nout="'+nn+'");'
print command
newFileObj = open(file_name, 'w')
newFileObj.write(command)
newFileObj.close()
subprocess.call([r'C:\Program Files (x86)\SpatialEcology\GME\SEGME.exe', '-c', 'run(in="' + file_name + '");'])
os.remove(file_name)
return nn
def countFeatures(shapefile):
'''count features in shapefile'''
ds = ogr.Open(shapefile, 0)
layer = ds.GetLayer()
num_feat = layer.GetFeatureCount()
del ds
layer=None
return num_feat
def nearestLabel(point, compare_pt, nn_file, dic):
'''transcribe nearest label between points from csv of nearest neighbors'''
ds = ogr.Open(point, update=0)
pointf = ds.GetLayer()
sql = 'SELECT RefID FROM ' +point.split('/')[-1].split('.')[0]+' WHERE label=254'
replacef = ds.ExecuteSQL(sql)
replace=[]
for x,y in enumerate(replacef):
replace.append(y.GetField(0))
print 'replace', replace
if replace:
nns=[] #nearest neighbors for all points
with open(nn_file, 'rU') as infile:
in_nn=csv.reader(infile, delimiter=',')
for row in in_nn:
nns.append(row)
del ds
pointf=None
ds=ogr.Open(point, update=0)
layer=ds.GetLayer()
num_feat=layer.GetFeatureCount()
ds2=ogr.Open(allpt, update=0)
layer2=ds2.GetLayer()
for i in range(num_feat):
feature=layer.GetFeature(i)
fid=feature.GetFieldAsString("RefID")
if int(fid) in replace:
nn=[x[1] for x in nns if x[0]==fid]
print nn
nsql='SELECT RefId, label FROM ' + compare_pt.split('/')[-1].split('.')[0] + ' WHERE RefId='+nn[0]
nn_label=ds2.ExecuteSQL(nsql)
for i, j in enumerate(nn_label):
fid=int(fid)
dic[fid]=int(j.GetField(1))
return dic
def update_dict(polygon, point):
'''get update label values from point file'''
update_dict={}
ds = ogr.Open(point, update=0)
pointf = ds.GetLayer()
sql = 'SELECT RefID, label FROM '+ point.split('/')[-1].split('.')[0]+' WHERE label<255'
att = ds.ExecuteSQL(sql)
if att:
for i, j in enumerate(att):
update_dict[j.GetField(0)]=int(j.GetField(1))
return update_dict
def updateLabel(layer, dic):
'''Update shapefile 'label' field according to points'''
keys=dic.keys()
feature = layer.GetNextFeature()
keys=[x for x in dic.keys()]
while feature:
refid = feature.GetFieldAsInteger("ID")
if refid in keys:
feature.SetField("label", dic[refid])
layer.SetFeature(feature)
feature = layer.GetNextFeature()
print len(keys), ' features updated.'
def gid(layer, fieldname):
'''serial another field'''
num_features = layer.GetFeatureCount()
print num_features
for i in range(num_features):
feature = layer.GetFeature(i)
if feature:
feature.SetField(fieldname, i)
layer.SetFeature(feature)
def calcMajority(layer, classes):
'''calcualte majority over values in column# start to column# end'''
num_features = layer.GetFeatureCount()
for i in range(num_features):
feature = layer.GetFeature(i)
zonal = [feature.GetField(x) for x in classes]
max_cnt = max(zonal)
labels=[a for a,b in enumerate(zonal) if b==max_cnt]
#resolve ties
if len(labels)==1:
label=labels[0]
elif len(labels)>1 and max_cnt>0:
label=labels[random.randrange(0,len(labels))] #if tied, take random
else: label=255 #no data
feature.SetField('label', label)
layer.SetFeature(feature)
source = None
print 'shapefile updated'
def overruleMaj(grids, mask):
'''overrule majority if segment intersects another raster'''
def getFieldNames(layer):
'''get field names from shapefile layer'''
layer_defn = layer.GetLayerDefn()
field_names = [layer_defn.GetFieldDefn(i).GetName() for i in range(layer_defn.GetFieldCount())]
return field_names
def dissolvePolys(out_file, infile, fieldname):
'''dissolve polygons on fieldname'''
inlayer = infile.split('/')[-1].split('.')[0]
dissolve = "ogr2ogr " + out_file + " " + infile + ' -dialect sqlite -sql \
"SELECT ST_UNION(geometry), ' + fieldname + " FROM '" + inlayer + "' GROUP BY " + fieldname + '"'
print 'Dissolving...'
os.system(dissolve)
def addIntField(layer, fieldname):
'''add integer type field to shapefile'''
print "adding new field, '", fieldname, "'"
new_field = ogr.FieldDefn(fieldname, ogr.OFTInteger)
layer.CreateField(new_field)
def rasterizeSeg(infile, out_file, fieldname):
'''Rasterize shapefile to 30m raster'''
rasterize = 'gdal_rasterize -a ' + fieldname + ' -tr 30 30 -ot Byte -a_nodata 255 ' + infile + ' ' + out_file
print 'Rasterizing segments...'
os.system(rasterize)
def mosaicGrids(out_file, infiles):
'''mosaic rasterized segment grids to statewide'''
mosaic = 'gdal_merge.py -o ' + out_file + ' -of GTiff -n 255 -a_nodata 255 ' + ' '.join(infiles)
print 'Mosaicking grids...'
os.system(mosaic)
def sieveMap(infile, out_file):
'''sieve classification results to reinforce 2 acre MMU'''
sieve='gdal_sieve.py -st 9 -8 ' + infile + ' ' + out_file
print sieve
os.system(sieve)
def remapRaster(infile, out_file, lookup):
'''remap raster values to those in lookup table'''
inmap = gdal.Open(infile)
rows = inmap.RasterYSize
cols = inmap.RasterXSize
map_arr = inmap.ReadAsArray()
#remap values
remap_dict = df.getDictfromCSV(lookup,'\t',1,0)
remap_dict[0]=2000 #ag
remap_dict[255]=32767 #nodata
map_out = map_arr.astype(np.int16)
print 'input map labels', np.unique(map_out)
for r in remap_dict:
print 'reclassifying', r, ': ', remap_dict[r]
outval=int(remap_dict[r])
temp=np.equal(map_out, int(r))
np.putmask(map_out, temp, int(remap_dict[r]))
temp=None
print 'output map labels', np.unique(map_out)
#output raster
driver=inmap.GetDriver()
outDs = driver.Create(out_file, cols, rows, 1, GDT_Int16)
outDs.SetGeoTransform(inmap.GetGeoTransform())
outDs.SetProjection(inmap.GetProjection())
outband = outDs.GetRasterBand(1)
outband.WriteArray(map_out, 0 ,0)
outband.SetNoDataValue(32767)
outband.FlushCache()
'''______________________________________________________________________________'''
shapefiles=glob.glob(wd+'/**/poly*.shp')
grid_dir=wd+'segmented_grids'
segmented_map=map_file.split('.')[0]+'_segmented.tif'
##skip this -- testing shows insignificant difference in results
##rsmap = map_file.split('.')[0]+'_rs.tif'
##rsmap = resampleMap(map_file, 4)
if not os.path.exists(grid_dir): os.mkdir(grid_dir)
for shapefile in shapefiles:
grid=shapefile.split('/')[-1][4:-4] #grid number from filename
point=wd+'point'+grid+'.shp'
allpt=wd+'all'+grid+'.shp'
print 'Processing Grid, ', grid
'''_____________________________________________________ZONAL STATISTICS OVER MAP'''
if not point in glob.glob(wd+'/*'):
print 'points for ', grid
gme_isectpolyrst(shapefile, map_file)
'''__________________________________________________OPEN VECTOR FILE AND ADD FIELD'''
source = ogr.Open(shapefile, 1)
layer = source.GetLayer()
addIntField(layer, "label")
field_names = getFieldNames(layer)
maj_class = [x for x in field_names if 'levV' in x][:-2] #labels for majority (skip 254, sum)
'''_______________________________________________________CALCULATE MAJORITY LABEL'''
if 'label_1' in field_names:
print '"label" field already exists for grid ' + str(grid)
sys.exit()
else:
print 'CLASS LABELS FOR MAJORITY: ', ' '.join([x for x in maj_class])
calcMajority(layer, maj_class) #calc majority, skipping first and last two columns (ID, 254, sum)
'''_____________________________________________GET LABEL FOR UNLABELED POLYGONS'''
gme_ptinpoly(shapefile, point, 'where=""label"=255"')
if countFeatures(point) > 0:
gme_ptinpoly(shapefile, allpt, 'where=""label"<255"')
gme_isectptrst(point, map_file)
nearestPoint(point, allpt)
subdict = update_dict(shapefile, point)
subdict = nearestLabel(point, allpt, point.split('.')[0]+'nn.csv', subdict)
updateLabel(layer, subdict)
'''_____________________________________________________________________RASTERIZE'''
outrast = grid_dir + '/grid' + grid + '.tif'
rasterizeSeg(shapefile, outrast, 'label')
'''________________________________________________________________________________MOSAIC'''
mosaicGrids(segmented_map, glob.glob(grid_dir+'/*.tif'))
'''_________________________________________________________________________________SIEVE'''
sieved_map=segmented_map.split('.')[0]+'_sieved.tif'
sieveMap(segmented_map, sieved_map)
'''_________________________________________________________CONVERT DATA TYPE AND RELABEL'''
remap=remapRaster(sieved_map, out_file,remap_file)
edit = "gdal_edit.py -a_srs EPSG:3071 " + out_file #add projection info to metadata
os.system(edit)