sampling.py 5.6 KB

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