# Coverage for grm\plugin\flow_layer_back.py : 0%

## 60 statements

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

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 PyQt4.QtCore import *

8 import os

9 import utils

10 import numpy as np

11

12 def get_flow_layer(fd_layer,canvas,stream_layer):

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

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

15

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

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

18

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

20 crs = fd_layer.crs().toWkt()

21 point_layer = QgsVectorLayer('Point?crs=' + crs, 'Arrow', 'memory')

22 point_provider = point_layer.dataProvider()

23

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

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

26

27 gridWidth = fd_layer.rasterUnitsPerPixelX()

28 gridHeight = fd_layer.rasterUnitsPerPixelY()

29

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

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

32

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

34

35 if is_TauDEM_FD(ascii_grid_fd):

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

37 else:

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

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

40

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

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

43 strStylePath = strStylePath[:-6] + "DLL\FD_Style_Template_v3.qml" # We Will change the path to relative path

44

45 myColour = QtGui.QColor('#ffee00')

46 mySymbol1 = QgsSymbolV2.defaultSymbol(myVectorLayer.geometryType())

47 mySymbol1.setColor(myColour)

48

49 #2020-01-20 박: 색상 지정으로 임시 주석

50 #point_layer.loadNamedStyle(strStylePath)

51

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

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

54 ptCellCenter = QgsPoint(xmin + i * gridWidth + gridWidth / 2.0, ymin + j * gridHeight + gridHeight / 2.0)

55

56 feat = QgsFeature()

57 feat.setGeometry(QgsGeometry().fromPoint(ptCellCenter))

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

59 streamTag = 0

60 if ascii_grid_st[rows - j - 1, i] > 0:

61 streamTag = 1

62 feat.setAttributes([direction,streamTag])

63 point_provider.addFeatures([feat])

64

65 point_layer.commitChanges()

66

67 # symbol_layer = QgsMarkerSymbolV2.createSimple({'name': 'arrow', 'color': 'blue', 'size': '15', 'size_unit': 'mm'})

68 # symbol_layer.setDataDefinedAngle(QgsDataDefined("cellvalue"))

69 # renderer = QgsSingleSymbolRendererV2(symbol_layer)

70 # point_layer.setRendererV2(renderer)

71

72 point_layer.updateExtents()

73

74 point_layer.triggerRepaint()

75

76 return point_layer

77

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

79 value = None

80 try:

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

82 direction = directions.get(value)

83 return direction

84

85 except IndexError:

86 return "Intentional except"

87

88

89 def is_TauDEM_FD(ascii_grid):

90 # 20127.11.2 Ice : if all cell values are 1,2,4,8 , then this check is not good.

91 myMax = max(ascii_grid.flatten())

92# print ("MAX CELL ", myMax)

93 if max(ascii_grid.flatten()) < 9:

94 return True

95 else:

96 return False