t.vect.what.strds.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. ############################################################################
  4. #
  5. # MODULE: t.vect.what.strds
  6. # AUTHOR(S): Soeren Gebbert
  7. #
  8. # PURPOSE: Uploads raster map values at spatial and temporal positions of vector points to the tables. The maps are registered in space time datasets
  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: Uploads raster map values at spatial and temporal positions of vector points to the tables. The maps are registered in space time datasets.
  18. #% keywords: temporal
  19. #% keywords: sampling
  20. #%end
  21. #%option G_OPT_STVDS_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. The use of a column name forces t.vect.what.rast to sample only values from the first map found in an interval. Otherwise the raster map names are used as column names
  30. #% required: no
  31. #% multiple: no
  32. #%end
  33. #%option G_OPT_DB_WHERE
  34. #%end
  35. #%option G_OPT_T_WHERE
  36. #% key: t_where
  37. #%end
  38. #%option G_OPT_T_SAMPLE
  39. #%end
  40. import grass.script as grass
  41. import grass.temporal as tgis
  42. import grass.script.raster as raster
  43. ############################################################################
  44. def main():
  45. # Get the options
  46. input = options["input"]
  47. strds = options["strds"]
  48. where = options["where"]
  49. column = options["column"]
  50. tempwhere = options["t_where"]
  51. sampling = options["sampling"]
  52. if where == "" or where == " " or where == "\n":
  53. where = None
  54. # Make sure the temporal database exists
  55. tgis.create_temporal_database()
  56. # We need a database interface
  57. dbif = tgis.sql_database_interface_connection()
  58. dbif.connect()
  59. mapset = grass.gisenv()["MAPSET"]
  60. if input.find("@") >= 0:
  61. id = input
  62. else:
  63. id = input + "@" + mapset
  64. sp = tgis.space_time_vector_dataset(id)
  65. if sp.is_in_db() == False:
  66. dbif.close()
  67. grass.fatal(_("Space time %s dataset <%s> not found") % (sp.get_new_map_instance(None).get_type(), id))
  68. sp.select(dbif)
  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(_("Dataset <%s> not found in temporal database") % (id))
  77. strds_sp.select(dbif)
  78. if strds_sp.get_temporal_type() != sp.get_temporal_type():
  79. dbif.close()
  80. grass.fatal(_("Input and aggregation dataset must have the same temporal type"))
  81. # Check if intervals are present
  82. if sp.get_temporal_type() == "absolute":
  83. map_time = strds_sp.absolute_time.get_map_time()
  84. else:
  85. map_time = strds_sp.relative_time.get_map_time()
  86. if map_time != "interval":
  87. dbif.close()
  88. grass.fatal(_("All registered maps of the space time vector dataset must have time intervals"))
  89. rows = sp.get_registered_maps("name,layer,mapset,start_time,end_time", tempwhere, "start_time", dbif)
  90. dummy = tgis.vector_dataset(None)
  91. if not rows:
  92. dbif.close()
  93. grass.fatal(_("Space time vector dataset <%s> is empty") % sp.get_id())
  94. # Sample the raster dataset with the vector dataset and run v.what.rast
  95. for row in rows:
  96. start = row["start_time"]
  97. end = row["end_time"]
  98. vectmap = row["name"] + "@" + row["mapset"]
  99. layer = row["layer"]
  100. raster_maps = tgis.collect_map_names(strds_sp, dbif, start, end, sampling)
  101. if raster_maps:
  102. # Collect the names of the raster maps
  103. for rastermap in raster_maps:
  104. if column:
  105. col_name = column
  106. else:
  107. # Create a new column with the SQL compliant name of the sampled raster map
  108. col_name = rastermap.split("@")[0].replace(".", "_")
  109. coltype = "DOUBLE PRECISION"
  110. # Get raster type
  111. rasterinfo = raster.raster_info(rastermap)
  112. if rasterinfo["datatype"] == "CELL":
  113. coltype = "INT"
  114. if layer:
  115. ret = grass.run_command("v.db.addcolumn", map=vectmap, layer=layer, column="%s %s" % (col_name, coltype), overwrite=grass.overwrite())
  116. else:
  117. ret = grass.run_command("v.db.addcolumn", map=vectmap, column="%s %s" % (col_name, coltype), overwrite=grass.overwrite())
  118. if ret != 0:
  119. dbif.close()
  120. grass.fatal(_("Unable to add column %s to vector map <%s>")%(col_name, vectmap))
  121. # Call v.what.rast
  122. if layer:
  123. ret = grass.run_command("v.what.rast", map=vectmap, layer=layer, raster=rastermap, column=col_name, where=where)
  124. else:
  125. ret = grass.run_command("v.what.rast", map=vectmap, raster=rastermap, column=col_name, where=where)
  126. if ret != 0:
  127. dbif.close()
  128. grass.fatal(_("Unable to run v.what.rast for vector map <%s> and raster map <%s>")%(vectmap, rastermap))
  129. # Use the first map in case a column names was provided
  130. if column:
  131. break
  132. dbif.close()
  133. if __name__ == "__main__":
  134. options, flags = grass.parser()
  135. main()