t.vect.observe.strds.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. ############################################################################
  4. #
  5. # MODULE: t.vect.oberve.rast
  6. # AUTHOR(S): Soeren Gebbert
  7. #
  8. # PURPOSE: Observe specific locations in a space time raster dataset over a period of time using vector points
  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: Observes specific locations in a space time raster dataset over a period of time using vector points.
  18. #% keywords: temporal
  19. #% keywords: sampling
  20. #%end
  21. #%option G_OPT_V_INPUT
  22. #%end
  23. #%option G_OPT_STRDS_INPUT
  24. #% key: strds
  25. #%end
  26. #%option G_OPT_STVDS_OUTPUT
  27. #%end
  28. #%option G_OPT_V_OUTPUT
  29. #% key: vector_output
  30. #% description: Name of the new created vector map that stores the sampled values
  31. #%end
  32. #%option
  33. #% key: column
  34. #% type: string
  35. #% description: Name of the vector column to be created and to store sampled raster values, otherwise the space time raster name is used as column name
  36. #% required: no
  37. #% multiple: no
  38. #%end
  39. #%option G_OPT_DB_WHERE
  40. #%end
  41. #%option G_OPT_T_WHERE
  42. #% key: t_where
  43. #%end
  44. import grass.script as grass
  45. import grass.temporal as tgis
  46. import grass.script.raster as raster
  47. ############################################################################
  48. def main():
  49. # Get the options
  50. input = options["input"]
  51. output = options["output"]
  52. vector_output = options["vector_output"]
  53. strds = options["strds"]
  54. where = options["where"]
  55. column = options["column"]
  56. tempwhere = options["t_where"]
  57. if where == "" or where == " " or where == "\n":
  58. where = None
  59. overwrite = grass.overwrite()
  60. # Make sure the temporal database exists
  61. tgis.init()
  62. # We need a database interface
  63. dbif = tgis.SQLDatabaseInterfaceConnection()
  64. dbif.connect()
  65. mapset = grass.gisenv()["MAPSET"]
  66. strds_sp = tgis.open_old_space_time_dataset(strds, "strds", dbif)
  67. title = _("Observaion of space time raster dataset <%s>") % (strds_sp.get_id())
  68. description= _("Observation of space time raster dataset <%s>"
  69. " with vector map <%s>") % (strds_sp.get_id(),
  70. input)
  71. out_sp = tgis.check_new_space_time_dataset(output, "stvds", dbif,
  72. overwrite)
  73. # Select the raster maps
  74. rows = strds_sp.get_registered_maps(
  75. "name,mapset,start_time,end_time", tempwhere, "start_time", dbif)
  76. if not rows:
  77. dbif.close()
  78. grass.fatal(_("Space time vector dataset <%s> is empty") %
  79. out_sp.get_id())
  80. num_rows = len(rows)
  81. # Get the layer and database connections of the input vector
  82. vector_db = grass.vector.vector_db(input)
  83. # We copy the vector table and create the new layers
  84. if vector_db:
  85. # Use the first layer to copy the categories from
  86. layers = "1,"
  87. else:
  88. layers = ""
  89. first = True
  90. for layer in range(num_rows):
  91. layer += 1
  92. # Skip existing layer
  93. if vector_db and layer in vector_db and \
  94. vector_db[layer]["layer"] == layer:
  95. continue
  96. if first:
  97. layers += "%i" % (layer)
  98. first = False
  99. else:
  100. layers += ",%i" % (layer)
  101. # Use the name of the space time vector dataset as new vector name
  102. vectmap = vector_output
  103. # We create a new vector map using the categories of the original map
  104. ret = grass.run_command("v.category", input=input, layer=layers,
  105. output=vectmap, option="transfer",
  106. overwrite=overwrite)
  107. if ret != 0:
  108. grass.fatal(_("Unable to create new layers for vector map <%s>")
  109. % (vectmap))
  110. # Create the output space time vector dataset
  111. out_sp = tgis.open_new_space_time_dataset(output, "stvds",
  112. strds_sp.get_temporal_type(),
  113. title, description,
  114. strds_sp.get_semantic_type(),
  115. dbif, overwrite)
  116. dummy = out_sp.get_new_map_instance(None)
  117. # Sample the space time raster dataset with the vector
  118. # map at specific layer with v.what.rast
  119. count = 1
  120. for row in rows:
  121. start = row["start_time"]
  122. end = row["end_time"]
  123. rastmap = row["name"] + "@" + row["mapset"]
  124. if column:
  125. col_name = column
  126. else:
  127. # Create a new column with name of the
  128. # sampled space time raster dataset
  129. col_name = row["name"]
  130. coltype = "DOUBLE PRECISION"
  131. # Get raster map type
  132. rasterinfo = raster.raster_info(rastmap)
  133. if rasterinfo["datatype"] == "CELL":
  134. coltype = "INT"
  135. # Try to add a column
  136. if vector_db and count in vector_db and vector_db[count]["table"]:
  137. ret = grass.run_command("v.db.addcolumn", map=vectmap,
  138. layer=count,
  139. column="%s %s" % (col_name, coltype),
  140. overwrite=overwrite)
  141. if ret != 0:
  142. dbif.close()
  143. grass.fatal(_("Unable to add column %s to vector map <%s> "
  144. "with layer %i") % (col_name, vectmap, count))
  145. else:
  146. # Try to add a new table
  147. grass.message("Add table to layer %i" % (count))
  148. ret = grass.run_command("v.db.addtable", map=vectmap, layer=count,
  149. columns="%s %s" % (col_name, coltype),
  150. overwrite=overwrite)
  151. if ret != 0:
  152. dbif.close()
  153. grass.fatal(_("Unable to add table to vector map "
  154. "<%s> with layer %i") % (vectmap, count))
  155. # Call v.what.rast
  156. ret = grass.run_command("v.what.rast", map=vectmap,
  157. layer=count, raster=rastmap,
  158. column=col_name, where=where)
  159. if ret != 0:
  160. dbif.close()
  161. grass.fatal(_("Unable to run v.what.rast for vector map <%s> "
  162. "with layer %i and raster map <%s>") % \
  163. (vectmap, count, rastmap))
  164. vect = out_sp.get_new_map_instance(dummy.build_id(vectmap,
  165. mapset, str(count)))
  166. vect.load()
  167. if out_sp.is_time_absolute():
  168. vect.set_absolute_time(start, end)
  169. else:
  170. vect.set_relative_time(
  171. start, end, strds_sp.get_relative_time_unit())
  172. if vect.is_in_db(dbif):
  173. vect.update_all(dbif)
  174. else:
  175. vect.insert(dbif)
  176. out_sp.register_map(vect, dbif)
  177. count += 1
  178. out_sp.update_from_registered_maps(dbif)
  179. dbif.close()
  180. if __name__ == "__main__":
  181. options, flags = grass.parser()
  182. main()