sampling.py 5.4 KB

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