1# -*- coding: utf-8 -*-

2 from math import ceil

3

4 from qgis.core import *

5 from qgis.gui import *

6#from PyQt4.QtGui import *

7 from PyQt5.QtWidgets import *

8 from PyQt5.QtCore import *

9 import os

10 from .utils import *

11 import numpy as np

12

13

14 def get_flow_layer(fd_layer, canvas, stream_layer,strFDType,_size):

15 asc_file_fd = fd_layer.dataProvider().dataSourceUri()

16 ascii_grid_fd = np.loadtxt(asc_file_fd, skiprows=6) # skip 6 . caution!

17

18 asc_file_st = stream_layer.dataProvider().dataSourceUri()

19 ascii_grid_st = np.loadtxt(asc_file_st, skiprows=6) # skip 6 . caution!

20

21 xmin, ymin, xmax, ymax = fd_layer.extent().toRectF().getCoords()

22 crs = fd_layer.crs().toWkt()

23 point_layer = QgsVectorLayer('Point?crs=' + crs, 'flow_layer', 'memory')

24 point_provider = point_layer.dataProvider()

25

26 resV = point_provider.addAttributes([QgsField("cellvalue", QVariant.Int)])

27 resS = point_provider.addAttributes([QgsField("stream", QVariant.Int)])

28

29 gridWidth = fd_layer.rasterUnitsPerPixelX()

30 gridHeight = fd_layer.rasterUnitsPerPixelY()

31

32 rows = ceil((ymax - ymin) / gridHeight)

33 cols = ceil((xmax - xmin) / gridWidth)

34

35 point_layer.startEditing() # if omitted , setAttributes has no effect.

36

37 # Reference : GRM Manual page 25 a b c d . 2018.3

38 if strFDType == 'StartsFromN':

39 MyDirections = {1: 0, 2: 45, 4: 90, 8: 135, 16: 180, 32: 225, 64: 270, 128: 315}

40 elif strFDType == 'StartsFromNE':

41 MyDirections = {128: 0, 1: 45, 2: 90, 4: 135, 8: 180, 16: 225, 32: 270, 64: 315} # HyGIS(O)

42 elif strFDType == 'StartsFromE':

43 MyDirections = {64: 0, 128: 45, 1: 90, 2: 135, 4: 180, 8: 225, 16: 270, 32: 315} # HyGIS(x). TOPAZ

44 elif strFDType == 'StartsFromE_TauDEM':

45 MyDirections = {3: 0, 2: 45, 1: 90, 8: 135, 7: 180, 6: 225, 5: 270, 4: 315} # TauDEM

46 else:

47 print("FD Code is Wrong!")

48 return

49

50 # strStylePath = "C:\GRM\sample\data\FD_Style_Template_v3.qml" # We Will change the path to relative path

51 strStylePath = os.path.dirname(os.path.realpath(__file__))

52 strStylePath = strStylePath[:-6] + "DLL\FD_Style_Template_v5.qml"

53

54 lines = []

55 with open(strStylePath, 'r+') as f:

56 lines = f.readlines()

57 for i in [35, 61]:

58# for i in [35, 60]:

59 marker = lines[i]

60 frist = marker.index('v=')+3

61 last = marker.index('"', frist, len(marker))

62

63 lines[i] = marker[:frist]+str(_size)+marker[last:]

64

65 with open(strStylePath, 'w') as f:

66 f.writelines(lines)

67

68

69

70

71

72 point_layer.loadNamedStyle(strStylePath)

73

74 for i in range(int(cols)):

75 for j in range(int(rows)):

76 direction = get_direction(i, rows - j - 1, ascii_grid_fd, MyDirections)

77 if not direction == None:

78 ptCellCenter = QgsPointXY(xmin + i * gridWidth + gridWidth / 2.0,

79 ymin + j * gridHeight + gridHeight / 2.0)

80 feat = QgsFeature()

81 feat.setGeometry(QgsGeometry().fromPointXY(ptCellCenter))

82 streamTag = 0

83 if ascii_grid_st[rows - j - 1, i] > 0: streamTag = 1

84 feat.setAttributes([direction, streamTag])

85 point_provider.addFeatures([feat])

86 point_layer.commitChanges()

87 point_layer.updateExtents()

88 point_layer.triggerRepaint()

89

90

91

92

93

94

95

96

97

98

99 return point_layer

100

101

102 def get_direction(x, y, ascii_grid, directions):

103 value = None

104 try:

105 value = ascii_grid[y][x] # order is [y][x]. top to bottom, zero base

106 direction = directions.get(value)

107 return direction

108

109 except IndexError:

110 return "Intentional except"