sampling.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. def sample_stds_by_stds_topology(intype, sampletype, inputs, sampler, header,
  19. separator, method, spatial=False,
  20. print_only=True):
  21. """!Sample the input space time datasets with a sample
  22. space time dataset, return the created map matrix and optionally
  23. print the result to stdout
  24. In case multiple maps are located in the current granule,
  25. the map names are separated by comma.
  26. In case a layer is present, the names map ids are extended
  27. in this form: "name:layer@mapset"
  28. Attention: Do not use the comma as separator for printing
  29. @param intype Type of the input space time dataset (strds, stvds or str3ds)
  30. @param sampletype Type of the sample space time datasets (strds, stvds or str3ds)
  31. @param inputs Name or comma separated names of space time datasets or 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 string
  37. 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 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 = get_current_mapset()
  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. core.fatal(_("Dataset <%s> not found in temporal database") % (st.get_id()))
  69. st.select(dbif)
  70. if sst.is_in_db(dbif) == False:
  71. core.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