File: t.remove.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 (210 lines) | stat: -rwxr-xr-x 6,546 bytes parent folder | download | duplicates (3)
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env python3

############################################################################
#
# MODULE:   t.remove
# AUTHOR(S):    Soeren Gebbert
#
# PURPOSE:  Remove space time datasets from the temporal database
# COPYRIGHT:    (C) 2011-2017 by the GRASS Development Team
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#############################################################################

# %module
# % description: Removes space time datasets from temporal database.
# % keyword: temporal
# % keyword: map management
# % keyword: remove
# % keyword: time
# %end

# %option G_OPT_STDS_INPUTS
# % guisection: Input
# % required: no
# %end

# %option
# % key: type
# % type: string
# % description: Type of the space time dataset, default is strds
# % guidependency: inputs
# % guisection: Input
# % required: no
# % options: strds, str3ds, stvds
# % answer: strds
# %end

# %option G_OPT_F_INPUT
# % key: file
# % description: Input file with dataset names, one per line
# % guisection: Input
# % required: no
# %end

# %flag
# % key: r
# % description: Remove stds and unregister maps from temporal database
# %end

# %flag
# % key: f
# % description: Force removal (required for actual deletion of files)
# %end

# %flag
# % key: d
# % description: Remove stds, unregister maps from temporal database and delete them from mapset
# %end

import grass.script as grass


# lazy imports at the end of the file

############################################################################


def main():
    # Get the options
    datasets = options["inputs"]
    file = options["file"]
    type = options["type"]
    recursive = flags["r"]
    force = flags["f"]
    clean = flags["d"]

    if datasets and file:
        grass.fatal(_("%s= and %s= are mutually exclusive") % ("input", "file"))

    # Make sure the temporal database exists
    tgis.init()

    dbif = tgis.SQLDatabaseInterfaceConnection()
    dbif.connect()

    dataset_list = []

    # Dataset names as comma separated string
    if datasets:
        if datasets.find(",") == -1:
            dataset_list = (datasets,)
        else:
            dataset_list = tuple(datasets.split(","))

    # Read the dataset list from file
    if file:
        fd = open(file, "r")

        line = True
        while True:
            line = fd.readline()
            if not line:
                break

            line_list = line.split("\n")
            dataset_name = line_list[0]
            dataset_list.append(dataset_name)

    statement = ""

    # Create the pygrass Module object for g.remove
    remove = pyg.Module("g.remove", quiet=True, flags="f", run_=False)

    if not force:
        grass.message(_("The following data base element files will be deleted:"))

    for name in dataset_list:
        name = name.strip()
        sp = tgis.open_old_stds(name, type, dbif)
        if not force:
            grass.message(
                _("{stds}: {gid}".format(stds=sp.get_type().upper(), gid=sp.get_id()))
            )
        if recursive or clean:
            if not force:
                if recursive:
                    msg = (
                        "The following maps of {stds} {gid} will be "
                        "unregistered from temporal database:"
                    )
                elif clean:
                    msg = (
                        "The following maps of {stds} {gid} will be "
                        "unregistered from temporal database and removed "
                        "from spatial database:"
                    )
                grass.message(_(msg.format(stds=sp.get_type(), gid=sp.get_id())))
            maps = sp.get_registered_maps_as_objects(dbif=dbif)
            map_statement = ""
            count = 1
            name_list = []
            for map in maps:
                map.select(dbif)
                # We may have multiple layer for a single map, hence we need
                # to avoid multiple deletation of the same map,
                # but the database entries are still present and must be removed
                if not force:
                    grass.message(_("- %s" % map.get_name()))
                    continue
                if clean and force:
                    if map.get_name() not in name_list:
                        name_list.append(str(map.get_name()))
                map_statement += map.delete(dbif=dbif, execute=False)

                count += 1
                # Delete every 100 maps
                if count % 100 == 0:
                    dbif.execute_transaction(map_statement)
                    if clean:
                        if type == "strds":
                            remove(type="raster", name=name_list, run_=True)
                        if type == "stvds":
                            remove(type="vector", name=name_list, run_=True)
                        if type == "str3ds":
                            remove(type="raster_3d", name=name_list, run_=True)
                    map_statement = ""
                    name_list = []

            if map_statement:
                dbif.execute_transaction(map_statement)
            if clean and name_list:
                if type == "strds":
                    remove(type="raster", name=name_list, run_=True)
                if type == "stvds":
                    remove(type="vector", name=name_list, run_=True)
                if type == "str3ds":
                    remove(type="raster_3d", name=name_list, run_=True)
        if force:
            statement += sp.delete(dbif=dbif, execute=False)

    if not force:
        grass.message(
            _(
                "Nothing removed. You must use the force flag (-{flag}) to actually "
                "remove them.".format(flag="f")
            )
        )
    else:
        # Execute the collected SQL statenents
        dbif.execute_transaction(statement)
        dbif.close()


if __name__ == "__main__":
    options, flags = grass.parser()

    # lazy imports
    import grass.temporal as tgis
    import grass.pygrass.modules as pyg

    tgis.profile_function(main)