t.vect.observe.strds.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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
  27. #% key: column
  28. #% type: string
  29. #% 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
  30. #% required: no
  31. #% multiple: no
  32. #%end
  33. #%option
  34. #% key: output
  35. #% type: string
  36. #% description: Name the new created space time vector dataset and the new vector map
  37. #% required: yes
  38. #% multiple: no
  39. #%end
  40. #%option G_OPT_DB_WHERE
  41. #%end
  42. #%option G_OPT_T_WHERE
  43. #% key: t_where
  44. #%end
  45. import grass.script as grass
  46. import grass.temporal as tgis
  47. import grass.script.raster as raster
  48. ############################################################################
  49. def main():
  50. # Get the options
  51. input = options["input"]
  52. output = options["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. # Create the output space time vector dataset
  93. out_sp.set_initial_values(strds_sp.get_temporal_type(), \
  94. strds_sp.get_semantic_type(),\
  95. _("Observaion of space time raster dataset <%s>")%(strds_id),\
  96. _("Observation of space time raster dataset <%s> with vector map <%s>")%(strds_id, input))
  97. out_sp.insert(dbif)
  98. num_rows = len(rows)
  99. # We copy the vector table and create the new layers
  100. layers= ""
  101. first = True
  102. for layer in range(num_rows):
  103. layer += 1
  104. if first:
  105. layers += "%i"%(layer)
  106. first = False
  107. else:
  108. layers += ",%i"%(layer)
  109. print layers
  110. vectmap = output
  111. ret = grass.run_command("v.category", input=input, layer=layers, output=vectmap, option="transfer", overwrite=grass.overwrite())
  112. if ret != 0:
  113. grass.fatal(_("Unable to create new layers for vector map <%s>") % (vectmap))
  114. dummy = out_sp.get_new_map_instance(None)
  115. # Sample the space time raster dataset with the vector map at specific layer with v.what.rast
  116. count = 1
  117. for row in rows:
  118. start = row["start_time"]
  119. end = row["end_time"]
  120. rastmap = row["name"] + "@" + row["mapset"]
  121. if column:
  122. col_name = column
  123. else:
  124. # Create a new column with name of the sampled space time raster dataset
  125. col_name = row["name"]
  126. coltype = "DOUBLE PRECISION"
  127. # Get raster map type
  128. rasterinfo = raster.raster_info(rastmap)
  129. if rasterinfo["datatype"] == "CELL":
  130. coltype = "INT"
  131. # Try to add a column
  132. ret = grass.run_command("v.db.addcolumn", map=vectmap, layer=count, column="%s %s" % (col_name, coltype), overwrite=grass.overwrite())
  133. if ret != 0:
  134. # Try to add a new table
  135. grass.message("Add table to layer %i"%(count))
  136. ret = grass.run_command("v.db.addtable", map=vectmap, layer=count, columns="%s %s" % (col_name, coltype), overwrite=grass.overwrite())
  137. if ret != 0:
  138. dbif.close()
  139. grass.fatal(_("Unable to add column %s to vector map <%s> with layer %i")%(col_name, vectmap, count))
  140. # Call v.what.rast
  141. ret = grass.run_command("v.what.rast", map=vectmap, layer=count, raster=rastmap, column=col_name, where=where)
  142. if ret != 0:
  143. dbif.close()
  144. grass.fatal(_("Unable to run v.what.rast for vector map <%s> with layer %i and raster map <%s>")%(vectmap, count, rastmap))
  145. vect = out_sp.get_new_map_instance(dummy.build_id(vectmap, mapset, str(count)))
  146. vect.load()
  147. if out_sp.is_time_absolute():
  148. vect.set_absolute_time(start, end)
  149. else:
  150. vect.set_relative_time(start, end, strds_ds.get_relative_time_unit())
  151. if vect.is_in_db(dbif):
  152. vect.update_all(dbif)
  153. else:
  154. vect.insert(dbif)
  155. out_sp.register_map(vect, dbif)
  156. count += 1
  157. out_sp.update_from_registered_maps(dbif)
  158. dbif.close()
  159. if __name__ == "__main__":
  160. options, flags = grass.parser()
  161. main()