sampling.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. """!@package grass.temporal
  2. @brief GRASS Python scripting module (temporal GIS functions)
  3. Temporal GIS related functions to be used in Python scripts.
  4. Usage:
  5. @code
  6. import grass.temporal as tgis
  7. tgis.register_maps_in_space_time_dataset(type, name, maps)
  8. ...
  9. @endcode
  10. (C) 2008-2011 by the GRASS Development Team
  11. This program is free software under the GNU General Public
  12. License (>=v2). Read the file COPYING that comes with GRASS
  13. for details.
  14. @author Soeren Gebbert
  15. """
  16. from space_time_datasets import *
  17. ###############################################################################
  18. def sample_stds_by_stds_topology(intype, sampletype, inputs, sampler, header,
  19. separator, method, spatial=False,
  20. print_only=True):
  21. """!Sample the input space time datasets with a sample
  22. space time dataset, return the created map matrix and optionally
  23. print the result to stdout
  24. In case multiple maps are located in the current granule,
  25. the map names are separated by comma.
  26. In case a layer is present, the names map ids are extended
  27. in this form: "name:layer@mapset"
  28. Attention: Do not use the comma as separator for printing
  29. @param intype Type of the input space time dataset (strds, stvds or str3ds)
  30. @param sampletype Type of the sample space time dataset (strds, stvds or str3ds)
  31. @param inputs Name or comma separated names of space time datasets
  32. @param sampler Name of a space time dataset used for temporal sampling
  33. @param header Set True to print column names
  34. @param separator The field separator character between the columns
  35. @param method The method to be used for temporal sampling
  36. (start,during,contain,overlap,equal)
  37. @param spatial Perform spatial overlapping check
  38. @param print_only If set True (default) then the result of the sampling will be
  39. printed to stdout, if set to False the resulting map matrix
  40. will be returned.
  41. @return The map matrix or None if nothing found
  42. """
  43. mapset = core.gisenv()["MAPSET"]
  44. # Make a method list
  45. method = method.split(",")
  46. # Split the inputs
  47. input_list = inputs.split(",")
  48. sts = []
  49. for input in input_list:
  50. if input.find("@") >= 0:
  51. id = input
  52. else:
  53. id = input + "@" + mapset
  54. st = dataset_factory(intype, id)
  55. sts.append(st)
  56. if sampler.find("@") >= 0:
  57. sid = sampler
  58. else:
  59. sid = sampler + "@" + mapset
  60. sst = dataset_factory(sampletype, sid)
  61. dbif = SQLDatabaseInterfaceConnection()
  62. dbif.connect()
  63. for st in sts:
  64. if st.is_in_db(dbif) == False:
  65. core.fatal(_("Dataset <%s> not found in temporal database") % (st.get_id()))
  66. st.select(dbif)
  67. if sst.is_in_db(dbif) == False:
  68. core.fatal(_("Dataset <%s> not found in temporal database") % (sid))
  69. sst.select(dbif)
  70. if separator is None or separator == "" or separator.find(",") >= 0:
  71. separator = " | "
  72. mapmatrizes = []
  73. for st in sts:
  74. mapmatrix = st.sample_by_dataset(sst, method, spatial, dbif)
  75. if mapmatrix and len(mapmatrix) > 0:
  76. mapmatrizes.append(mapmatrix)
  77. if len(mapmatrizes) > 0:
  78. # Simply return the map matrix
  79. if not print_only:
  80. dbif.close()
  81. return mapmatrizes
  82. if header:
  83. string = ""
  84. string += "%s%s" % (sst.get_id(), separator)
  85. for st in sts:
  86. string += "%s%s" % (st.get_id(), separator)
  87. string += "%s%s" % ("start_time", separator)
  88. string += "%s%s" % ("end_time", separator)
  89. string += "%s%s" % ("interval_length", separator)
  90. string += "%s" % ("distance_from_begin")
  91. print string
  92. first_time, dummy = mapmatrizes[0][0]["granule"].get_valid_time()
  93. for i in range(len(mapmatrizes[0])):
  94. mapname_list = []
  95. for mapmatrix in mapmatrizes:
  96. mapnames = ""
  97. count = 0
  98. entry = mapmatrix[i]
  99. for sample in entry["samples"]:
  100. if count == 0:
  101. mapnames += str(sample.get_id())
  102. else:
  103. mapnames += ",%s" % str(sample.get_id())
  104. count += 1
  105. mapname_list.append(mapnames)
  106. entry = mapmatrizes[0][i]
  107. map = entry["granule"]
  108. start, end = map.get_valid_time()
  109. if end:
  110. delta = end - start
  111. else:
  112. delta = None
  113. delta_first = start - first_time
  114. if map.is_time_absolute():
  115. if end:
  116. delta = time_delta_to_relative_time(delta)
  117. delta_first = time_delta_to_relative_time(delta_first)
  118. string = ""
  119. string += "%s%s" % (map.get_id(), separator)
  120. for mapnames in mapname_list:
  121. string += "%s%s" % (mapnames, separator)
  122. string += "%s%s" % (start, separator)
  123. string += "%s%s" % (end, separator)
  124. string += "%s%s" % (delta, separator)
  125. string += "%s" % (delta_first)
  126. print string
  127. dbif.close()
  128. if len(mapmatrizes) > 0:
  129. return mapmatrizes
  130. return None