forked from OSGeo/grass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht.vect.what.strds.py
executable file
·251 lines (216 loc) · 8.12 KB
/
t.vect.what.strds.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/env python3
############################################################################
#
# MODULE: t.vect.what.strds
# AUTHOR(S): Soeren Gebbert
#
# PURPOSE: Store raster map values at spatial and temporal positions of vector points as vector attributes.
# COPYRIGHT: (C) 2011-2017 by the GRASS Development Team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
#############################################################################
# %module
# % description: Stores raster map values at spatial and temporal positions of vector points as vector attributes.
# % keyword: temporal
# % keyword: sampling
# % keyword: vector
# % keyword: time
# %end
# %option G_OPT_STVDS_INPUT
# %end
# %option G_OPT_STRDS_INPUT
# % key: strds
# %end
# %option
# % key: column
# % type: string
# % label: Name of the vector column to be created and to store sampled raster values
# % 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
# % required: no
# % multiple: no
# %end
# %option
# % key: method
# % type: string
# % description: Aggregate operation to be performed on the raster maps
# % required: yes
# % multiple: no
# % 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
# % answer: disabled
# %end
# %option G_OPT_DB_WHERE
# %end
# %option G_OPT_T_WHERE
# % key: t_where
# %end
# %option G_OPT_T_SAMPLE
# %end
import os
import grass.script as grass
import grass.script.raster as raster
from grass.exceptions import CalledModuleError
############################################################################
def main():
# lazy imports
import grass.temporal as tgis
# Get the options
input = options["input"]
strds = options["strds"]
where = options["where"]
column = options["column"]
method = options["method"]
tempwhere = options["t_where"]
sampling = options["sampling"]
if where == "" or where == " " or where == "\n":
where = None
# Make sure the temporal database exists
tgis.init()
# We need a database interface
dbif = tgis.SQLDatabaseInterfaceConnection()
dbif.connect()
sp = tgis.open_old_stds(input, "stvds", dbif)
strds_sp = tgis.open_old_stds(strds, "strds", dbif)
if strds_sp.get_temporal_type() != sp.get_temporal_type():
dbif.close()
grass.fatal(
_("Input and aggregation dataset must " "have the same temporal type")
)
# Check if intervals are present in the sample dataset
if sp.get_temporal_type() == "absolute":
map_time = sp.absolute_time.get_map_time()
else:
map_time = sp.relative_time.get_map_time()
if map_time != "interval":
dbif.close()
grass.fatal(
_(
"All registered maps of the space time vector "
"dataset must have time intervals"
)
)
rows = sp.get_registered_maps(
"name,layer,mapset,start_time,end_time", tempwhere, "start_time", dbif
)
if not rows:
dbif.close()
grass.fatal(_("Space time vector dataset <%s> is empty") % sp.get_id())
# Sample the raster dataset with the vector dataset and run v.what.rast
for row in rows:
start = row["start_time"]
end = row["end_time"]
vectmap = row["name"] + "@" + row["mapset"]
layer = row["layer"]
raster_maps = tgis.collect_map_names(strds_sp, dbif, start, end, sampling)
aggreagated_map_name = None
if raster_maps:
# Aggregation
if method != "disabled" and len(raster_maps) > 1:
# Generate the temporary map name
aggreagated_map_name = "aggreagated_map_name_" + str(os.getpid())
new_map = tgis.aggregate_raster_maps(
raster_maps,
aggreagated_map_name,
start,
end,
0,
method,
False,
dbif,
)
aggreagated_map_name = aggreagated_map_name + "_0"
if new_map is None:
continue
# We overwrite the raster_maps list
raster_maps = (new_map.get_id(),)
for rastermap in raster_maps:
if column:
col_name = column
else:
# Create a new column with the SQL compliant
# name of the sampled raster map
col_name = rastermap.split("@")[0].replace(".", "_")
coltype = "DOUBLE PRECISION"
# Get raster type
rasterinfo = raster.raster_info(rastermap)
if rasterinfo["datatype"] == "CELL":
coltype = "INT"
try:
if layer:
grass.run_command(
"v.db.addcolumn",
map=vectmap,
layer=layer,
column="%s %s" % (col_name, coltype),
overwrite=grass.overwrite(),
)
else:
grass.run_command(
"v.db.addcolumn",
map=vectmap,
column="%s %s" % (col_name, coltype),
overwrite=grass.overwrite(),
)
except CalledModuleError:
dbif.close()
grass.fatal(
_("Unable to add column %s to vector map <%s>")
% (col_name, vectmap)
)
# Call v.what.rast
try:
if layer:
grass.run_command(
"v.what.rast",
map=vectmap,
layer=layer,
raster=rastermap,
column=col_name,
where=where,
)
else:
grass.run_command(
"v.what.rast",
map=vectmap,
raster=rastermap,
column=col_name,
where=where,
)
except CalledModuleError:
dbif.close()
grass.fatal(
_(
"Unable to run v.what.rast for vector map "
"<%s> and raster map <%s>"
)
% (vectmap, rastermap)
)
if aggreagated_map_name:
try:
grass.run_command(
"g.remove",
flags="f",
type="raster",
name=aggreagated_map_name,
)
except CalledModuleError:
dbif.close()
grass.fatal(
_("Unable to remove raster map <%s>")
% (aggreagated_map_name)
)
# Use the first map in case a column names was provided
if column:
break
dbif.close()
if __name__ == "__main__":
options, flags = grass.parser()
main()