1#!/usr/local/bin/python3.8 2# -*- coding: utf-8 -*- 3############################################################################ 4# 5# MODULE: t.merge 6# AUTHOR(S): Soeren Gebbert 7# 8# PURPOSE: Merge several space time datasets into a single one 9# COPYRIGHT: (C) 2011-2017 by the GRASS Development Team 10# 11# This program is free software; you can redistribute it and/or modify 12# it under the terms of the GNU General Public License as published by 13# the Free Software Foundation; either version 2 of the License, or 14# (at your option) any later version. 15# 16# This program is distributed in the hope that it will be useful, 17# but WITHOUT ANY WARRANTY; without even the implied warranty of 18# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19# GNU General Public License for more details. 20# 21############################################################################# 22 23#%module 24#% description: Merges several space time datasets into a single space time dataset. 25#% keyword: temporal 26#% keyword: time management 27#% keyword: merge 28#% keyword: time 29#%end 30 31#%option G_OPT_STDS_INPUTS 32#%end 33 34#%option G_OPT_STDS_OUTPUT 35#%end 36 37#%option G_OPT_STDS_TYPE 38#% guidependency: inputs 39#% guisection: Required 40#%end 41 42import grass.script as grass 43 44 45############################################################################ 46grass.set_raise_on_error(True) 47 48def main(): 49 # lazy imports 50 import grass.temporal as tgis 51 52 # Get the options 53 inputs = options["inputs"] 54 output = options["output"] 55 type = options["type"] 56 57 # Make sure the temporal database exists 58 tgis.init() 59 60 #Get the current mapset to create the id of the space time dataset 61 mapset = grass.gisenv()["MAPSET"] 62 63 inputs_split = inputs.split(",") 64 input_ids = [] 65 66 for input in inputs_split: 67 if input.find("@") >= 0: 68 input_ids.append(input) 69 else: 70 input_ids.append(input + "@" + mapset) 71 72 # Set the output name correct 73 if output.find("@") >= 0: 74 out_mapset = output.split("@")[1] 75 if out_mapset != mapset: 76 grass.fatal(_("Output space time dataset <%s> must be located in this mapset") % (output)) 77 else: 78 output_id = output + "@" + mapset 79 80 dbif = tgis.SQLDatabaseInterfaceConnection() 81 dbif.connect() 82 83 stds_list = [] 84 first = None 85 86 for id in input_ids: 87 stds = tgis.open_old_stds(id, type, dbif) 88 if first is None: 89 first = stds 90 91 if first.get_temporal_type() != stds.get_temporal_type(): 92 dbif.close() 93 grass.fatal(_("Space time datasets to merge must have the same temporal type")) 94 95 stds_list.append(stds) 96 97 # Do nothing if nothing to merge 98 if first is None: 99 dbif.close() 100 return 101 102 # Check if the new id is in the database 103 output_stds = tgis.dataset_factory(type, output_id) 104 output_exists = output_stds.is_in_db(dbif=dbif) 105 106 if output_exists == True and grass.overwrite() == False: 107 dbif.close() 108 grass.fatal(_("Unable to merge maps into space time %s dataset <%s> "\ 109 "please use the overwrite flag.") % \ 110 (stds.get_new_map_instance(None).get_type(), output_id)) 111 112 if not output_exists: 113 output_stds = tgis.open_new_stds(output, type, 114 first.get_temporal_type(), 115 "Merged space time dataset", 116 "Merged space time dataset", 117 "mean", dbif=dbif, overwrite=False) 118 else: 119 output_stds.select(dbif=dbif) 120 121 registered_output_maps = {} 122 # Maps that are already registered in an existing dataset 123 # are not registered again 124 if output_exists == True: 125 rows = output_stds.get_registered_maps(columns="id", dbif=dbif) 126 if rows: 127 for row in rows: 128 registered_output_maps[row["id"]] = row["id"] 129 130 for stds in stds_list: 131 # Avoid merging of already registered maps 132 if stds.get_id() != output_stds.get_id(): 133 maps = stds.get_registered_maps_as_objects(dbif=dbif) 134 135 if maps: 136 for map in maps: 137 # Jump over already registered maps 138 if map.get_id() in registered_output_maps: 139 continue 140 141 map.select(dbif=dbif) 142 output_stds.register_map(map=map, dbif=dbif) 143 # Update the registered map list 144 registered_output_maps[map.get_id()] = map.get_id() 145 146 output_stds.update_from_registered_maps(dbif=dbif) 147 148 if output_exists == True: 149 output_stds.update_command_string(dbif=dbif) 150 151if __name__ == "__main__": 152 options, flags = grass.parser() 153 main() 154