def registerToTemporal(basename, suffixes, mapset, start_day, day_step, title, desc): """ Register daily output maps in spatio-temporal raster data set """ maps = ','.join( [basename + suf + '@' + mapset for suf in suffixes]) tgis.open_new_stds(basename, type='strds', temporaltype='relative', title=title, descr=desc, semantic='sum', dbif=None, overwrite=grass.overwrite()) tgis.register_maps_in_space_time_dataset(type='rast', name=basename, maps=maps, start=start_day, end=None, unit='days', increment=day_step, dbif=None, interval=False)
def setUp(self): """Create the test maps and the space time raster datasets""" self.runModule( "r.mapcalc", overwrite=True, quiet=True, expression="register_map_1 = 1" ) self.runModule( "r.mapcalc", overwrite=True, quiet=True, expression="register_map_2 = 2" ) self.runModule( "r.mapcalc", overwrite=True, quiet=True, expression="register_map_null = null()", ) self.strds_abs = tgis.open_new_stds( name="register_test_abs", type="strds", temporaltype="absolute", title="Test strds", descr="Test strds", semantic="field", overwrite=True, ) self.strds_rel = tgis.open_new_stds( name="register_test_rel", type="strds", temporaltype="relative", title="Test strds", descr="Test strds", semantic="field", overwrite=True, )
def registerToTemporal(basename, suffixes, mapset, start_day, day_step, title, desc): """ Register daily output maps in spatio-temporal raster data set """ maps = ",".join( [basename + suf + "@" + mapset for suf in suffixes]) tgis.open_new_stds( basename, type="strds", temporaltype="relative", title=title, descr=desc, semantic="sum", dbif=None, overwrite=grass.overwrite(), ) tgis.register_maps_in_space_time_dataset( type="rast", name=basename, maps=maps, start=start_day, end=None, unit="days", increment=day_step, dbif=None, interval=False, )
def setUpClass(cls): """Initiate the temporal GIS and set the region""" tgis.init(True) # Raise on error instead of exit(1) cls.use_temp_region() cls.runModule("g.region", n=80.0, s=0.0, e=120.0, w=0.0, t=1.0, b=0.0, res=10.0) cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a1 = 1") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a2 = 2") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a3 = 3") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a4 = 4") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="singletmap = 100") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="singlemap = 1000") tgis.open_new_stds( name="A", type="strds", temporaltype="absolute", title="A", descr="A", semantic="field", overwrite=True, ) tgis.register_maps_in_space_time_dataset( type="raster", name="A", maps="a1,a2,a3,a4", start="2001-01-01", interval=False, ) tgis.register_maps_in_space_time_dataset(type="raster", name=None, maps="singletmap", start="2001-01-01")
def registerToTemporal(basename, suffixes, mapset, start_time, time_step, title, desc): maps = ','.join([basename + suf + '@' + mapset for suf in suffixes]) tgis.open_new_stds(basename, type='strds', temporaltype='absolute', title=title, descr=desc, semantic='mean', dbif=None, overwrite=grass.overwrite()) tgis.register_maps_in_space_time_dataset( type='raster', name=basename, maps=maps, start=start_time, end=None, increment=time_step, dbif=None, interval=False)
def setUp(self): """!Create the test maps and the space time raster datasets """ self.runModule("r.mapcalc", overwrite=True, quiet=True, expression="register_map_1 = 1") self.runModule("r.mapcalc", overwrite=True, quiet=True, expression="register_map_2 = 2") self.strds_abs = tgis.open_new_stds(name="register_test_abs", type="strds", temporaltype="absolute", title="Test strds", descr="Test strds", semantic="field") self.strds_rel = tgis.open_new_stds(name="register_test_rel", type="strds", temporaltype="relative", title="Test strds", descr="Test strds", semantic="field")
def setUp(self): """Create the test maps and the space time raster datasets""" self.runModule( "v.random", overwrite=True, quiet=True, output="register_map_1", npoints=5, seed=1, ) self.runModule( "v.random", overwrite=True, quiet=True, output="register_map_2", npoints=5, seed=1, ) self.runModule( "r.mapcalc", overwrite=True, quiet=True, expression="register_map_null = null()", ) self.runModule( "r.to.vect", overwrite=True, quiet=True, input="register_map_null", type="point", output="register_map_empty", ) self.stvds_abs = tgis.open_new_stds( name="register_test_abs", type="stvds", temporaltype="absolute", title="Test stvds", descr="Test stvds", semantic="field", overwrite=True, ) self.stvds_rel = tgis.open_new_stds( name="register_test_rel", type="stvds", temporaltype="relative", title="Test stvds", descr="Test stvds", semantic="field", overwrite=True, )
def main(): # Get the options name = options["output"] type = options["type"] temporaltype = options["temporaltype"] title = options["title"] descr = options["description"] semantic = options["semantictype"] tgis.init() tgis.open_new_stds(name, type, temporaltype, title, descr, semantic, None, grass.overwrite())
def setUpClass(cls): """Initiate the temporal GIS and set the region""" os.putenv("GRASS_OVERWRITE", "1") tgis.init(True) # Raise on error instead of exit(1) cls.use_temp_region() ret = grass.script.run_command("g.region", n=80.0, s=0.0, e=120.0, w=0.0, t=100.0, b=0.0, res=10.0) cls.runModule("r3.mapcalc", overwrite=True, quiet=True, expression="a1 = 1") cls.runModule("r3.mapcalc", overwrite=True, quiet=True, expression="a2 = 2") cls.runModule("r3.mapcalc", overwrite=True, quiet=True, expression="a3 = 3") cls.runModule("r3.mapcalc", overwrite=True, quiet=True, expression="a4 = 4") tgis.open_new_stds( name="A", type="str3ds", temporaltype="absolute", title="A", descr="A", semantic="field", overwrite=True, ) tgis.register_maps_in_space_time_dataset( type="raster_3d", name="A", maps="a1,a2,a3,a4", start="2001-01-01", increment="1 day", interval=True, )
def main(): import grass.temporal as tgis tgis.init() dbif = tgis.SQLDatabaseInterfaceConnection() dbif.connect() inp = tgis.open_old_stds(options['input'], 'raster') temp_type, sem_type, title, descr = inp.get_initial_values() out = tgis.open_new_stds(options['output'], 'strds', temp_type, title, descr, sem_type, dbif=dbif, overwrite=gcore.overwrite()) dates = [] for mapp in inp.get_registered_maps_as_objects(): if mapp.get_absolute_time() not in dates: dates.append(mapp.get_absolute_time()) dates.sort() idx = 1 out_maps = [] for dat in dates: outraster = "{ba}_{su}".format(ba=options['basename'], su=idx) out_maps.append(outraster) calculate(inp, dat, out, outraster, options['method']) idx += 1 queue.wait() times = inp.get_absolute_time() tgis.register_maps_in_space_time_dataset('raster', out.get_name(), ','.join(out_maps), start=times[0].strftime(date_format), end=times[1].strftime(date_format), dbif=dbif)
def test_mapset_access_1(self): """Test the registration of maps from a different mapset.""" self.strds_abs_2 = tgis.open_new_stds( name="register_test_abs", type="strds", temporaltype="absolute", title="Test strds", descr="Test strds", semantic="field", overwrite=True, ) # register maps from another mapset # names are not fully qualified, maps are in a different mapset strdsname = self.strds_abs_2.get_name() + "@" + self.newmapset maps = "register_map_1,register_map_2" tgis.register_maps_in_space_time_dataset( type="raster", name=strdsname, maps=maps, start="2001-01-01", increment="1 day", interval=True, ) self.assertModule( "t.remove", type="strds", inputs=strdsname, flags="rf", quiet=True, )
def main(options, flags): tgis.init() dbif = tgis.SQLDatabaseInterfaceConnection() dbif.connect() oldStds = tgis.open_old_stds(options['input'], "stvds", dbif) stampedMaps = oldStds.get_registered_maps_as_objects(dbif=dbif) vectorMaps = get_maps(options['input']) rasterMaps = rasterize(options, vectorMaps, stampedMaps, dbif, gscript.overwrite()) tempType, semanticType, title, description = oldStds.get_initial_values() newStds = tgis.open_new_stds(options['output'], 'strds', tempType, title, description, semanticType, dbif, overwrite=gscript.overwrite()) for map in rasterMaps: map.load() map.insert(dbif) newStds.register_map(map, dbif) newStds.update_from_registered_maps(dbif) dbif.close()
def setUpClass(cls): """Initiate the temporal GIS and set the region """ tgis.init(True) # Raise on error instead of exit(1) cls.use_temp_region() cls.runModule("g.region", n=80.0, s=0.0, e=120.0, w=0.0, t=1.0, b=0.0, res=10.0) cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a1 = 1") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a2 = 2") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a3 = 3") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a4 = 4") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="b1 = 5") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="b2 = 6") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="c1 = 7") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="d1 = 8") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="d2 = 9") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="d3 = 10") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="e1 = 11") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="e2 = 12") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="e3 = 13") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="singletmap = 99") tgis.open_new_stds(name="A", type="strds", temporaltype="absolute", title="A", descr="A", semantic="field", overwrite=True) tgis.open_new_stds(name="B", type="strds", temporaltype="absolute", title="B", descr="B", semantic="field", overwrite=True) tgis.open_new_stds(name="C", type="strds", temporaltype="absolute", title="C", descr="C", semantic="field", overwrite=True) tgis.open_new_stds(name="D", type="strds", temporaltype="absolute", title="D", descr="D", semantic="field", overwrite=True) tgis.open_new_stds(name="E", type="strds", temporaltype="absolute", title="E", descr="E", semantic="field", overwrite=True) tgis.register_maps_in_space_time_dataset(type="raster", name="A", maps="a1,a2,a3,a4", start="2001-01-01", increment="1 day", interval=True) tgis.register_maps_in_space_time_dataset(type="raster", name="B", maps="b1,b2", start="2001-01-01", increment="2 day", interval=True) tgis.register_maps_in_space_time_dataset(type="raster", name="C", maps="c1", start="2001-01-02", increment="2 day", interval=True) tgis.register_maps_in_space_time_dataset(type="raster", name="D", maps="d1,d2,d3", start="2001-01-03", increment="1 day", interval=True) tgis.register_maps_in_space_time_dataset(type="raster", name="E", maps="e1,e2,e3", start="2000-12-31", increment="2 day", interval=True) tgis.register_maps_in_space_time_dataset(type="raster", name=None, maps="singletmap", start="2001-01-03", end="2001-01-04")
def main(): # lazy imports import grass.temporal as tgis # Get the options name = options["output"] type = options["type"] temporaltype = options["temporaltype"] title = options["title"] descr = options["description"] semantic = options["semantictype"] # Make sure the temporal database exists tgis.init() tgis.open_new_stds(name, type, temporaltype, title, descr, semantic, None, grass.overwrite())
def setUpClass(cls): """Initiate the temporal GIS and set the region """ tgis.init(True) # Raise on error instead of exit(1) cls.use_temp_region() ret = grass.script.run_command("g.region", n=80.0, s=0.0, e=120.0, w=0.0, t=100.0, b=0.0, res=10.0) cls.runModule("r3.mapcalc", overwrite=True, quiet=True, expression="a1 = 1") cls.runModule("r3.mapcalc", overwrite=True, quiet=True, expression="a2 = 2") cls.runModule("r3.mapcalc", overwrite=True, quiet=True, expression="a3 = 3") cls.runModule("r3.mapcalc", overwrite=True, quiet=True, expression="a4 = 4") tgis.open_new_stds(name="A", type="str3ds", temporaltype="absolute", title="A", descr="A", semantic="field", overwrite=True) tgis.register_maps_in_space_time_dataset(type="raster_3d", name="A", maps="a1,a2,a3,a4", start="2001-01-01", increment="1 day", interval=True)
def register_maps_in_stds(self, stds_title, stds_name, map_list, stds_type, t_type): """Create a STDS, create one mapdataset for each map and register them in the temporal database """ assert isinstance(stds_title, str), u"not a string!" assert isinstance(stds_name, str), u"not a string!" assert isinstance(t_type, str), u"not a string!" # Print message in case of decreased GRASS verbosity if msgr.verbosity() <= 2: msgr.message(u"Registering maps in temporal framework...") # create stds stds_id = self.format_id(stds_name) stds_desc = "" stds = tgis.open_new_stds(stds_id, stds_type, t_type, stds_title, stds_desc, "mean", overwrite=self.overwrite) # create MapDataset objects list map_dts_lst = [] for map_name, map_time in map_list: # create MapDataset map_id = self.format_id(map_name) map_dts_type = { 'strds': tgis.RasterDataset, 'stvds': tgis.VectorDataset } map_dts = map_dts_type[stds_type](map_id) # load spatial data from map map_dts.load() # set time assert isinstance(map_time, datetime) if t_type == 'relative': rel_time = (map_time - self.start_time).total_seconds() map_dts.set_relative_time(rel_time, None, 'seconds') elif t_type == 'absolute': map_dts.set_absolute_time(start_time=map_time) else: assert False, "unknown temporal type!" # populate the list map_dts_lst.append(map_dts) # Finally register the maps t_unit = {'relative': 'seconds', 'absolute': ''} stds_corresp = {'strds': 'raster', 'stvds': 'vector'} del_empty = {'strds': True, 'stvds': False} tgis.register.register_map_object_list( stds_corresp[stds_type], map_dts_lst, stds, delete_empty=del_empty[stds_type], unit=t_unit[t_type]) return self
def setUpClass(cls): """Initiate the temporal GIS and set the region""" os.putenv("GRASS_OVERWRITE", "1") tgis.init(True) # Raise on error instead of exit(1) cls.use_temp_region() cls.runModule("g.region", n=80.0, s=0.0, e=120.0, w=0.0, t=1.0, b=0.0, res=10.0) cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a1 = 7") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a2 = 8") tgis.open_new_stds( name="A", type="strds", temporaltype="absolute", title="A", descr="A", semantic="field", overwrite=True, ) tgis.register_maps_in_space_time_dataset(type="raster", name="A", maps="a1", start="2001-02-01", end="2001-04-01") tgis.register_maps_in_space_time_dataset(type="raster", name="A", maps="a2", start="2001-03-01", end="2001-05-01")
def setUpClass(cls): """Initiate the temporal GIS and set the region """ os.putenv("GRASS_OVERWRITE", "1") tgis.init(True) # Raise on error instead of exit(1) cls.use_temp_region() cls.runModule("g.region", n=80.0, s=0.0, e=120.0, w=0.0, t=1.0, b=0.0, res=10.0) cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a1 = 7") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a2 = 8") tgis.open_new_stds(name="A", type="strds", temporaltype="absolute", title="A", descr="A", semantic="field", overwrite=True) tgis.register_maps_in_space_time_dataset(type="raster", name="A", maps="a1", start="2001-02-01", end="2001-04-01") tgis.register_maps_in_space_time_dataset(type="raster", name="A", maps="a2", start="2001-03-01", end="2001-05-01")
def setUpClass(cls): """Initiate the temporal GIS and set the region """ tgis.init(True) # Raise on error instead of exit(1) cls.use_temp_region() cls.runModule("g.region", n=80.0, s=0.0, e=120.0, w=0.0, t=1.0, b=0.0, res=10.0) cls.runModule("r3.mapcalc", overwrite=True, quiet=True, expression="a1 = 1") cls.runModule("r3.mapcalc", overwrite=True, quiet=True, expression="a2 = 2") cls.runModule("r3.mapcalc", overwrite=True, quiet=True, expression="a3 = 3") cls.runModule("r3.mapcalc", overwrite=True, quiet=True, expression="a4 = 4") tgis.open_new_stds(name="A", type="str3ds", temporaltype="absolute", title="A", descr="A", semantic="field", overwrite=True) tgis.register_maps_in_space_time_dataset(type="raster3d", name="A", maps="a1,a2,a3,a4", start="2001-01-01", increment="1 day", interval=True) cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="b1 = 5") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="b2 = 6") tgis.open_new_stds(name="B", type="strds", temporaltype="absolute", title="B", descr="B", semantic="field", overwrite=True) tgis.register_maps_in_space_time_dataset(type="raster", name="B", maps="b1,b2", start="2001-01-01", increment="2 day", interval=True) cls.runModule("v.random", overwrite=True, quiet=True, npoints=20, seed=3, output='c1') tgis.open_new_stds(name="C", type="stvds", temporaltype="absolute", title="B", descr="C", semantic="field", overwrite=True) tgis.register_maps_in_space_time_dataset(type="vector", name="C", maps="c1", start="2001-01-02", increment="2 day", interval=True)
def register_maps_in_strds(strds_name, raster_dts_lst): '''Register given maps ''' # create strds strds_id = format_id(strds_name) strds_title = "data from GPM" strds_desc = "" strds = tgis.open_new_stds(strds_id, 'strds', 'absolute', strds_title, strds_desc, "mean", overwrite=grass.overwrite()) # Register the maps tgis.register.register_map_object_list('raster', raster_dts_lst, strds, delete_empty=True, unit=None)
def create_stds(self, stds_name, overwrite): stds_id = tgis.AbstractMapDataset.build_id(stds_name, self.mapset) stds_type="strds" temporal_type="relative" tgis.init() self.dbif = tgis.SQLDatabaseInterfaceConnection() self.dbif.connect() self.stds_h = tgis.open_new_stds(name=stds_id, type=stds_type, temporaltype=temporal_type, title='', descr='', semantic="mean", dbif=self.dbif, overwrite=overwrite) return self
def setUpClass(cls): """Initiate the temporal GIS and set the region """ os.putenv("GRASS_OVERWRITE", "1") tgis.init(True) # Raise on error instead of exit(1) cls.use_temp_region() cls.runModule("g.region", n=80.0, s=0.0, e=120.0, w=0.0, t=1.0, b=0.0, res=10.0) cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a1 = 1") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a2 = 2") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a3 = 3") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a4 = 4") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a5 = 5") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="a6 = 6") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="b1 = 7") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="b2 = 8") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="c1 = 9") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="d1 = 10") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="d2 = 11") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="d3 = 12") cls.runModule("r.mapcalc", overwrite=True, quiet=True, expression="singletmap = 99") tgis.open_new_stds(name="A", type="strds", temporaltype="absolute", title="A", descr="A", semantic="field", overwrite=True) tgis.open_new_stds(name="B", type="strds", temporaltype="absolute", title="B", descr="B", semantic="field", overwrite=True) tgis.open_new_stds(name="C", type="strds", temporaltype="absolute", title="C", descr="C", semantic="field", overwrite=True) tgis.open_new_stds(name="D", type="strds", temporaltype="absolute", title="D", descr="D", semantic="field", overwrite=True) tgis.register_maps_in_space_time_dataset(type="raster", name="A", maps="a1,a2,a3,a4,a5,a6", start="2001-01-01", increment="1 month", interval=True) tgis.register_maps_in_space_time_dataset(type="raster", name="B", maps="b1,b2", start="2001-01-01", increment="3 months", interval=True) tgis.register_maps_in_space_time_dataset(type="raster", name="C", maps="c1", start="2001-01-01", increment="1 year", interval=True) tgis.register_maps_in_space_time_dataset(type="raster", name="D", maps="d1", start="2001-01-01", increment="5 days", interval=True) tgis.register_maps_in_space_time_dataset(type="raster", name="D", maps="d2", start="2001-03-01", increment="5 days", interval=True) tgis.register_maps_in_space_time_dataset(type="raster", name="D", maps="d3", start="2001-05-01", increment="5 days", interval=True) tgis.register_maps_in_space_time_dataset(type="raster", name=None, maps="singletmap", start="2001-03-01", end="2001-04-01")
def registerToTemporal( basename, suffixes, mapset, start_time, time_step, title, desc ): maps = ",".join([basename + suf + "@" + mapset for suf in suffixes]) tgis.open_new_stds( basename, type="strds", temporaltype="absolute", title=title, descr=desc, semantic="mean", dbif=None, overwrite=grass.overwrite(), ) tgis.register_maps_in_space_time_dataset( type="raster", name=basename, maps=maps, start=start_time, end=None, increment=time_step, dbif=None, interval=False, )
def setUp(self): """Create the space time raster dataset""" self.strds_abs = tgis.open_new_stds( name="register_test_abs", type="strds", temporaltype="absolute", title="Test strds", descr="Test strds", semantic="field", overwrite=True, ) tgis.register_maps_in_space_time_dataset( type="raster", name=self.strds_abs.get_name(), maps="register_map_1,register_map_2", start="2001-01-01", increment="1 day", interval=True, ) self.currmapset = tgis.get_current_mapset() self.newmapset = "test_temporal_register_mapset_access" # create and switch to new mapset self.runModule( "g.mapset", mapset=self.newmapset, flags="c", quiet=True, ) # add old mapset to search path self.runModule( "g.mapsets", mapset=self.currmapset, operation="add", quiet=True, ) self.runModule( "g.mapsets", flags="p", verbose=True, ) tgis.stop_subprocesses() tgis.init() self.assertNotEqual(self.currmapset, tgis.get_current_mapset())
def setUpClass(cls): """Initiate the temporal GIS and set the region """ os.putenv("GRASS_OVERWRITE", "1") tgis.init(True) # Raise on error instead of exit(1) cls.use_temp_region() cls.runModule("g.region", n=80.0, s=0.0, e=120.0, w=0.0, t=1.0, b=0.0, res=10.0) cls.runModule("v.random", quiet=True, npoints=20, seed=1, output='a1') cls.runModule("v.random", quiet=True, npoints=20, seed=1, output='a2') cls.runModule("v.random", quiet=True, npoints=20, seed=1, output='a3') cls.runModule("v.random", quiet=True, npoints=20, seed=1, output='a4') cls.runModule("v.random", quiet=True, npoints=20, seed=2, output='b1') cls.runModule("v.random", quiet=True, npoints=20, seed=2, output='b2') cls.runModule("v.random", quiet=True, npoints=20, seed=3, output='c1') cls.runModule("v.random", quiet=True, npoints=20, seed=4, output='d1') cls.runModule("v.random", quiet=True, npoints=20, seed=4, output='d2') cls.runModule("v.random", quiet=True, npoints=20, seed=4, output='d3') cls.runModule("v.random", quiet=True, npoints=20, seed=5, output='singletmap') cls.runModule("v.random", quiet=True, npoints=20, seed=6, output='singlemap') tgis.open_new_stds(name="A", type="stvds", temporaltype="absolute", title="A", descr="A", semantic="field") tgis.open_new_stds(name="B", type="stvds", temporaltype="absolute", title="B", descr="B", semantic="field") tgis.open_new_stds(name="C", type="stvds", temporaltype="absolute", title="B", descr="C", semantic="field") tgis.open_new_stds(name="D", type="stvds", temporaltype="absolute", title="D", descr="D", semantic="field") tgis.register_maps_in_space_time_dataset(type="vector", name="A", maps="a1,a2,a3,a4", start="2001-01-01", increment="1 day", interval=True) tgis.register_maps_in_space_time_dataset(type="vector", name="B", maps="b1,b2", start="2001-01-01", increment="2 day", interval=True) tgis.register_maps_in_space_time_dataset(type="vector", name="C", maps="c1", start="2001-01-02", increment="2 day", interval=True) tgis.register_maps_in_space_time_dataset(type="vector", name="D", maps="d1,d2,d3", start="2001-01-03", increment="1 day", interval=True) tgis.register_maps_in_space_time_dataset(type="vector", name=None, maps="singletmap", start="2001-01-03", end="2001-01-04")
def setUpClass(cls): """Initiate the temporal GIS and set the region """ tgis.init(True) # Raise on error instead of exit(1) cls.use_temp_region() cls.runModule("g.region", n=80.0, s=0.0, e=120.0, w=0.0, t=1.0, b=0.0, res=10.0) cls.runModule("v.random", overwrite=True, quiet=True, npoints=20, seed=1, output='a1') cls.runModule("v.random", overwrite=True, quiet=True, npoints=20, seed=1, output='a2') cls.runModule("v.random", overwrite=True, quiet=True, npoints=20, seed=1, output='a3') cls.runModule("v.random", overwrite=True, quiet=True, npoints=20, seed=1, output='a4') cls.runModule("v.random", overwrite=True, quiet=True, npoints=20, seed=2, output='b1') cls.runModule("v.random", overwrite=True, quiet=True, npoints=20, seed=2, output='b2') cls.runModule("v.random", overwrite=True, quiet=True, npoints=20, seed=3, output='c1') cls.runModule("v.random", overwrite=True, quiet=True, npoints=20, seed=4, output='d1') cls.runModule("v.random", overwrite=True, quiet=True, npoints=20, seed=4, output='d2') cls.runModule("v.random", overwrite=True, quiet=True, npoints=20, seed=4, output='d3') cls.runModule("v.random", overwrite=True, quiet=True, npoints=20, seed=5, output='singletmap') cls.runModule("v.random", overwrite=True, quiet=True, npoints=20, seed=6, output='singlemap') tgis.open_new_stds(name="A", type="stvds", temporaltype="absolute", title="A", descr="A", semantic="field", overwrite=True) tgis.open_new_stds(name="B", type="stvds", temporaltype="absolute", title="B", descr="B", semantic="field", overwrite=True) tgis.open_new_stds(name="C", type="stvds", temporaltype="absolute", title="B", descr="C", semantic="field", overwrite=True) tgis.open_new_stds(name="D", type="stvds", temporaltype="absolute", title="D", descr="D", semantic="field", overwrite=True) tgis.register_maps_in_space_time_dataset(type="vector", name="A", maps="a1,a2,a3,a4", start="2001-01-01", increment="1 day", interval=True) tgis.register_maps_in_space_time_dataset(type="vector", name="B", maps="b1,b2", start="2001-01-01", increment="2 day", interval=True) tgis.register_maps_in_space_time_dataset(type="vector", name="C", maps="c1", start="2001-01-02", increment="2 day", interval=True) tgis.register_maps_in_space_time_dataset(type="vector", name="D", maps="d1,d2,d3", start="2001-01-03", increment="1 day", interval=True) tgis.register_maps_in_space_time_dataset(type="vector", name=None, maps="singletmap", start="2001-01-03", end="2001-01-04")
def setUpClass(cls): """!Initiate the temporal GIS and set the region """ os.putenv("GRASS_OVERWRITE", "1") tgis.init(True) # Raise on error instead of exit(1) grass.script.use_temp_region() ret = grass.script.run_command("g.region", n=80.0, s=0.0, e=120.0, w=0.0, t=1.0, b=0.0, res=10.0) ret += grass.script.run_command("r.mapcalc", overwrite=True, quiet=True, expression="a1 = 1") ret += grass.script.run_command("r.mapcalc", overwrite=True, quiet=True, expression="a2 = 2") ret += grass.script.run_command("r.mapcalc", overwrite=True, quiet=True, expression="a3 = 3") ret += grass.script.run_command("r.mapcalc", overwrite=True, quiet=True, expression="a4 = 4") ret += grass.script.run_command("r.mapcalc", overwrite=True, quiet=True, expression="b1 = 5") ret += grass.script.run_command("r.mapcalc", overwrite=True, quiet=True, expression="b2 = 6") ret += grass.script.run_command("r.mapcalc", overwrite=True, quiet=True, expression="c1 = 7") tgis.open_new_stds( name="A", type="strds", temporaltype="absolute", title="A", descr="A", semantic="field", overwrite=True ) tgis.open_new_stds( name="B", type="strds", temporaltype="absolute", title="B", descr="B", semantic="field", overwrite=True ) tgis.open_new_stds( name="C", type="strds", temporaltype="absolute", title="B", descr="C", semantic="field", overwrite=True ) tgis.register_maps_in_space_time_dataset( type="rast", name="A", maps="a1,a2,a3,a4", start="2001-01-01", increment="1 day", interval=True ) tgis.register_maps_in_space_time_dataset( type="rast", name="B", maps="b1,b2", start="2001-01-01", increment="2 day", interval=True ) tgis.register_maps_in_space_time_dataset( type="rast", name="C", maps="c1", start="2001-01-02", increment="2 day", interval=True )
def main(): # 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.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)
def main(): # lazy imports import grass.temporal as tgis # Get the options input = options["input"] output = options["output"] sampler = options["sample"] where = options["where"] base = options["basename"] register_null = flags["n"] method = options["method"] sampling = options["sampling"] offset = options["offset"] nprocs = options["nprocs"] time_suffix = options["suffix"] type = options["type"] topo_list = sampling.split(",") tgis.init() dbif = tgis.SQLDatabaseInterfaceConnection() dbif.connect() sp = tgis.open_old_stds(input, "strds", dbif) sampler_sp = tgis.open_old_stds(sampler, type, dbif) if sampler_sp.get_temporal_type() != sp.get_temporal_type(): dbif.close() gcore.fatal( _("Input and aggregation dataset must have " "the same temporal type")) # Check if intervals are present if sampler_sp.temporal_extent.get_map_time() != "interval": dbif.close() gcore.fatal( _("All registered maps of the aggregation dataset " "must have time intervals")) # We will create the strds later, but need to check here tgis.check_new_stds(output, "strds", dbif, gcore.overwrite()) map_list = sp.get_registered_maps_as_objects(where=where, order="start_time", dbif=dbif) if not map_list: dbif.close() gcore.fatal(_("Space time raster dataset <%s> is empty") % input) granularity_list = sampler_sp.get_registered_maps_as_objects( where=where, order="start_time", dbif=dbif) if not granularity_list: dbif.close() gcore.fatal(_("Space time raster dataset <%s> is empty") % sampler) gran = sampler_sp.get_granularity() output_list = tgis.aggregate_by_topology( granularity_list=granularity_list, granularity=gran, map_list=map_list, topo_list=topo_list, basename=base, time_suffix=time_suffix, offset=offset, method=method, nprocs=nprocs, spatial=None, overwrite=gcore.overwrite(), ) if output_list: temporal_type, semantic_type, title, description = sp.get_initial_values( ) output_strds = tgis.open_new_stds( output, "strds", temporal_type, title, description, semantic_type, dbif, gcore.overwrite(), ) tgis.register_map_object_list( "rast", output_list, output_strds, register_null, sp.get_relative_time_unit(), dbif, ) # Update the raster metadata table entries with aggregation type output_strds.set_aggregation_type(method) output_strds.metadata.update(dbif) dbif.close()
def main(): # Get the options input = options["input"] output = options["output"] where = options["where"] gran = options["granularity"] base = options["basename"] register_null = flags["n"] method = options["method"] sampling = options["sampling"] offset = options["offset"] nprocs = options["nprocs"] time_suffix = flags["s"] topo_list = sampling.split(",") tgis.init() dbif = tgis.SQLDatabaseInterfaceConnection() dbif.connect() sp = tgis.open_old_stds(input, "strds", dbif) map_list = sp.get_registered_maps_as_objects(where=where, order="start_time", dbif=dbif) if not map_list: dbif.close() gcore.fatal(_("Space time raster dataset <%s> is empty") % input) # We will create the strds later, but need to check here tgis.check_new_stds(output, "strds", dbif, gcore.overwrite()) start_time = map_list[0].temporal_extent.get_start_time() if sp.is_time_absolute(): start_time = tgis.adjust_datetime_to_granularity(start_time, gran) # We use the end time first end_time = map_list[-1].temporal_extent.get_end_time() has_end_time = True # In case no end time is available, then we use the start time of the last map layer if end_time is None: end_time = map_list[- 1].temporal_extent.get_start_time() has_end_time = False granularity_list = [] # Build the granularity list while True: if has_end_time is True: if start_time >= end_time: break else: if start_time > end_time: break granule = tgis.RasterDataset(None) start = start_time if sp.is_time_absolute(): end = tgis.increment_datetime_by_string(start_time, gran) granule.set_absolute_time(start, end) else: end = start_time + int(gran) granule.set_relative_time(start, end, sp.get_relative_time_unit()) start_time = end granularity_list.append(granule) output_list = tgis.aggregate_by_topology(granularity_list=granularity_list, granularity=gran, map_list=map_list, topo_list=topo_list, basename=base, time_suffix=time_suffix, offset=offset, method=method, nprocs=nprocs, spatial=None, overwrite=gcore.overwrite()) if output_list: temporal_type, semantic_type, title, description = sp.get_initial_values() output_strds = tgis.open_new_stds(output, "strds", temporal_type, title, description, semantic_type, dbif, gcore.overwrite()) tgis.register_map_object_list("rast", output_list, output_strds, register_null, sp.get_relative_time_unit(), dbif) # Update the raster metadata table entries with aggregation type output_strds.set_aggregation_type(method) output_strds.metadata.update(dbif) dbif.close()
def main(): # Get the options input = options["input"] output = options["output"] sampler = options["sample"] where = options["where"] base = options["basename"] register_null = flags["n"] method = options["method"] sampling = options["sampling"] offset = options["offset"] nprocs = options["nprocs"] time_suffix = flags["s"] type = options["type"] topo_list = sampling.split(",") tgis.init() dbif = tgis.SQLDatabaseInterfaceConnection() dbif.connect() sp = tgis.open_old_stds(input, "strds", dbif) sampler_sp = tgis.open_old_stds(sampler, type, dbif) if sampler_sp.get_temporal_type() != sp.get_temporal_type(): dbif.close() gcore.fatal(_("Input and aggregation dataset must have " "the same temporal type")) # Check if intervals are present if sampler_sp.temporal_extent.get_map_time() != "interval": dbif.close() gcore.fatal(_("All registered maps of the aggregation dataset " "must have time intervals")) # We will create the strds later, but need to check here tgis.check_new_stds(output, "strds", dbif, gcore.overwrite()) map_list = sp.get_registered_maps_as_objects(where=where, order="start_time", dbif=dbif) if not map_list: dbif.close() gcore.fatal(_("Space time raster dataset <%s> is empty") % input) granularity_list = sampler_sp.get_registered_maps_as_objects(where=where, order="start_time", dbif=dbif) if not granularity_list: dbif.close() gcore.fatal(_("Space time raster dataset <%s> is empty") % sampler) gran = sampler_sp.get_granularity() output_list = tgis.aggregate_by_topology(granularity_list=granularity_list, granularity=gran, map_list=map_list, topo_list=topo_list, basename=base, time_suffix=time_suffix, offset=offset, method=method, nprocs=nprocs, spatial=None, overwrite=gcore.overwrite()) if output_list: temporal_type, semantic_type, title, description = sp.get_initial_values() output_strds = tgis.open_new_stds(output, "strds", temporal_type, title, description, semantic_type, dbif, gcore.overwrite()) tgis.register_map_object_list("rast", output_list, output_strds, register_null, sp.get_relative_time_unit(), dbif) # Update the raster metadata table entries with aggregation type output_strds.set_aggregation_type(method) output_strds.metadata.update(dbif) dbif.close()
def main(): # lazy imports import grass.temporal as tgis import grass.pygrass.modules as pymod # Get the options input = options["input"] output = options["output"] where = options["where"] size = options["size"] base = options["basename"] register_null = flags["n"] use_raster_region = flags["r"] method = options["method"] nprocs = options["nprocs"] time_suffix = options["suffix"] # Make sure the temporal database exists tgis.init() # We need a database interface dbif = tgis.SQLDatabaseInterfaceConnection() dbif.connect() overwrite = grass.overwrite() sp = tgis.open_old_stds(input, "strds", dbif) maps = sp.get_registered_maps_as_objects(where=where, dbif=dbif) if not maps: dbif.close() grass.warning( _("Space time raster dataset <%s> is empty") % sp.get_id()) return new_sp = tgis.check_new_stds(output, "strds", dbif=dbif, overwrite=overwrite) # Configure the r.neighbor module neighbor_module = pymod.Module("r.neighbors", input="dummy", output="dummy", run_=False, finish_=False, size=int(size), method=method, overwrite=overwrite, quiet=True) gregion_module = pymod.Module( "g.region", raster="dummy", run_=False, finish_=False, ) # The module queue for parallel execution process_queue = pymod.ParallelModuleQueue(int(nprocs)) count = 0 num_maps = len(maps) new_maps = [] # run r.neighbors all selected maps for map in maps: count += 1 if sp.get_temporal_type() == 'absolute' and time_suffix == 'gran': suffix = tgis.create_suffix_from_datetime( map.temporal_extent.get_start_time(), sp.get_granularity()) map_name = "{ba}_{su}".format(ba=base, su=suffix) elif sp.get_temporal_type() == 'absolute' and time_suffix == 'time': suffix = tgis.create_time_suffix(map) map_name = "{ba}_{su}".format(ba=base, su=suffix) else: map_name = tgis.create_numeric_suffix(base, count, time_suffix) new_map = tgis.open_new_map_dataset( map_name, None, type="raster", temporal_extent=map.get_temporal_extent(), overwrite=overwrite, dbif=dbif) new_maps.append(new_map) mod = copy.deepcopy(neighbor_module) mod(input=map.get_id(), output=new_map.get_id()) if use_raster_region is True: reg = copy.deepcopy(gregion_module) reg(raster=map.get_id()) print(reg.get_bash()) print(mod.get_bash()) mm = pymod.MultiModule([reg, mod], sync=False, set_temp_region=True) process_queue.put(mm) else: print(mod.get_bash()) process_queue.put(mod) # Wait for unfinished processes process_queue.wait() proc_list = process_queue.get_finished_modules() # Check return status of all finished modules error = 0 for proc in proc_list: if proc.popen.returncode != 0: grass.error( _("Error running module: %\n stderr: %s") % (proc.get_bash(), proc.outputs.stderr)) error += 1 if error > 0: grass.fatal(_("Error running modules.")) # Open the new space time raster dataset ttype, stype, title, descr = sp.get_initial_values() new_sp = tgis.open_new_stds(output, "strds", ttype, title, descr, stype, dbif, overwrite) num_maps = len(new_maps) # collect empty maps to remove them empty_maps = [] # Register the maps in the database count = 0 for map in new_maps: count += 1 if count % 10 == 0: grass.percent(count, num_maps, 1) # Do not register empty maps map.load() if map.metadata.get_min() is None and \ map.metadata.get_max() is None: if not register_null: empty_maps.append(map) continue # Insert map in temporal database map.insert(dbif) new_sp.register_map(map, dbif) # Update the spatio-temporal extent and the metadata table entries new_sp.update_from_registered_maps(dbif) grass.percent(1, 1, 1) # Remove empty maps if len(empty_maps) > 0: names = "" count = 0 for map in empty_maps: if count == 0: count += 1 names += "%s" % (map.get_name()) else: names += ",%s" % (map.get_name()) grass.run_command("g.remove", flags='f', type='raster', name=names, quiet=True) dbif.close()
from grass.pygrass.raster import RasterRow import matplotlib.pyplot as plt import grass.temporal as tgis from datetime import datetime # realizamos la conexion con la base de datos temporal tgis.init() dbif = tgis.SQLDatabaseInterfaceConnection() dbif.connect() # creamos el strds que debemos rellenar SPI_RF = 'spi_rf' dataset = tgis.open_new_stds(name=SPI_RF, type='strds', temporaltype='absolute', title="SPI RF", descr="SPI predicho por RF", semantic='mean', overwrite=True) dataset_name_rf = 'spi_rf@PERMANENT' dataset = tgis.open_old_stds(dataset_name_rf, "strds", dbif=dbif) SPI_XG = 'spi_xg' dataset = tgis.open_new_stds(name=SPI_XG, type='strds', temporaltype='absolute', title="SPI XG", descr="SPI predicho por XG", semantic='mean', overwrite=True)
def main(): # lazy imports import grass.temporal as tgis import grass.pygrass.modules as pymod # Get the options input = options["input"] output = options["output"] where = options["where"] register_null = flags["n"] # Make sure the temporal database exists tgis.init() # We need a database interface dbif = tgis.SQLDatabaseInterfaceConnection() dbif.connect() overwrite = grass.overwrite() sp = tgis.open_old_stds(input, "strds", dbif) maps = sp.get_registered_maps_as_objects(where=where, dbif=dbif) if not maps: dbif.close() grass.warning( _("Space time raster dataset <%s> is empty") % sp.get_id()) return new_sp = tgis.check_new_stds(output, "strds", dbif=dbif, overwrite=overwrite) # Configure the HANTS module hants_flags = "" if flags["l"]: hants_flags = hants_flags + 'l' if flags["h"]: hants_flags = hants_flags + 'h' if flags["i"]: hants_flags = hants_flags + 'i' kwargs = dict() kwargs['nf'] = options['nf'] if options['fet']: kwargs['fet'] = options['fet'] kwargs['dod'] = options['dod'] if options['range']: kwargs['range'] = options['range'] kwargs['suffix'] = "_hants" if len(hants_flags) > 0: kwargs['flags'] = hants_flags count = 0 num_maps = len(maps) new_maps = [] maplistfile = script.tempfile() fd = open(maplistfile, 'w') # create list of input maps and their time stamps for map in maps: count += 1 map_name = "{ba}_hants".format(ba=map.get_id()) new_map = tgis.open_new_map_dataset( map_name, None, type="raster", temporal_extent=map.get_temporal_extent(), overwrite=overwrite, dbif=dbif) new_maps.append(new_map) f.write("{0}\n".format(map.get_id())) f.close() # run r.hants grass.run_command('r.hants', file=maplistfile, suffix="_hants", quiet=True, **kwargs) # Open the new space time raster dataset ttype, stype, title, descr = sp.get_initial_values() new_sp = tgis.open_new_stds(output, "strds", ttype, title, descr, stype, dbif, overwrite) num_maps = len(new_maps) # collect empty maps to remove them empty_maps = [] # Register the maps in the database count = 0 for map in new_maps: count += 1 if count % 10 == 0: grass.percent(count, num_maps, 1) # Do not register empty maps map.load() if map.metadata.get_min() is None and \ map.metadata.get_max() is None: if not register_null: empty_maps.append(map) continue # Insert map in temporal database map.insert(dbif) new_sp.register_map(map, dbif) # Update the spatio-temporal extent and the metadata table entries new_sp.update_from_registered_maps(dbif) grass.percent(1, 1, 1) # Remove empty maps if len(empty_maps) > 0: names = "" count = 0 for map in empty_maps: if count == 0: count += 1 names += "%s" % (map.get_name()) else: names += ",%s" % (map.get_name()) grass.run_command("g.remove", flags='f', type='raster', name=names, quiet=True) dbif.close()
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.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)
def main(): # Get the options input = options["input"] output = options["output"] vector_output = options["vector_output"] strds = options["strds"] where = options["where"] columns = options["columns"] if where == "" or where == " " or where == "\n": where = None overwrite = grass.overwrite() # Check the number of sample strds and the number of columns strds_names = strds.split(",") column_names = columns.split(",") if len(strds_names) != len(column_names): grass.fatal(_("The number of columns must be equal to the number of space time raster datasets")) # Make sure the temporal database exists tgis.init() # We need a database interface dbif = tgis.SQLDatabaseInterfaceConnection() dbif.connect() mapset = grass.gisenv()["MAPSET"] out_sp = tgis.check_new_stds(output, "stvds", dbif, overwrite) samples = [] first_strds = tgis.open_old_stds(strds_names[0], "strds", dbif) # Single space time raster dataset if len(strds_names) == 1: rows = first_strds.get_registered_maps( columns="name,mapset,start_time,end_time", order="start_time", dbif=dbif) if not rows: dbif.close() grass.fatal(_("Space time raster dataset <%s> is empty") % out_sp.get_id()) for row in rows: start = row["start_time"] end = row["end_time"] raster_maps = [row["name"] + "@" + row["mapset"],] s = Sample(start, end, raster_maps) samples.append(s) else: # Multiple space time raster datasets for name in strds_names[1:]: dataset = tgis.open_old_stds(name, "strds", dbif) if dataset.get_temporal_type() != first_strds.get_temporal_type(): grass.fatal(_("Temporal type of space time raster datasets must be equal\n" "<%(a)s> of type %(type_a)s do not match <%(b)s> of type %(type_b)s"%\ {"a":first_strds.get_id(), "type_a":first_strds.get_temporal_type(), "b":dataset.get_id(), "type_b":dataset.get_temporal_type()})) mapmatrizes = tgis.sample_stds_by_stds_topology("strds", "strds", strds_names, strds_names[0], False, None, "equal", False, False) for i in range(len(mapmatrizes[0])): isvalid = True mapname_list = [] for mapmatrix in mapmatrizes: entry = mapmatrix[i] if entry["samples"]: sample = entry["samples"][0] name = sample.get_id() if name is None: isvalid = False break else: mapname_list.append(name) if isvalid: entry = mapmatrizes[0][i] map = entry["granule"] start, end = map.get_temporal_extent_as_tuple() s = Sample(start, end, mapname_list) samples.append(s) num_samples = len(samples) # Get the layer and database connections of the input vector vector_db = grass.vector.vector_db(input) # We copy the vector table and create the new layers if vector_db: # Use the first layer to copy the categories from layers = "1," else: layers = "" first = True for layer in range(num_samples): layer += 1 # Skip existing layer if vector_db and layer in vector_db and \ vector_db[layer]["layer"] == layer: continue if first: layers += "%i" % (layer) first = False else: layers += ",%i" % (layer) vectmap = vector_output # We create a new vector map using the categories of the original map try: grass.run_command("v.category", input=input, layer=layers, output=vectmap, option="transfer", overwrite=overwrite) except CalledModuleError: grass.fatal(_("Unable to create new layers for vector map <%s>") % (vectmap)) title = _("Observaion of space time raster dataset(s) <%s>") % (strds) description= _("Observation of space time raster dataset(s) <%s>" " with vector map <%s>") % (strds, input) # Create the output space time vector dataset out_sp = tgis.open_new_stds(output, "stvds", first_strds.get_temporal_type(), title, description, first_strds.get_semantic_type(), dbif, overwrite) dummy = out_sp.get_new_map_instance(None) # Sample the space time raster dataset with the vector # map at specific layer with v.what.rast count = 1 for sample in samples: raster_names = sample.raster_names if len(raster_names) != len(column_names): grass.fatal(_("The number of raster maps in a granule must " "be equal to the number of column names")) # Create the columns creation string columns_string = "" for name, column in zip(raster_names, column_names): # The column is by default double precision coltype = "DOUBLE PRECISION" # Get raster map type raster_map = tgis.RasterDataset(name) raster_map.load() if raster_map.metadata.get_datatype() == "CELL": coltype = "INT" tmp_string = "%s %s,"%(column, coltype) columns_string += tmp_string # Remove last comma columns_string = columns_string[0:len(columns_string) - 1] # Try to add a column if vector_db and count in vector_db and vector_db[count]["table"]: try: grass.run_command("v.db.addcolumn", map=vectmap, layer=count, column=columns_string, overwrite=overwrite) except CalledModuleError: dbif.close() grass.fatal(_("Unable to add column %s to vector map <%s> " "with layer %i") % (columns_string, vectmap, count)) else: # Try to add a new table grass.message("Add table to layer %i" % (count)) try: grass.run_command("v.db.addtable", map=vectmap, layer=count, columns=columns_string, overwrite=overwrite) except CalledModuleError: dbif.close() grass.fatal(_("Unable to add table to vector map " "<%s> with layer %i") % (vectmap, count)) # Call v.what.rast for each raster map for name, column in zip(raster_names, column_names): try: grass.run_command("v.what.rast", map=vectmap, layer=count, raster=name, column=column, where=where) except CalledModuleError: dbif.close() grass.fatal(_("Unable to run v.what.rast for vector map <%s> " "with layer %i and raster map <%s>") % \ (vectmap, count, str(raster_names))) vect = out_sp.get_new_map_instance(dummy.build_id(vectmap, mapset, str(count))) vect.load() start = sample.start end = sample.end if out_sp.is_time_absolute(): vect.set_absolute_time(start, end) else: vect.set_relative_time( start, end, first_strds.get_relative_time_unit()) if vect.is_in_db(dbif): vect.update_all(dbif) else: vect.insert(dbif) out_sp.register_map(vect, dbif) count += 1 out_sp.update_from_registered_maps(dbif) dbif.close()
def main(): # Get the options input = options["input"] output = options["output"] where = options["where"] size = options["size"] base = options["basename"] register_null = flags["n"] method = options["method"] nprocs = options["nprocs"] time_suffix = options["suffix"] # Make sure the temporal database exists tgis.init() # We need a database interface dbif = tgis.SQLDatabaseInterfaceConnection() dbif.connect() overwrite = grass.overwrite() sp = tgis.open_old_stds(input, "strds", dbif) maps = sp.get_registered_maps_as_objects(where=where, dbif=dbif) if not maps: dbif.close() grass.warning(_("Space time raster dataset <%s> is empty") % sp.get_id()) return new_sp = tgis.check_new_stds(output, "strds", dbif=dbif, overwrite=overwrite) # Configure the r.neighbor module neighbor_module = pymod.Module("r.neighbors", input="dummy", output="dummy", run_=False, finish_=False, size=int(size), method=method, overwrite=overwrite, quiet=True) # The module queue for parallel execution process_queue = pymod.ParallelModuleQueue(int(nprocs)) count = 0 num_maps = len(maps) new_maps = [] # run r.neighbors all selected maps for map in maps: count += 1 if sp.get_temporal_type() == 'absolute' and time_suffix == 'gran': suffix = tgis.create_suffix_from_datetime(map.temporal_extent.get_start_time(), sp.get_granularity()) map_name = "{ba}_{su}".format(ba=base, su=suffix) elif sp.get_temporal_type() == 'absolute' and time_suffix == 'time': suffix = tgis.create_time_suffix(map) map_name = "{ba}_{su}".format(ba=base, su=suffix) else: map_name = tgis.create_numeric_suffic(base, count, time_suffix) new_map = tgis.open_new_map_dataset(map_name, None, type="raster", temporal_extent=map.get_temporal_extent(), overwrite=overwrite, dbif=dbif) new_maps.append(new_map) mod = copy.deepcopy(neighbor_module) mod(input=map.get_id(), output=new_map.get_id()) print(mod.get_bash()) process_queue.put(mod) # Wait for unfinished processes process_queue.wait() # Open the new space time raster dataset ttype, stype, title, descr = sp.get_initial_values() new_sp = tgis.open_new_stds(output, "strds", ttype, title, descr, stype, dbif, overwrite) num_maps = len(new_maps) # collect empty maps to remove them empty_maps = [] # Register the maps in the database count = 0 for map in new_maps: count += 1 if count%10 == 0: grass.percent(count, num_maps, 1) # Do not register empty maps map.load() if map.metadata.get_min() is None and \ map.metadata.get_max() is None: if not register_null: empty_maps.append(map) continue # Insert map in temporal database map.insert(dbif) new_sp.register_map(map, dbif) # Update the spatio-temporal extent and the metadata table entries new_sp.update_from_registered_maps(dbif) grass.percent(1, 1, 1) # Remove empty maps if len(empty_maps) > 0: names = "" count = 0 for map in empty_maps: if count == 0: count += 1 names += "%s" % (map.get_name()) else: names += ",%s" % (map.get_name()) grass.run_command("g.remove", flags='f', type='raster', name=names, quiet=True) dbif.close()
def main(): options, flags = gcore.parser() elevation = options["elevation"] strds = options["output"] basename = strds start_water_level = float(options["start_water_level"]) end_water_level = float(options["end_water_level"]) water_level_step = float(options["water_level_step"]) # if options['coordinates']: # options['coordinates'].split(',') # passing coordinates parameter as is coordinates = options["coordinates"] seed_raster = options["seed_raster"] if seed_raster and coordinates: gcore.fatal( _("Both seed raster and coordinates cannot be specified" " together, please specify only one of them.")) time_unit = options["time_unit"] time_step = options[ "time_step"] # temporal fucntions accepts only string now if int(time_step) <= 0: gcore.fatal( _("Time step must be greater than zero." " Please specify number > 0.")) mapset = gcore.gisenv()["MAPSET"] title = _("r.lake series") desctiption = _("r.lake series") water_levels = [ step for step in frange(start_water_level, end_water_level, water_level_step) ] outputs = [ "%s%s%s" % (basename, "_", water_level) for water_level in water_levels ] if not gcore.overwrite(): check_maps_exist(outputs, mapset) kwargs = {} if seed_raster: kwargs["seed"] = seed_raster elif coordinates: kwargs["coordinates"] = coordinates if flags["n"]: pass_flags = "n" else: pass_flags = None for i, water_level in enumerate(water_levels): try: gcore.run_command( "r.lake", flags=pass_flags, elevation=elevation, lake=outputs[i], water_level=water_level, overwrite=gcore.overwrite( ), # TODO: really works? Its seems that hardcoding here False does not prevent overwriting. **kwargs) except CalledModuleError: # remove maps created so far, try to remove also i-th map remove_raster_maps(outputs[:i], quiet=True) gcore.fatal( _("r.lake command failed. Check above error messages." " Try different water levels or seed points.")) gcore.info(_("Registering created maps into temporal dataset...")) # Make sure the temporal database exists tgis.init() tgis.open_new_stds( strds, type="strds", temporaltype="relative", title=title, descr=desctiption, semantic="sum", dbif=None, overwrite=gcore.overwrite(), ) # TODO: we must start from 1 because there is a bug in register_maps_in_space_time_dataset tgis.register_maps_in_space_time_dataset( type="raster", name=basename, maps=",".join(outputs), start=str(1), end=None, unit=time_unit, increment=time_step, interval=False, dbif=None, )
def main(): #lazy imports import grass.temporal as tgis # Get the options input = options["input"] output = options["output"] vector_output = options["vector_output"] strds = options["strds"] where = options["where"] columns = options["columns"] if where == "" or where == " " or where == "\n": where = None overwrite = grass.overwrite() # Check the number of sample strds and the number of columns strds_names = strds.split(",") column_names = columns.split(",") if len(strds_names) != len(column_names): grass.fatal(_("The number of columns must be equal to the number of space time raster datasets")) # Make sure the temporal database exists tgis.init() # We need a database interface dbif = tgis.SQLDatabaseInterfaceConnection() dbif.connect() mapset = grass.gisenv()["MAPSET"] out_sp = tgis.check_new_stds(output, "stvds", dbif, overwrite) samples = [] first_strds = tgis.open_old_stds(strds_names[0], "strds", dbif) # Single space time raster dataset if len(strds_names) == 1: rows = first_strds.get_registered_maps( columns="name,mapset,start_time,end_time", order="start_time", dbif=dbif) if not rows: dbif.close() grass.fatal(_("Space time raster dataset <%s> is empty") % out_sp.get_id()) for row in rows: start = row["start_time"] end = row["end_time"] raster_maps = [row["name"] + "@" + row["mapset"],] s = Sample(start, end, raster_maps) samples.append(s) else: # Multiple space time raster datasets for name in strds_names[1:]: dataset = tgis.open_old_stds(name, "strds", dbif) if dataset.get_temporal_type() != first_strds.get_temporal_type(): grass.fatal(_("Temporal type of space time raster datasets must be equal\n" "<%(a)s> of type %(type_a)s do not match <%(b)s> of type %(type_b)s"% {"a":first_strds.get_id(), "type_a":first_strds.get_temporal_type(), "b":dataset.get_id(), "type_b":dataset.get_temporal_type()})) mapmatrizes = tgis.sample_stds_by_stds_topology("strds", "strds", strds_names, strds_names[0], False, None, "equal", False, False) for i in range(len(mapmatrizes[0])): isvalid = True mapname_list = [] for mapmatrix in mapmatrizes: entry = mapmatrix[i] if entry["samples"]: sample = entry["samples"][0] name = sample.get_id() if name is None: isvalid = False break else: mapname_list.append(name) if isvalid: entry = mapmatrizes[0][i] map = entry["granule"] start, end = map.get_temporal_extent_as_tuple() s = Sample(start, end, mapname_list) samples.append(s) num_samples = len(samples) # Get the layer and database connections of the input vector vector_db = grass.vector.vector_db(input) # We copy the vector table and create the new layers if vector_db: # Use the first layer to copy the categories from layers = "1," else: layers = "" first = True for layer in range(num_samples): layer += 1 # Skip existing layer if vector_db and layer in vector_db and \ vector_db[layer]["layer"] == layer: continue if first: layers += "%i" % (layer) first = False else: layers += ",%i" % (layer) vectmap = vector_output # We create a new vector map using the categories of the original map try: grass.run_command("v.category", input=input, layer=layers, output=vectmap, option="transfer", overwrite=overwrite) except CalledModuleError: grass.fatal(_("Unable to create new layers for vector map <%s>") % (vectmap)) title = _("Observaion of space time raster dataset(s) <%s>") % (strds) description= _("Observation of space time raster dataset(s) <%s>" " with vector map <%s>") % (strds, input) # Create the output space time vector dataset out_sp = tgis.open_new_stds(output, "stvds", first_strds.get_temporal_type(), title, description, first_strds.get_semantic_type(), dbif, overwrite) dummy = out_sp.get_new_map_instance(None) # Sample the space time raster dataset with the vector # map at specific layer with v.what.rast count = 1 for sample in samples: raster_names = sample.raster_names if len(raster_names) != len(column_names): grass.fatal(_("The number of raster maps in a granule must " "be equal to the number of column names")) # Create the columns creation string columns_string = "" for name, column in zip(raster_names, column_names): # The column is by default double precision coltype = "DOUBLE PRECISION" # Get raster map type raster_map = tgis.RasterDataset(name) raster_map.load() if raster_map.metadata.get_datatype() == "CELL": coltype = "INT" tmp_string = "%s %s," %(column, coltype) columns_string += tmp_string # Remove last comma columns_string = columns_string[0:len(columns_string) - 1] # Try to add a column if vector_db and count in vector_db and vector_db[count]["table"]: try: grass.run_command("v.db.addcolumn", map=vectmap, layer=count, column=columns_string, overwrite=overwrite) except CalledModuleError: dbif.close() grass.fatal(_("Unable to add column %s to vector map <%s> " "with layer %i") % (columns_string, vectmap, count)) else: # Try to add a new table grass.message("Add table to layer %i" % (count)) try: grass.run_command("v.db.addtable", map=vectmap, layer=count, columns=columns_string, overwrite=overwrite) except CalledModuleError: dbif.close() grass.fatal(_("Unable to add table to vector map " "<%s> with layer %i") % (vectmap, count)) # Call v.what.rast for each raster map for name, column in zip(raster_names, column_names): try: grass.run_command("v.what.rast", map=vectmap, layer=count, raster=name, column=column, where=where) except CalledModuleError: dbif.close() grass.fatal(_("Unable to run v.what.rast for vector map <%s> " "with layer %i and raster map <%s>") % (vectmap, count, str(raster_names))) vect = out_sp.get_new_map_instance(dummy.build_id(vectmap, mapset, str(count))) vect.load() start = sample.start end = sample.end if out_sp.is_time_absolute(): vect.set_absolute_time(start, end) else: vect.set_relative_time( start, end, first_strds.get_relative_time_unit()) if vect.is_in_db(dbif): vect.update_all(dbif) else: vect.insert(dbif) out_sp.register_map(vect, dbif) count += 1 out_sp.update_from_registered_maps(dbif) dbif.close()
def main(options, flags): # Get the options input = options["input"] output = options["output"] where = options["where"] base = options["basename"] nprocs = int(options["nprocs"]) step = options["step"] levels = options["levels"] minlevel = options["minlevel"] maxlevel = options["maxlevel"] cut = options["cut"] register_null = flags["n"] t_flag = flags["t"] # Make sure the temporal database exists tgis.init() # We need a database interface dbif = tgis.SQLDatabaseInterfaceConnection() dbif.connect() overwrite = gscript.overwrite() sp = tgis.open_old_stds(input, "strds", dbif) maps = sp.get_registered_maps_as_objects(where=where, dbif=dbif) if not maps: dbif.close() gscript.warning(_("Space time raster dataset <%s> is empty") % sp.get_id()) return # Check the new stvds new_sp = tgis.check_new_stds(output, "stvds", dbif=dbif, overwrite=overwrite) # Setup the flags flags = "" if t_flag is True: flags += "t" # Configure the r.to.vect module contour_module = pymod.Module("r.contour", input="dummy", output="dummy", run_=False, finish_=False, flags=flags, overwrite=overwrite, quiet=True) if step: contour_module.inputs.step = float(step) if minlevel: contour_module.inputs.minlevel = float(minlevel) if maxlevel: contour_module.inputs.maxlevel = float(maxlevel) if levels: contour_module.inputs.levels = levels.split(",") if cut: contour_module.inputs.cut = int(cut) # The module queue for parallel execution, except if attribute tables should # be created. Then force single process use if t_flag is False: if nprocs > 1: nprocs = 1 gscript.warning(_("The number of parellel r.contour processes was "\ "reduced to 1 because of the table attribute "\ "creation")) process_queue = pymod.ParallelModuleQueue(int(nprocs)) count = 0 num_maps = len(maps) new_maps = [] # run r.to.vect all selected maps for map in maps: count += 1 map_name = "%s_%i" % (base, count) new_map = tgis.open_new_map_dataset(map_name, None, type="vector", temporal_extent=map.get_temporal_extent(), overwrite=overwrite, dbif=dbif) new_maps.append(new_map) mod = copy.deepcopy(contour_module) mod(input=map.get_id(), output=new_map.get_id()) sys.stderr.write(mod.get_bash() + "\n") process_queue.put(mod) if count%10 == 0: gscript.percent(count, num_maps, 1) # Wait for unfinished processes process_queue.wait() # Open the new space time vector dataset ttype, stype, title, descr = sp.get_initial_values() new_sp = tgis.open_new_stds(output, "stvds", ttype, title, descr, stype, dbif, overwrite) # collect empty maps to remove them num_maps = len(new_maps) empty_maps = [] # Register the maps in the database count = 0 for map in new_maps: count += 1 if count%10 == 0: gscript.percent(count, num_maps, 1) # Do not register empty maps try: if map.load() is not True: continue except FatalError: continue if map.metadata.get_number_of_primitives() == 0: if not register_null: empty_maps.append(map) continue # Insert map in temporal database map.insert(dbif) new_sp.register_map(map, dbif) # Update the spatio-temporal extent and the metadata table entries new_sp.update_from_registered_maps(dbif) gscript.percent(1, 1, 1) # Remove empty maps if len(empty_maps) > 0: names = "" count = 0 for map in empty_maps: if count == 0: count += 1 names += "%s" % (map.get_name()) else: names += ",%s" % (map.get_name()) gscript.run_command("g.remove", flags='f', type='vector', name=names, quiet=True) dbif.close()
def main(options, flags): # Get the options input = options["input"] output = options["output"] where = options["where"] base = options["basename"] method = options["type"] nprocs = int(options["nprocs"]) column = options["column"] time_suffix = options["suffix"] register_null = flags["n"] t_flag = flags["t"] s_flag = flags["s"] v_flag = flags["v"] b_flag = flags["b"] z_flag = flags["z"] # Make sure the temporal database exists tgis.init() # We need a database interface dbif = tgis.SQLDatabaseInterfaceConnection() dbif.connect() overwrite = gscript.overwrite() sp = tgis.open_old_stds(input, "strds", dbif) maps = sp.get_registered_maps_as_objects(where=where, dbif=dbif) if not maps: dbif.close() gscript.warning(_("Space time raster dataset <%s> is empty") % sp.get_id()) return # Check the new stvds new_sp = tgis.check_new_stds(output, "stvds", dbif=dbif, overwrite=overwrite) # Setup the flags flags = "" if t_flag is True: flags += "t" if s_flag is True: flags += "s" if v_flag is True: flags += "v" if b_flag is True: flags += "b" if z_flag is True: flags += "z" # Configure the r.to.vect module to_vector_module = pymod.Module("r.to.vect", input="dummy", output="dummy", run_=False, finish_=False, flags=flags, type=method, overwrite=overwrite, quiet=True) # The module queue for parallel execution, except if attribute tables should # be created. Then force single process use if t_flag is False: if nprocs > 1: nprocs = 1 gscript.warning(_("The number of parellel r.to.vect processes was "\ "reduced to 1 because of the table attribute "\ "creation")) process_queue = pymod.ParallelModuleQueue(int(nprocs)) count = 0 num_maps = len(maps) new_maps = [] # run r.to.vect all selected maps for map in maps: count += 1 if sp.get_temporal_type() == 'absolute' and time_suffix == 'gran': suffix = tgis.create_suffix_from_datetime(map.temporal_extent.get_start_time(), sp.get_granularity()) map_name = "{ba}_{su}".format(ba=base, su=suffix) elif sp.get_temporal_type() == 'absolute' and time_suffix == 'time': suffix = tgis.create_time_suffix(map) map_name = "{ba}_{su}".format(ba=base, su=suffix) else: map_name = tgis.create_numeric_suffic(base, count, time_suffix) new_map = tgis.open_new_map_dataset(map_name, None, type="vector", temporal_extent=map.get_temporal_extent(), overwrite=overwrite, dbif=dbif) new_maps.append(new_map) mod = copy.deepcopy(to_vector_module) mod(input=map.get_id(), output=new_map.get_id()) sys.stderr.write(mod.get_bash() + "\n") process_queue.put(mod) if count%10 == 0: gscript.percent(count, num_maps, 1) # Wait for unfinished processes process_queue.wait() # Open the new space time vector dataset ttype, stype, title, descr = sp.get_initial_values() new_sp = tgis.open_new_stds(output, "stvds", ttype, title, descr, stype, dbif, overwrite) # collect empty maps to remove them num_maps = len(new_maps) empty_maps = [] # Register the maps in the database count = 0 for map in new_maps: count += 1 if count%10 == 0: gscript.percent(count, num_maps, 1) # Do not register empty maps map.load() if map.metadata.get_number_of_primitives() == 0: if not register_null: empty_maps.append(map) continue # Insert map in temporal database map.insert(dbif) new_sp.register_map(map, dbif) # Update the spatio-temporal extent and the metadata table entries new_sp.update_from_registered_maps(dbif) gscript.percent(1, 1, 1) # Remove empty maps if len(empty_maps) > 0: names = "" count = 0 for map in empty_maps: if count == 0: count += 1 names += "%s" % (map.get_name()) else: names += ",%s" % (map.get_name()) gscript.run_command("g.remove", flags='f', type='vector', name=names, quiet=True) dbif.close()
def main(): # Get the options input = options["input"] output = options["output"] where = options["where"] size = options["size"] base = options["basename"] register_null = flags["n"] method = options["method"] nprocs = options["nprocs"] # Make sure the temporal database exists tgis.init() # We need a database interface dbif = tgis.SQLDatabaseInterfaceConnection() dbif.connect() overwrite = grass.overwrite() sp = tgis.open_old_stds(input, "strds", dbif) maps = sp.get_registered_maps_as_objects(where=where, dbif=dbif) if not maps: dbif.close() grass.warning( _("Space time raster dataset <%s> is empty") % sp.get_id()) return new_sp = tgis.check_new_stds(output, "strds", dbif=dbif, overwrite=overwrite) # Configure the r.neighbor module neighbor_module = pymod.Module("r.neighbors", input="dummy", output="dummy", run_=False, finish_=False, size=int(size), method=method, overwrite=overwrite, quiet=True) # The module queue for parallel execution process_queue = pymod.ParallelModuleQueue(int(nprocs)) count = 0 num_maps = len(maps) new_maps = [] # run r.neighbors all selected maps for map in maps: count += 1 map_name = "%s_%i" % (base, count) new_map = tgis.open_new_map_dataset( map_name, None, type="raster", temporal_extent=map.get_temporal_extent(), overwrite=overwrite, dbif=dbif) new_maps.append(new_map) mod = copy.deepcopy(neighbor_module) mod(input=map.get_id(), output=new_map.get_id()) print(mod.get_bash()) process_queue.put(mod) # Wait for unfinished processes process_queue.wait() # Open the new space time raster dataset ttype, stype, title, descr = sp.get_initial_values() new_sp = tgis.open_new_stds(output, "strds", ttype, title, descr, stype, dbif, overwrite) num_maps = len(new_maps) # collect empty maps to remove them empty_maps = [] # Register the maps in the database count = 0 for map in new_maps: count += 1 if count % 10 == 0: grass.percent(count, num_maps, 1) # Do not register empty maps map.load() if map.metadata.get_min() is None and \ map.metadata.get_max() is None: if not register_null: empty_maps.append(map) continue # Insert map in temporal database map.insert(dbif) new_sp.register_map(map, dbif) # Update the spatio-temporal extent and the metadata table entries new_sp.update_from_registered_maps(dbif) grass.percent(1, 1, 1) # Remove empty maps if len(empty_maps) > 0: names = "" count = 0 for map in empty_maps: if count == 0: count += 1 names += "%s" % (map.get_name()) else: names += ",%s" % (map.get_name()) grass.run_command("g.remove", flags='f', type='raster', name=names, quiet=True) dbif.close()
def main(options, flags): # lazy imports import grass.temporal as tgis import grass.pygrass.modules as pymod # Get the options input = options["input"] output = options["output"] where = options["where"] base = options["basename"] nprocs = int(options["nprocs"]) step = options["step"] levels = options["levels"] minlevel = options["minlevel"] maxlevel = options["maxlevel"] cut = options["cut"] time_suffix = options["suffix"] register_null = flags["n"] t_flag = flags["t"] # Make sure the temporal database exists tgis.init() # We need a database interface dbif = tgis.SQLDatabaseInterfaceConnection() dbif.connect() overwrite = gscript.overwrite() sp = tgis.open_old_stds(input, "strds", dbif) maps = sp.get_registered_maps_as_objects(where=where, dbif=dbif) if not maps: dbif.close() gscript.warning( _("Space time raster dataset <%s> is empty") % sp.get_id()) return # Check the new stvds new_sp = tgis.check_new_stds(output, "stvds", dbif=dbif, overwrite=overwrite) # Setup the flags flags = "" if t_flag is True: flags += "t" # Configure the r.to.vect module contour_module = pymod.Module("r.contour", input="dummy", output="dummy", run_=False, finish_=False, flags=flags, overwrite=overwrite, quiet=True) if step: contour_module.inputs.step = float(step) if minlevel: contour_module.inputs.minlevel = float(minlevel) if maxlevel: contour_module.inputs.maxlevel = float(maxlevel) if levels: contour_module.inputs.levels = levels.split(",") if cut: contour_module.inputs.cut = int(cut) # The module queue for parallel execution, except if attribute tables should # be created. Then force single process use if t_flag is False: if nprocs > 1: nprocs = 1 gscript.warning( _("The number of parellel r.contour processes was " "reduced to 1 because of the table attribute " "creation")) process_queue = pymod.ParallelModuleQueue(int(nprocs)) count = 0 num_maps = len(maps) new_maps = [] # run r.to.vect all selected maps for map in maps: count += 1 if sp.get_temporal_type() == 'absolute' and time_suffix == 'gran': suffix = tgis.create_suffix_from_datetime( map.temporal_extent.get_start_time(), sp.get_granularity()) map_name = "{ba}_{su}".format(ba=base, su=suffix) elif sp.get_temporal_type() == 'absolute' and time_suffix == 'time': suffix = tgis.create_time_suffix(map) map_name = "{ba}_{su}".format(ba=base, su=suffix) else: map_name = tgis.create_numeric_suffix(base, count, time_suffix) new_map = tgis.open_new_map_dataset( map_name, None, type="vector", temporal_extent=map.get_temporal_extent(), overwrite=overwrite, dbif=dbif) new_maps.append(new_map) mod = copy.deepcopy(contour_module) mod(input=map.get_id(), output=new_map.get_id()) sys.stderr.write(mod.get_bash() + "\n") process_queue.put(mod) if count % 10 == 0: gscript.percent(count, num_maps, 1) # Wait for unfinished processes process_queue.wait() # Open the new space time vector dataset ttype, stype, title, descr = sp.get_initial_values() new_sp = tgis.open_new_stds(output, "stvds", ttype, title, descr, stype, dbif, overwrite) # collect empty maps to remove them num_maps = len(new_maps) empty_maps = [] # Register the maps in the database count = 0 for map in new_maps: count += 1 if count % 10 == 0: gscript.percent(count, num_maps, 1) # Do not register empty maps try: if map.load() is not True: continue except FatalError: continue if map.metadata.get_number_of_primitives() == 0: if not register_null: empty_maps.append(map) continue # Insert map in temporal database map.insert(dbif) new_sp.register_map(map, dbif) # Update the spatio-temporal extent and the metadata table entries new_sp.update_from_registered_maps(dbif) gscript.percent(1, 1, 1) # Remove empty maps if len(empty_maps) > 0: names = "" count = 0 for map in empty_maps: if count == 0: count += 1 names += "%s" % (map.get_name()) else: names += ",%s" % (map.get_name()) gscript.run_command("g.remove", flags='f', type='vector', name=names, quiet=True) dbif.close()