vnet_utils.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. try:
  18. import grass.lib.vector as vectlib
  19. from ctypes import pointer, byref, c_char_p, c_int, c_double, POINTER
  20. haveCtypes = True
  21. except ImportError:
  22. haveCtypes = False
  23. def ParseMapStr(mapStr):
  24. """!Create full map name (add current mapset if it is not present in name)"""
  25. mapValSpl = mapStr.strip().split("@")
  26. if len(mapValSpl) > 1:
  27. mapSet = mapValSpl[1]
  28. else:
  29. mapSet = grass.gisenv()['MAPSET']
  30. mapName = mapValSpl[0]
  31. return mapName, mapSet
  32. def DegreesToRadians(degrees):
  33. return degrees * math.pi / 180
  34. def RadiansToDegrees(radians):
  35. return radians * 180 / math.pi
  36. def SnapToNode(e, n, tresh, vectMap):
  37. """!Find nearest node to click coordinates (within given threshold)"""
  38. if not haveCtypes:
  39. return None
  40. vectMap, mapSet = ParseMapStr(vectMap)
  41. openedMap = pointer(vectlib.Map_info())
  42. ret = vectlib.Vect_open_old(openedMap,
  43. c_char_p(vectMap),
  44. c_char_p(mapSet))
  45. if ret == 1:
  46. vectlib.Vect_close(openedMap)
  47. if ret != 2:
  48. return None
  49. nodeNum = vectlib.Vect_find_node(openedMap,
  50. c_double(e),
  51. c_double(n),
  52. c_double(0),
  53. c_double(tresh),
  54. vectlib.WITHOUT_Z)
  55. if nodeNum > 0:
  56. e = c_double(0)
  57. n = c_double(0)
  58. vectlib.Vect_get_node_coor(openedMap,
  59. nodeNum,
  60. byref(e),
  61. byref(n),
  62. None); # z
  63. e = e.value
  64. n = n.value
  65. else:
  66. vectlib.Vect_close(openedMap)
  67. return False
  68. return e, n
  69. def GetNearestNodeCat(e, n, layer, tresh, vectMap):
  70. if not haveCtypes:
  71. return -2
  72. vectMapName, mapSet = ParseMapStr(vectMap)
  73. openedMap = pointer(vectlib.Map_info())
  74. ret = vectlib.Vect_open_old(openedMap,
  75. c_char_p(vectMapName),
  76. c_char_p(mapSet))
  77. if ret == 1:
  78. vectlib.Vect_close(openedMap)
  79. if ret != 2:
  80. return -1
  81. nodeNum = vectlib.Vect_find_node(openedMap,
  82. c_double(e),
  83. c_double(n),
  84. c_double(0),
  85. c_double(tresh),
  86. vectlib.WITHOUT_Z)
  87. if nodeNum > 0:
  88. e = c_double(0)
  89. n = c_double(0)
  90. vectlib.Vect_get_node_coor(openedMap,
  91. nodeNum,
  92. byref(e),
  93. byref(n),
  94. None); # z
  95. e = e.value
  96. n = n.value
  97. else:
  98. vectlib.Vect_close(openedMap)
  99. return -1
  100. box = vectlib.bound_box();
  101. List = POINTER(vectlib.boxlist);
  102. List = vectlib.Vect_new_boxlist(c_int(0));
  103. box.E = box.W = e;
  104. box.N = box.S = n;
  105. box.T = box.B = 0;
  106. vectlib.Vect_select_lines_by_box(openedMap, byref(box), vectlib.GV_POINT, List);
  107. found = 0;
  108. dcost = 0;
  109. Cats = POINTER(vectlib.line_cats)
  110. Cats = vectlib.Vect_new_cats_struct()
  111. cat = c_int(0)
  112. for j in range(List.contents.n_values):
  113. line = List.contents.id[j]
  114. type = vectlib.Vect_read_line(openedMap, None, Cats, line)
  115. if type != vectlib.GV_POINT:
  116. continue
  117. if vectlib.Vect_cat_get(Cats, c_int(layer), byref(cat)):
  118. found = 1
  119. break
  120. if found:
  121. return cat.value
  122. return -1