-
-
Notifications
You must be signed in to change notification settings - Fork 294
/
t.merge.py
executable file
·147 lines (117 loc) · 4.41 KB
/
t.merge.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
############################################################################
#
# MODULE: t.merge
# AUTHOR(S): Soeren Gebbert
#
# PURPOSE: Merge several space time datasets into a single one
# COPYRIGHT: (C) 2011-2014 by the GRASS Development Team
#
# This program is free software under the GNU General Public
# License (version 2). Read the file COPYING that comes with GRASS
# for details.
#
#############################################################################
#%module
#% description: Merges several space time datasets into a single space time dataset.
#% keyword: temporal
#% keyword: time management
#% keyword: merge
#% keyword: time
#%end
#%option G_OPT_STDS_INPUTS
#%end
#%option G_OPT_STDS_OUTPUT
#%end
#%option G_OPT_STDS_TYPE
#% guidependency: inputs
#% guisection: Required
#%end
import grass.script as grass
############################################################################
grass.set_raise_on_error(True)
def main():
# lazy imports
import grass.temporal as tgis
# Get the options
inputs = options["inputs"]
output = options["output"]
type = options["type"]
# Make sure the temporal database exists
tgis.init()
#Get the current mapset to create the id of the space time dataset
mapset = grass.encode(grass.gisenv()["MAPSET"])
inputs_split = inputs.split(",")
input_ids = []
for input in inputs_split:
if input.find("@") >= 0:
input_ids.append(input)
else:
input_ids.append(input + "@" + mapset)
# Set the output name correct
if output.find("@") >= 0:
out_mapset = output.split("@")[1]
if out_mapset != mapset:
grass.fatal(_("Output space time dataset <%s> must be located in this mapset") % (output))
else:
output_id = output + "@" + mapset
dbif = tgis.SQLDatabaseInterfaceConnection()
dbif.connect()
stds_list = []
first = None
for id in input_ids:
stds = tgis.open_old_stds(id, type, dbif)
if first is None:
first = stds
if first.get_temporal_type() != stds.get_temporal_type():
dbif.close()
grass.fatal(_("Space time datasets to merge must have the same temporal type"))
stds_list.append(stds)
# Do nothing if nothing to merge
if first is None:
dbif.close()
return
# Check if the new id is in the database
output_stds = tgis.dataset_factory(type, output_id)
output_exists = output_stds.is_in_db(dbif=dbif)
if output_exists == True and grass.overwrite() == False:
dbif.close()
grass.fatal(_("Unable to merge maps into space time %s dataset <%s> "\
"please use the overwrite flag.") % \
(stds.get_new_map_instance(None).get_type(), output_id))
if not output_exists:
output_stds = tgis.open_new_stds(output, type,
first.get_temporal_type(),
"Merged space time dataset",
"Merged space time dataset",
"mean", dbif=dbif, overwrite=False)
else:
output_stds.select(dbif=dbif)
registered_output_maps = {}
# Maps that are already registered in an existing dataset
# are not registered again
if output_exists == True:
rows = output_stds.get_registered_maps(columns="id", dbif=dbif)
if rows:
for row in rows:
registered_output_maps[row["id"]] = row["id"]
for stds in stds_list:
# Avoid merging of already registered maps
if stds.get_id() != output_stds.get_id():
maps = stds.get_registered_maps_as_objects(dbif=dbif)
if maps:
for map in maps:
# Jump over already registered maps
if map.get_id() in registered_output_maps:
continue
map.select(dbif=dbif)
output_stds.register_map(map=map, dbif=dbif)
# Update the registered map list
registered_output_maps[map.get_id()] = map.get_id()
output_stds.update_from_registered_maps(dbif=dbif)
if output_exists == True:
output_stds.update_command_string(dbif=dbif)
if __name__ == "__main__":
options, flags = grass.parser()
main()