|
@@ -123,202 +123,9 @@ def main():
|
|
# Make sure the temporal database exists
|
|
# Make sure the temporal database exists
|
|
tgis.create_temporal_database()
|
|
tgis.create_temporal_database()
|
|
# Register maps
|
|
# Register maps
|
|
- register_maps_in_space_time_dataset(type=type, name=name, maps=maps, file=file, start=start, end=end, \
|
|
|
|
|
|
+ tgis.register_maps_in_space_time_dataset(type=type, name=name, maps=maps, file=file, start=start, end=end, \
|
|
unit=unit, increment=increment, dbif=None, interval=interval, fs=fs)
|
|
unit=unit, increment=increment, dbif=None, interval=interval, fs=fs)
|
|
-
|
|
|
|
-###############################################################################
|
|
|
|
-
|
|
|
|
-def register_maps_in_space_time_dataset(type, name, maps=None, file=None, start=None, \
|
|
|
|
- end=None, unit=None, increment=None, dbif = None, \
|
|
|
|
- interval=False, fs="|"):
|
|
|
|
- """Use this method to register maps in space time datasets. This function is generic and
|
|
|
|
-
|
|
|
|
- Additionally a start time string and an increment string can be specified
|
|
|
|
- to assign a time interval automatically to the maps.
|
|
|
|
-
|
|
|
|
- It takes care of the correct update of the space time datasets from all
|
|
|
|
- registered maps.
|
|
|
|
-
|
|
|
|
- @param type: The type of the maps rast, rast3d or vect
|
|
|
|
- @param name: The name of the space time dataset
|
|
|
|
- @param maps: A comma separated list of map names
|
|
|
|
- @param file: Input file one map with start and optional end time, one per line
|
|
|
|
- @param start: The start date and time of the first raster map (format absolute: "yyyy-mm-dd HH:MM:SS" or "yyyy-mm-dd", format relative is integer 5)
|
|
|
|
- @param end: The end date and time of the first raster map (format absolute: "yyyy-mm-dd HH:MM:SS" or "yyyy-mm-dd", format relative is integer 5)
|
|
|
|
- @param unit: The unit of the relative time: years, months, days, hours, minutes, seconds
|
|
|
|
- @param increment: Time increment between maps for time stamp creation (format absolute: NNN seconds, minutes, hours, days, weeks, months, years; format relative: 1.0)
|
|
|
|
- @param dbif: The database interface to be used
|
|
|
|
- @param interval: If True, time intervals are created in case the start time and an increment is provided
|
|
|
|
- @param fs: Field separator used in input file
|
|
|
|
- """
|
|
|
|
-
|
|
|
|
- start_time_in_file = False
|
|
|
|
- end_time_in_file = False
|
|
|
|
- if maps and file:
|
|
|
|
- grass.fatal(_("%s= and %s= are mutually exclusive") % ("input","file"))
|
|
|
|
-
|
|
|
|
- if end and increment:
|
|
|
|
- grass.fatal(_("%s= and %s= are mutually exclusive") % ("end","increment"))
|
|
|
|
-
|
|
|
|
- if end and not start:
|
|
|
|
- grass.fatal(_("Please specify %s= and %s=") % ("start_time","end_time"))
|
|
|
|
-
|
|
|
|
- if not maps and not file:
|
|
|
|
- grass.fatal(_("Please specify %s= or %s=") % ("input","file"))
|
|
|
|
-
|
|
|
|
- # We may need the mapset
|
|
|
|
- mapset = grass.gisenv()["MAPSET"]
|
|
|
|
-
|
|
|
|
- # Check if the dataset name contains the mapset as well
|
|
|
|
- if name.find("@") < 0:
|
|
|
|
- id = name + "@" + mapset
|
|
|
|
- else:
|
|
|
|
- id = name
|
|
|
|
-
|
|
|
|
- if type == "rast":
|
|
|
|
- sp = tgis.dataset_factory("strds", id)
|
|
|
|
- if type == "rast3d":
|
|
|
|
- sp = tgis.dataset_factory("str3ds", id)
|
|
|
|
- if type == "vect":
|
|
|
|
- sp = tgis.dataset_factory("stvds", id)
|
|
|
|
-
|
|
|
|
- connect = False
|
|
|
|
-
|
|
|
|
- if dbif == None:
|
|
|
|
- dbif = tgis.sql_database_interface()
|
|
|
|
- dbif.connect()
|
|
|
|
- connect = True
|
|
|
|
-
|
|
|
|
- # Read content from temporal database
|
|
|
|
- sp.select(dbif)
|
|
|
|
-
|
|
|
|
- if sp.is_in_db(dbif) == False:
|
|
|
|
- dbif.close()
|
|
|
|
- grass.fatal(_("Space time %s dataset <%s> no found") % (sp.get_new_map_instance(None).get_type(), name))
|
|
|
|
-
|
|
|
|
- if sp.is_time_relative() and not unit:
|
|
|
|
- dbif.close()
|
|
|
|
- grass.fatal(_("Space time %s dataset <%s> with relative time found, but no relative unit set for %s maps") % (sp.get_new_map_instance(None).get_type(), name, sp.get_new_map_instance(None).get_type()))
|
|
|
|
-
|
|
|
|
- dummy = sp.get_new_map_instance(None)
|
|
|
|
-
|
|
|
|
- maplist = []
|
|
|
|
-
|
|
|
|
- # Map names as comma separated string
|
|
|
|
- if maps:
|
|
|
|
- if maps.find(",") < 0:
|
|
|
|
- maplist = [maps,]
|
|
|
|
- else:
|
|
|
|
- maplist = maps.split(",")
|
|
|
|
-
|
|
|
|
- # Build the maplist again with the ids
|
|
|
|
- for count in range(len(maplist)):
|
|
|
|
- row = {}
|
|
|
|
- mapid = dummy.build_id(maplist[count], mapset, None)
|
|
|
|
-
|
|
|
|
- row["id"] = mapid
|
|
|
|
- maplist[count] = row
|
|
|
|
-
|
|
|
|
- # Read the map list from file
|
|
|
|
- if file:
|
|
|
|
- fd = open(file, "r")
|
|
|
|
-
|
|
|
|
- line = True
|
|
|
|
- while True:
|
|
|
|
- line = fd.readline()
|
|
|
|
- if not line:
|
|
|
|
- break
|
|
|
|
-
|
|
|
|
- line_list = line.split(fs)
|
|
|
|
-
|
|
|
|
- # Detect start and end time
|
|
|
|
- if len(line_list) == 2:
|
|
|
|
- start_time_in_file = True
|
|
|
|
- end_time_in_file = False
|
|
|
|
- elif len(line_list) == 3:
|
|
|
|
- start_time_in_file = True
|
|
|
|
- end_time_in_file = True
|
|
|
|
- else:
|
|
|
|
- start_time_in_file = False
|
|
|
|
- end_time_in_file = False
|
|
|
|
-
|
|
|
|
- mapname = line_list[0].strip()
|
|
|
|
- row = {}
|
|
|
|
-
|
|
|
|
- if start_time_in_file and end_time_in_file:
|
|
|
|
- row["start"] = line_list[1].strip()
|
|
|
|
- row["end"] = line_list[2].strip()
|
|
|
|
-
|
|
|
|
- if start_time_in_file and not end_time_in_file:
|
|
|
|
- row["start"] = line_list[1].strip()
|
|
|
|
-
|
|
|
|
- row["id"] = dummy.build_id(mapname, mapset)
|
|
|
|
-
|
|
|
|
- maplist.append(row)
|
|
|
|
-
|
|
|
|
- num_maps = len(maplist)
|
|
|
|
- for count in range(len(maplist)):
|
|
|
|
- grass.percent(count, num_maps, 1)
|
|
|
|
-
|
|
|
|
- # Get a new instance of the space time dataset map type
|
|
|
|
- map = sp.get_new_map_instance(maplist[count]["id"])
|
|
|
|
-
|
|
|
|
- # Use the time data from file
|
|
|
|
- if maplist[count].has_key("start"):
|
|
|
|
- start = maplist[count]["start"]
|
|
|
|
- if maplist[count].has_key("end"):
|
|
|
|
- end = maplist[count]["end"]
|
|
|
|
-
|
|
|
|
- # Put the map into the database
|
|
|
|
- if map.is_in_db(dbif) == False:
|
|
|
|
- # Break in case no valid time is provided
|
|
|
|
- if start == "" or start == None:
|
|
|
|
- dbif.close()
|
|
|
|
- if map.get_layer():
|
|
|
|
- grass.fatal(_("Unable to register %s map <%s> with layer %s. The map has no valid time and the start time is not set.") % \
|
|
|
|
- (map.get_type(), map.get_map_id(), map.get_layer() ))
|
|
|
|
- else:
|
|
|
|
- grass.fatal(_("Unable to register %s map <%s>. The map has no valid time and the start time is not set.") % \
|
|
|
|
- (map.get_type(), map.get_map_id() ))
|
|
|
|
- # Load the data from the grass file database
|
|
|
|
- map.load()
|
|
|
|
-
|
|
|
|
- if sp.get_temporal_type() == "absolute":
|
|
|
|
- map.set_time_to_absolute()
|
|
|
|
- else:
|
|
|
|
- map.set_time_to_relative()
|
|
|
|
-
|
|
|
|
- # Put it into the temporal database
|
|
|
|
- map.insert(dbif)
|
|
|
|
- else:
|
|
|
|
- map.select(dbif)
|
|
|
|
- if map.get_temporal_type() != sp.get_temporal_type():
|
|
|
|
- dbif.close()
|
|
|
|
- if map.get_layer():
|
|
|
|
- grass.fatal(_("Unable to register %s map <%s> with layer. The temporal types are different.") % \
|
|
|
|
- (map.get_type(), map.get_map_id(), map.get_layer()))
|
|
|
|
- grass.fatal(_("Unable to register %s map <%s>. The temporal types are different.") % \
|
|
|
|
- (map.get_type(), map.get_map_id()))
|
|
|
|
-
|
|
|
|
- # In case the time is in the input file we ignore the increment counter
|
|
|
|
- if start_time_in_file:
|
|
|
|
- count = 1
|
|
|
|
-
|
|
|
|
- # Set the valid time
|
|
|
|
- if start:
|
|
|
|
- tgis.assign_valid_time_to_map(ttype=sp.get_temporal_type(), map=map, start=start, end=end, unit=unit, increment=increment, mult=count, dbif=dbif, interval=interval)
|
|
|
|
-
|
|
|
|
- # Finally Register map in the space time dataset
|
|
|
|
- sp.register_map(map, dbif)
|
|
|
|
-
|
|
|
|
- # Update the space time tables
|
|
|
|
- sp.update_from_registered_maps(dbif)
|
|
|
|
-
|
|
|
|
- if connect == True:
|
|
|
|
- dbif.close()
|
|
|
|
|
|
|
|
- grass.percent(num_maps, num_maps, 1)
|
|
|
|
-
|
|
|
|
###############################################################################
|
|
###############################################################################
|
|
|
|
|
|
if __name__ == "__main__":
|
|
if __name__ == "__main__":
|