-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht.rast.sample.py
executable file
·331 lines (262 loc) · 8.77 KB
/
t.rast.sample.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#!/usr/bin/env python
# -*- coding: utf-8 -*-
############################################################################
#
# MODULE: t.rast.sample
# AUTHOR(S): Soeren Gebbert
#
# PURPOSE: Sample a space time raster dataset at specific point
# coordinates and write the output into a file or stdout
#
# COPYRIGHT: (C) 2015 by the GRASS Development Team
#
# This program is free software under the GNU General Public
# License (version 2). Read the file COPYING that comes with GRASS
# for details.
#
#############################################################################
#%module
#% description: Sample a space time raster dataset at specific vector point coordinates and write the output into a file or stdout
#% keyword: temporal
#% keyword: raster
#% keyword: sampling
#% keyword: time
#%end
#%option G_OPT_V_INPUT
#% required: no
#% key: points
#%end
#%option G_OPT_M_COORDS
#% required: no
#% description: Comma separated list of coordinates
#%end
#%option G_OPT_STRDS_INPUT
#% key: strds
#%end
#%option G_OPT_F_OUTPUT
#% required: no
#% description: Name for the output file, stdout will be used if not specified
#%end
#%option G_OPT_T_WHERE
#%end
#%option G_OPT_F_SEP
#%end
#%option
#% key: column
#% type: string
#% description: The vector point column to be used as column header
#% required: no
#% answer: cat
#%end
#%option
#% key: order
#% type: string
#% description: Sort the maps by category
#% required: no
#% multiple: yes
#% options: id, name, creator, mapset, creation_time, modification_time, start_time, end_time, north, south, west, east, min, max
#% answer: start_time
#%end
#%flag
#% key: n
#% description: Output header row
#%end
#%flag
#% key: r
#% description: Use the raster region of each map for sampling
#%end
import grass.script as gscript
import grass.temporal as tgis
import grass.pygrass.vector as pyvect
import grass.pygrass.vector.geometry as pygeom
import grass.pygrass.raster as pyrast
import grass.pygrass.gis.region as pyregion
import grass.lib.vector as libvect
import grass.lib.raster as librast
import sys
STR_RTYPE = {'CELL':librast.CELL_TYPE,
'FCELL':librast.FCELL_TYPE,
'DCELL':librast.DCELL_TYPE}
class SamplePoint(object):
def __init__(self, x, y, cat, column=None):
self.x = x
self.y = y
self.cat = cat
self.column = column
self.value = None
def __str__(self):
return str(self.x) + " " + \
str(self.y) + " " + \
str(self.cat) + " " + \
str(self.column)
def coords(self):
return (self.x, self.y)
###############################################################################
class SamplePointComparisonY(object):
"""This comparison key can be used to sort lists of SamplePoint
by Y coordinate
Example:
.. code-block:: python
# Sort the SamplePoint's in the list by Y coordinate
sorted_p_list = sorted(p_list, key=SamplePointComparisonY)
"""
def __init__(self, obj, *args):
self.obj = obj
def __lt__(self, other):
return self.obj.y < other.obj.y
def __gt__(self, other):
return self.obj.y > other.obj.y
def __eq__(self, other):
return self.obj.y == other.obj.y
def __le__(self, other):
return self.obj.y <= other.obj.y
def __ge__(self, other):
return self.obj.y >= other.obj.y
def __ne__(self, other):
return self.obj.y != other.obj.y
############################################################################
def main(options, flags):
# Get the options
points = options["points"]
strds = options["strds"]
output = options["output"]
where = options["where"]
order = options["order"]
column = options["column"]
separator = options["separator"]
coordinates = options["coordinates"]
# Setup separator
if separator == "pipe":
separator = "|"
if separator == "comma":
separator = ","
if separator == "space":
separator = " "
if separator == "tab":
separator = "\t"
if separator == "newline":
separator = "\n"
use_cats = False
write_header = flags["n"]
use_raster_region = flags["r"]
overwrite = gscript.overwrite()
if points and coordinates:
gscript.fatal(_("points and coordinates are mutually exclusive"))
if not points and not coordinates:
gscript.fatal(_("You must specify points or coordinates"))
# Make sure the temporal database exists
tgis.init()
# We need a database interface
dbif = tgis.SQLDatabaseInterfaceConnection()
dbif.connect()
sp = tgis.open_old_stds(strds, "strds", dbif)
maps = sp.get_registered_maps_as_objects(where=where, order=order,
dbif=dbif)
dbif.close()
if not maps:
gscript.fatal(_("Space time raster dataset <%s> is empty") % sp.get_id())
# The list of sample points
p_list = []
if not coordinates:
# Check if the chosen header column is in the vector map
vname = points
vmapset= ""
if "@" in points:
vname, vmapset = points.split("@")
v = pyvect.VectorTopo(vname, vmapset)
v.open("r")
col_index = 0
if v.exist() is False:
gscript.fatal(_("Vector map <%s> does not exist" %(points)))
if not v.table:
use_cats = True
gscript.warning(_("Vector map <%s> does not have an attribute table, using cats as header column." %(points)))
if v.table and column not in v.table.columns:
gscript.fatal(_("Vector map <%s> has no column named %s" %(points, column)))
if use_cats is False:
col_index = list(v.table.columns.names()).index(column)
# Create the point list
for line in v:
if line.gtype == libvect.GV_POINT:
if use_cats is False:
p = SamplePoint(line.x, line.y, line.cat, line.attrs.values()[col_index])
elif use_cats is True:
p = SamplePoint(line.x, line.y, line.cat)
p_list.append(p)
v.close()
else:
# Convert the coordinates into sample points
coord_list = coordinates.split(",")
use_cats = True
count = 0
cat = 1
while count < len(coord_list):
x = coord_list[count]
count += 1
y = coord_list[count]
count += 1
p = SamplePoint(float(x), float(y), cat)
p_list.append(p)
cat += 1
if output:
out_file = open(output, "w")
else:
out_file = sys.stdout
# Write the header
if write_header:
out_file.write("start_time")
out_file.write(separator)
out_file.write("end_time")
out_file.write(separator)
count = 0
for p in p_list:
count += 1
if use_cats is True:
out_file.write(str(p.cat))
else:
out_file.write(str(p.column))
if count != len(p_list):
out_file.write(separator)
out_file.write("\n")
# Sorting the points by y-coordinate to make use of the single row cache and read direction
sorted_p_list = sorted(p_list, key=SamplePointComparisonY)
# Sample the raster maps
num = 0
for map in maps:
num += 1
sys.stderr.write("Sample map <%s> number %i out of %i\n" %(map.get_name(), num, len(maps)))
start, end = map.get_temporal_extent_as_tuple()
out_file.write(str(start))
out_file.write(separator)
if not end:
out_file.write(str(start))
else:
out_file.write(str(end))
out_file.write(separator)
r = pyrast.RasterRow(map.get_name(), map.get_mapset())
if r.exist() is False:
gscript.fatal(_("Raster map <%s> does not exist" %(map.get_id())))
region = None
if use_raster_region is True:
r.set_region_from_rast()
region = pyregion.Region()
region.from_rast(map.get_name())
# Open the raster layer after the region settings
r.open("r")
# Sample the raster maps with the sorted points
for p in sorted_p_list:
p.value = r.get_value(point=p, region=region)
# Write the point values from the original list
count = 0
for p in p_list:
count += 1
out_file.write(str(p.value))
if count != len(p_list):
out_file.write(separator)
out_file.write("\n")
r.close()
out_file.close()
############################################################################
if __name__ == "__main__":
options, flags = gscript.parser()
main(options, flags)