t.vect.observe.strds.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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 periode 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 periode 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. # Make sure the temporal database exists
  60. tgis.create_temporal_database()
  61. # We need a database interface
  62. dbif = tgis.sql_database_interface_connection()
  63. dbif.connect()
  64. mapset = grass.gisenv()["MAPSET"]
  65. if strds.find("@") >= 0:
  66. strds_id = strds
  67. else:
  68. strds_id = strds + "@" + mapset
  69. strds_sp = tgis.space_time_raster_dataset(strds_id)
  70. if strds_sp.is_in_db() == False:
  71. dbif.close()
  72. grass.fatal(_("Space time raster dataset <%s> not found") % (strds_id))
  73. strds_sp.select(dbif)
  74. if output.find("@") >= 0:
  75. id = output
  76. else:
  77. id = output + "@" + mapset
  78. out_sp = tgis.space_time_vector_dataset(id)
  79. if out_sp.is_in_db(dbif) == True and grass.overwrite() == False:
  80. dbif.close()
  81. grass.fatal(_("Dataset <%s> found in temporal database, use the overwrite flag to overwrite") % (id))
  82. # Overwrite existing stvds
  83. if out_sp.is_in_db(dbif) == True and grass.overwrite() == True:
  84. out_sp.select(dbif)
  85. out_sp.delete(dbif)
  86. out_sp = tgis.space_time_vector_dataset(id)
  87. # Select the raster maps
  88. rows = strds_sp.get_registered_maps("name,mapset,start_time,end_time", tempwhere, "start_time", dbif)
  89. if not rows:
  90. dbif.close()
  91. grass.fatal(_("Space time vector dataset <%s> is empty") % out_sp.get_id())
  92. num_rows = len(rows)
  93. # Get the layer and database connections of the input vector
  94. vector_db = grass.vector.vector_db(input)
  95. # We copy the vector table and create the new layers
  96. if vector_db:
  97. # Use the first layer to copy the categories from
  98. layers= "1,"
  99. else:
  100. layers= ""
  101. first = True
  102. for layer in range(num_rows):
  103. layer += 1
  104. # Skip existing layer
  105. if vector_db and vector_db.has_key(layer) and vector_db[layer]["layer"] == layer:
  106. continue
  107. if first:
  108. layers += "%i"%(layer)
  109. first = False
  110. else:
  111. layers += ",%i"%(layer)
  112. # Use the name of the space time vector dataset as new vector name
  113. vectmap = vector_output
  114. # We create a new vector map using the categories of the original map
  115. ret = grass.run_command("v.category", input=input, layer=layers, output=vectmap, option="transfer", overwrite=grass.overwrite())
  116. if ret != 0:
  117. grass.fatal(_("Unable to create new layers for vector map <%s>") % (vectmap))
  118. # Create the output space time vector dataset
  119. out_sp.set_initial_values(strds_sp.get_temporal_type(), \
  120. strds_sp.get_semantic_type(),\
  121. _("Observaion of space time raster dataset <%s>")%(strds_id),\
  122. _("Observation of space time raster dataset <%s> with vector map <%s>")%(strds_id, input))
  123. out_sp.insert(dbif)
  124. dummy = out_sp.get_new_map_instance(None)
  125. # Sample the space time raster dataset with the vector map at specific layer with v.what.rast
  126. count = 1
  127. for row in rows:
  128. start = row["start_time"]
  129. end = row["end_time"]
  130. rastmap = row["name"] + "@" + row["mapset"]
  131. if column:
  132. col_name = column
  133. else:
  134. # Create a new column with name of the sampled space time raster dataset
  135. col_name = row["name"]
  136. coltype = "DOUBLE PRECISION"
  137. # Get raster map type
  138. rasterinfo = raster.raster_info(rastmap)
  139. if rasterinfo["datatype"] == "CELL":
  140. coltype = "INT"
  141. # Try to add a column
  142. if vector_db and vector_db.has_key(count) and vector_db[count]["table"]:
  143. ret = grass.run_command("v.db.addcolumn", map=vectmap, layer=count, column="%s %s" % (col_name, coltype), overwrite=grass.overwrite())
  144. if ret != 0:
  145. dbif.close()
  146. grass.fatal(_("Unable to add column %s to vector map <%s> with layer %i")%(col_name, vectmap, count))
  147. else:
  148. # Try to add a new table
  149. grass.message("Add table to layer %i"%(count))
  150. ret = grass.run_command("v.db.addtable", map=vectmap, layer=count, columns="%s %s" % (col_name, coltype), overwrite=grass.overwrite())
  151. if ret != 0:
  152. dbif.close()
  153. grass.fatal(_("Unable to add table to vector map <%s> with layer %i")%(vectmap, count))
  154. # Call v.what.rast
  155. ret = grass.run_command("v.what.rast", map=vectmap, layer=count, raster=rastmap, column=col_name, where=where)
  156. if ret != 0:
  157. dbif.close()
  158. grass.fatal(_("Unable to run v.what.rast for vector map <%s> with layer %i and raster map <%s>")%(vectmap, count, rastmap))
  159. vect = out_sp.get_new_map_instance(dummy.build_id(vectmap, mapset, str(count)))
  160. vect.load()
  161. if out_sp.is_time_absolute():
  162. vect.set_absolute_time(start, end)
  163. else:
  164. vect.set_relative_time(start, end, strds_sp.get_relative_time_unit())
  165. if vect.is_in_db(dbif):
  166. vect.update_all(dbif)
  167. else:
  168. vect.insert(dbif)
  169. out_sp.register_map(vect, dbif)
  170. count += 1
  171. out_sp.update_from_registered_maps(dbif)
  172. dbif.close()
  173. if __name__ == "__main__":
  174. options, flags = grass.parser()
  175. main()