|
@@ -23,7 +23,7 @@
|
|
#%option G_OPT_V_INPUT
|
|
#%option G_OPT_V_INPUT
|
|
#%end
|
|
#%end
|
|
|
|
|
|
-#%option G_OPT_STRDS_INPUT
|
|
|
|
|
|
+#%option G_OPT_STRDS_INPUTS
|
|
#% key: strds
|
|
#% key: strds
|
|
#%end
|
|
#%end
|
|
|
|
|
|
@@ -32,18 +32,17 @@
|
|
|
|
|
|
#%option G_OPT_V_OUTPUT
|
|
#%option G_OPT_V_OUTPUT
|
|
#% key: vector_output
|
|
#% key: vector_output
|
|
-#% description: Name of the new created vector map that stores the sampled values
|
|
|
|
|
|
+#% description: Name of the new created vector map that stores the sampled values in different layers
|
|
#%end
|
|
#%end
|
|
|
|
|
|
#%option
|
|
#%option
|
|
-#% key: column
|
|
|
|
|
|
+#% key: columns
|
|
#% type: string
|
|
#% type: string
|
|
-#% description: Name of the vector column to be created and to store sampled raster values, otherwise the space time raster name is used as column name
|
|
|
|
-#% required: no
|
|
|
|
-#% multiple: no
|
|
|
|
|
|
+#% description: Names of the vector columns to be created and to store sampled raster values, one name for each STRDS
|
|
|
|
+#% required: yes
|
|
|
|
+#% multiple: yes
|
|
#%end
|
|
#%end
|
|
|
|
|
|
-
|
|
|
|
#%option G_OPT_DB_WHERE
|
|
#%option G_OPT_DB_WHERE
|
|
#%end
|
|
#%end
|
|
|
|
|
|
@@ -57,6 +56,19 @@ import grass.script.raster as raster
|
|
|
|
|
|
############################################################################
|
|
############################################################################
|
|
|
|
|
|
|
|
+class Sample(object):
|
|
|
|
+ def __init__(self, start = None, end = None, raster_names = None):
|
|
|
|
+ self.start = start
|
|
|
|
+ self.end = end
|
|
|
|
+ if raster_names != None:
|
|
|
|
+ self.raster_names = raster_names
|
|
|
|
+ else:
|
|
|
|
+ self.raster_names = []
|
|
|
|
+
|
|
|
|
+ def __str__(self):
|
|
|
|
+ return "Start: %s\nEnd: %s\nNames: %s\n"%(str(self.start), str(self.end), str(self.raster_names))
|
|
|
|
+
|
|
|
|
+############################################################################
|
|
|
|
|
|
def main():
|
|
def main():
|
|
|
|
|
|
@@ -66,7 +78,7 @@ def main():
|
|
vector_output = options["vector_output"]
|
|
vector_output = options["vector_output"]
|
|
strds = options["strds"]
|
|
strds = options["strds"]
|
|
where = options["where"]
|
|
where = options["where"]
|
|
- column = options["column"]
|
|
|
|
|
|
+ columns = options["columns"]
|
|
tempwhere = options["t_where"]
|
|
tempwhere = options["t_where"]
|
|
|
|
|
|
if where == "" or where == " " or where == "\n":
|
|
if where == "" or where == " " or where == "\n":
|
|
@@ -74,6 +86,13 @@ def main():
|
|
|
|
|
|
overwrite = grass.overwrite()
|
|
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
|
|
# Make sure the temporal database exists
|
|
tgis.init()
|
|
tgis.init()
|
|
# We need a database interface
|
|
# We need a database interface
|
|
@@ -82,27 +101,70 @@ def main():
|
|
|
|
|
|
mapset = grass.gisenv()["MAPSET"]
|
|
mapset = grass.gisenv()["MAPSET"]
|
|
|
|
|
|
- strds_sp = tgis.open_old_space_time_dataset(strds, "strds", dbif)
|
|
|
|
|
|
+ out_sp = tgis.check_new_space_time_dataset(output, "stvds", dbif, overwrite)
|
|
|
|
|
|
|
|
+ samples = []
|
|
|
|
|
|
- title = _("Observaion of space time raster dataset <%s>") % (strds_sp.get_id())
|
|
|
|
- description= _("Observation of space time raster dataset <%s>"
|
|
|
|
- " with vector map <%s>") % (strds_sp.get_id(),
|
|
|
|
- input)
|
|
|
|
|
|
+ first_strds = tgis.open_old_space_time_dataset(strds_names[0], "strds", dbif)
|
|
|
|
|
|
- out_sp = tgis.check_new_space_time_dataset(output, "stvds", dbif,
|
|
|
|
- overwrite)
|
|
|
|
|
|
+ # Single space time raster dataset
|
|
|
|
+ if len(strds_names) == 1:
|
|
|
|
+ rows = first_strds.get_registered_maps(
|
|
|
|
+ "name,mapset,start_time,end_time", tempwhere, "start_time", dbif)
|
|
|
|
|
|
- # Select the raster maps
|
|
|
|
- rows = strds_sp.get_registered_maps(
|
|
|
|
- "name,mapset,start_time,end_time", tempwhere, "start_time", dbif)
|
|
|
|
|
|
+ if not rows:
|
|
|
|
+ dbif.close()
|
|
|
|
+ grass.fatal(_("Space time raster dataset <%s> is empty") %
|
|
|
|
+ out_sp.get_id())
|
|
|
|
|
|
- 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"],]
|
|
|
|
|
|
- num_rows = len(rows)
|
|
|
|
|
|
+ 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_space_time_dataset(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 xrange(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
|
|
# Get the layer and database connections of the input vector
|
|
vector_db = grass.vector.vector_db(input)
|
|
vector_db = grass.vector.vector_db(input)
|
|
@@ -114,7 +176,7 @@ def main():
|
|
else:
|
|
else:
|
|
layers = ""
|
|
layers = ""
|
|
first = True
|
|
first = True
|
|
- for layer in range(num_rows):
|
|
|
|
|
|
+ for layer in range(num_samples):
|
|
layer += 1
|
|
layer += 1
|
|
# Skip existing layer
|
|
# Skip existing layer
|
|
if vector_db and layer in vector_db and \
|
|
if vector_db and layer in vector_db and \
|
|
@@ -136,11 +198,15 @@ def main():
|
|
grass.fatal(_("Unable to create new layers for vector map <%s>")
|
|
grass.fatal(_("Unable to create new layers for vector map <%s>")
|
|
% (vectmap))
|
|
% (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
|
|
# Create the output space time vector dataset
|
|
out_sp = tgis.open_new_space_time_dataset(output, "stvds",
|
|
out_sp = tgis.open_new_space_time_dataset(output, "stvds",
|
|
- strds_sp.get_temporal_type(),
|
|
|
|
|
|
+ first_strds.get_temporal_type(),
|
|
title, description,
|
|
title, description,
|
|
- strds_sp.get_semantic_type(),
|
|
|
|
|
|
+ first_strds.get_semantic_type(),
|
|
dbif, overwrite)
|
|
dbif, overwrite)
|
|
|
|
|
|
dummy = out_sp.get_new_map_instance(None)
|
|
dummy = out_sp.get_new_map_instance(None)
|
|
@@ -148,64 +214,72 @@ def main():
|
|
# Sample the space time raster dataset with the vector
|
|
# Sample the space time raster dataset with the vector
|
|
# map at specific layer with v.what.rast
|
|
# map at specific layer with v.what.rast
|
|
count = 1
|
|
count = 1
|
|
- for row in rows:
|
|
|
|
- start = row["start_time"]
|
|
|
|
- end = row["end_time"]
|
|
|
|
- rastmap = row["name"] + "@" + row["mapset"]
|
|
|
|
-
|
|
|
|
- if column:
|
|
|
|
- col_name = column
|
|
|
|
- else:
|
|
|
|
- # Create a new column with name of the
|
|
|
|
- # sampled space time raster dataset
|
|
|
|
- col_name = row["name"]
|
|
|
|
-
|
|
|
|
- coltype = "DOUBLE PRECISION"
|
|
|
|
- # Get raster map type
|
|
|
|
- rasterinfo = raster.raster_info(rastmap)
|
|
|
|
- if rasterinfo["datatype"] == "CELL":
|
|
|
|
- coltype = "INT"
|
|
|
|
|
|
+ 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
|
|
# Try to add a column
|
|
if vector_db and count in vector_db and vector_db[count]["table"]:
|
|
if vector_db and count in vector_db and vector_db[count]["table"]:
|
|
ret = grass.run_command("v.db.addcolumn", map=vectmap,
|
|
ret = grass.run_command("v.db.addcolumn", map=vectmap,
|
|
- layer=count,
|
|
|
|
- column="%s %s" % (col_name, coltype),
|
|
|
|
|
|
+ layer=count, column=columns_string,
|
|
overwrite=overwrite)
|
|
overwrite=overwrite)
|
|
if ret != 0:
|
|
if ret != 0:
|
|
dbif.close()
|
|
dbif.close()
|
|
grass.fatal(_("Unable to add column %s to vector map <%s> "
|
|
grass.fatal(_("Unable to add column %s to vector map <%s> "
|
|
- "with layer %i") % (col_name, vectmap, count))
|
|
|
|
|
|
+ "with layer %i") % (columns_string, vectmap, count))
|
|
else:
|
|
else:
|
|
# Try to add a new table
|
|
# Try to add a new table
|
|
grass.message("Add table to layer %i" % (count))
|
|
grass.message("Add table to layer %i" % (count))
|
|
ret = grass.run_command("v.db.addtable", map=vectmap, layer=count,
|
|
ret = grass.run_command("v.db.addtable", map=vectmap, layer=count,
|
|
- columns="%s %s" % (col_name, coltype),
|
|
|
|
- overwrite=overwrite)
|
|
|
|
|
|
+ columns=columns_string, overwrite=overwrite)
|
|
if ret != 0:
|
|
if ret != 0:
|
|
dbif.close()
|
|
dbif.close()
|
|
grass.fatal(_("Unable to add table to vector map "
|
|
grass.fatal(_("Unable to add table to vector map "
|
|
"<%s> with layer %i") % (vectmap, count))
|
|
"<%s> with layer %i") % (vectmap, count))
|
|
|
|
|
|
- # Call v.what.rast
|
|
|
|
- ret = grass.run_command("v.what.rast", map=vectmap,
|
|
|
|
- layer=count, raster=rastmap,
|
|
|
|
- column=col_name, where=where)
|
|
|
|
- if ret != 0:
|
|
|
|
- dbif.close()
|
|
|
|
- grass.fatal(_("Unable to run v.what.rast for vector map <%s> "
|
|
|
|
- "with layer %i and raster map <%s>") % \
|
|
|
|
- (vectmap, count, rastmap))
|
|
|
|
|
|
+ # Call v.what.rast for each raster map
|
|
|
|
+ for name, column in zip(raster_names, column_names):
|
|
|
|
+ ret = grass.run_command("v.what.rast", map=vectmap,
|
|
|
|
+ layer=count, raster=name,
|
|
|
|
+ column=column, where=where)
|
|
|
|
+ if ret != 0:
|
|
|
|
+ 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,
|
|
vect = out_sp.get_new_map_instance(dummy.build_id(vectmap,
|
|
mapset, str(count)))
|
|
mapset, str(count)))
|
|
vect.load()
|
|
vect.load()
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+ start = sample.start
|
|
|
|
+ end = sample.end
|
|
|
|
+
|
|
if out_sp.is_time_absolute():
|
|
if out_sp.is_time_absolute():
|
|
vect.set_absolute_time(start, end)
|
|
vect.set_absolute_time(start, end)
|
|
else:
|
|
else:
|
|
vect.set_relative_time(
|
|
vect.set_relative_time(
|
|
- start, end, strds_sp.get_relative_time_unit())
|
|
|
|
|
|
+ start, end, first_strds.get_relative_time_unit())
|
|
|
|
|
|
if vect.is_in_db(dbif):
|
|
if vect.is_in_db(dbif):
|
|
vect.update_all(dbif)
|
|
vect.update_all(dbif)
|