2
# -*- coding: utf-8 -*-
3
############################################################################
5
# MODULE: t.vect.what.strds
6
# AUTHOR(S): Soeren Gebbert
8
# PURPOSE: Store raster map values at spatial and temporal positions of vector points as vector attributes.
9
# COPYRIGHT: (C) 2011-2014 by the GRASS Development Team
11
# This program is free software under the GNU General Public
12
# License (version 2). Read the file COPYING that comes with GRASS
15
#############################################################################
18
#% description: Stores raster map values at spatial and temporal positions of vector points as vector attributes.
24
#%option G_OPT_STVDS_INPUT
27
#%option G_OPT_STRDS_INPUT
34
#% label: Name of the vector column to be created and to store sampled raster values
35
#% description: The use of a column name forces t.vect.what.rast to sample only values from the first map found in an interval. Otherwise the raster map names are used as column names
43
#% description: Aggregate operation to be performed on the raster maps
46
#% options: disabled,average,count,median,mode,minimum,min_raster,maximum,max_raster,stddev,range,sum,variance,diversity,slope,offset,detcoeff,quart1,quart3,perc90,quantile,skewness,kurtosis
50
#%option G_OPT_DB_WHERE
53
#%option G_OPT_T_WHERE
57
#%option G_OPT_T_SAMPLE
61
import grass.script as grass
62
import grass.temporal as tgis
63
import grass.script.raster as raster
64
from grass.exceptions import CalledModuleError
66
############################################################################
72
input = options["input"]
73
strds = options["strds"]
74
where = options["where"]
75
column = options["column"]
76
method = options["method"]
77
tempwhere = options["t_where"]
78
sampling = options["sampling"]
80
if where == "" or where == " " or where == "\n":
83
# Make sure the temporal database exists
85
# We need a database interface
86
dbif = tgis.SQLDatabaseInterfaceConnection()
89
sp = tgis.open_old_stds(input, "stvds", dbif)
90
strds_sp = tgis.open_old_stds(strds, "strds", dbif)
92
if strds_sp.get_temporal_type() != sp.get_temporal_type():
94
grass.fatal(_("Input and aggregation dataset must "
95
"have the same temporal type"))
97
# Check if intervals are present in the sample dataset
98
if sp.get_temporal_type() == "absolute":
99
map_time = sp.absolute_time.get_map_time()
101
map_time = sp.relative_time.get_map_time()
103
if map_time != "interval":
105
grass.fatal(_("All registered maps of the space time vector "
106
"dataset must have time intervals"))
108
rows = sp.get_registered_maps("name,layer,mapset,start_time,end_time",
109
tempwhere, "start_time", dbif)
113
grass.fatal(_("Space time vector dataset <%s> is empty") % sp.get_id())
115
# Sample the raster dataset with the vector dataset and run v.what.rast
117
start = row["start_time"]
118
end = row["end_time"]
119
vectmap = row["name"] + "@" + row["mapset"]
122
raster_maps = tgis.collect_map_names(
123
strds_sp, dbif, start, end, sampling)
125
aggreagated_map_name = None
129
if method != "disabled" and len(raster_maps) > 1:
130
# Generate the temporary map name
131
aggreagated_map_name = "aggreagated_map_name_" + \
133
new_map = tgis.aggregate_raster_maps(raster_maps,
134
aggreagated_map_name,
135
start, end, 0, method,
137
aggreagated_map_name = aggreagated_map_name + "_0"
140
# We overwrite the raster_maps list
141
raster_maps = (new_map.get_id(), )
143
for rastermap in raster_maps:
148
# Create a new column with the SQL compliant
149
# name of the sampled raster map
150
col_name = rastermap.split("@")[0].replace(".", "_")
152
coltype = "DOUBLE PRECISION"
154
rasterinfo = raster.raster_info(rastermap)
155
if rasterinfo["datatype"] == "CELL":
160
grass.run_command("v.db.addcolumn",
161
map=vectmap, layer=layer,
162
column="%s %s" % (col_name, coltype),
163
overwrite=grass.overwrite())
165
grass.run_command("v.db.addcolumn", map=vectmap,
166
column="%s %s" % (col_name, coltype),
167
overwrite=grass.overwrite())
168
except CalledModuleError:
170
grass.fatal(_("Unable to add column %s to vector map <%s>")
171
% (col_name, vectmap))
176
grass.run_command("v.what.rast", map=vectmap,
177
layer=layer, raster=rastermap,
178
column=col_name, where=where)
180
grass.run_command("v.what.rast", map=vectmap,
181
raster=rastermap, column=col_name,
183
except CalledModuleError:
185
grass.fatal(_("Unable to run v.what.rast for vector map "
186
"<%s> and raster map <%s>") % (vectmap,
189
if aggreagated_map_name:
191
grass.run_command("g.remove", flags='f', type='raster',
192
name=aggreagated_map_name)
193
except CalledModuleError:
195
grass.fatal(_("Unable to remove raster map <%s>")
196
% (aggreagated_map_name))
198
# Use the first map in case a column names was provided
204
if __name__ == "__main__":
205
options, flags = grass.parser()