File: functions.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 (167 lines) | stat: -rw-r--r-- 4,941 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
156
157
158
159
160
161
162
163
164
165
166
167
"""
Created on Mon Nov 26 11:48:03 2012

@author: lucadelu
"""

import wx
import os
import sys
from grass.script import core as grass
from core.gcmd import GError


class SamplingType:
    """ "
    KMVWINC = samplingtype=moving, regionbox=keyboard, shape=circle
    KMVWINR = samplingtype moving, regionbox=keyboard, shape=rectangle
    MMVWINC = samplingtype=moving, regionbox=mouse, shape=circle
    MMVWINR = samplingtype moving, regionbox=mouse, shape=rectangle

    KUNITSC = samplingtype=units, regionbox=keyboard, shape=cirlce
    KUNITSR = samplingtype=units, regionbox=keyboard, shape=rectangle
    MUNITSC = samplingtype=units, regionbox=mouse, shape=cirlce
    MUNITSR = samplingtype=units, regionbox=mouse, shape=rectangle
    """

    WHOLE = "whole"
    REGIONS = "regions"
    UNITS = "units"
    VECT = "vector"
    MVWIN = "moving"

    KMVWINC = "kmvwin_circle"
    KMVWINR = "kmvwin_rectangle"
    MMVWINC = "mmvwin_circle"
    MMVWINR = "mmvwin_rectangle"

    KUNITSC = "kunits_circle"
    KUNITSR = "kunits_rectangle"
    MUNITSC = "munits_circle"
    MUNITSR = "munits_rectangle"


def checkValue(value):
    if value == "":
        wx.FindWindowById(wx.ID_FORWARD).Enable(False)
    else:
        wx.FindWindowById(wx.ID_FORWARD).Enable(True)


def retRLiPath():
    """Return the directory of configuration files for r.li"""
    if sys.platform == "win32":
        grass_config_dirname = "GRASS8"
        grass_config_dir = os.path.join(os.getenv("APPDATA"), grass_config_dirname)
    else:
        grass_config_dirname = ".grass8"
        grass_config_dir = os.path.join(os.getenv("HOME"), grass_config_dirname)

    rlipath = os.path.join(grass_config_dir, "r.li")
    if os.path.exists(rlipath):
        return rlipath
    else:
        os.mkdir(rlipath)
        return rlipath


def checkMapExists(name, typ="raster"):
    """Check if a map already exist in the working mapset"""
    env = grass.gisenv()
    mapset = env["MAPSET"]
    mapp = grass.find_file(name, typ, mapset)
    if mapp.name != "":
        return True
    else:
        return False


def convertFeature(vect, outrast, cat, origrast, layer="1", overwrite=False):
    """Convert a single feature to a raster"""
    tmp_vect = "tmp_{rast}".format(rast=outrast)
    grass.run_command(
        "v.extract",
        input=vect,
        cats=cat,
        type="area",
        layer=layer,
        output=tmp_vect,
        flags="d",
        overwrite=overwrite,
        quiet=True,
    )
    grass.run_command("g.region", raster=origrast)
    grass.run_command("g.region", vector=tmp_vect)
    grass.run_command("g.region", align=origrast)
    grass.run_command(
        "v.to.rast",
        input=tmp_vect,
        type="area",
        layer=layer,
        use="value",
        value=cat,
        output=outrast,
        overwrite=overwrite,
        quiet=True,
    )
    grass.run_command("g.remove", flags="f", type="vector", name=tmp_vect, quiet=True)


def obtainCategories(vector, layer="1"):
    """This function returns a list of categories for all areas in
    the given layer"""
    vect_cats = []
    vc = grass.read_command(
        "v.category", input=vector, layer=layer, option="print", type="centroid"
    )
    for lc in vc.splitlines():
        for cat in lc.split("/"):
            vect_cats.append(int(cat))

    return sorted(set(vect_cats))


def obtainAreaVector(outrast):
    """Create the string for configuration file"""
    reg = grass.region()
    return "MASKEDOVERLAYAREA {name}|{n}|{s}|{e}|{w}\n".format(
        name=outrast, n=reg["n"], s=reg["s"], e=reg["e"], w=reg["w"]
    )


def sampleAreaVector(
    vect, rast, vect_cats, layer="1", overwrite=False, progDialog=None
):
    """Create the strings to add to the configuration file using vector"""
    areanum = len(vect_cats)
    output = []
    # TODO if areanum == 0 exit from the program
    if areanum == 0:
        GError(message=_("The polygon seems to have 0 areas"))
        return None
    for n in range(areanum):
        cat = str(vect_cats[n])
        outpref = "{rast}_{vect}_".format(
            vect=vect.split("@")[0], rast=rast.split("@")[0]
        )
        rast_name = "{pref}{cat}".format(pref=outpref, cat=cat)
        # check if raster already axist

        if (
            len(grass.list_strings("raster", pattern=rast_name, mapset=".")) == 1
            and not overwrite
        ):
            GError(
                message=_(
                    "The raster map <%s> already exists."
                    " Please remove or rename the maps "
                    "with the prefix '%s' or select the "
                    "option to overwrite existing maps" % (rast_name, outpref)
                )
            )
            return None
        convertFeature(vect, rast_name, cat, rast, layer, overwrite)
        output.append(obtainAreaVector(rast_name))
        if progDialog:
            progDialog.Update(n)
    return output