File: vnet_utils.py

package info (click to toggle)
grass 8.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 277,040 kB
  • sloc: ansic: 460,798; python: 227,732; cpp: 42,026; sh: 11,262; makefile: 7,007; xml: 3,637; sql: 968; lex: 520; javascript: 484; yacc: 450; asm: 387; perl: 157; sed: 25; objc: 6; ruby: 4
file content (155 lines) | stat: -rw-r--r-- 3,778 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
"""
@package vnet.vnet_utils

@brief Vector network analysis utilities.

Classes:
 - vnet_core::VNETTmpVectMaps
 - vnet_core::VectMap
 - vnet_core::History

(C) 2013 by the GRASS Development Team

This program is free software under the GNU General Public License
(>=v2). Read the file COPYING that comes with GRASS for details.

@author Stepan Turek <stepan.turek seznam.cz> (GSoC 2012, mentor: Martin Landa)
@author Lukas Bocan <silent_bob centrum.cz> (turn costs support)
@author Eliska Kyzlikova <eliska.kyzlikova gmail.com> (turn costs support)
"""

import math
from grass.script import core as grass
from grass.script.utils import encode

try:
    import grass.lib.vector as vectlib
    from ctypes import pointer, byref, c_char_p, c_int, c_double, POINTER

    haveCtypes = True
except ImportError:
    haveCtypes = False


def ParseMapStr(mapStr):
    """Create full map name (add current mapset if it is not present in name)"""
    mapValSpl = mapStr.strip().split("@")
    if len(mapValSpl) > 1:
        mapSet = mapValSpl[1]
    else:
        mapSet = grass.gisenv()["MAPSET"]
    mapName = mapValSpl[0]

    return mapName, mapSet


def DegreesToRadians(degrees):
    return degrees * math.pi / 180


def RadiansToDegrees(radians):
    return radians * 180 / math.pi


def SnapToNode(e, n, tresh, vectMap):
    """Find nearest node to click coordinates (within given threshold)"""
    if not haveCtypes:
        return None

    vectMap, mapSet = ParseMapStr(vectMap)

    openedMap = pointer(vectlib.Map_info())
    ret = vectlib.Vect_open_old(
        openedMap, c_char_p(encode(vectMap)), c_char_p(encode(mapSet))
    )
    if ret == 1:
        vectlib.Vect_close(openedMap)
    if ret != 2:
        return None

    nodeNum = vectlib.Vect_find_node(
        openedMap,
        c_double(e),
        c_double(n),
        c_double(0),
        c_double(tresh),
        vectlib.WITHOUT_Z,
    )

    if nodeNum > 0:
        e = c_double(0)
        n = c_double(0)
        vectlib.Vect_get_node_coor(openedMap, nodeNum, byref(e), byref(n), None)  # z
        e = e.value
        n = n.value
    else:
        vectlib.Vect_close(openedMap)
        return False

    return e, n


def GetNearestNodeCat(e, n, layer, tresh, vectMap):
    if not haveCtypes:
        return -2

    vectMapName, mapSet = ParseMapStr(vectMap)

    openedMap = pointer(vectlib.Map_info())
    ret = vectlib.Vect_open_old(
        openedMap, c_char_p(encode(vectMapName)), c_char_p(encode(mapSet))
    )
    if ret == 1:
        vectlib.Vect_close(openedMap)
    if ret != 2:
        return -1

    nodeNum = vectlib.Vect_find_node(
        openedMap,
        c_double(e),
        c_double(n),
        c_double(0),
        c_double(tresh),
        vectlib.WITHOUT_Z,
    )

    if nodeNum > 0:
        e = c_double(0)
        n = c_double(0)
        vectlib.Vect_get_node_coor(openedMap, nodeNum, byref(e), byref(n), None)  # z
        e = e.value
        n = n.value
    else:
        vectlib.Vect_close(openedMap)
        return -1

    box = vectlib.bound_box()
    List = POINTER(vectlib.boxlist)
    List = vectlib.Vect_new_boxlist(c_int(0))

    box.E = box.W = e
    box.N = box.S = n
    box.T = box.B = 0
    vectlib.Vect_select_lines_by_box(openedMap, byref(box), vectlib.GV_POINT, List)

    found = 0
    dcost = 0

    Cats = POINTER(vectlib.line_cats)
    Cats = vectlib.Vect_new_cats_struct()

    cat = c_int(0)

    for j in range(List.contents.n_values):
        line = List.contents.id[j]
        type = vectlib.Vect_read_line(openedMap, None, Cats, line)
        if type != vectlib.GV_POINT:
            continue

        if vectlib.Vect_cat_get(Cats, c_int(layer), byref(cat)):
            found = 1
            break
    if found:
        return cat.value

    return -1