pylineament 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of pylineament might be problematic. Click here for more details.
- pylineament/__init__.py +0 -0
- pylineament/pylineament.py +641 -0
- pylineament/pylineament_ui.py +766 -0
- pylineament-0.1.0.dist-info/METADATA +39 -0
- pylineament-0.1.0.dist-info/RECORD +8 -0
- pylineament-0.1.0.dist-info/WHEEL +5 -0
- pylineament-0.1.0.dist-info/entry_points.txt +2 -0
- pylineament-0.1.0.dist-info/top_level.txt +1 -0
pylineament/__init__.py
ADDED
|
File without changes
|
|
@@ -0,0 +1,641 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
import numpy as np
|
|
3
|
+
import glob
|
|
4
|
+
import fiona
|
|
5
|
+
import os
|
|
6
|
+
import pandas as pd
|
|
7
|
+
import numpy as np
|
|
8
|
+
import rasterio
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def read_raster(im_path, split_size=500):
|
|
12
|
+
|
|
13
|
+
img_format = os.path.split(im_path)[-1].split('.')[-1]
|
|
14
|
+
|
|
15
|
+
im_path = os.path.abspath(im_path)
|
|
16
|
+
dataset = rasterio.open(im_path)
|
|
17
|
+
try:
|
|
18
|
+
crs_espg = dataset.crs.to_epsg()
|
|
19
|
+
|
|
20
|
+
except:
|
|
21
|
+
crs_espg = None
|
|
22
|
+
|
|
23
|
+
e = list(dataset.bounds),
|
|
24
|
+
extent = [e[0][0], e[0][2], e[0][1], e[0][3]]
|
|
25
|
+
|
|
26
|
+
if dataset.count > 1:
|
|
27
|
+
im = dataset.read().mean(axis=0)
|
|
28
|
+
else:
|
|
29
|
+
im = dataset.read(1)
|
|
30
|
+
|
|
31
|
+
shape = im.shape
|
|
32
|
+
left, bottom, right, top = list(dataset.bounds)
|
|
33
|
+
resX = (right-left)/im.shape[1]
|
|
34
|
+
resY = (top-bottom)/im.shape[0]
|
|
35
|
+
|
|
36
|
+
sz = split_size
|
|
37
|
+
|
|
38
|
+
# print(shape, sz)
|
|
39
|
+
|
|
40
|
+
if shape[0]<=sz:
|
|
41
|
+
v_split = np.array([[0, shape[0]]])
|
|
42
|
+
else:
|
|
43
|
+
v_split = np.c_[np.arange(0, shape[0]-sz, sz), np.arange(0, shape[0]-sz, sz)+sz]
|
|
44
|
+
v_split = np.vstack([v_split, [v_split[-1,-1],shape[0]] ])
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
if shape[1]<=sz:
|
|
48
|
+
h_split = np.array([[0, shape[1]]])
|
|
49
|
+
else:
|
|
50
|
+
h_split = np.c_[np.arange(0, shape[1]-sz, sz), np.arange(0, shape[1]-sz, sz)+sz]
|
|
51
|
+
h_split = np.vstack([h_split, [h_split[-1,-1],shape[1]]])
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
xx, yy = np.meshgrid(np.arange(len(v_split)), np.arange(len(h_split)))
|
|
55
|
+
xx, yy = xx.flatten(), yy.flatten()
|
|
56
|
+
|
|
57
|
+
grids = np.c_[ h_split[yy], v_split[xx],]
|
|
58
|
+
'''grid format: [[L, R, B, T]] '''
|
|
59
|
+
|
|
60
|
+
ns = np.arange(len(grids))
|
|
61
|
+
lefts = [left]*len(grids)
|
|
62
|
+
tops = [top]*len(grids)
|
|
63
|
+
resXs = [resX]*len(grids)
|
|
64
|
+
resYs = [resY]*len(grids)
|
|
65
|
+
szs = [sz]*len(grids)
|
|
66
|
+
|
|
67
|
+
df = pd.DataFrame(zip(ns, lefts, tops, resXs, resYs, szs), columns=['ns', 'lefts', 'tops', 'resXs', 'resYs', 'szs'])
|
|
68
|
+
|
|
69
|
+
df[['L', 'R', 'B', 'T']] = grids
|
|
70
|
+
|
|
71
|
+
df['left_bound'] = df['lefts'] + df['L']*df['resXs']
|
|
72
|
+
df['bottom_bound'] = df['tops'] - df['T']*df['resYs']
|
|
73
|
+
df['right_bound'] = df['left_bound'] + abs(df['L'] - df['R'])*df['resXs']
|
|
74
|
+
df['top_bound'] = df['bottom_bound'] + abs(df['T'] - df['B'])*df['resYs']
|
|
75
|
+
|
|
76
|
+
dem = im
|
|
77
|
+
|
|
78
|
+
return df, dem, extent,crs_espg
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def reduce_lines(lines, extent, dem_shape, min_dist=10, seg_len=10):
|
|
82
|
+
# frag lines
|
|
83
|
+
|
|
84
|
+
d = np.sqrt((lines['max_x'] - lines['min_x'])**2 + (lines['max_y'] - lines['min_y'])**2)
|
|
85
|
+
dx = lines['max_x'] - lines['min_x']
|
|
86
|
+
dy = lines['max_y'] - lines['min_y']
|
|
87
|
+
m = dy / dx
|
|
88
|
+
c = lines['max_y'] - m * lines['max_x']
|
|
89
|
+
|
|
90
|
+
dr_dx = dx/d*seg_len
|
|
91
|
+
|
|
92
|
+
# lines['d'] = d
|
|
93
|
+
# lines['dx'] = dx
|
|
94
|
+
# lines['dr_dx'] = d/dx
|
|
95
|
+
|
|
96
|
+
lines['dr_dx'] = (dx/d)*seg_len
|
|
97
|
+
lines['coef'] = m
|
|
98
|
+
lines['intercept'] = c
|
|
99
|
+
|
|
100
|
+
container = []
|
|
101
|
+
|
|
102
|
+
for i in range(len(lines)):
|
|
103
|
+
azi, clust, deg, xmin, xmax, ymin, ymax, dr_dx, coef, intercept = lines.iloc[i].values
|
|
104
|
+
|
|
105
|
+
if (xmax != xmin) & (ymin != ymax):
|
|
106
|
+
xs = np.arange(xmin, xmax+dr_dx, dr_dx)
|
|
107
|
+
ys = coef*xs + intercept
|
|
108
|
+
|
|
109
|
+
xs_a, xs_b = xs[:-1], xs[1:]
|
|
110
|
+
ys_a, ys_b = ys[:-1], ys[1:]
|
|
111
|
+
df_ = pd.DataFrame(np.c_[xs_a, xs_b, ys_a, ys_b], columns=['xs_a', 'xs_b', 'ys_a', 'ys_b'])
|
|
112
|
+
df_[['quad', 'group', 'min_x', 'max_x', 'min_y', 'max_y', 'dr_dx', 'coef', 'intercept']] = azi, clust, xmin, xmax, ymin, ymax, dr_dx, coef, intercept
|
|
113
|
+
container.append(df_)
|
|
114
|
+
|
|
115
|
+
broken_lines = pd.concat(container)
|
|
116
|
+
|
|
117
|
+
if len(broken_lines)>10:
|
|
118
|
+
|
|
119
|
+
from sklearn.neighbors import KDTree
|
|
120
|
+
|
|
121
|
+
tree= KDTree(broken_lines[['xs_a', 'xs_b', 'ys_a', 'ys_b']])
|
|
122
|
+
dist, idxs = tree.query(broken_lines[['xs_a', 'xs_b', 'ys_a', 'ys_b']], k=10, return_distance=True)
|
|
123
|
+
idxs = np.where(dist>min_dist,dist.shape[0]+1,idxs)
|
|
124
|
+
|
|
125
|
+
keepit = np.unique(np.sort(idxs, axis=1)[:,0])
|
|
126
|
+
broken_lines_ = broken_lines.iloc[keepit]
|
|
127
|
+
# broken_lines_ = broken_lines.iloc[keepit].drop_duplicates(['azi', 'clust'])
|
|
128
|
+
else:
|
|
129
|
+
broken_lines_ = broken_lines
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
container = []
|
|
133
|
+
for quad in broken_lines_.quad.unique():
|
|
134
|
+
temp = broken_lines_[broken_lines_.quad == quad]
|
|
135
|
+
for group in temp.group.unique():
|
|
136
|
+
|
|
137
|
+
temp = broken_lines_[(broken_lines_.quad == quad) & (broken_lines_.group == group)].sort_values('xs_a').reset_index(drop=True)
|
|
138
|
+
|
|
139
|
+
min_x_ = temp.iloc[0]['xs_a']
|
|
140
|
+
max_x_ = temp.iloc[-1]['xs_b']
|
|
141
|
+
min_y_ = temp.iloc[0]['ys_a']
|
|
142
|
+
max_y_ = temp.iloc[-1]['ys_b']
|
|
143
|
+
|
|
144
|
+
L = np.sqrt((max_x_ - min_x_)**2 + (max_y_ - min_y_)**2)
|
|
145
|
+
deg = -np.degrees(np.arctan((max_y_ - min_y_) / (max_x_ - min_x_)))+90
|
|
146
|
+
container.append([quad, group, min_x_, max_x_, min_y_, max_y_, L, deg])
|
|
147
|
+
|
|
148
|
+
broken_lines__ = pd.DataFrame(container, columns=['quad', 'group', 'min_x', 'max_x', 'min_y', 'max_y', 'L', 'deg'])
|
|
149
|
+
|
|
150
|
+
left, right, top, bot = extent
|
|
151
|
+
|
|
152
|
+
broken_lines__['min_x_'] = broken_lines__['min_x']
|
|
153
|
+
broken_lines__['max_x_'] = broken_lines__['max_x']
|
|
154
|
+
|
|
155
|
+
broken_lines__['min_y_'] = broken_lines__['min_y']
|
|
156
|
+
broken_lines__['max_y_'] = broken_lines__['max_y']
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
broken_lines__['min_x'] = (broken_lines__['min_x']/dem_shape[1])*(right - left) + left
|
|
160
|
+
broken_lines__['max_x'] = (broken_lines__['max_x']/dem_shape[1])*(right - left) + left
|
|
161
|
+
|
|
162
|
+
broken_lines__['min_y'] = -(broken_lines__['min_y']/dem_shape[0])*(bot - top) + bot
|
|
163
|
+
broken_lines__['max_y'] = -(broken_lines__['max_y']/dem_shape[0])*(bot - top) + bot
|
|
164
|
+
broken_lines__['length'] = (broken_lines__['max_x'] - broken_lines__['min_x'])**2 + (broken_lines__['max_y'] - broken_lines__['min_y'])**2
|
|
165
|
+
|
|
166
|
+
return broken_lines, broken_lines_, broken_lines__
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def extract_lineament_points(dem, eps=1.2, thresh=40,z_multip=1):
|
|
170
|
+
from sklearn.cluster import DBSCAN
|
|
171
|
+
# import rasterio
|
|
172
|
+
from skimage.filters import prewitt
|
|
173
|
+
import pandas as pd
|
|
174
|
+
import numpy as np
|
|
175
|
+
|
|
176
|
+
container = []
|
|
177
|
+
|
|
178
|
+
im_prewitt = None
|
|
179
|
+
im_prewitt_clip = None
|
|
180
|
+
|
|
181
|
+
|
|
182
|
+
for num, deg in enumerate(range(0, 360, 30)):
|
|
183
|
+
|
|
184
|
+
im = prewitt(hillshade(dem, deg, 0,z_multip))
|
|
185
|
+
|
|
186
|
+
if num ==0:
|
|
187
|
+
im_prewitt = im
|
|
188
|
+
|
|
189
|
+
im_ = np.where(im>100,1,0)
|
|
190
|
+
im = np.where(im>100)
|
|
191
|
+
im = np.c_[im[1], im[0]]
|
|
192
|
+
|
|
193
|
+
if num ==0:
|
|
194
|
+
im_prewitt_clip = im_
|
|
195
|
+
|
|
196
|
+
try:
|
|
197
|
+
db = DBSCAN(eps=eps, min_samples=3,leaf_size=50)
|
|
198
|
+
pred = db.fit_predict(im)
|
|
199
|
+
except:
|
|
200
|
+
break
|
|
201
|
+
|
|
202
|
+
flag = np.where(pred != -1)[0]
|
|
203
|
+
|
|
204
|
+
im_ = im[flag]
|
|
205
|
+
pred = pred[flag]
|
|
206
|
+
|
|
207
|
+
elev = dem[im_[:,1], im_[:,0]]
|
|
208
|
+
flag = np.where(elev>10)[0]
|
|
209
|
+
im_ = im_[flag]
|
|
210
|
+
pred = pred[flag].astype('int')
|
|
211
|
+
elev = elev[flag]
|
|
212
|
+
|
|
213
|
+
df = pd.DataFrame(np.c_[im_, pred, elev], columns=['X', 'Y', 'GROUP','elev'])
|
|
214
|
+
|
|
215
|
+
temp = df.groupby('GROUP').count().reset_index()
|
|
216
|
+
good_points = temp[temp['X']>thresh]['GROUP'].values
|
|
217
|
+
|
|
218
|
+
idx = df[['GROUP']].isin(good_points)
|
|
219
|
+
idx = list(idx[idx['GROUP'] == True].index)
|
|
220
|
+
|
|
221
|
+
df = df.iloc[idx]
|
|
222
|
+
df['quad'] = num
|
|
223
|
+
|
|
224
|
+
container.append(df)
|
|
225
|
+
|
|
226
|
+
return container, im_prewitt, im_prewitt_clip
|
|
227
|
+
|
|
228
|
+
|
|
229
|
+
def convert_points_to_line(container):
|
|
230
|
+
from sklearn.linear_model import LinearRegression
|
|
231
|
+
|
|
232
|
+
if len(container)>0:
|
|
233
|
+
linea = pd.concat(container)
|
|
234
|
+
|
|
235
|
+
linea['GROUP'] = linea['GROUP'].astype('int')
|
|
236
|
+
linea['quad'] = linea['quad'].astype('int')
|
|
237
|
+
|
|
238
|
+
ugroup, uquad = linea['GROUP'].unique(), linea['quad'].unique()
|
|
239
|
+
container = []
|
|
240
|
+
for g in ugroup:
|
|
241
|
+
for q in uquad:
|
|
242
|
+
temp = linea[(linea['GROUP'] == g) & (linea['quad'] == q)]
|
|
243
|
+
|
|
244
|
+
if len(temp) == 0:
|
|
245
|
+
continue
|
|
246
|
+
mla = LinearRegression()
|
|
247
|
+
mla.fit(temp[['X']],temp['Y'])
|
|
248
|
+
|
|
249
|
+
m = mla.coef_[0]
|
|
250
|
+
c = mla.intercept_
|
|
251
|
+
deg = np.degrees(np.arctan(m))
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
|
|
255
|
+
min_x1, max_x1 = np.nanpercentile(temp['X'],5), np.nanpercentile(temp['X'],95)
|
|
256
|
+
min_y1, max_y1 = m*min_x1 + c, m*max_x1 + c
|
|
257
|
+
|
|
258
|
+
container.append([q, g, deg, min_x1, max_x1, min_y1, max_y1])
|
|
259
|
+
|
|
260
|
+
lines = pd.DataFrame(container, columns=['quad', 'group', 'deg',
|
|
261
|
+
'min_x', 'max_x', 'min_y', 'max_y',])
|
|
262
|
+
|
|
263
|
+
return lines
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
def hillshade(array, azimuth=315, angle_altitude=45, z_multip=1):
|
|
267
|
+
array = array*z_multip
|
|
268
|
+
azimuth = 360.0 - azimuth # Convert azimuth to geographic convention
|
|
269
|
+
x, y = np.gradient(array)
|
|
270
|
+
slope = np.pi / 2. - np.arctan(np.sqrt(x * x + y * y))
|
|
271
|
+
aspect = np.arctan2(-x, y)
|
|
272
|
+
azm_rad = azimuth * np.pi / 180. # Convert azimuth to radians
|
|
273
|
+
alt_rad = angle_altitude * np.pi / 180. # Convert altitude to radians
|
|
274
|
+
|
|
275
|
+
shaded = np.sin(alt_rad) * np.sin(slope) + np.cos(alt_rad) * np.cos(slope) * np.cos((azm_rad - np.pi / 2.) - aspect)
|
|
276
|
+
|
|
277
|
+
return 255 * (shaded + 1) / 2 # Scale to 0-255
|
|
278
|
+
|
|
279
|
+
|
|
280
|
+
def image_splitting(im_path='srtm/srtm_java.tif', split_size=500, tempfolder='temp'):
|
|
281
|
+
import pandas as pd
|
|
282
|
+
import numpy as np
|
|
283
|
+
import rasterio
|
|
284
|
+
|
|
285
|
+
|
|
286
|
+
dataset = rasterio.open(im_path)
|
|
287
|
+
im = dataset.read(1)
|
|
288
|
+
shape = im.shape
|
|
289
|
+
|
|
290
|
+
left, bottom, right, top = list(dataset.bounds)
|
|
291
|
+
|
|
292
|
+
if (left == 0.0) and (top == 0.0):
|
|
293
|
+
bottom = -bottom
|
|
294
|
+
|
|
295
|
+
resX = (right-left)/im.shape[1]
|
|
296
|
+
resY = (top-bottom)/im.shape[0]
|
|
297
|
+
|
|
298
|
+
sz = split_size
|
|
299
|
+
|
|
300
|
+
if shape[0]<=sz:
|
|
301
|
+
v_split = np.array([[0, shape[0]]])
|
|
302
|
+
# print('small', v_split)
|
|
303
|
+
else:
|
|
304
|
+
v_split = np.c_[np.arange(0, shape[0]-sz, sz), np.arange(0, shape[0]-sz, sz)+sz]
|
|
305
|
+
# print('v_split \n', v_split)
|
|
306
|
+
v_split = np.vstack([v_split, [v_split[-1,-1],shape[0]]])
|
|
307
|
+
# print('v_split \n', v_split)
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
if shape[1]<=sz:
|
|
311
|
+
h_split = np.array([[0, shape[1]]])
|
|
312
|
+
# print('small', h_split)
|
|
313
|
+
|
|
314
|
+
else:
|
|
315
|
+
h_split = np.c_[np.arange(0, shape[1]-sz, sz), np.arange(0, shape[1]-sz, sz)+sz]
|
|
316
|
+
# print('h_split \n', h_split)
|
|
317
|
+
h_split = np.vstack([h_split, [h_split[-1,-1],shape[1]]])
|
|
318
|
+
# print('h_split \n', h_split)
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
xx, yy = np.meshgrid(np.arange(len(v_split)), np.arange(len(h_split)))
|
|
322
|
+
xx, yy = xx.flatten(), yy.flatten()
|
|
323
|
+
|
|
324
|
+
grids = np.c_[ h_split[yy], v_split[xx],]
|
|
325
|
+
'''grid format: [[L, R, B, T]] '''
|
|
326
|
+
|
|
327
|
+
ns = np.arange(len(grids))
|
|
328
|
+
target_folders = [tempfolder]*len(grids)
|
|
329
|
+
lefts = [left]*len(grids)
|
|
330
|
+
tops = [top]*len(grids)
|
|
331
|
+
resXs = [resX]*len(grids)
|
|
332
|
+
resYs = [resY]*len(grids)
|
|
333
|
+
szs = [sz]*len(grids)
|
|
334
|
+
|
|
335
|
+
df = pd.DataFrame(zip(ns, target_folders, lefts, tops, resXs, resYs, szs), columns=['ns', 'target_folders', 'lefts', 'tops', 'resXs', 'resYs', 'szs'])
|
|
336
|
+
|
|
337
|
+
df[['L', 'R', 'B', 'T']] = grids
|
|
338
|
+
|
|
339
|
+
for n, tempfolder, left, top, resX, resY, sz, L, R, B, T in df.values:
|
|
340
|
+
l = left + L*resX
|
|
341
|
+
# b = top - T*resY - sz*resY
|
|
342
|
+
b = -top - T*resY
|
|
343
|
+
|
|
344
|
+
transform = rasterio.Affine.translation(l - resX / 2, b - resY / 2) * rasterio.Affine.scale(resX, resY)
|
|
345
|
+
|
|
346
|
+
Z = im[B:T, L:R].astype('float')
|
|
347
|
+
Z = Z[::-1]
|
|
348
|
+
n_ = str(n).zfill(4)
|
|
349
|
+
new_dataset = rasterio.open(
|
|
350
|
+
f'{tempfolder}/{n_}.tiff',
|
|
351
|
+
'w',
|
|
352
|
+
driver='GTiff',
|
|
353
|
+
height=Z.shape[0],
|
|
354
|
+
width=Z.shape[1],
|
|
355
|
+
count=1,
|
|
356
|
+
dtype=Z.dtype,
|
|
357
|
+
crs=dataset.crs,
|
|
358
|
+
transform=transform,
|
|
359
|
+
)
|
|
360
|
+
new_dataset.write(Z, 1)
|
|
361
|
+
new_dataset.close()
|
|
362
|
+
|
|
363
|
+
df['left_bound'] = df['lefts'] + df['L']*df['resXs']
|
|
364
|
+
df['bottom_bound'] = df['tops'] - df['T']*df['resYs']
|
|
365
|
+
df['right_bound'] = df['left_bound'] + abs(df['L'] - df['R'])*df['resXs']
|
|
366
|
+
df['top_bound'] = df['bottom_bound'] + abs(df['T'] - df['B'])*df['resYs']
|
|
367
|
+
|
|
368
|
+
return df
|
|
369
|
+
|
|
370
|
+
|
|
371
|
+
def merge_lines_csv_to_shp(tempfolder, shppath, save_to_file):
|
|
372
|
+
|
|
373
|
+
flist = glob.glob(f'{tempfolder}/*.csv')
|
|
374
|
+
|
|
375
|
+
dfs = [pd.read_csv(f) for f in flist]
|
|
376
|
+
|
|
377
|
+
df = pd.concat(dfs).reset_index(drop=True)
|
|
378
|
+
df = df[['quad', 'group', 'min_x', 'max_x', 'min_y', 'max_y', 'L', 'deg', 'length', 'crs']]
|
|
379
|
+
|
|
380
|
+
if save_to_file==True:
|
|
381
|
+
|
|
382
|
+
crs_espg = df['crs'].values[0]
|
|
383
|
+
|
|
384
|
+
if type(crs_espg) != int:
|
|
385
|
+
crs_espg = 4326
|
|
386
|
+
|
|
387
|
+
crs_espg = f"EPSG:{crs_espg}"
|
|
388
|
+
|
|
389
|
+
|
|
390
|
+
# if crs_espg != None:
|
|
391
|
+
# crs_espg = f"EPSG:{crs_espg}"
|
|
392
|
+
# else:
|
|
393
|
+
# crs_espg = f"EPSG:{4326}"
|
|
394
|
+
|
|
395
|
+
|
|
396
|
+
# crs_espg = f"EPSG:{crs_espg}"
|
|
397
|
+
|
|
398
|
+
schema = {
|
|
399
|
+
'geometry':'LineString',
|
|
400
|
+
'properties':[('quad', 'int'),
|
|
401
|
+
('group', 'int'),
|
|
402
|
+
('deg', 'float'),
|
|
403
|
+
('min_x', 'float'),
|
|
404
|
+
('max_x', 'float'),
|
|
405
|
+
('min_y', 'float'),
|
|
406
|
+
('max_y', 'float'),
|
|
407
|
+
('length', 'float')]
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
lineShp = fiona.open(shppath, mode='w', driver='ESRI Shapefile',
|
|
411
|
+
schema = schema, crs = crs_espg)
|
|
412
|
+
|
|
413
|
+
for quad, group,min_x, max_x, min_y, max_y, L_px, deg, L_crs, crs in df.values:
|
|
414
|
+
g = ([(min_x, min_y), (max_x, max_y)])
|
|
415
|
+
rowDict = {
|
|
416
|
+
'geometry' : {'type':'LineString',
|
|
417
|
+
'coordinates': g},
|
|
418
|
+
'properties': {'quad' : quad,
|
|
419
|
+
'group' : group,
|
|
420
|
+
'min_x' : min_x,
|
|
421
|
+
'max_x' : max_x,
|
|
422
|
+
'min_y' : min_y,
|
|
423
|
+
'max_y' : max_y,
|
|
424
|
+
'length' : L_crs,
|
|
425
|
+
'deg' : deg,}}
|
|
426
|
+
lineShp.write(rowDict)
|
|
427
|
+
|
|
428
|
+
lineShp.close()
|
|
429
|
+
|
|
430
|
+
return df
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
def merge_single_csv_to_shp(fname, shppath, save_to_file):
|
|
434
|
+
df = pd.read_csv(fname)
|
|
435
|
+
|
|
436
|
+
df = df[['quad', 'group', 'min_x', 'max_x', 'min_y', 'max_y', 'L', 'deg', 'length', 'crs']]
|
|
437
|
+
|
|
438
|
+
crs_espg = df['crs'].values[0]
|
|
439
|
+
|
|
440
|
+
if save_to_file == True:
|
|
441
|
+
|
|
442
|
+
schema = {
|
|
443
|
+
'geometry':'LineString',
|
|
444
|
+
'properties':[('quad', 'int'),
|
|
445
|
+
('group', 'int'),
|
|
446
|
+
('min_x', 'float'),
|
|
447
|
+
('max_x', 'float'),
|
|
448
|
+
('min_y', 'float'),
|
|
449
|
+
('max_y', 'float'),
|
|
450
|
+
('length', 'float'),
|
|
451
|
+
('deg', 'float'),]
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
lineShp = fiona.open(shppath, mode='w', driver='ESRI Shapefile',
|
|
455
|
+
schema = schema, crs = f"EPSG:{crs_espg}")
|
|
456
|
+
|
|
457
|
+
for quad, group,min_x, max_x, min_y, max_y, L_px, deg, L_crs, crs in df.values:
|
|
458
|
+
g = ([(min_x, min_y), (max_x, max_y)])
|
|
459
|
+
rowDict = {
|
|
460
|
+
'geometry' : {'type':'LineString',
|
|
461
|
+
'coordinates': g},
|
|
462
|
+
'properties': {'quad' : quad,
|
|
463
|
+
'group' : group,
|
|
464
|
+
'min_x' : min_x,
|
|
465
|
+
'max_x' : max_x,
|
|
466
|
+
'min_y' : min_y,
|
|
467
|
+
'max_y' : max_y,
|
|
468
|
+
'length' : L_crs,
|
|
469
|
+
'deg' : deg,}}
|
|
470
|
+
|
|
471
|
+
lineShp.write(rowDict)
|
|
472
|
+
|
|
473
|
+
lineShp.close()
|
|
474
|
+
|
|
475
|
+
return df
|
|
476
|
+
|
|
477
|
+
|
|
478
|
+
def raster_resize (dem, factor=1):
|
|
479
|
+
|
|
480
|
+
from skimage.transform import rescale
|
|
481
|
+
|
|
482
|
+
dem = dem
|
|
483
|
+
|
|
484
|
+
dem_min, dem_max = dem.min(), dem.max()
|
|
485
|
+
|
|
486
|
+
dem_ = rescale(dem, factor, anti_aliasing=True)
|
|
487
|
+
|
|
488
|
+
dem2_min, dem2_max = dem_.min(), dem_.max()
|
|
489
|
+
|
|
490
|
+
if (dem_min != dem2_min) & (dem_max != dem2_max) :
|
|
491
|
+
dem_ = (dem_ - dem2_min)/(dem2_max - dem2_min)
|
|
492
|
+
dem_ = dem_min + dem_*(dem_max-dem_min)
|
|
493
|
+
|
|
494
|
+
return dem_
|
|
495
|
+
|
|
496
|
+
else:
|
|
497
|
+
return dem
|
|
498
|
+
|
|
499
|
+
|
|
500
|
+
def dem_to_line(path,
|
|
501
|
+
tempfolder='temp',
|
|
502
|
+
eps=1.2,
|
|
503
|
+
thresh=40,
|
|
504
|
+
min_dist=10,
|
|
505
|
+
seg_len=10,
|
|
506
|
+
z_multip=1.0,
|
|
507
|
+
downscale = 1.0):
|
|
508
|
+
|
|
509
|
+
path = os.path.normpath(path)
|
|
510
|
+
|
|
511
|
+
regions, dem, extent,crs_espg = read_raster(path)
|
|
512
|
+
|
|
513
|
+
dem = raster_resize(dem, factor=downscale)
|
|
514
|
+
|
|
515
|
+
container, im_prewitt, im_prewitt_clip = extract_lineament_points(dem,
|
|
516
|
+
eps=eps,
|
|
517
|
+
thresh=thresh,
|
|
518
|
+
z_multip=z_multip)
|
|
519
|
+
|
|
520
|
+
lines = convert_points_to_line(container)
|
|
521
|
+
|
|
522
|
+
if len(lines) > 0:
|
|
523
|
+
_,_,lines = reduce_lines(lines, extent=extent, dem_shape=dem.shape, min_dist=min_dist, seg_len=seg_len)
|
|
524
|
+
fname = path.split('\\')[-1].split('.')[0]
|
|
525
|
+
|
|
526
|
+
# fname = path.replace('.tiff', '.csv')
|
|
527
|
+
lines['crs'] = crs_espg
|
|
528
|
+
lines.to_csv(f'{tempfolder}\\{fname}.csv', index=False)
|
|
529
|
+
return dem, extent, lines, im_prewitt, im_prewitt_clip, container
|
|
530
|
+
|
|
531
|
+
else:
|
|
532
|
+
lines['crs'] = crs_espg
|
|
533
|
+
return dem, extent, lines, im_prewitt, im_prewitt_clip, container
|
|
534
|
+
|
|
535
|
+
|
|
536
|
+
def dem_to_line_runner(im_path,
|
|
537
|
+
tempfolder='temp',
|
|
538
|
+
shpname='test',
|
|
539
|
+
eps=1.2,
|
|
540
|
+
thresh=40,
|
|
541
|
+
min_dist=10,
|
|
542
|
+
seg_len=10,
|
|
543
|
+
split_size=500,
|
|
544
|
+
z_multip=1,
|
|
545
|
+
downscale = 1,
|
|
546
|
+
save_to_file=True,
|
|
547
|
+
keep_intermediate_file = True):
|
|
548
|
+
|
|
549
|
+
im_path = os.path.normpath(im_path)
|
|
550
|
+
|
|
551
|
+
if os.path.isdir(tempfolder):
|
|
552
|
+
'delete content'
|
|
553
|
+
files = glob.glob(f'{tempfolder}/*')
|
|
554
|
+
for f in files:
|
|
555
|
+
os.remove(f)
|
|
556
|
+
else:
|
|
557
|
+
'create new folder'
|
|
558
|
+
os.mkdir(tempfolder)
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
image_splitting(im_path=im_path, split_size=split_size, tempfolder=tempfolder)
|
|
562
|
+
|
|
563
|
+
flist = glob.glob(f'{tempfolder}/*.tiff')
|
|
564
|
+
cases = pd.DataFrame(zip(flist,
|
|
565
|
+
[tempfolder]*len(flist),
|
|
566
|
+
[eps]*len(flist),
|
|
567
|
+
[thresh]*len(flist),
|
|
568
|
+
[min_dist]*len(flist),
|
|
569
|
+
[seg_len]*len(flist),
|
|
570
|
+
[z_multip]*len(flist),
|
|
571
|
+
[downscale]*len(flist))).values
|
|
572
|
+
|
|
573
|
+
from joblib import Parallel, delayed
|
|
574
|
+
Parallel(n_jobs=-1)(delayed(dem_to_line)(*c) for c in cases)
|
|
575
|
+
|
|
576
|
+
df = merge_lines_csv_to_shp(tempfolder=tempfolder,
|
|
577
|
+
shppath=shpname,
|
|
578
|
+
save_to_file=save_to_file)
|
|
579
|
+
|
|
580
|
+
|
|
581
|
+
if keep_intermediate_file == False:
|
|
582
|
+
|
|
583
|
+
if os.path.isdir(tempfolder):
|
|
584
|
+
'delete content'
|
|
585
|
+
files = glob.glob(f'{tempfolder}/*')
|
|
586
|
+
for f in files:
|
|
587
|
+
os.remove(f)
|
|
588
|
+
os.removedirs(tempfolder)
|
|
589
|
+
|
|
590
|
+
return df
|
|
591
|
+
|
|
592
|
+
|
|
593
|
+
def dem_to_line_runner_small(im_path,
|
|
594
|
+
tempfolder='temp',
|
|
595
|
+
eps=1.2,
|
|
596
|
+
thresh=40,
|
|
597
|
+
min_dist=10,
|
|
598
|
+
seg_len=10,
|
|
599
|
+
z_multip=1,
|
|
600
|
+
downscale=1,
|
|
601
|
+
shp_name=None,
|
|
602
|
+
save_to_file=True):
|
|
603
|
+
|
|
604
|
+
im_path = os.path.normpath(im_path)
|
|
605
|
+
if os.path.isdir(tempfolder):
|
|
606
|
+
'delete content'
|
|
607
|
+
files = glob.glob(f'{tempfolder}/*')
|
|
608
|
+
for f in files:
|
|
609
|
+
os.remove(f)
|
|
610
|
+
else:
|
|
611
|
+
'create new folder'
|
|
612
|
+
os.mkdir(tempfolder)
|
|
613
|
+
print(im_path)
|
|
614
|
+
dem, extent, lines, im_prewitt, im_prewitt_clip, container = dem_to_line(im_path,
|
|
615
|
+
tempfolder=tempfolder,
|
|
616
|
+
eps=eps,
|
|
617
|
+
thresh=thresh,
|
|
618
|
+
min_dist=min_dist,
|
|
619
|
+
seg_len=seg_len,
|
|
620
|
+
z_multip=z_multip,
|
|
621
|
+
downscale=downscale)
|
|
622
|
+
|
|
623
|
+
fname = im_path.split('\\')[-1].split('.')[0]
|
|
624
|
+
csv_name = f'{tempfolder}\\{fname}.csv'
|
|
625
|
+
|
|
626
|
+
if shp_name == None:
|
|
627
|
+
shp_name = f'{fname}'
|
|
628
|
+
|
|
629
|
+
|
|
630
|
+
merge_single_csv_to_shp(csv_name, shp_name, save_to_file)
|
|
631
|
+
|
|
632
|
+
if os.path.isdir(tempfolder):
|
|
633
|
+
'delete content'
|
|
634
|
+
files = glob.glob(f'{tempfolder}/*')
|
|
635
|
+
for f in files:
|
|
636
|
+
os.remove(f)
|
|
637
|
+
os.removedirs(tempfolder)
|
|
638
|
+
|
|
639
|
+
|
|
640
|
+
return dem, extent, lines, im_prewitt, im_prewitt_clip, container
|
|
641
|
+
|