-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
402 lines (367 loc) · 13.8 KB
/
test.py
File metadata and controls
402 lines (367 loc) · 13.8 KB
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# -*- coding: utf-8 -*-
"""
Created on Sun Nov 8 19:56:08 2020
@author: Lenovo
"""
# -*- coding: utf-8 -*-
"""
Created on Sat Nov 7 20:34:19 2020
@author: Lenovo
"""
import numpy as np
from osgeo import gdal
from osgeo.gdalconst import GA_ReadOnly,GDT_Float32
import random
#参数
N=3 #摩尔邻域
td1=np.matrix([[46989,54427,59899,49516,38090],[80016,54427,43599,42433,28653]], dtype=int)
td2=np.matrix(
[[0.90370087,0.06478112,0.02560174,0.00312839,0.00278789],
[0.29073805,0.59132416,0.10125489,0.01038088,0.00630202],
[0.21582998,0.21876826,0.46934006,0.04833136,0.04773035],
[0.075876 ,0.06050692,0.09534484,0.60086842,0.16740382],
[0.13234445,0.07608296,0.10635337,0.23809399,0.44712523]])
td3=np.matrix([1,0.9,0.5,1,0.1])
pre_Intertia=[1,1,1,1,1]
# Open a pre-classified image
in_path = "C:\\Users\\Lenovo\\Desktop\\dg2001coor.tif"
prob_path="C:\\Users\\Lenovo\\Desktop\\Probability-of-occurrence.tif"
restric_path="C:\\Users\\Lenovo\\Desktop\\restrictedarea.tif"
f = open("C:\\Users\\Lenovo\\Desktop\\out.txt", "w")
f.truncate()
gdal.AllRegister()
raw_image = gdal.Open(in_path,GA_ReadOnly)
prob_image=gdal.Open(prob_path,GA_ReadOnly)
restric_image=gdal.Open(restric_path,GA_ReadOnly)
try:
cols = raw_image.RasterXSize
rows = raw_image.RasterYSize
bands = raw_image.RasterCount
except:
print ("Error: It is not an image")
try:
cols1 = prob_image.RasterXSize
rows1 = prob_image.RasterYSize
bands1 = prob_image.RasterCount
except:
print ("Error: It is not an image")
try:
cols2 = restric_image.RasterXSize
rows2 = restric_image.RasterYSize
bands2 = restric_image.RasterCount
except:
print ("Error: It is not an image")
# Get the spatial reference of the input image
projInfo = raw_image.GetProjection()
transInfo = raw_image.GetGeoTransform()
# Read the image as an array
pre_classified = raw_image.ReadAsArray(0, 0, cols, rows)
prob_value=prob_image.ReadAsArray(0, 0, cols1, rows1)
restric_value=restric_image.ReadAsArray(0, 0, cols2, rows2) #约束条件
now_classified=np.zeros((rows,cols))
q =[pre_classified,pre_classified]
# Starting from the 2nd row and 2nd col
i = 0
j = 0
M = 1
#类型1:城市
#类型2:水体
#类型3:耕地
#类型4:林地
#类型5:果园
def wheel(p,self_type):
sum_p=np.sum(p)
p1=p[0,0]/sum_p
p2=p1+p[0,1]/sum_p
p3=p2+p[0,2]/sum_p
p4=p3+p[0,3]/sum_p
p5=p4+p[0,4]/sum_p
change_type=0
temp_prob = random.uniform(0, 1) # 均匀分布下,随机生成一个0,sum_p之间的概率
if temp_prob<=p1:
change_type= 1
if temp_prob>p1 and temp_prob<=p2:
change_type= 2
if temp_prob>p2 and temp_prob<=p3:
change_type= 3
if temp_prob>p3 and temp_prob<=p4:
change_type= 4
if temp_prob>p4 and temp_prob<=p5:
change_type= 5
now_demand=td1[1, (change_type-1)] - sum(next_classified==change_type)
if now_demand<=0:
return self_type
if now_demand>0:
return change_type
# run 10 iterations
while M <= 10: # run 10 iterations
next_classified=np.array(q[1], copy=True)
stop = 0
for j in range(5):
d1 = td1[1, j] - sum(q[1] == (j+1)) # t-1
d2 = td1[1, j] - sum(q[0] == (j+1)) # t-2
print("需求=",td1[1, j],"t-1分配=",sum(q[1] == (j+1)),"t-2分配=",sum(q[0] == (j+1)),"d1=",d1,"d2=",d2)
if d1 == 0:
stop += 1
if abs(d1) <= abs(d2):
continue
if d1 < d2 and d2 < 0:
pre_Intertia[j] *= (d2 / d1)
if 0 < d2 and d2 < d1:
pre_Intertia[j] *= (d1 / d2)
if stop == 5:
break
# while i < 11:
# while j < cols - 1:
while i < rows - 1:
print(i)
while j < cols - 1:
if restric_value[i][j]==0 or q[1][i,j] >5:
j=j+1
continue;
if q[1][i,j] == 1 and restric_value[i][j]==1:
# If the center pixel is urban
# define variables to record the nearest pixel value
U1 = 0 # urban
W1 = 0 # water
C1 = 0 # cropland
T1 = 0 # tree
O1 = 0 # orchard
for n in range(-1,2) :
for m in range(-1,2) :
# check the pixel above the center pixel
if q[1][i+n,j+m] == 1:
U1 = U1 + 1
if q[1][i+n,j+m] == 2:
W1 = W1 + 1
if q[1][i+n,j+m] == 3:
C1 = C1 + 1
if q[1][i+n,j+m] == 4:
T1 = T1 + 1
if q[1][i+n,j+m] == 5:
O1 = O1 + 1
#邻域密度
U1 = U1*td3[0,0]/(N*N-1)
W1 = W1*td3[0,1]/(N*N-1)
C1 = C1*td3[0,2]/(N*N-1)
T1 = T1*td3[0,3]/(N*N-1)
O1 = O1*td3[0,4]/(N*N-1)
#转化成本
U1 = U1*td2[0,0]
W1 = W1*td2[0,1]
C1 = C1*td2[0,2]
T1 = T1*td2[0,3]
O1 = O1*td2[0,4]
#适宜性概率
U1 = U1*prob_value[0][i][j]
W1 = W1*prob_value[1][i][j]
C1 = C1*prob_value[2][i][j]
T1 = T1*prob_value[3][i][j]
O1 = O1*prob_value[4][i][j]
#惯性系数
U1 = U1 *pre_Intertia[0]
#赌盘选择
temp_p=np.matrix([U1,W1,C1,T1,O1])
next_classified[i,j] = wheel(temp_p,q[1][i,j])
# If the center pixel is water
if q[1][i,j] == 2 and restric_value[i][j]==1:
# If the center pixel is urban
# define variables to record the nearest pixel value
U1 = 0 # urban
W1 = 0 # water
C1 = 0 # cropland
T1 = 0 # tree
O1 = 0 # orchard
for n in range(-1,2) :
for m in range(-1,2) :
# check the pixel above the center pixel
if q[1][i+n,j+m] == 1:
U1 = U1 + 1
if q[1][i+n,j+m] == 2:
W1 = W1 + 1
if q[1][i+n,j+m] == 3:
C1 = C1 + 1
if q[1][i+n,j+m] == 4:
T1 = T1 + 1
if q[1][i+n,j+m] == 5:
O1 = O1 + 1
#邻域密度
U1 = U1*td3[0,0]/(N*N-1)
W1 = W1*td3[0,1]/(N*N-1)
C1 = C1*td3[0,2]/(N*N-1)
T1 = T1*td3[0,3]/(N*N-1)
O1 = O1*td3[0,4]/(N*N-1)
#转化成本
U1 = U1*td2[1,0]
W1 = W1*td2[1,1]
C1 = C1*td2[1,2]
T1 = T1*td2[1,3]
O1 = O1*td2[1,4]
#适宜性概率
U1 = U1*prob_value[0][i][j]
W1 = W1*prob_value[1][i][j]
C1 = C1*prob_value[2][i][j]
T1 = T1*prob_value[3][i][j]
O1 = O1*prob_value[4][i][j]
#惯性系数
W1 = W1 *pre_Intertia[1]
#赌盘选择
temp_p=np.matrix([U1,W1,C1,T1,O1])
next_classified[i,j] = wheel(temp_p,q[1][i,j])
# If the center pixel is crop
if q[1][i,j] == 3 and restric_value[i][j]==1:
# If the center pixel is urban
# define variables to record the nearest pixel value
U1 = 0 # urban
W1 = 0 # water
C1 = 0 # cropland
T1 = 0 # tree
O1 = 0 # orchard
for n in range(-1,2) :
for m in range(-1,2) :
# check the pixel above the center pixel
if q[1][i+n,j+m] == 1:
U1 = U1 + 1
if q[1][i+n,j+m] == 2:
W1 = W1 + 1
if q[1][i+n,j+m] == 3:
C1 = C1 + 1
if q[1][i+n,j+m] == 4:
T1 = T1 + 1
if q[1][i+n,j+m] == 5:
O1 = O1 + 1
#邻域密度
U1 = U1*td3[0,0]/(N*N-1)
W1 = W1*td3[0,1]/(N*N-1)
C1 = C1*td3[0,2]/(N*N-1)
T1 = T1*td3[0,3]/(N*N-1)
O1 = O1*td3[0,4]/(N*N-1)
#转化成本
U1 = U1*td2[2,0]
W1 = W1*td2[2,1]
C1 = C1*td2[2,2]
T1 = T1*td2[2,3]
O1 = O1*td2[2,4]
#适宜性概率
U1 = U1*prob_value[0][i][j]
W1 = W1*prob_value[1][i][j]
C1 = C1*prob_value[2][i][j]
T1 = T1*prob_value[3][i][j]
O1 = O1*prob_value[4][i][j]
#惯性系数
C1 = C1 *pre_Intertia[2]
#赌盘选择
temp_p=np.matrix([U1,W1,C1,T1,O1])
next_classified[i,j] = wheel(temp_p,q[1][i,j])
# If the center pixel is tree
if q[1][i,j] == 4 and restric_value[i][j]==1:
# define variables to record the nearest pixel value
U1 = 0 # urban
W1 = 0 # water
C1 = 0 # cropland
T1 = 0 # tree
O1 = 0 # orchard
for n in range(-1,2) :
for m in range(-1,2) :
# check the pixel above the center pixel
if q[1][i+n,j+m] == 1:
U1 = U1 + 1
if q[1][i+n,j+m] == 2:
W1 = W1 + 1
if q[1][i+n,j+m] == 3:
C1 = C1 + 1
if q[1][i+n,j+m] == 4:
T1 = T1 + 1
if q[1][i+n,j+m] == 5:
O1 = O1 + 1
#邻域密度
U1 = U1*td3[0,0]/(N*N-1)
W1 = W1*td3[0,1]/(N*N-1)
C1 = C1*td3[0,2]/(N*N-1)
T1 = T1*td3[0,3]/(N*N-1)
O1 = O1*td3[0,4]/(N*N-1)
#转化成本
U1 = U1*td2[3,0]
W1 = W1*td2[3,1]
C1 = C1*td2[3,2]
T1 = T1*td2[3,3]
O1 = O1*td2[3,4]
#适宜性概率
U1 = U1*prob_value[0][i][j]
W1 = W1*prob_value[1][i][j]
C1 = C1*prob_value[2][i][j]
T1 = T1*prob_value[3][i][j]
O1 = O1*prob_value[4][i][j]
#惯性系数
T1 = T1 *pre_Intertia[3]
#赌盘选择
temp_p=np.matrix([U1,W1,C1,T1,O1])
next_classified[i,j] = wheel(temp_p,q[1][i,j])
# If the center pixel is fruit
if q[1][i,j] == 5 and restric_value[i][j]==1:
# If the center pixel is urban
# define variables to record the nearest pixel value
U1 = 0 # urban
W1 = 0 # water
C1 = 0 # cropland
T1 = 0 # tree
O1 = 0 # orchard
for n in range(-1,2) :
for m in range(-1,2) :
# check the pixel above the center pixel
if q[1][i+n,j+m] == 1:
U1 = U1 + 1
if q[1][i+n,j+m] == 2:
W1 = W1 + 1
if q[1][i+n,j+m] == 3:
C1 = C1 + 1
if q[1][i+n,j+m] == 4:
T1 = T1 + 1
if q[1][i+n,j+m] == 5:
O1 = O1 + 1
#邻域密度
U1 = U1*td3[0,0]/(N*N-1)
W1 = W1*td3[0,1]/(N*N-1)
C1 = C1*td3[0,2]/(N*N-1)
T1 = T1*td3[0,3]/(N*N-1)
O1 = O1*td3[0,4]/(N*N-1)
#转化成本
U1 = U1*td2[4,0]
W1 = W1*td2[4,1]
C1 = C1*td2[4,2]
T1 = T1*td2[4,3]
O1 = O1*td2[4,4]
#适宜性概率
U1 = U1*prob_value[0][i][j]
W1 = W1*prob_value[1][i][j]
C1 = C1*prob_value[2][i][j]
T1 = T1*prob_value[3][i][j]
O1 = O1*prob_value[4][i][j]
#惯性系数
O1 = O1 *pre_Intertia[4]
#赌盘选择
temp_p=np.matrix([U1,W1,C1,T1,O1])
next_classified[i,j] = wheel(temp_p,q[1][i,j])
j = j + 1
j = 1
i = i + 1
print (M, "iteration(s) finished")
print(" ")
del q[0]
q.append(next_classified)
i = 1
j = 1
M = M + 1
# write result to disk
driver = gdal.GetDriverByName("GTiff")
outDataset = driver.Create("E:/CA.tif",
cols,rows,bands,GDT_Float32)
outDataset.SetProjection(projInfo)
outDataset.SetGeoTransform(transInfo)
CA = outDataset.GetRasterBand(1)
CA.SetNoDataValue(2147483647)
result_classified=q[1]
CA.WriteArray(result_classified[:,:])
CA.FlushCache()
CA = None
outDataset = None