vnet_utils.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. """
  2. @package vnet.vnet_utils
  3. @brief Vector network analysis utilities.
  4. Classes:
  5. - vnet_core::VNETTmpVectMaps
  6. - vnet_core::VectMap
  7. - vnet_core::History
  8. (C) 2013 by the GRASS Development Team
  9. This program is free software under the GNU General Public License
  10. (>=v2). Read the file COPYING that comes with GRASS for details.
  11. @author Stepan Turek <stepan.turek seznam.cz> (GSoC 2012, mentor: Martin Landa)
  12. @author Lukas Bocan <silent_bob centrum.cz> (turn costs support)
  13. @author Eliska Kyzlikova <eliska.kyzlikova gmail.com> (turn costs support)
  14. """
  15. import math
  16. from grass.script import core as grass
  17. from grass.script.utils import encode
  18. try:
  19. import grass.lib.vector as vectlib
  20. from ctypes import pointer, byref, c_char_p, c_int, c_double, POINTER
  21. haveCtypes = True
  22. except ImportError:
  23. haveCtypes = False
  24. def ParseMapStr(mapStr):
  25. """Create full map name (add current mapset if it is not present in name)"""
  26. mapValSpl = mapStr.strip().split("@")
  27. if len(mapValSpl) > 1:
  28. mapSet = mapValSpl[1]
  29. else:
  30. mapSet = grass.gisenv()["MAPSET"]
  31. mapName = mapValSpl[0]
  32. return mapName, mapSet
  33. def DegreesToRadians(degrees):
  34. return degrees * math.pi / 180
  35. def RadiansToDegrees(radians):
  36. return radians * 180 / math.pi
  37. def SnapToNode(e, n, tresh, vectMap):
  38. """Find nearest node to click coordinates (within given threshold)"""
  39. if not haveCtypes:
  40. return None
  41. vectMap, mapSet = ParseMapStr(vectMap)
  42. openedMap = pointer(vectlib.Map_info())
  43. ret = vectlib.Vect_open_old(
  44. openedMap, c_char_p(encode(vectMap)), c_char_p(encode(mapSet))
  45. )
  46. if ret == 1:
  47. vectlib.Vect_close(openedMap)
  48. if ret != 2:
  49. return None
  50. nodeNum = vectlib.Vect_find_node(
  51. openedMap,
  52. c_double(e),
  53. c_double(n),
  54. c_double(0),
  55. c_double(tresh),
  56. vectlib.WITHOUT_Z,
  57. )
  58. if nodeNum > 0:
  59. e = c_double(0)
  60. n = c_double(0)
  61. vectlib.Vect_get_node_coor(openedMap, nodeNum, byref(e), byref(n), None) # z
  62. e = e.value
  63. n = n.value
  64. else:
  65. vectlib.Vect_close(openedMap)
  66. return False
  67. return e, n
  68. def GetNearestNodeCat(e, n, layer, tresh, vectMap):
  69. if not haveCtypes:
  70. return -2
  71. vectMapName, mapSet = ParseMapStr(vectMap)
  72. openedMap = pointer(vectlib.Map_info())
  73. ret = vectlib.Vect_open_old(
  74. openedMap, c_char_p(encode(vectMapName)), c_char_p(encode(mapSet))
  75. )
  76. if ret == 1:
  77. vectlib.Vect_close(openedMap)
  78. if ret != 2:
  79. return -1
  80. nodeNum = vectlib.Vect_find_node(
  81. openedMap,
  82. c_double(e),
  83. c_double(n),
  84. c_double(0),
  85. c_double(tresh),
  86. vectlib.WITHOUT_Z,
  87. )
  88. if nodeNum > 0:
  89. e = c_double(0)
  90. n = c_double(0)
  91. vectlib.Vect_get_node_coor(openedMap, nodeNum, byref(e), byref(n), None) # z
  92. e = e.value
  93. n = n.value
  94. else:
  95. vectlib.Vect_close(openedMap)
  96. return -1
  97. box = vectlib.bound_box()
  98. List = POINTER(vectlib.boxlist)
  99. List = vectlib.Vect_new_boxlist(c_int(0))
  100. box.E = box.W = e
  101. box.N = box.S = n
  102. box.T = box.B = 0
  103. vectlib.Vect_select_lines_by_box(openedMap, byref(box), vectlib.GV_POINT, List)
  104. found = 0
  105. dcost = 0
  106. Cats = POINTER(vectlib.line_cats)
  107. Cats = vectlib.Vect_new_cats_struct()
  108. cat = c_int(0)
  109. for j in range(List.contents.n_values):
  110. line = List.contents.id[j]
  111. type = vectlib.Vect_read_line(openedMap, None, Cats, line)
  112. if type != vectlib.GV_POINT:
  113. continue
  114. if vectlib.Vect_cat_get(Cats, c_int(layer), byref(cat)):
  115. found = 1
  116. break
  117. if found:
  118. return cat.value
  119. return -1