sampling.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. """!@package grass.temporal
  2. @brief GRASS Python scripting module (temporal GIS functions)
  3. Temporal GIS related functions to be used in Python scripts.
  4. Usage:
  5. @code
  6. import grass.temporal as tgis
  7. tgis.register_maps_in_space_time_dataset(type, name, maps)
  8. ...
  9. @endcode
  10. (C) 2008-2011 by the GRASS Development Team
  11. This program is free software under the GNU General Public
  12. License (>=v2). Read the file COPYING that comes with GRASS
  13. for details.
  14. @author Soeren Gebbert
  15. """
  16. from space_time_datasets import *
  17. from factory import *
  18. ###############################################################################
  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 str3ds)
  31. @param sampletype Type of the sample space time dataset (strds, stvds or str3ds)
  32. @param inputs Name or comma separated names of space time datasets
  33. @param sampler Name of a space time dataset used for temporal sampling
  34. @param header Set True to print column names
  35. @param separator The field separator character between the columns
  36. @param method The method to be used for temporal sampling
  37. (start,during,contain,overlap,equal)
  38. @param spatial Perform spatial overlapping check
  39. @param print_only If set True (default) then the result of the sampling will be
  40. printed to stdout, if set to False the resulting map matrix
  41. will be returned.
  42. @return The map matrix or None if nothing found
  43. """
  44. mapset = core.gisenv()["MAPSET"]
  45. # Make a method list
  46. method = method.split(",")
  47. # Split the inputs
  48. input_list = inputs.split(",")
  49. sts = []
  50. for input in input_list:
  51. if input.find("@") >= 0:
  52. id = input
  53. else:
  54. id = input + "@" + mapset
  55. st = dataset_factory(intype, id)
  56. sts.append(st)
  57. if sampler.find("@") >= 0:
  58. sid = sampler
  59. else:
  60. sid = sampler + "@" + mapset
  61. sst = dataset_factory(sampletype, sid)
  62. dbif = SQLDatabaseInterfaceConnection()
  63. dbif.connect()
  64. for st in sts:
  65. if st.is_in_db(dbif) == False:
  66. core.fatal(_("Dataset <%s> not found in temporal database") % (st.get_id()))
  67. st.select(dbif)
  68. if sst.is_in_db(dbif) == False:
  69. core.fatal(_("Dataset <%s> not found in temporal database") % (sid))
  70. sst.select(dbif)
  71. if separator is None or separator == "" or separator.find(",") >= 0:
  72. separator = " | "
  73. mapmatrizes = []
  74. for st in sts:
  75. mapmatrix = st.sample_by_dataset(sst, method, spatial, dbif)
  76. if mapmatrix and len(mapmatrix) > 0:
  77. mapmatrizes.append(mapmatrix)
  78. if len(mapmatrizes) > 0:
  79. # Simply return the map matrix
  80. if not print_only:
  81. dbif.close()
  82. return mapmatrizes
  83. if header:
  84. string = ""
  85. string += "%s%s" % (sst.get_id(), separator)
  86. for st in sts:
  87. string += "%s%s" % (st.get_id(), separator)
  88. string += "%s%s" % ("start_time", separator)
  89. string += "%s%s" % ("end_time", separator)
  90. string += "%s%s" % ("interval_length", separator)
  91. string += "%s" % ("distance_from_begin")
  92. print string
  93. first_time, dummy = mapmatrizes[0][0]["granule"].get_valid_time()
  94. for i in range(len(mapmatrizes[0])):
  95. mapname_list = []
  96. for mapmatrix in mapmatrizes:
  97. mapnames = ""
  98. count = 0
  99. entry = mapmatrix[i]
  100. for sample in entry["samples"]:
  101. if count == 0:
  102. mapnames += str(sample.get_id())
  103. else:
  104. mapnames += ",%s" % str(sample.get_id())
  105. count += 1
  106. mapname_list.append(mapnames)
  107. entry = mapmatrizes[0][i]
  108. map = entry["granule"]
  109. start, end = map.get_valid_time()
  110. if end:
  111. delta = end - start
  112. else:
  113. delta = None
  114. delta_first = start - first_time
  115. if map.is_time_absolute():
  116. if end:
  117. delta = time_delta_to_relative_time(delta)
  118. delta_first = time_delta_to_relative_time(delta_first)
  119. string = ""
  120. string += "%s%s" % (map.get_id(), separator)
  121. for mapnames in mapname_list:
  122. string += "%s%s" % (mapnames, separator)
  123. string += "%s%s" % (start, separator)
  124. string += "%s%s" % (end, separator)
  125. string += "%s%s" % (delta, separator)
  126. string += "%s" % (delta_first)
  127. print string
  128. dbif.close()
  129. if len(mapmatrizes) > 0:
  130. return mapmatrizes
  131. return None