sampling.py 5.8 KB

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