-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
t.vect.db.select.py
executable file
·116 lines (95 loc) · 3.6 KB
/
t.vect.db.select.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
############################################################################
#
# MODULE: t.vect.db.select
# AUTHOR(S): Soeren Gebbert
#
# PURPOSE: Prints attributes of vector maps registered in a space time vector dataset.
# COPYRIGHT: (C) 2011-2014, Soeren Gebbert and 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: Prints attributes of vector maps registered in a space time vector dataset.
#% keyword: temporal
#% keyword: attribute table
#% keyword: vector
#% keyword: database
#% keyword: select
#% keyword: time
#%end
#%option G_OPT_STVDS_INPUT
#%end
#%option G_OPT_DB_COLUMNS
#%end
#%option G_OPT_F_SEP
#% label: Field separator character between the output columns
#%end
#%option G_OPT_V_FIELD
#%end
#%option G_OPT_DB_WHERE
#%end
#%option G_OPT_T_WHERE
#% key: t_where
#%end
import grass.script as grass
import grass.temporal as tgis
############################################################################
def main():
# Get the options
input = options["input"]
where = options["where"]
columns = options["columns"]
tempwhere = options["t_where"]
layer = options["layer"]
separator = grass.separator(options["separator"])
if where == "" or where == " " or where == "\n":
where = None
if columns == "" or columns == " " or columns == "\n":
columns = None
# Make sure the temporal database exists
tgis.init()
sp = tgis.open_old_stds(input, "stvds")
rows = sp.get_registered_maps("name,layer,mapset,start_time,end_time",
tempwhere, "start_time", None)
col_names = ""
if rows:
for row in rows:
vector_name = "%s@%s" % (row["name"], row["mapset"])
# In case a layer is defined in the vector dataset,
# we override the option layer
if row["layer"]:
layer = row["layer"]
select = grass.read_command("v.db.select", map=vector_name,
layer=layer, columns=columns,
separator="%s" % (separator), where=where)
if not select:
grass.fatal(_("Unable to run v.db.select for vector map <%s> "
"with layer %s") % (vector_name, layer))
# The first line are the column names
list = select.split("\n")
count = 0
for entry in list:
if entry.strip() != "":
# print the column names in case they change
if count == 0:
col_names_new = "start_time%send_time%s%s" % (
separator, separator, entry)
if col_names != col_names_new:
col_names = col_names_new
print col_names
else:
if row["end_time"]:
print "%s%s%s%s%s" % (row["start_time"], separator,
row["end_time"], separator, entry)
else:
print "%s%s%s%s" % (row["start_time"],
separator, separator, entry)
count += 1
if __name__ == "__main__":
options, flags = grass.parser()
main()