-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskyline_v1.py
598 lines (487 loc) · 17.5 KB
/
skyline_v1.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
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
import numpy
import math
#import matplotlib.pyplot as plt
#import matplotlib.image
import gdal
#import osr
#import ogr
import psycopg2
#import os
#import sys
# load complete raster
img = gdal.Open('C:\ds_test_data\ign_mnt25_alpes.tif')
band1 = img.GetRasterBand(1)
rastinit = img.GetGeoTransform()
#x,y geographic reference matrix
imgx=numpy.zeros((1,img.RasterXSize)).astype(numpy.float)
imgy=numpy.zeros((img.RasterYSize,1)).astype(numpy.float)
for i in range(0,imgx.shape[1]):
imgx[0,i]=rastinit[0]+(i*rastinit[1])
for i in range(0,imgy.shape[0]):
imgy[i,0]=rastinit[3]+(i*rastinit[5])
#Connect to DB
myconn = psycopg2.connect("host = psql9.grenoble.cemagref.fr dbname = xxxx user=xxxx password=xxxx")
#load points and view extent
wsta=myconn.cursor()
query="""
with a as (
select gid, st_x(the_geom) x, st_y(the_geom) y, st_buffer(the_geom,%s) geom
from stations.geo_station_meteofrance
)
select gid, x, y, st_xmin(geom) xmin, st_ymin(geom), st_xmax(geom) xmax, st_ymax(geom) ymax
from a
where gid = '73024400'
order by gid
;
"""
viewmax=1000
wsta.execute(query,(viewmax,))
final_data = []
#extract from original raster
for sta in wsta:
print sta[0]
minrow = None
maxrow = None
mincol = None
maxcol = None
for i in range(0,imgx.shape[1]-1):
if i+1 <= imgx.shape[1] and sta[3]-imgx[0,i]>0 and sta[3]-imgx[0,i+1]<=0:
mincol = i
if i+1 <= imgx.shape[1] and sta[5]-imgx[0,i]>0 and sta[5]-imgx[0,i+1]<=0:
maxcol = i
for i in range(0,imgx.shape[1]-1):
if sta[1]-imgx[0,i]>0 and sta[1]-imgx[0,i+1]<=0:
stacol = i-mincol
stax = imgx[0,i]+((imgx[0,i+1]-imgx[0,i])/2)
if minrow is None:
minrow = 0
if maxrow is None:
maxrow = imgx.shape[1]
for i in range(0,imgy.shape[0]-1):
if i+1 <= imgy.shape[0] and imgy[i,0]-sta[6]>0 and imgy[i+1,0]-sta[6]<=0:
minrow = i
if i+1 <= imgy.shape[0] and imgy[i,0]-sta[4]>0 and imgy[i+1,0]-sta[4]<=0:
maxrow = i
for i in range(0,imgy.shape[0]-1):
if imgy[i,0]-sta[2]>0 and imgy[i+1,0]-sta[2]<=0:
starow = i-minrow
stay = imgy[i+1,0]+((imgy[i,0]-imgy[i+1,0])/2)
if mincol is None:
mincol = 0
if maxcol is None:
maxcol = imgy.shape[0]
sta_xy = (stax, stay)
sta_rc = (starow, stacol)
height = band1.ReadAsArray(mincol, minrow, maxcol-mincol, maxrow-minrow)
# get width and heigth of image
w,h = height.shape
print "raster extracted"
print w, h
#Get all intersected cells on azimuth
for azimut in range (0, 360, 5):
print azimut
myview=0
c= None
#TESTS on azimut
######################################AZIMUT 0 / 360######################################
if azimut == 0:
imax = viewmax/25
viewpt_rc = []
for i in range(1,imax):
#incrementation depending on azimut value (take care of x,y and row, col order and sign for ptx (or pty) calcul
ptrow = sta_rc[0]-i
ptcol = sta_rc[1]
ptrc=(ptrow, ptcol)
viewpt_rc.append(ptrc)
print "cells collected"
pth = []
for p in viewpt_rc:
pth.append(height[p])
maxh = max(pth)
myindex = pth.index(maxh)
#calculate distance of line from ref point to max alt point: take care of row col and order between viewpt and sta
d = ((sta_rc[0]-viewpt_rc[myindex][0])*25)
######################################AZIMUT 180######################################
if azimut == 180:
imax = viewmax/25
viewpt_rc = []
for i in range(1,imax):
#incrementation depending on azimut value (take care of x,y and row, col order and sign for ptx (or pty) calcul
ptrow = sta_rc[0]+i
ptcol = sta_rc[1]
ptrc=(ptrow, ptcol)
viewpt_rc.append(ptrc)
print "cells collected"
pth = []
for p in viewpt_rc:
pth.append(height[p])
maxh = max(pth)
myindex = pth.index(maxh)
#calculate distance of line from ref point to max alt point: take care of row col and order between viewpt and sta
d = ((viewpt_rc[myindex][0]-sta_rc[0])*25)
######################################AZIMUT 90######################################
if azimut == 90:
viewpt_rc = []
for i in range(1,imax):
#incrementation depending on azimut value (take care of x,y and row, col order
ptcol = sta_rc[1]+i
ptrow=sta_rc[0]
ptrc=(ptrow, ptcol)
viewpt_rc.append(ptrc)
print "cells collected"
pth = []
for p in viewpt_rc:
pth.append(height[p])
maxh = max(pth)
myindex = pth.index(maxh)
#calculate distance of line from ref point to max alt point: take care of row col
d = ((viewpt_rc[myindex][1]-sta_rc[1])*25)
######################################AZIMUT 270######################################
if azimut == 270:
viewpt_rc = []
for i in range(1,imax):
#incrementation depending on azimut value (take care of x,y and row, col order
ptcol = sta_rc[1]-i
ptrow=sta_rc[0]
ptrc=(ptrow, ptcol)
viewpt_rc.append(ptrc)
print "cells collected"
pth = []
for p in viewpt_rc:
pth.append(height[p])
maxh = max(pth)
myindex = pth.index(maxh)
#calculate distance of line from ref point to max alt point: take care of row col
d = ((sta_rc[1]-viewpt_rc[myindex][1])*25)
######################################AZIMUT 45######################################
if azimut == 45:
#calculate corresponding angle of right-angled triangle
alpha = 45
imax = int(math.floor(viewmax*math.cos(math.radians(alpha))/25))
viewpt_rc = []
for i in range(1,imax):
#incrementation depending on azimut value (take care of x,y and row, col order
ptcol = sta_rc[1]+i
ptrow = sta_rc[0]-i
ptrc=(ptrow, ptcol)
viewpt_rc.append(ptrc)
print "cells collected"
pth = []
for p in viewpt_rc:
pth.append(height[p])
maxh = max(pth)
myindex = pth.index(maxh)
#calculate distance of line from ref point to max alt point: take care of row col
a = ((viewpt_rc[myindex][1]-sta_rc[1])*25)
d = a/math.cos(math.radians(alpha))
######################################AZIMUT 135######################################
if azimut == 135:
#calculate corresponding angle of right-angled triangle
alpha = 45
imax = int(math.floor(viewmax*math.cos(math.radians(alpha))/25))
viewpt_rc = []
for i in range(1,imax):
#incrementation depending on azimut value (take care of x,y and row, col order
ptcol = sta_rc[1]+i
ptrow = sta_rc[0]+i
ptrc=(ptrow, ptcol)
viewpt_rc.append(ptrc)
print "cells collected"
pth = []
for p in viewpt_rc:
pth.append(height[p])
maxh = max(pth)
myindex = pth.index(maxh)
#calculate distance of line from ref point to max alt point: take care of row col
a = ((viewpt_rc[myindex][1]-sta_rc[1])*25)
d = a/math.cos(math.radians(alpha))
######################################AZIMUT 225######################################
if azimut == 225:
#calculate corresponding angle of right-angled triangle
alpha = 45
imax = int(math.floor(viewmax*math.cos(math.radians(alpha))/25))
viewpt_rc = []
for i in range(1,imax):
#incrementation depending on azimut value (take care of x,y and row, col order
ptcol = sta_rc[1]-i
ptrow = sta_rc[0]+i
ptrc=(ptrow, ptcol)
viewpt_rc.append(ptrc)
print "cells collected"
pth = []
for p in viewpt_rc:
pth.append(height[p])
maxh = max(pth)
myindex = pth.index(maxh)
#calculate distance of line from ref point to max alt point: take care of row col
a = ((viewpt_rc[myindex][0]-sta_rc[0])*25)
d = a/math.cos(math.radians(alpha))
######################################AZIMUT 315######################################
if azimut == 315:
#calculate corresponding angle of right-angled triangle
alpha = 45
imax = int(math.floor(viewmax*math.cos(math.radians(alpha))/25))
viewpt_rc = []
for i in range(1,imax):
#incrementation depending on azimut value (take care of x,y and row, col order
ptcol = sta_rc[1]-i
ptrow = sta_rc[0]-i
ptrc=(ptrow, ptcol)
viewpt_rc.append(ptrc)
print "cells collected"
pth = []
for p in viewpt_rc:
pth.append(height[p])
maxh = max(pth)
myindex = pth.index(maxh)
#calculate distance of line from ref point to max alt point: take care of row col
a = ((sta_rc[0]-viewpt_rc[myindex][0])*25)
d = a/math.cos(math.radians(alpha))
######################################AZIMUTS 0 => 45######################################
if azimut > 0 and azimut < 45:
#calculate corresponding angle of right-angled triangle
alpha = azimut
imax = int(math.floor(viewmax*math.cos(math.radians(alpha))/25))
viewpt_rc = []
for i in range(1,imax):
myview = myview+25
c = myview/math.cos(math.radians(alpha))
#incrementation depending on azimut value (take care of x,y and row, col order and sign for ptx (or pty) calcul
ptrow = sta_rc[0]-i
ptx = sta_xy[0]+(math.sin(math.radians(alpha))*c)
for i in range(0,imgx.shape[1]-1):
if ptx-imgx[0,i]>0 and ptx-imgx[0,i+1]<=0:
ptcol = i-mincol
ptrc=(ptrow, ptcol)
viewpt_rc.append(ptrc)
print "cells collected"
pth = []
for p in viewpt_rc:
pth.append(height[p])
maxh = max(pth)
myindex = pth.index(maxh)
#calculate distance of line from ref point to max alt point: take care of row col and order between viewpt and sta
a = ((sta_rc[0]-viewpt_rc[myindex][0])*25)
d = a/math.cos(math.radians(alpha))
######################################AZIMUTS 45 => 90######################################
if azimut > 45 and azimut < 90:
#calculate corresponding angle of right-angled triangle
alpha = 90-azimut
imax = int(math.floor(viewmax*math.cos(math.radians(alpha))/25))
viewpt_rc = []
for i in range(1,imax):
myview = myview+25
c = myview/math.cos(math.radians(alpha))
#incrementation depending on azimut value (take care of x,y and row, col order
ptcol = sta_rc[1]+i
pty = sta_xy[1]+(math.sin(math.radians(alpha))*c)
for i in range(0,imgy.shape[0]-1):
if imgy[i,0]-pty>0 and imgy[i+1,0]-pty<=0:
ptrow = i-minrow
ptrc=(ptrow, ptcol)
viewpt_rc.append(ptrc)
print "cells collected"
pth = []
for p in viewpt_rc:
pth.append(height[p])
maxh = max(pth)
myindex = pth.index(maxh)
#calculate distance of line from ref point to max alt point: take care of row col
a = ((viewpt_rc[myindex][1]-sta_rc[1])*25)
d = a/math.cos(math.radians(alpha))
######################################AZIMUTS 90 => 135######################################
if azimut > 90 and azimut < 135:
#calculate corresponding angle of right-angled triangle
alpha = azimut-90
imax = int(math.floor(viewmax*math.cos(math.radians(alpha))/25))
viewpt_rc = []
for i in range(1,imax):
myview = myview+25
c = myview/math.cos(math.radians(alpha))
#incrementation depending on azimut value (take care of x,y and row, col order
ptcol = sta_rc[1]+i
pty = sta_xy[1]-(math.sin(math.radians(alpha))*c)
for i in range(0,imgy.shape[0]-1):
if imgy[i,0]-pty>0 and imgy[i+1,0]-pty<=0:
ptrow = i-minrow
ptrc=(ptrow, ptcol)
viewpt_rc.append(ptrc)
print "cells collected"
pth = []
for p in viewpt_rc:
pth.append(height[p])
maxh = max(pth)
myindex = pth.index(maxh)
#calculate distance of line from ref point to max alt point: take care of row col and order between viewpt and sta
a = ((viewpt_rc[myindex][1]-sta_rc[1])*25)
d = a/math.cos(math.radians(alpha))
######################################AZIMUTS 135 => 180######################################
if azimut > 135 and azimut < 180:
#calculate corresponding angle of right-angled triangle
alpha = 180-azimut
imax = int(math.floor(viewmax*math.cos(math.radians(alpha))/25))
viewpt_rc = []
for i in range(1,imax):
myview = myview+25
c = myview/math.cos(math.radians(alpha))
#incrementation depending on azimut value (take care of x,y and row, col order
ptrow = sta_rc[0]+i
ptx = sta_xy[0]+(math.sin(math.radians(alpha))*c)
for i in range(0,imgx.shape[1]-1):
if ptx-imgx[0,i]>0 and ptx-imgx[0,i+1]<=0:
ptcol = i-mincol
ptrc=(ptrow, ptcol)
viewpt_rc.append(ptrc)
print "cells collected"
pth = []
for p in viewpt_rc:
pth.append(height[p])
maxh = max(pth)
myindex = pth.index(maxh)
#calculate distance of line from ref point to max alt point: take care of row col and order between viewpt and sta
a = ((viewpt_rc[myindex][0]-sta_rc[0])*25)
d = a/math.cos(math.radians(alpha))
######################################AZIMUTS 180 => 225######################################
if azimut > 180 and azimut < 225:
#calculate corresponding angle of right-angled triangle
alpha = azimut - 180
imax = int(math.floor(viewmax*math.cos(math.radians(alpha))/25))
viewpt_rc = []
for i in range(1,imax):
myview = myview+25
c = myview/math.cos(math.radians(alpha))
#incrementation depending on azimut value (take care of x,y and row, col order and sign for ptx (or pty) calcul
ptrow = sta_rc[0]+i
ptx = sta_xy[0]-(math.sin(math.radians(alpha))*c)
for i in range(0,imgx.shape[1]-1):
if ptx-imgx[0,i]>0 and ptx-imgx[0,i+1]<=0:
ptcol = i-mincol
ptrc=(ptrow, ptcol)
viewpt_rc.append(ptrc)
print "cells collected"
pth = []
for p in viewpt_rc:
pth.append(height[p])
maxh = max(pth)
myindex = pth.index(maxh)
#calculate distance of line from ref point to max alt point: take care of row col and order between viewpt and sta
a = ((viewpt_rc[myindex][0]-sta_rc[0])*25)
d = a/math.cos(math.radians(alpha))
######################################AZIMUTS 225 => 270######################################
if azimut > 225 and azimut < 270:
#calculate corresponding angle of right-angled triangle
alpha = 270 - azimut
imax = int(math.floor(viewmax*math.cos(math.radians(alpha))/25))
viewpt_rc = []
for i in range(1,imax):
myview = myview+25
c = myview/math.cos(math.radians(alpha))
#incrementation depending on azimut value (take care of x,y and row, col order
ptx = sta_xy[0]-25
ptcol = sta_rc[1]-i
pty = sta_xy[1]-(math.sin(math.radians(alpha))*c)
for i in range(0,imgy.shape[0]-1):
if imgy[i,0]-pty>0 and imgy[i+1,0]-pty<=0:
ptrow = i-minrow
ptrc=(ptrow, ptcol)
viewpt_rc.append(ptrc)
print "cells collected"
pth = []
for p in viewpt_rc:
pth.append(height[p])
maxh = max(pth)
myindex = pth.index(maxh)
#calculate distance of line from ref point to max alt point: take care of row col and order between viewpt and sta
a = ((sta_rc[1]-viewpt_rc[myindex][1])*25)
d = a/math.cos(math.radians(alpha))
######################################AZIMUTS 270 => 315######################################
if azimut > 270 and azimut < 315:
#calculate corresponding angle of right-angled triangle
alpha = azimut-270
imax = int(math.floor(viewmax*math.cos(math.radians(alpha))/25))
viewpt_rc = []
for i in range(1,imax):
myview = myview+25
c = myview/math.cos(math.radians(alpha))
#incrementation depending on azimut value (take care of x,y and row, col order
ptx = sta_xy[0]-25
ptcol = sta_rc[1]-i
pty = sta_xy[1]+(math.sin(math.radians(alpha))*c)
for i in range(0,imgy.shape[0]-1):
if imgy[i,0]-pty>0 and imgy[i+1,0]-pty<=0:
ptrow = i-minrow
ptrc=(ptrow, ptcol)
viewpt_rc.append(ptrc)
print "cells collected"
pth = []
for p in viewpt_rc:
pth.append(height[p])
maxh = max(pth)
myindex = pth.index(maxh)
#calculate distance of line from ref point to max alt point: take care of row col and order between viewpt and sta
a = ((sta_rc[1]-viewpt_rc[myindex][1])*25)
d = a/math.cos(math.radians(alpha))
######################################AZIMUTS 315 => 360######################################
if azimut > 315 and azimut < 360:
#calculate corresponding angle of right-angled triangle
alpha = 360-azimut
imax = int(math.floor(viewmax*math.cos(math.radians(alpha))/25))
viewpt_rc = []
for i in range(1,imax):
myview = myview+25
c = myview/math.cos(math.radians(alpha))
#incrementation depending on azimut value (take care of x,y and row, col order and sign for ptx (or pty) calcul
ptrow = sta_rc[0]-i
ptx = sta_xy[0]-(math.sin(math.radians(alpha))*c)
for i in range(0,imgx.shape[1]-1):
if ptx-imgx[0,i]>0 and ptx-imgx[0,i+1]<=0:
ptcol = i-mincol
ptrc=(ptrow, ptcol)
viewpt_rc.append(ptrc)
print "cells collected"
pth = []
for p in viewpt_rc:
pth.append(height[p])
maxh = max(pth)
myindex = pth.index(maxh)
#calculate distance of line from ref point to max alt point: take care of row col and order between viewpt and sta
a = ((sta_rc[0]-viewpt_rc[myindex][0])*25)
d = a/math.cos(math.radians(alpha))
#TEST ANGLE TO SEE UPPER THAN UPPER ELEV
for beta in range(0,90, 2):
mybeta=beta
h = (d*math.sin(math.radians(beta))/math.cos(math.radians(beta)))+height[sta_rc]
if h > maxh:
break
data = (sta[0], azimut, mybeta)
final_data.append(data)
print(data)
final_data.append((final_data[0][0], 360, final_data[0][2]))
#create table in postgres
#load points and view extent
skylinetable=myconn.cursor()
query="""
drop table if exists stations.station_meteo_skyline;
"""
skylinetable.execute(query)
myconn.commit()
query="""
create table stations.station_meteo_skyline(
gid varchar(50),
azimut int4,
angle int4);
"""
skylinetable.execute(query)
myconn.commit()
print "table created"
#insert values into new table
for values in final_data:
skylinetable=myconn.cursor()
query="""
insert into stations.station_meteo_skyline
values(%s, %s, %s);
"""
skylinetable.execute(query,(values[0],values[1], values[2]))
myconn.commit()
print "azimut", values[1], "done"
print(final_data)