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