t.vect.observe.strds.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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.SQLDatabaseInterfaceConnection()
  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.SpaceTimeRasterDataset(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.SpaceTimeVectorDataset(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, "
  82. "use the overwrite flag to overwrite") % (id))
  83. # Overwrite existing stvds
  84. if out_sp.is_in_db(dbif) == True and grass.overwrite() == True:
  85. out_sp.select(dbif)
  86. out_sp.delete(dbif)
  87. out_sp = tgis.SpaceTimeVectorDataset(id)
  88. # Select the raster maps
  89. rows = strds_sp.get_registered_maps(
  90. "name,mapset,start_time,end_time", tempwhere, "start_time", dbif)
  91. if not rows:
  92. dbif.close()
  93. grass.fatal(_("Space time vector dataset <%s> is empty") %
  94. out_sp.get_id())
  95. num_rows = len(rows)
  96. # Get the layer and database connections of the input vector
  97. vector_db = grass.vector.vector_db(input)
  98. # We copy the vector table and create the new layers
  99. if vector_db:
  100. # Use the first layer to copy the categories from
  101. layers = "1,"
  102. else:
  103. layers = ""
  104. first = True
  105. for layer in range(num_rows):
  106. layer += 1
  107. # Skip existing layer
  108. if vector_db and layer in vector_db and \
  109. vector_db[layer]["layer"] == layer:
  110. continue
  111. if first:
  112. layers += "%i" % (layer)
  113. first = False
  114. else:
  115. layers += ",%i" % (layer)
  116. # Use the name of the space time vector dataset as new vector name
  117. vectmap = vector_output
  118. # We create a new vector map using the categories of the original map
  119. ret = grass.run_command("v.category", input=input, layer=layers,
  120. output=vectmap, option="transfer",
  121. overwrite=grass.overwrite())
  122. if ret != 0:
  123. grass.fatal(_("Unable to create new layers for vector map <%s>")
  124. % (vectmap))
  125. # Create the output space time vector dataset
  126. out_sp.set_initial_values(strds_sp.get_temporal_type(),
  127. strds_sp.get_semantic_type(),
  128. _("Observaion of space time raster "
  129. "dataset <%s>") % (strds_id),
  130. _("Observation of space time raster dataset <%s>"
  131. " with vector map <%s>") % (strds_id, input))
  132. out_sp.insert(dbif)
  133. dummy = out_sp.get_new_map_instance(None)
  134. # Sample the space time raster dataset with the vector
  135. # map at specific layer with v.what.rast
  136. count = 1
  137. for row in rows:
  138. start = row["start_time"]
  139. end = row["end_time"]
  140. rastmap = row["name"] + "@" + row["mapset"]
  141. if column:
  142. col_name = column
  143. else:
  144. # Create a new column with name of the
  145. # sampled space time raster dataset
  146. col_name = row["name"]
  147. coltype = "DOUBLE PRECISION"
  148. # Get raster map type
  149. rasterinfo = raster.raster_info(rastmap)
  150. if rasterinfo["datatype"] == "CELL":
  151. coltype = "INT"
  152. # Try to add a column
  153. if vector_db and count in vector_db and vector_db[count]["table"]:
  154. ret = grass.run_command("v.db.addcolumn", map=vectmap,
  155. layer=count,
  156. column="%s %s" % (col_name, coltype),
  157. overwrite=grass.overwrite())
  158. if ret != 0:
  159. dbif.close()
  160. grass.fatal(_("Unable to add column %s to vector map <%s> "
  161. "with layer %i") % (col_name, vectmap, count))
  162. else:
  163. # Try to add a new table
  164. grass.message("Add table to layer %i" % (count))
  165. ret = grass.run_command("v.db.addtable", map=vectmap, layer=count,
  166. columns="%s %s" % (col_name, coltype),
  167. overwrite=grass.overwrite())
  168. if ret != 0:
  169. dbif.close()
  170. grass.fatal(_("Unable to add table to vector map "
  171. "<%s> with layer %i") % (vectmap, count))
  172. # Call v.what.rast
  173. ret = grass.run_command("v.what.rast", map=vectmap,
  174. layer=count, raster=rastmap,
  175. column=col_name, where=where)
  176. if ret != 0:
  177. dbif.close()
  178. grass.fatal(_("Unable to run v.what.rast for vector map <%s> "
  179. "with layer %i and raster map <%s>") % \
  180. (vectmap, count, rastmap))
  181. vect = out_sp.get_new_map_instance(dummy.build_id(vectmap,
  182. mapset, str(count)))
  183. vect.load()
  184. if out_sp.is_time_absolute():
  185. vect.set_absolute_time(start, end)
  186. else:
  187. vect.set_relative_time(
  188. start, end, strds_sp.get_relative_time_unit())
  189. if vect.is_in_db(dbif):
  190. vect.update_all(dbif)
  191. else:
  192. vect.insert(dbif)
  193. out_sp.register_map(vect, dbif)
  194. count += 1
  195. out_sp.update_from_registered_maps(dbif)
  196. dbif.close()
  197. if __name__ == "__main__":
  198. options, flags = grass.parser()
  199. main()