sampling.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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) 2012-2013 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 factory import *
  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 str3ds)
  29. @param sampletype Type of the sample space time datasets (strds, stvds or str3ds)
  30. @param inputs Name or comma separated names of space time datasets or 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 string
  36. 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 sampling will be
  39. printed to stdout, if set to False the resulting map matrix
  40. 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) == False:
  68. msgr.fatal(_("Dataset <%s> not found in temporal database") % (st.get_id()))
  69. st.select(dbif)
  70. if sst.is_in_db(dbif) == False:
  71. msgr.fatal(_("Dataset <%s> not found in temporal database") % (sid))
  72. sst.select(dbif)
  73. if separator is None or separator == "" or separator.find(",") >= 0:
  74. separator = " | "
  75. mapmatrizes = []
  76. for st in sts:
  77. mapmatrix = st.sample_by_dataset(sst, method, spatial, dbif)
  78. if mapmatrix and len(mapmatrix) > 0:
  79. mapmatrizes.append(mapmatrix)
  80. if len(mapmatrizes) > 0:
  81. # Simply return the map matrix
  82. if not print_only:
  83. dbif.close()
  84. return mapmatrizes
  85. if header:
  86. string = ""
  87. string += "%s%s" % (sst.get_id(), separator)
  88. for st in sts:
  89. string += "%s%s" % (st.get_id(), separator)
  90. string += "%s%s" % ("start_time", separator)
  91. string += "%s%s" % ("end_time", separator)
  92. string += "%s%s" % ("interval_length", separator)
  93. string += "%s" % ("distance_from_begin")
  94. print string
  95. first_time, dummy = mapmatrizes[0][0]["granule"].get_temporal_extent_as_tuple()
  96. for i in range(len(mapmatrizes[0])):
  97. mapname_list = []
  98. for mapmatrix in mapmatrizes:
  99. mapnames = ""
  100. count = 0
  101. entry = mapmatrix[i]
  102. for sample in entry["samples"]:
  103. if count == 0:
  104. mapnames += str(sample.get_id())
  105. else:
  106. mapnames += ",%s" % str(sample.get_id())
  107. count += 1
  108. mapname_list.append(mapnames)
  109. entry = mapmatrizes[0][i]
  110. map = entry["granule"]
  111. start, end = map.get_temporal_extent_as_tuple()
  112. if end:
  113. delta = end - start
  114. else:
  115. delta = None
  116. delta_first = start - first_time
  117. if map.is_time_absolute():
  118. if end:
  119. delta = time_delta_to_relative_time(delta)
  120. delta_first = time_delta_to_relative_time(delta_first)
  121. string = ""
  122. string += "%s%s" % (map.get_id(), separator)
  123. for mapnames in mapname_list:
  124. string += "%s%s" % (mapnames, separator)
  125. string += "%s%s" % (start, separator)
  126. string += "%s%s" % (end, separator)
  127. string += "%s%s" % (delta, separator)
  128. string += "%s" % (delta_first)
  129. print string
  130. dbif.close()
  131. if len(mapmatrizes) > 0:
  132. return mapmatrizes
  133. return None