t.vect.observe.strds.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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: Observe 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_STVDS_INPUT
  22. #%end
  23. #%option
  24. #% key: strds
  25. #% type: string
  26. #% description: The space time raster dataset to use
  27. #% required: yes
  28. #% multiple: no
  29. #%end
  30. #%option
  31. #% key: column
  32. #% type: string
  33. #% 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
  34. #% required: no
  35. #% multiple: no
  36. #%end
  37. #%option
  38. #% key: output
  39. #% type: string
  40. #% description: Name the new created space time vector dataset and the new vector map
  41. #% required: yes
  42. #% multiple: no
  43. #%end
  44. #%option G_OPT_DB_WHERE
  45. #%end
  46. #%option G_OPT_T_WHERE
  47. #% key: t_where
  48. #%end
  49. import grass.script as grass
  50. import grass.temporal as tgis
  51. import grass.script.raster as raster
  52. ############################################################################
  53. def main():
  54. # Get the options
  55. input = options["input"]
  56. output = options["output"]
  57. strds = options["strds"]
  58. where = options["where"]
  59. column = options["column"]
  60. tempwhere = options["t_where"]
  61. if where == "" or where == " " or where == "\n":
  62. where = None
  63. # Make sure the temporal database exists
  64. tgis.create_temporal_database()
  65. # We need a database interface
  66. dbif = tgis.sql_database_interface_connection()
  67. dbif.connect()
  68. mapset = grass.gisenv()["MAPSET"]
  69. if strds.find("@") >= 0:
  70. strds_id = strds
  71. else:
  72. strds_id = strds + "@" + mapset
  73. strds_sp = tgis.space_time_raster_dataset(strds_id)
  74. if strds_sp.is_in_db() == False:
  75. dbif.close()
  76. grass.fatal(_("Space time %s dataset <%s> not found") % (sp.get_new_map_instance(None).get_type(), id))
  77. strds_sp.select(dbif)
  78. if output.find("@") >= 0:
  79. id = output
  80. else:
  81. id = output + "@" + mapset
  82. out_sp = tgis.space_time_vector_dataset(id)
  83. if out_sp.is_in_db(dbif) == True and grass.overwrite() == False:
  84. dbif.close()
  85. grass.fatal(_("Dataset <%s> found in temporal database, use the overwrite flag to overwrite") % (id))
  86. # Overwrite existing stvds
  87. if out_sp.is_in_db(dbif) == True and grass.overwrite() == True:
  88. out_sp.select(dbif)
  89. out_sp.delete(dbif)
  90. out_sp = tgis.space_time_vector_dataset(id)
  91. # Select the raster maps
  92. rows = strds_sp.get_registered_maps("name,mapset,start_time,end_time", tempwhere, "start_time", dbif)
  93. if not rows:
  94. dbif.close()
  95. grass.fatal(_("Space time vector dataset <%s> is empty") % sg.get_id())
  96. # Create the output space time vector dataset
  97. out_sp.set_initial_values(strds_sp.get_temporal_type(), \
  98. strds_sp.get_semantic_type(),\
  99. _("Observaion of space time raster dataset <%s>")%(strds_id),\
  100. _("Observation of space time raster dataset <%s> with vector map <%s>")%(strds_id, input))
  101. out_sp.insert(dbif)
  102. num_rows = len(rows)
  103. # We copy the vector table and create the new layers
  104. layers= ""
  105. first = True
  106. for layer in range(num_rows):
  107. layer += 1
  108. if first:
  109. layers += "%i"%(layer)
  110. first = False
  111. else:
  112. layers += ",%i"%(layer)
  113. print layers
  114. vectmap = output
  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. dummy = out_sp.get_new_map_instance(None)
  119. # Sample the space time raster dataset with the vector map at specific layer with v.what.rast
  120. count = 1
  121. for row in rows:
  122. start = row["start_time"]
  123. end = row["end_time"]
  124. rastmap = row["name"] + "@" + row["mapset"]
  125. if column:
  126. col_name = column
  127. else:
  128. # Create a new column with name of the 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. ret = grass.run_command("v.db.addcolumn", map=vectmap, layer=count, column="%s %s" % (col_name, coltype), overwrite=grass.overwrite())
  137. if ret != 0:
  138. # Try to add a new table
  139. grass.message("Add table to layer %i"%(count))
  140. ret = grass.run_command("v.db.addtable", map=vectmap, layer=count, columns="%s %s" % (col_name, coltype), overwrite=grass.overwrite())
  141. if ret != 0:
  142. dbif.close()
  143. grass.fatal(_("Unable to add column %s to vector map <%s> with layer %i")%(col_name, vectmap, count))
  144. # Call v.what.rast
  145. ret = grass.run_command("v.what.rast", map=vectmap, layer=count, raster=rastmap, column=col_name, where=where)
  146. if ret != 0:
  147. dbif.close()
  148. grass.fatal(_("Unable to run v.what.rast for vector map <%s> with layer %i and raster map <%s>")%(vectmap, count, rastmap))
  149. vect = out_sp.get_new_map_instance(dummy.build_id(vectmap, mapset, str(count)))
  150. vect.load()
  151. if out_sp.is_time_absolute():
  152. vect.set_absolute_time(start, end)
  153. else:
  154. vect.set_relative_time(start, end, strds_ds.get_relative_time_unit())
  155. if vect.is_in_db(dbif):
  156. vect.update(dbif)
  157. else:
  158. vect.insert(dbif)
  159. out_sp.register_map(vect, dbif)
  160. count += 1
  161. out_sp.update_from_registered_maps(dbif)
  162. dbif.close()
  163. if __name__ == "__main__":
  164. options, flags = grass.parser()
  165. main()