t.vect.db.select.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. ############################################################################
  4. #
  5. # MODULE: t.vect.db.select
  6. # AUTHOR(S): Soeren Gebbert
  7. #
  8. # PURPOSE: Prints attributes of vector maps registered in a space time vector dataset.
  9. # COPYRIGHT: (C) 2011 by the GRASS Development Team
  10. #
  11. # This program is free software under the GNU General Public
  12. # License (version 2). Read the file COPYING that comes with GRASS
  13. # for details.
  14. #
  15. #############################################################################
  16. #%module
  17. #% description: Prints attributes of vector maps registered in a space time vector dataset.
  18. #% keywords: temporal
  19. #% keywords: attribute table
  20. #% keywords: vector
  21. #% keywords: database
  22. #% keywords: select
  23. #%end
  24. #%option G_OPT_STVDS_INPUT
  25. #%end
  26. #%option G_OPT_DB_COLUMNS
  27. #%end
  28. #%option G_OPT_F_SEP
  29. #% description: Separator character between the output columns
  30. #%end
  31. #%option G_OPT_V_FIELD
  32. #%end
  33. #%option G_OPT_DB_WHERE
  34. #%end
  35. #%option G_OPT_T_WHERE
  36. #% key: t_where
  37. #%end
  38. import grass.script as grass
  39. import grass.temporal as tgis
  40. ############################################################################
  41. def main():
  42. # Get the options
  43. input = options["input"]
  44. where = options["where"]
  45. columns = options["columns"]
  46. tempwhere = options["t_where"]
  47. layer = options["layer"]
  48. fs = options["separator"]
  49. if where == "" or where == " " or where == "\n":
  50. where = None
  51. if columns == "" or columns == " " or columns == "\n":
  52. columns = None
  53. # Make sure the temporal database exists
  54. tgis.init()
  55. sp = tgis.open_old_space_time_dataset(input, "stvds")
  56. rows = sp.get_registered_maps("name,layer,mapset,start_time,end_time",
  57. tempwhere, "start_time", None)
  58. col_names = ""
  59. if rows:
  60. for row in rows:
  61. vector_name = "%s@%s" % (row["name"], row["mapset"])
  62. # In case a layer is defined in the vector dataset,
  63. # we override the option layer
  64. if row["layer"]:
  65. layer = row["layer"]
  66. select = grass.read_command("v.db.select", map=vector_name,
  67. layer=layer, columns=columns,
  68. separator="%s" % (fs), where=where)
  69. if not select:
  70. grass.fatal(_("Unable to run v.db.select for vector map <%s> "
  71. "with layer %s") % (vector_name, layer))
  72. # The first line are the column names
  73. list = select.split("\n")
  74. count = 0
  75. for entry in list:
  76. if entry.strip() != "":
  77. # print the column names in case they change
  78. if count == 0:
  79. col_names_new = "start_time%send_time%s%s" % (
  80. fs, fs, entry)
  81. if col_names != col_names_new:
  82. col_names = col_names_new
  83. print col_names
  84. else:
  85. if row["end_time"]:
  86. print "%s%s%s%s%s" % (row["start_time"], fs,
  87. row["end_time"], fs, entry)
  88. else:
  89. print "%s%s%s%s" % (row["start_time"],
  90. fs, fs, entry)
  91. count += 1
  92. if __name__ == "__main__":
  93. options, flags = grass.parser()
  94. main()