t.vect.observe.strds.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. ############################################################################
  4. #
  5. # MODULE: t.vect.observe.strds
  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 raster 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. vectmap = vector_output
  102. # We create a new vector map using the categories of the original map
  103. ret = grass.run_command("v.category", input=input, layer=layers,
  104. output=vectmap, option="transfer",
  105. overwrite=overwrite)
  106. if ret != 0:
  107. grass.fatal(_("Unable to create new layers for vector map <%s>")
  108. % (vectmap))
  109. # Create the output space time vector dataset
  110. out_sp = tgis.open_new_space_time_dataset(output, "stvds",
  111. strds_sp.get_temporal_type(),
  112. title, description,
  113. strds_sp.get_semantic_type(),
  114. dbif, overwrite)
  115. dummy = out_sp.get_new_map_instance(None)
  116. # Sample the space time raster dataset with the vector
  117. # map at specific layer with v.what.rast
  118. count = 1
  119. for row in rows:
  120. start = row["start_time"]
  121. end = row["end_time"]
  122. rastmap = row["name"] + "@" + row["mapset"]
  123. if column:
  124. col_name = column
  125. else:
  126. # Create a new column with name of the
  127. # sampled space time raster dataset
  128. col_name = row["name"]
  129. coltype = "DOUBLE PRECISION"
  130. # Get raster map type
  131. rasterinfo = raster.raster_info(rastmap)
  132. if rasterinfo["datatype"] == "CELL":
  133. coltype = "INT"
  134. # Try to add a column
  135. if vector_db and count in vector_db and vector_db[count]["table"]:
  136. ret = grass.run_command("v.db.addcolumn", map=vectmap,
  137. layer=count,
  138. column="%s %s" % (col_name, coltype),
  139. overwrite=overwrite)
  140. if ret != 0:
  141. dbif.close()
  142. grass.fatal(_("Unable to add column %s to vector map <%s> "
  143. "with layer %i") % (col_name, vectmap, count))
  144. else:
  145. # Try to add a new table
  146. grass.message("Add table to layer %i" % (count))
  147. ret = grass.run_command("v.db.addtable", map=vectmap, layer=count,
  148. columns="%s %s" % (col_name, coltype),
  149. overwrite=overwrite)
  150. if ret != 0:
  151. dbif.close()
  152. grass.fatal(_("Unable to add table to vector map "
  153. "<%s> with layer %i") % (vectmap, count))
  154. # Call v.what.rast
  155. ret = grass.run_command("v.what.rast", map=vectmap,
  156. layer=count, raster=rastmap,
  157. column=col_name, where=where)
  158. if ret != 0:
  159. dbif.close()
  160. grass.fatal(_("Unable to run v.what.rast for vector map <%s> "
  161. "with layer %i and raster map <%s>") % \
  162. (vectmap, count, rastmap))
  163. vect = out_sp.get_new_map_instance(dummy.build_id(vectmap,
  164. mapset, str(count)))
  165. vect.load()
  166. if out_sp.is_time_absolute():
  167. vect.set_absolute_time(start, end)
  168. else:
  169. vect.set_relative_time(
  170. start, end, strds_sp.get_relative_time_unit())
  171. if vect.is_in_db(dbif):
  172. vect.update_all(dbif)
  173. else:
  174. vect.insert(dbif)
  175. out_sp.register_map(vect, dbif)
  176. count += 1
  177. out_sp.update_from_registered_maps(dbif)
  178. dbif.close()
  179. if __name__ == "__main__":
  180. options, flags = grass.parser()
  181. main()