sampling.py 5.7 KB

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