def compute_occurrence(occurrence_maps, input_strds, input_maps, start, base, count, mapset, where, reverse, range, minimum_strds, maximum_strds, dbif): if minimum_strds: input_maps_minimum = input_strds.get_registered_maps_as_objects(where=where, dbif=dbif) minimum_maps = minimum_strds.get_registered_maps_as_objects(dbif=dbif) minimum_topo = tgis.SpatioTemporalTopologyBuilder() minimum_topo.build(input_maps_minimum, minimum_maps) if maximum_strds: input_maps_maximum = input_strds.get_registered_maps_as_objects(where=where, dbif=dbif) maximum_maps = maximum_strds.get_registered_maps_as_objects(dbif=dbif) maximum_topo = tgis.SpatioTemporalTopologyBuilder() maximum_topo.build(input_maps_maximum, maximum_maps) # Aggregate num_maps = len(input_maps) for i in xrange(num_maps): if reverse: map = input_maps[num_maps - i - 1] else: map = input_maps[i] # Compute the days since start input_start, input_end = map.get_temporal_extent_as_tuple() td = input_start - start if map.is_time_absolute(): days = tgis.time_delta_to_relative_time(td) else: days = td occurrence_map_name = "%s_%i" % (base, count) occurrence_map_id = map.build_id(occurrence_map_name, mapset) occurrence_map = input_strds.get_new_map_instance(occurrence_map_id) # Check if new map is in the temporal database if occurrence_map.is_in_db(dbif): if grass.overwrite(): # Remove the existing temporal database entry occurrence_map.delete(dbif) occurrence_map = input_strds.get_new_map_instance(occurrence_map_id) else: grass.fatal(_("Map <%s> is already registered in the temporal" " database, use overwrite flag to overwrite.") % (occurrence_map.get_map_id())) range_vals = range.split(",") min = range_vals[0] max = range_vals[1] if minimum_strds: relations = input_maps_minimum[i].get_temporal_relations() for relation in range_relations: if relation in relations: min = str(relations[relation][0].get_id()) break if maximum_strds: relations = input_maps_maximum[i].get_temporal_relations() for relation in range_relations: if relation in relations: max = str(relations[relation][0].get_id()) break expression = "%s = if(%s > %s && %s < %s, %s, null())"%(occurrence_map_name, map.get_name(), min, map.get_name(), max, days) print expression grass.mapcalc(expression, overwrite=True) map_start, map_end = map.get_temporal_extent_as_tuple() if map.is_time_absolute(): occurrence_map.set_absolute_time(map_start, map_end) else: occurrence_map.set_relative_time(map_start, map_end, map.get_relative_time_unit()) # Store the new maps occurrence_maps[map.get_id()] = occurrence_map count += 1 return count
def main(): # Get the options input = options["input"] output = options["output"] # Make sure the temporal database exists tgis.init() mapset = grass.gisenv()["MAPSET"] sp = tgis.open_old_stds(input, "strds") grass.use_temp_region() maps = sp.get_registered_maps_as_objects_by_granularity() num_maps = len(maps) # get datatype of the first map if maps: maps[0][0].select() datatype = maps[0][0].metadata.get_datatype() else: datatype = None # Get the granularity and set bottom, top and top-bottom resolution granularity = sp.get_granularity() # This is the reference time to scale the z coordinate reftime = datetime(1900, 1, 1) # We set top and bottom according to the start time in relation # to the date 1900-01-01 00:00:00 # In case of days, hours, minutes and seconds, a double number # is used to represent days and fracs of a day # Space time voxel cubes with montly or yearly granularity can not be # mixed with other temporal units # Compatible temporal units are : days, hours, minutes and seconds # Incompatible are years and moths start, end = sp.get_temporal_extent_as_tuple() if sp.is_time_absolute(): unit = granularity.split(" ")[1] granularity = float(granularity.split(" ")[0]) print "Gran from stds %0.15f"%(granularity) if unit == "years" or unit == "year": bottom = float(start.year - 1900) top = float(granularity * num_maps) elif unit == "months" or unit == "month": bottom = float((start.year - 1900) * 12 + start.month) top = float(granularity * num_maps) else: bottom = float(tgis.time_delta_to_relative_time(start - reftime)) days = 0.0 hours = 0.0 minutes = 0.0 seconds = 0.0 if unit == "days" or unit == "day": days = float(granularity) if unit == "hours" or unit == "hour": hours = float(granularity) if unit == "minutes" or unit == "minute": minutes = float(granularity) if unit == "seconds" or unit == "second": seconds = float(granularity) granularity = float(days + hours / 24.0 + minutes / \ 1440.0 + seconds / 86400.0) else: unit = sp.get_relative_time_unit() bottom = start top = float(bottom + granularity * float(num_maps)) try: grass.run_command("g.region", t=top, b=bottom, tbres=granularity) except CalledModuleError: grass.fatal(_("Unable to set 3D region")) # Create a NULL map to fill the gaps null_map = "temporary_null_map_%i" % os.getpid() if datatype == 'DCELL': grass.mapcalc("%s = double(null())" % (null_map)) elif datatype == 'FCELL': grass.mapcalc("%s = float(null())" % (null_map)) else: grass.mapcalc("%s = null()" % (null_map)) if maps: count = 0 map_names = "" for map in maps: # Use the first map id = map[0].get_id() # None ids will be replaced by NULL maps if id is None: id = null_map if count == 0: map_names = id else: map_names += ",%s" % id count += 1 try: grass.run_command("r.to.rast3", input=map_names, output=output, overwrite=grass.overwrite()) except CalledModuleError: grass.fatal(_("Unable to create 3D raster map <%s>" % output)) grass.run_command("g.remove", flags='f', type='raster', name=null_map) title = _("Space time voxel cube") descr = _("This space time voxel cube was created with t.rast.to.rast3") # Set the unit try: grass.run_command("r3.support", map=output, vunit=unit, title=title, description=descr, overwrite=grass.overwrite()) except CalledModuleError: grass.warning(_("%s failed to set units.") % 'r3.support') # Register the space time voxel cube in the temporal GIS if output.find("@") >= 0: id = output else: id = output + "@" + mapset start, end = sp.get_temporal_extent_as_tuple() r3ds = tgis.Raster3DDataset(id) if r3ds.is_in_db(): r3ds.select() r3ds.delete() r3ds = tgis.Raster3DDataset(id) r3ds.load() if sp.is_time_absolute(): r3ds.set_absolute_time(start, end) else: r3ds.set_relative_time(start, end, sp.get_relative_time_unit()) r3ds.insert()
def compute_occurrence(occurrence_maps, input_strds, input_maps, start, base, count, mapset, where, reverse, range, minimum_strds, maximum_strds, dbif): if minimum_strds: input_maps_minimum = input_strds.get_registered_maps_as_objects( where=where, dbif=dbif) minimum_maps = minimum_strds.get_registered_maps_as_objects(dbif=dbif) minimum_topo = tgis.SpatioTemporalTopologyBuilder() minimum_topo.build(input_maps_minimum, minimum_maps) if maximum_strds: input_maps_maximum = input_strds.get_registered_maps_as_objects( where=where, dbif=dbif) maximum_maps = maximum_strds.get_registered_maps_as_objects(dbif=dbif) maximum_topo = tgis.SpatioTemporalTopologyBuilder() maximum_topo.build(input_maps_maximum, maximum_maps) # Aggregate num_maps = len(input_maps) for i in xrange(num_maps): if reverse: map = input_maps[num_maps - i - 1] else: map = input_maps[i] # Compute the days since start input_start, input_end = map.get_temporal_extent_as_tuple() td = input_start - start if map.is_time_absolute(): days = tgis.time_delta_to_relative_time(td) else: days = td occurrence_map_name = "%s_%i" % (base, count) occurrence_map_id = map.build_id(occurrence_map_name, mapset) occurrence_map = input_strds.get_new_map_instance(occurrence_map_id) # Check if new map is in the temporal database if occurrence_map.is_in_db(dbif): if grass.overwrite(): # Remove the existing temporal database entry occurrence_map.delete(dbif) occurrence_map = input_strds.get_new_map_instance( occurrence_map_id) else: grass.fatal( _("Map <%s> is already registered in the temporal" " database, use overwrite flag to overwrite.") % (occurrence_map.get_map_id())) range_vals = range.split(",") min = range_vals[0] max = range_vals[1] if minimum_strds: relations = input_maps_minimum[i].get_temporal_relations() for relation in range_relations: if relation in relations: min = str(relations[relation][0].get_id()) break if maximum_strds: relations = input_maps_maximum[i].get_temporal_relations() for relation in range_relations: if relation in relations: max = str(relations[relation][0].get_id()) break expression = "%s = if(%s > %s && %s < %s, %s, null())" % ( occurrence_map_name, map.get_name(), min, map.get_name(), max, days) print expression grass.mapcalc(expression, overwrite=True) map_start, map_end = map.get_temporal_extent_as_tuple() if map.is_time_absolute(): occurrence_map.set_absolute_time(map_start, map_end) else: occurrence_map.set_relative_time(map_start, map_end, map.get_relative_time_unit()) # Store the new maps occurrence_maps[map.get_id()] = occurrence_map count += 1 return count