-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidw_basic.py
182 lines (155 loc) · 4.78 KB
/
idw_basic.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
# -*- coding: utf-8 -*-
import numpy as np
#DISTANCE FUNCTION
def distance(x1,y1,x2,y2):
d=np.sqrt((x1-x2)**2+(y1-y2)**2)
return d
# fixed radius and power
def run_rblock(x, y, z, xz, yz, r, p):
x_block=[]
y_block=[]
z_block=[]
xr_min=xz-r
xr_max=xz+r
yr_min=yz-r
yr_max=yz+r
for i in range(len(x)):
# condition to test if a point is within the block
if ((x[i]>=xr_min and x[i]<=xr_max) and (y[i]>=yr_min and y[i]<=yr_max)):
x_block.append(x[i])
y_block.append(y[i])
z_block.append(z[i])
#calculate weight based on distance and p value
w_list=[]
for j in range(len(x_block)):
d=distance(xz,yz,x_block[j],y_block[j]) #distance function is created outside this function
if d>0:
w=1/(d**p)
w_list.append(w)
z0=0
else:
w_list.append(0) #if meet this condition, it means d<=0, weight is set to 0
#check if there is 0 in weight list
w_check=0 in w_list
if w_check==True:
idx=w_list.index(0) # find index for weight=0
z_idw=z_block[idx] # set the value to the current sample value
else:
wt=np.transpose(w_list)
z_idw=np.dot(z_block,wt)/sum(w_list) # idw calculation using dot product
return z_idw
def idw_rblock(x, y, z, grid_side_length, search_radius, p):
# n=100
n = grid_side_length
# setup frame of reference
# left,right,lower,upper coordinate boundaries
x_min=min(x)
x_max=max(x)
y_min=min(y)
y_max=max(y)
#width
w=x_max-x_min
#length
h=y_max-y_min
#x interval
wn=w/n
#y interval
hn=h/n
# target data lists to store interpolated points and values
x_idw_list=[]
y_idw_list=[]
z_head=[]
# initialisation
y_init=y_min
x_init=x_min
for i in range(n):
xz=x_init+wn*i
yz=y_init+hn*i
y_idw_list.append(yz)
x_idw_list.append(xz)
z_idw_list=[]
for j in range(n):
xz=x_init+wn*j
# search_radius=100, inv. power value p=1.5
z_idw=run_rblock(x, y, z, xz, yz, search_radius, p)
z_idw_list.append(z_idw)
z_head.append(z_idw_list)
return (x_idw_list, y_idw_list, z_head)
def run_npoint(x, y, z, xz, yz, n_point, p, rblock_iter_distance=10):
# block radius iteration distance
# r=10
r = rblock_iter_distance
nf=0
while nf<=n_point: #will stop when np reaching at least n_point
x_block=[]
y_block=[]
z_block=[]
r +=10 # add 10 unit each iteration
xr_min=xz-r
xr_max=xz+r
yr_min=yz-r
yr_max=yz+r
for i in range(len(x)):
# condition to test if a point is within the block
if ((x[i]>=xr_min and x[i]<=xr_max) and (y[i]>=yr_min and y[i]<=yr_max)):
x_block.append(x[i])
y_block.append(y[i])
z_block.append(z[i])
nf=len(x_block) #calculate number of point in the block
#calculate weight based on distance and p value
w_list=[]
for j in range(len(x_block)):
d=distance(xz,yz,x_block[j],y_block[j])
if d>0:
w=1/(d**p)
w_list.append(w)
z0=0
else:
w_list.append(0) #if meet this condition, it means d<=0, weight is set to 0
#check if there is 0 in weight list
w_check=0 in w_list
if w_check==True:
idx=w_list.index(0) # find index for weight=0
z_idw=z_block[idx] # set the value to the current sample value
else:
wt=np.transpose(w_list)
z_idw=np.dot(z_block,wt)/sum(w_list) # idw calculation using dot product
return z_idw
# min. number of search points=5, inv. power value p=1.5
def idw_npoint(x, y, z, grid_side_length, n_points, p, rblock_iter_distance=10):
# n=100
n = grid_side_length
# setup frame of reference
# left,right,lower,upper coordinate boundaries
x_min=min(x)
x_max=max(x)
y_min=min(y)
y_max=max(y)
#width
w=x_max-x_min
#length
h=y_max-y_min
#x interval
wn=w/n
#y interval
hn=h/n
# target data lists to store interpolated points and values
x_idw_list=[]
y_idw_list=[]
z_head=[]
# initialisation
y_init=y_min
x_init=x_min
for i in range(n):
xz=x_init+wn*i
yz=y_init+hn*i
y_idw_list.append(yz)
x_idw_list.append(xz)
z_idw_list=[]
for j in range(n):
xz=x_init+wn*j
# min. number of search points=5, inv. power value p=1.5
z_idw=run_npoint(x, y, z, xz, yz, n_points, p)
z_idw_list.append(z_idw)
z_head.append(z_idw_list)
return (x_idw_list, y_idw_list, z_head)