vnet_utils.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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(openedMap,
  44. c_char_p(encode(vectMap)),
  45. c_char_p(encode(mapSet)))
  46. if ret == 1:
  47. vectlib.Vect_close(openedMap)
  48. if ret != 2:
  49. return None
  50. nodeNum = vectlib.Vect_find_node(openedMap,
  51. c_double(e),
  52. c_double(n),
  53. c_double(0),
  54. c_double(tresh),
  55. vectlib.WITHOUT_Z)
  56. if nodeNum > 0:
  57. e = c_double(0)
  58. n = c_double(0)
  59. vectlib.Vect_get_node_coor(openedMap,
  60. nodeNum,
  61. byref(e),
  62. byref(n),
  63. None) # z
  64. e = e.value
  65. n = n.value
  66. else:
  67. vectlib.Vect_close(openedMap)
  68. return False
  69. return e, n
  70. def GetNearestNodeCat(e, n, layer, tresh, vectMap):
  71. if not haveCtypes:
  72. return -2
  73. vectMapName, mapSet = ParseMapStr(vectMap)
  74. openedMap = pointer(vectlib.Map_info())
  75. ret = vectlib.Vect_open_old(openedMap,
  76. c_char_p(encode(vectMapName)),
  77. c_char_p(encode(mapSet)))
  78. if ret == 1:
  79. vectlib.Vect_close(openedMap)
  80. if ret != 2:
  81. return -1
  82. nodeNum = vectlib.Vect_find_node(openedMap,
  83. c_double(e),
  84. c_double(n),
  85. c_double(0),
  86. c_double(tresh),
  87. vectlib.WITHOUT_Z)
  88. if nodeNum > 0:
  89. e = c_double(0)
  90. n = c_double(0)
  91. vectlib.Vect_get_node_coor(openedMap,
  92. nodeNum,
  93. byref(e),
  94. byref(n),
  95. None) # z
  96. e = e.value
  97. n = n.value
  98. else:
  99. vectlib.Vect_close(openedMap)
  100. return -1
  101. box = vectlib.bound_box()
  102. List = POINTER(vectlib.boxlist)
  103. List = vectlib.Vect_new_boxlist(c_int(0))
  104. box.E = box.W = e
  105. box.N = box.S = n
  106. box.T = box.B = 0
  107. vectlib.Vect_select_lines_by_box(
  108. openedMap, byref(box),
  109. vectlib.GV_POINT, List)
  110. found = 0
  111. dcost = 0
  112. Cats = POINTER(vectlib.line_cats)
  113. Cats = vectlib.Vect_new_cats_struct()
  114. cat = c_int(0)
  115. for j in range(List.contents.n_values):
  116. line = List.contents.id[j]
  117. type = vectlib.Vect_read_line(openedMap, None, Cats, line)
  118. if type != vectlib.GV_POINT:
  119. continue
  120. if vectlib.Vect_cat_get(Cats, c_int(layer), byref(cat)):
  121. found = 1
  122. break
  123. if found:
  124. return cat.value
  125. return -1