sampling.py 5.6 KB

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