abstract_space_time_dataset.py 100 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716
  1. """
  2. The abstract_space_time_dataset module provides the AbstractSpaceTimeDataset
  3. class that is the base class for all space time datasets.
  4. (C) 2011-2013 by the GRASS Development Team
  5. This program is free software under the GNU General Public
  6. License (>=v2). Read the file COPYING that comes with GRASS
  7. for details.
  8. :authors: Soeren Gebbert
  9. """
  10. from __future__ import print_function
  11. import sys
  12. import uuid
  13. import os
  14. import copy
  15. from datetime import datetime
  16. from abc import ABCMeta, abstractmethod
  17. from .core import (
  18. init_dbif,
  19. get_sql_template_path,
  20. get_tgis_metadata,
  21. get_current_mapset,
  22. get_enable_mapset_check,
  23. )
  24. from .abstract_dataset import AbstractDataset, AbstractDatasetComparisonKeyStartTime
  25. from .temporal_granularity import (
  26. check_granularity_string,
  27. compute_absolute_time_granularity,
  28. compute_relative_time_granularity,
  29. )
  30. from .spatio_temporal_relationships import (
  31. count_temporal_topology_relationships,
  32. print_spatio_temporal_topology_relationships,
  33. SpatioTemporalTopologyBuilder,
  34. create_temporal_relation_sql_where_statement,
  35. )
  36. from .datetime_math import increment_datetime_by_string, string_to_datetime
  37. ###############################################################################
  38. class AbstractSpaceTimeDataset(AbstractDataset):
  39. """Abstract space time dataset class
  40. Base class for all space time datasets.
  41. This class represents an abstract space time dataset. Convenient functions
  42. to select, update, insert or delete objects of this type in the SQL
  43. temporal database exists as well as functions to register or unregister
  44. raster maps.
  45. Parts of the temporal logic are implemented in the SQL temporal
  46. database, like the computation of the temporal and spatial extent as
  47. well as the collecting of metadata.
  48. """
  49. __metaclass__ = ABCMeta
  50. def __init__(self, ident):
  51. AbstractDataset.__init__(self)
  52. self.reset(ident)
  53. self.map_counter = 0
  54. # SpaceTimeRasterDataset related only
  55. self.band_reference = None
  56. def get_name(self, band_reference=True):
  57. """Get dataset name including band reference filter if enabled.
  58. :param bool band_reference: True to return dataset name
  59. including band reference filter if defined
  60. (eg. "landsat.L8_1") otherwise dataset name is returned only
  61. (eg. "landsat").
  62. :return str: dataset name
  63. """
  64. dataset_name = super(AbstractSpaceTimeDataset, self).get_name()
  65. if band_reference and self.band_reference:
  66. return "{}.{}".format(dataset_name, self.band_reference)
  67. return dataset_name
  68. def create_map_register_name(self):
  69. """Create the name of the map register table of this space time
  70. dataset
  71. A uuid and the map type are used to create the table name
  72. ATTENTION: It must be assured that the base object has selected its
  73. content from the database.
  74. :return: The name of the map register table
  75. """
  76. uuid_rand = str(uuid.uuid4()).replace("-", "")
  77. table_name = (
  78. self.get_new_map_instance(None).get_type() + "_map_register_" + uuid_rand
  79. )
  80. return table_name
  81. @abstractmethod
  82. def get_new_map_instance(self, ident=None):
  83. """Return a new instance of a map which is associated
  84. with the type of this object
  85. :param ident: The unique identifier of the new object
  86. """
  87. @abstractmethod
  88. def get_map_register(self):
  89. """Return the name of the map register table
  90. :return: The map register table name
  91. """
  92. @abstractmethod
  93. def set_map_register(self, name):
  94. """Set the name of the map register table
  95. This table stores all map names which are registered
  96. in this space time dataset.
  97. This method only modifies this object and does not commit
  98. the modifications to the temporal database.
  99. :param name: The name of the register table
  100. """
  101. def print_self(self):
  102. """Print the content of the internal structure to stdout"""
  103. self.base.print_self()
  104. self.temporal_extent.print_self()
  105. self.spatial_extent.print_self()
  106. self.metadata.print_self()
  107. def print_info(self):
  108. """Print information about this class in human readable style"""
  109. if self.get_type() == "strds":
  110. # 1 2 3 4 5 6 7
  111. # 0123456789012345678901234567890123456789012345678901234567890123456789012345678
  112. print(
  113. " +-------------------- Space Time Raster Dataset -----------------------------+"
  114. )
  115. if self.get_type() == "str3ds":
  116. # 1 2 3 4 5 6 7
  117. # 0123456789012345678901234567890123456789012345678901234567890123456789012345678
  118. print(
  119. " +-------------------- Space Time 3D Raster Dataset --------------------------+"
  120. )
  121. if self.get_type() == "stvds":
  122. # 1 2 3 4 5 6 7
  123. # 0123456789012345678901234567890123456789012345678901234567890123456789012345678
  124. print(
  125. " +-------------------- Space Time Vector Dataset -----------------------------+"
  126. )
  127. print(
  128. " | |"
  129. )
  130. self.base.print_info()
  131. self.temporal_extent.print_info()
  132. self.spatial_extent.print_info()
  133. self.metadata.print_info()
  134. print(
  135. " +----------------------------------------------------------------------------+"
  136. )
  137. def print_shell_info(self):
  138. """Print information about this class in shell style"""
  139. self.base.print_shell_info()
  140. self.temporal_extent.print_shell_info()
  141. self.spatial_extent.print_shell_info()
  142. self.metadata.print_shell_info()
  143. def print_history(self):
  144. """Print history information about this class in human readable
  145. shell style
  146. """
  147. self.metadata.print_history()
  148. def set_initial_values(
  149. self, temporal_type, semantic_type=None, title=None, description=None
  150. ):
  151. """Set the initial values of the space time dataset
  152. In addition the command creation string is generated
  153. an inserted into the metadata object.
  154. This method only modifies this object and does not commit
  155. the modifications to the temporal database.
  156. The insert() function must be called to commit
  157. this content into the temporal database.
  158. :param temporal_type: The temporal type of this space
  159. time dataset (absolute or relative)
  160. :param semantic_type: The semantic type of this dataset
  161. :param title: The title
  162. :param description: The description of this dataset
  163. """
  164. if temporal_type == "absolute":
  165. self.base.set_ttype("absolute")
  166. elif temporal_type == "relative":
  167. self.base.set_ttype("relative")
  168. else:
  169. self.msgr.fatal(_('Unknown temporal type "%s"') % (temporal_type))
  170. self.base.set_semantic_type(semantic_type)
  171. self.metadata.set_title(title)
  172. self.metadata.set_description(description)
  173. self.metadata.set_command(self.create_command_string())
  174. def set_aggregation_type(self, aggregation_type):
  175. """Set the aggregation type of the space time dataset
  176. :param aggregation_type: The aggregation type of the space time
  177. dataset
  178. """
  179. self.metadata.set_aggregation_type(aggregation_type)
  180. def update_command_string(self, dbif=None):
  181. """Append the current command string to any existing command string
  182. in the metadata class and calls metadata update
  183. :param dbif: The database interface to be used
  184. """
  185. self.metadata.select(dbif=dbif)
  186. command = self.metadata.get_command()
  187. if command is None:
  188. command = ""
  189. command += self.create_command_string()
  190. self.metadata.set_command(command)
  191. self.metadata.update(dbif=dbif)
  192. def create_command_string(self):
  193. """Create the command string that was used to create this
  194. space time dataset.
  195. The command string should be set with self.metadata.set_command()
  196. :return: The command string
  197. """
  198. # The grass module
  199. command = "# %s \n" % (str(datetime.today().strftime("%Y-%m-%d %H:%M:%S")))
  200. command += os.path.basename(sys.argv[0])
  201. # We will wrap the command line to fit into 80 character
  202. length = len(command)
  203. for token in sys.argv[1:]:
  204. # We need to remove specific characters
  205. token = token.replace("'", " ")
  206. token = token.replace('"', " ")
  207. # Check for sub strings
  208. if token.find("=") > 0:
  209. first = token.split("=")[0]
  210. second = ""
  211. flag = 0
  212. for t in token.split("=")[1:]:
  213. if flag == 0:
  214. second += t
  215. flag = 1
  216. else:
  217. second += "=" + t
  218. token = '%s="%s"' % (first, second)
  219. if length + len(token) >= 76:
  220. command += "\n %s" % (token)
  221. length = len(token) + 4
  222. else:
  223. command += " %s" % (token)
  224. length += len(token) + 1
  225. command += "\n"
  226. return str(command)
  227. def get_semantic_type(self):
  228. """Return the semantic type of this dataset
  229. :return: The semantic type
  230. """
  231. return self.base.get_semantic_type()
  232. def get_initial_values(self):
  233. """Return the initial values: temporal_type,
  234. semantic_type, title, description"""
  235. temporal_type = self.get_temporal_type()
  236. semantic_type = self.base.get_semantic_type()
  237. title = self.metadata.get_title()
  238. description = self.metadata.get_description()
  239. return temporal_type, semantic_type, title, description
  240. def get_granularity(self):
  241. """Return the granularity of the space time dataset
  242. Granularity can be of absolute time or relative time.
  243. In case of absolute time a string containing an integer
  244. value and the time unit (years, months, days, hours, minuts,
  245. seconds). In case of relative time an integer value is expected.
  246. :return: The granularity
  247. """
  248. return self.temporal_extent.get_granularity()
  249. def set_granularity(self, granularity):
  250. """Set the granularity
  251. The granularity is usually computed by the space time dataset at
  252. runtime.
  253. Granularity can be of absolute time or relative time.
  254. In case of absolute time a string containing an integer
  255. value and the time unit (years, months, days, hours, minuts,
  256. seconds). In case of relative time an integer value is expected.
  257. This method only modifies this object and does not commit
  258. the modifications to the temporal database.
  259. :param granularity: The granularity of the dataset
  260. """
  261. temporal_type = self.get_temporal_type()
  262. check = check_granularity_string(granularity, temporal_type)
  263. if not check:
  264. self.msgr.fatal(_('Wrong granularity: "%s"') % str(granularity))
  265. if temporal_type == "absolute":
  266. self.base.set_ttype("absolute")
  267. elif temporal_type == "relative":
  268. self.base.set_ttype("relative")
  269. else:
  270. self.msgr.fatal(_('Unknown temporal type "%s"') % (temporal_type))
  271. self.temporal_extent.set_granularity(granularity)
  272. def set_relative_time_unit(self, unit):
  273. """Set the relative time unit which may be of type:
  274. years, months, days, hours, minutes or seconds
  275. All maps registered in a (relative time)
  276. space time dataset must have the same unit
  277. This method only modifies this object and does not commit
  278. the modifications to the temporal database.
  279. :param unit: The relative time unit
  280. """
  281. temporal_type = self.get_temporal_type()
  282. if temporal_type == "relative":
  283. if not self.check_relative_time_unit(unit):
  284. self.msgr.fatal(_("Unsupported temporal unit: %s") % (unit))
  285. self.relative_time.set_unit(unit)
  286. def insert(self, dbif=None, execute=True):
  287. """Insert the space time dataset content into the database from the internal
  288. structure
  289. The map register table will be created, so that maps
  290. can be registered.
  291. :param dbif: The database interface to be used
  292. :param execute: If True the SQL statements will be executed.
  293. If False the prepared SQL statements are
  294. returned and must be executed by the caller.
  295. :return: The SQL insert statement in case execute=False, or an
  296. empty string otherwise
  297. """
  298. dbif, connection_state_changed = init_dbif(dbif)
  299. # We need to create the register table if it does not exist
  300. stds_register_table = self.get_map_register()
  301. # Create the map register table
  302. sql_path = get_sql_template_path()
  303. statement = ""
  304. # We need to create the map register table
  305. if stds_register_table is None:
  306. # Create table name
  307. stds_register_table = self.create_map_register_name()
  308. # Assure that the table and index do not exist
  309. # dbif.execute_transaction("DROP INDEX IF EXISTS %s; DROP TABLE IF EXISTS %s;"%(stds_register_table + "_index", stds_register_table))
  310. # Read the SQL template
  311. sql = open(
  312. os.path.join(sql_path, "stds_map_register_table_template.sql"), "r"
  313. ).read()
  314. # Create a raster, raster3d or vector tables
  315. sql = sql.replace("SPACETIME_REGISTER_TABLE", stds_register_table)
  316. statement += sql
  317. if dbif.get_dbmi().__name__ == "sqlite3":
  318. statement += "CREATE INDEX %s_index ON %s (id);" % (
  319. stds_register_table,
  320. stds_register_table,
  321. )
  322. # Set the map register table name
  323. self.set_map_register(stds_register_table)
  324. self.msgr.debug(
  325. 1,
  326. _("Created register table <%s> for space " "time %s dataset <%s>")
  327. % (
  328. stds_register_table,
  329. self.get_new_map_instance(None).get_type(),
  330. self.get_id(),
  331. ),
  332. )
  333. statement += AbstractDataset.insert(self, dbif=dbif, execute=False)
  334. if execute:
  335. dbif.execute_transaction(statement)
  336. statement = ""
  337. if connection_state_changed:
  338. dbif.close()
  339. return statement
  340. def get_map_time(self):
  341. """Return the type of the map time, interval, point, mixed or invalid"""
  342. return self.temporal_extent.get_map_time()
  343. def count_temporal_types(self, maps=None, dbif=None):
  344. """Return the temporal type of the registered maps as dictionary
  345. The map list must be ordered by start time
  346. The temporal type can be:
  347. - point -> only the start time is present
  348. - interval -> start and end time
  349. - invalid -> No valid time point or interval found
  350. :param maps: A sorted (start_time) list of AbstractDataset objects
  351. :param dbif: The database interface to be used
  352. """
  353. if maps is None:
  354. maps = self.get_registered_maps_as_objects(
  355. where=None, order="start_time", dbif=dbif
  356. )
  357. time_invalid = 0
  358. time_point = 0
  359. time_interval = 0
  360. tcount = {}
  361. for i in range(len(maps)):
  362. # Check for point and interval data
  363. if maps[i].is_time_absolute():
  364. start, end = maps[i].get_absolute_time()
  365. if maps[i].is_time_relative():
  366. start, end, unit = maps[i].get_relative_time()
  367. if start is not None and end is not None:
  368. time_interval += 1
  369. elif start is not None and end is None:
  370. time_point += 1
  371. else:
  372. time_invalid += 1
  373. tcount["point"] = time_point
  374. tcount["interval"] = time_interval
  375. tcount["invalid"] = time_invalid
  376. return tcount
  377. def count_gaps(self, maps=None, dbif=None):
  378. """Count the number of gaps between temporal neighbors
  379. :param maps: A sorted (start_time) list of AbstractDataset objects
  380. :param dbif: The database interface to be used
  381. :return: The numbers of gaps between temporal neighbors
  382. """
  383. if maps is None:
  384. maps = self.get_registered_maps_as_objects(
  385. where=None, order="start_time", dbif=dbif
  386. )
  387. gaps = 0
  388. # Check for gaps
  389. for i in range(len(maps)):
  390. if i < len(maps) - 1:
  391. relation = maps[i + 1].temporal_relation(maps[i])
  392. if relation == "after":
  393. gaps += 1
  394. return gaps
  395. def print_spatio_temporal_relationships(self, maps=None, spatial=None, dbif=None):
  396. """Print the spatio-temporal relationships for each map of the space
  397. time dataset or for each map of the optional list of maps
  398. :param maps: a ordered by start_time list of map objects, if None
  399. the registered maps of the space time dataset are used
  400. :param spatial: This indicates if the spatial topology is created as
  401. well: spatial can be None (no spatial topology),
  402. "2D" using west, east, south, north or "3D" using
  403. west, east, south, north, bottom, top
  404. :param dbif: The database interface to be used
  405. """
  406. if maps is None:
  407. maps = self.get_registered_maps_as_objects(
  408. where=None, order="start_time", dbif=dbif
  409. )
  410. print_spatio_temporal_topology_relationships(
  411. maps1=maps, maps2=maps, spatial=spatial, dbif=dbif
  412. )
  413. def count_temporal_relations(self, maps=None, dbif=None):
  414. """Count the temporal relations between the registered maps.
  415. The map list must be ordered by start time.
  416. Temporal relations are counted by analysing the sparse upper right
  417. side temporal relationships matrix.
  418. :param maps: A sorted (start_time) list of AbstractDataset objects
  419. :param dbif: The database interface to be used
  420. :return: A dictionary with counted temporal relationships
  421. """
  422. if maps is None:
  423. maps = self.get_registered_maps_as_objects(
  424. where=None, order="start_time", dbif=dbif
  425. )
  426. return count_temporal_topology_relationships(maps1=maps, dbif=dbif)
  427. def check_temporal_topology(self, maps=None, dbif=None):
  428. """Check the temporal topology of all maps of the current space time
  429. dataset or of an optional list of maps
  430. Correct topology means, that time intervals are not overlap or
  431. that intervals does not contain other intervals.
  432. Equal time intervals are not allowed.
  433. The optional map list must be ordered by start time
  434. Allowed and not allowed temporal relationships for correct topology:
  435. - after -> allowed
  436. - precedes -> allowed
  437. - follows -> allowed
  438. - precedes -> allowed
  439. - equal -> not allowed
  440. - during -> not allowed
  441. - contains -> not allowed
  442. - overlaps -> not allowed
  443. - overlapped -> not allowed
  444. - starts -> not allowed
  445. - finishes -> not allowed
  446. - started -> not allowed
  447. - finished -> not allowed
  448. :param maps: An optional list of AbstractDataset objects, in case of
  449. None all maps of the space time dataset are checked
  450. :param dbif: The database interface to be used
  451. :return: True if topology is correct
  452. """
  453. if maps is None:
  454. maps = self.get_registered_maps_as_objects(
  455. where=None, order="start_time", dbif=dbif
  456. )
  457. relations = count_temporal_topology_relationships(maps1=maps, dbif=dbif)
  458. if relations is None:
  459. return False
  460. map_time = self.get_map_time()
  461. if map_time == "interval" or map_time == "mixed":
  462. if "equal" in relations and relations["equal"] > 0:
  463. return False
  464. if "during" in relations and relations["during"] > 0:
  465. return False
  466. if "contains" in relations and relations["contains"] > 0:
  467. return False
  468. if "overlaps" in relations and relations["overlaps"] > 0:
  469. return False
  470. if "overlapped" in relations and relations["overlapped"] > 0:
  471. return False
  472. if "starts" in relations and relations["starts"] > 0:
  473. return False
  474. if "finishes" in relations and relations["finishes"] > 0:
  475. return False
  476. if "started" in relations and relations["started"] > 0:
  477. return False
  478. if "finished" in relations and relations["finished"] > 0:
  479. return False
  480. elif map_time == "point":
  481. if "equal" in relations and relations["equal"] > 0:
  482. return False
  483. else:
  484. return False
  485. return True
  486. def sample_by_dataset(self, stds, method=None, spatial=False, dbif=None):
  487. """Sample this space time dataset with the temporal topology
  488. of a second space time dataset
  489. In case spatial is True, the spatial overlap between
  490. temporal related maps is performed. Only
  491. temporal related and spatial overlapping maps are returned.
  492. Return all registered maps as ordered (by start_time) object list.
  493. Each list entry is a list of map
  494. objects which are potentially located in temporal relation to the
  495. actual granule of the second space time dataset.
  496. Each entry in the object list is a dict. The actual sampler
  497. map and its temporal extent (the actual granule) and
  498. the list of samples are stored:
  499. .. code-block:: python
  500. list = self.sample_by_dataset(stds=sampler, method=[
  501. "during","overlap","contains","equal"])
  502. for entry in list:
  503. granule = entry["granule"]
  504. maplist = entry["samples"]
  505. for map in maplist:
  506. map.select()
  507. map.print_info()
  508. A valid temporal topology (no overlapping or inclusion allowed)
  509. is needed to get correct results in case of gaps in the sample
  510. dataset.
  511. Gaps between maps are identified as unregistered maps with id==None.
  512. The objects are initialized with their id's' and the spatio-temporal
  513. extent (temporal type, start time, end time, west, east, south,
  514. north, bottom and top).
  515. In case more map information are needed, use the select()
  516. method for each listed object.
  517. :param stds: The space time dataset to be used for temporal sampling
  518. :param method: This option specifies what sample method should be
  519. used. In case the registered maps are of temporal
  520. point type, only the start time is used for sampling.
  521. In case of mixed of interval data the user can chose
  522. between:
  523. - Example ["start", "during", "equals"]
  524. - start: Select maps of which the start time is
  525. located in the selection granule::
  526. map : s
  527. granule: s-----------------e
  528. map : s--------------------e
  529. granule: s-----------------e
  530. map : s--------e
  531. granule: s-----------------e
  532. - contains: Select maps which are temporal
  533. during the selection granule::
  534. map : s-----------e
  535. granule: s-----------------e
  536. - overlap: Select maps which temporal overlap
  537. the selection granule, this includes overlaps and
  538. overlapped::
  539. map : s-----------e
  540. granule: s-----------------e
  541. map : s-----------e
  542. granule: s----------e
  543. - during: Select maps which temporally contains
  544. the selection granule::
  545. map : s-----------------e
  546. granule: s-----------e
  547. - equals: Select maps which temporally equal
  548. to the selection granule::
  549. map : s-----------e
  550. granule: s-----------e
  551. - follows: Select maps which temporally follow
  552. the selection granule::
  553. map : s-----------e
  554. granule: s-----------e
  555. - precedes: Select maps which temporally precedes
  556. the selection granule::
  557. map : s-----------e
  558. granule: s-----------e
  559. All these methods can be combined. Method must be of
  560. type tuple including the identification strings.
  561. :param spatial: If set True additional the 2d spatial overlapping
  562. is used for selection -> spatio-temporal relation.
  563. The returned map objects will have temporal and
  564. spatial extents
  565. :param dbif: The database interface to be used
  566. :return: A list of lists of map objects or None in case nothing was
  567. found None
  568. """
  569. if self.get_temporal_type() != stds.get_temporal_type():
  570. self.msgr.error(
  571. _("The space time datasets must be of " "the same temporal type")
  572. )
  573. return None
  574. if stds.get_map_time() != "interval":
  575. self.msgr.error(
  576. _("The temporal map type of the sample " "dataset must be interval")
  577. )
  578. return None
  579. dbif, connection_state_changed = init_dbif(dbif)
  580. relations = copy.deepcopy(method)
  581. # Tune the temporal relations
  582. if "start" in relations:
  583. if "overlapped" not in relations:
  584. relations.append("overlapped")
  585. if "starts" not in relations:
  586. relations.append("starts")
  587. if "started" not in relations:
  588. relations.append("started")
  589. if "finishes" not in relations:
  590. relations.append("finishes")
  591. if "contains" not in relations:
  592. relations.append("contains")
  593. if "equals" not in relations:
  594. relations.append("equals")
  595. if "overlap" in relations or "over" in relations:
  596. if "overlapped" not in relations:
  597. relations.append("overlapped")
  598. if "overlaps" not in relations:
  599. relations.append("overlaps")
  600. if "contain" in relations:
  601. if "contains" not in relations:
  602. relations.append("contains")
  603. # Remove start, equal, contain and overlap
  604. relations = [
  605. relation.upper().strip()
  606. for relation in relations
  607. if relation not in ["start", "overlap", "contain"]
  608. ]
  609. # print(relations)
  610. tb = SpatioTemporalTopologyBuilder()
  611. if spatial:
  612. spatial = "2D"
  613. else:
  614. spatial = None
  615. mapsA = self.get_registered_maps_as_objects(dbif=dbif)
  616. mapsB = stds.get_registered_maps_as_objects_with_gaps(dbif=dbif)
  617. tb.build(mapsB, mapsA, spatial)
  618. obj_list = []
  619. for map in mapsB:
  620. result = {}
  621. maplist = []
  622. # Get map relations
  623. map_relations = map.get_temporal_relations()
  624. # print(map.get_temporal_extent_as_tuple())
  625. # for key in map_relations.keys():
  626. # if key not in ["NEXT", "PREV"]:
  627. # print(key, map_relations[key][0].get_temporal_extent_as_tuple())
  628. result["granule"] = map
  629. # Append the maps that fulfill the relations
  630. for relation in relations:
  631. if relation in map_relations.keys():
  632. for sample_map in map_relations[relation]:
  633. if sample_map not in maplist:
  634. maplist.append(sample_map)
  635. # Add an empty map if no map was found
  636. if not maplist:
  637. empty_map = self.get_new_map_instance(None)
  638. empty_map.set_spatial_extent(map.get_spatial_extent())
  639. empty_map.set_temporal_extent(map.get_temporal_extent())
  640. maplist.append(empty_map)
  641. result["samples"] = maplist
  642. obj_list.append(result)
  643. if connection_state_changed:
  644. dbif.close()
  645. return obj_list
  646. def sample_by_dataset_sql(self, stds, method=None, spatial=False, dbif=None):
  647. """Sample this space time dataset with the temporal topology
  648. of a second space time dataset using SQL queries.
  649. This function is very slow for huge large space time datasets
  650. but can run several times in the same process without problems.
  651. The sample dataset must have "interval" as temporal map type,
  652. so all sample maps have valid interval time.
  653. In case spatial is True, the spatial overlap between
  654. temporal related maps is performed. Only
  655. temporal related and spatial overlapping maps are returned.
  656. Return all registered maps as ordered (by start_time) object list
  657. with "gap" map objects (id==None). Each list entry is a list of map
  658. objects which are potentially located in temporal relation to the
  659. actual granule of the second space time dataset.
  660. Each entry in the object list is a dict. The actual sampler
  661. map and its temporal extent (the actual granule) and
  662. the list of samples are stored:
  663. .. code-block:: python
  664. list = self.sample_by_dataset(stds=sampler, method=[
  665. "during","overlap","contain","equal"])
  666. for entry in list:
  667. granule = entry["granule"]
  668. maplist = entry["samples"]
  669. for map in maplist:
  670. map.select()
  671. map.print_info()
  672. A valid temporal topology (no overlapping or inclusion allowed)
  673. is needed to get correct results in case of gaps in the sample
  674. dataset.
  675. Gaps between maps are identified as unregistered maps with id==None.
  676. The objects are initialized with their id's' and the spatio-temporal
  677. extent (temporal type, start time, end time, west, east, south,
  678. north, bottom and top).
  679. In case more map information are needed, use the select()
  680. method for each listed object.
  681. :param stds: The space time dataset to be used for temporal sampling
  682. :param method: This option specifies what sample method should be
  683. used. In case the registered maps are of temporal
  684. point type, only the start time is used for sampling.
  685. In case of mixed of interval data the user can chose
  686. between:
  687. - Example ["start", "during", "equals"]
  688. - start: Select maps of which the start time is
  689. located in the selection granule::
  690. map : s
  691. granule: s-----------------e
  692. map : s--------------------e
  693. granule: s-----------------e
  694. map : s--------e
  695. granule: s-----------------e
  696. - contains: Select maps which are temporal
  697. during the selection granule::
  698. map : s-----------e
  699. granule: s-----------------e
  700. - overlap: Select maps which temporal overlap
  701. the selection granule, this includes overlaps and
  702. overlapped::
  703. map : s-----------e
  704. granule: s-----------------e
  705. map : s-----------e
  706. granule: s----------e
  707. - during: Select maps which temporally contains
  708. the selection granule::
  709. map : s-----------------e
  710. granule: s-----------e
  711. - equals: Select maps which temporally equal
  712. to the selection granule::
  713. map : s-----------e
  714. granule: s-----------e
  715. - follows: Select maps which temporally follow
  716. the selection granule::
  717. map : s-----------e
  718. granule: s-----------e
  719. - precedes: Select maps which temporally precedes
  720. the selection granule::
  721. map : s-----------e
  722. granule: s-----------e
  723. All these methods can be combined. Method must be of
  724. type tuple including the identification strings.
  725. :param spatial: If set True additional the 2d spatial overlapping
  726. is used for selection -> spatio-temporal relation.
  727. The returned map objects will have temporal and
  728. spatial extents
  729. :param dbif: The database interface to be used
  730. :return: A list of lists of map objects or None in case nothing was
  731. found None
  732. """
  733. use_start = False
  734. use_during = False
  735. use_overlap = False
  736. use_contain = False
  737. use_equal = False
  738. use_follows = False
  739. use_precedes = False
  740. # Initialize the methods
  741. if method is not None:
  742. for name in method:
  743. if name == "start":
  744. use_start = True
  745. if name == "during":
  746. use_during = True
  747. if name == "overlap":
  748. use_overlap = True
  749. if name == "contain" or name == "contains":
  750. use_contain = True
  751. if name == "equal" or name == "equals":
  752. use_equal = True
  753. if name == "follows":
  754. use_follows = True
  755. if name == "precedes":
  756. use_precedes = True
  757. else:
  758. use_during = True
  759. use_overlap = True
  760. use_contain = True
  761. use_equal = True
  762. if self.get_temporal_type() != stds.get_temporal_type():
  763. self.msgr.error(
  764. _("The space time datasets must be of " "the same temporal type")
  765. )
  766. return None
  767. if stds.get_map_time() != "interval":
  768. self.msgr.error(
  769. _("The temporal map type of the sample " "dataset must be interval")
  770. )
  771. return None
  772. # In case points of time are available, disable the interval specific
  773. # methods
  774. if self.get_map_time() == "point":
  775. use_start = True
  776. use_during = False
  777. use_overlap = False
  778. use_contain = False
  779. use_equal = False
  780. use_follows = False
  781. use_precedes = False
  782. dbif, connection_state_changed = init_dbif(dbif)
  783. obj_list = []
  784. sample_maps = stds.get_registered_maps_as_objects_with_gaps(
  785. where=None, dbif=dbif
  786. )
  787. for granule in sample_maps:
  788. # Read the spatial extent
  789. if spatial:
  790. granule.spatial_extent.select(dbif)
  791. start, end = granule.get_temporal_extent_as_tuple()
  792. where = create_temporal_relation_sql_where_statement(
  793. start,
  794. end,
  795. use_start,
  796. use_during,
  797. use_overlap,
  798. use_contain,
  799. use_equal,
  800. use_follows,
  801. use_precedes,
  802. )
  803. maps = self.get_registered_maps_as_objects(where, "start_time", dbif)
  804. result = {}
  805. result["granule"] = granule
  806. num_samples = 0
  807. maplist = []
  808. if maps is not None:
  809. for map in maps:
  810. # Read the spatial extent
  811. if spatial:
  812. map.spatial_extent.select(dbif)
  813. # Ignore spatial disjoint maps
  814. if not granule.spatial_overlapping(map):
  815. continue
  816. num_samples += 1
  817. maplist.append(copy.copy(map))
  818. # Fill with empty map in case no spatio-temporal relations found
  819. if maps is None or num_samples == 0:
  820. map = self.get_new_map_instance(None)
  821. if self.is_time_absolute():
  822. map.set_absolute_time(start, end)
  823. elif self.is_time_relative():
  824. map.set_relative_time(start, end, self.get_relative_time_unit())
  825. maplist.append(copy.copy(map))
  826. result["samples"] = maplist
  827. obj_list.append(copy.copy(result))
  828. if connection_state_changed:
  829. dbif.close()
  830. return obj_list
  831. def get_registered_maps_as_objects_by_granularity(self, gran=None, dbif=None):
  832. """Return all registered maps as ordered (by start_time) object list
  833. with "gap" map objects (id==None) for spatio-temporal topological
  834. operations that require the temporal extent only.
  835. Each list entry is a list of AbstractMapDatasets objects
  836. which are potentially equal the actual granule, contain the
  837. actual granule or are located in the actual granule.
  838. Hence for each granule a list of AbstractMapDatasets can be
  839. expected.
  840. Maps that overlap the granule are ignored.
  841. The granularity of the space time dataset is used as increment in
  842. case the granule is not user defined.
  843. A valid temporal topology (no overlapping or inclusion allowed)
  844. is needed to get correct results.
  845. Space time datasets with interval time, time instances and mixed
  846. time are supported.
  847. Gaps between maps are identified as unregistered maps with id==None.
  848. The objects are initialized with their id's' and the spatio-temporal
  849. extent (temporal type, start time, end time, west, east, south,
  850. north, bottom and top).
  851. In case more map information are needed, use the select()
  852. method for each listed object.
  853. :param gran: The granularity string to be used, if None the
  854. granularity of the space time dataset is used.
  855. Absolute time has the format "number unit", relative
  856. time has the format "number".
  857. The unit in case of absolute time can be one of "second,
  858. seconds, minute, minutes, hour, hours, day, days, week,
  859. weeks, month, months, year, years". The unit of the
  860. relative time granule is always the space time dataset
  861. unit and can not be changed.
  862. :param dbif: The database interface to be used
  863. :return: ordered list of map lists. Each list represents a single
  864. granule, or None in case nothing found
  865. """
  866. dbif, connection_state_changed = init_dbif(dbif)
  867. if gran is None:
  868. gran = self.get_granularity()
  869. check = check_granularity_string(gran, self.get_temporal_type())
  870. if not check:
  871. self.msgr.fatal(_('Wrong granularity: "%s"') % str(gran))
  872. start, end = self.get_temporal_extent_as_tuple()
  873. if start is None or end is None:
  874. return None
  875. maps = self.get_registered_maps_as_objects(dbif=dbif, order="start_time")
  876. if not maps:
  877. return None
  878. # We need to adjust the end time in case the the dataset has no
  879. # interval time, so we can catch time instances at the end
  880. if self.get_map_time() != "interval":
  881. if self.is_time_absolute():
  882. end = increment_datetime_by_string(end, gran)
  883. else:
  884. end = end + gran
  885. maplist = AbstractSpaceTimeDataset.resample_maplist_by_granularity(
  886. maps, start, end, gran
  887. )
  888. if connection_state_changed:
  889. dbif.close()
  890. return maplist
  891. @staticmethod
  892. def resample_maplist_by_granularity(maps, start, end, gran):
  893. """Resample a list of AbstractMapDatasets by a given granularity
  894. The provided map list must be sorted by start time.
  895. A valid temporal topology (no overlapping or inclusion allowed)
  896. is needed to receive correct results.
  897. Maps with interval time, time instances and mixed
  898. time are supported.
  899. The temporal topology search order is as follows:
  900. 1. Maps that are equal to the actual granule are used
  901. 2. If no euqal found then maps that contain the actual granule
  902. are used
  903. 3. If no maps are found that contain the actual granule then maps
  904. are used that overlaps the actual granule
  905. 4. If no overlaps maps found then overlapped maps are used
  906. 5. If no overlapped maps are found then maps are used that are
  907. durin the actual granule
  908. Each entry in the resulting list is a list of
  909. AbstractMapDatasets objects.
  910. Hence for each granule a list of AbstractMapDatasets can be
  911. expected.
  912. Gaps between maps are identified as unregistered maps with id==None.
  913. :param maps: An ordered list (by start time) of AbstractMapDatasets
  914. objects. All maps must have the same temporal type
  915. and the same unit in case of relative time.
  916. :param start: The start time of the provided map list
  917. :param end: The end time of the provided map list
  918. :param gran: The granularity string to be used, if None the
  919. granularity of the space time dataset is used.
  920. Absolute time has the format "number unit", relative
  921. time has the format "number".
  922. The unit in case of absolute time can be one of "second,
  923. seconds, minute, minutes, hour, hours, day, days, week,
  924. weeks, month, months, year, years". The unit of the
  925. relative time granule is always the space time dataset
  926. unit and can not be changed.
  927. :return: ordered list of map lists. Each list represents a single
  928. granule, or None in case nothing found
  929. Usage:
  930. .. code-block:: python
  931. >>> import grass.temporal as tgis
  932. >>> maps = []
  933. >>> for i in range(3):
  934. ... map = tgis.RasterDataset("map%i@PERMANENT"%i)
  935. ... check = map.set_relative_time(i + 2, i + 3, "days")
  936. ... maps.append(map)
  937. >>> grans = tgis.AbstractSpaceTimeDataset.resample_maplist_by_granularity(maps,0,8,1)
  938. >>> for map_list in grans:
  939. ... print(map_list[0].get_id(), map_list[0].get_temporal_extent_as_tuple())
  940. None (0, 1)
  941. None (1, 2)
  942. map0@PERMANENT (2, 3)
  943. map1@PERMANENT (3, 4)
  944. map2@PERMANENT (4, 5)
  945. None (5, 6)
  946. None (6, 7)
  947. None (7, 8)
  948. >>> maps = []
  949. >>> map1 = tgis.RasterDataset("map1@PERMANENT")
  950. >>> check = map1.set_relative_time(2, 6, "days")
  951. >>> maps.append(map1)
  952. >>> map2 = tgis.RasterDataset("map2@PERMANENT")
  953. >>> check = map2.set_relative_time(7, 13, "days")
  954. >>> maps.append(map2)
  955. >>> grans = tgis.AbstractSpaceTimeDataset.resample_maplist_by_granularity(maps,0,16,2)
  956. >>> for map_list in grans:
  957. ... print(map_list[0].get_id(), map_list[0].get_temporal_extent_as_tuple())
  958. None (0, 2)
  959. map1@PERMANENT (2, 4)
  960. map1@PERMANENT (4, 6)
  961. map2@PERMANENT (6, 8)
  962. map2@PERMANENT (8, 10)
  963. map2@PERMANENT (10, 12)
  964. map2@PERMANENT (12, 14)
  965. None (14, 16)
  966. >>> maps = []
  967. >>> map1 = tgis.RasterDataset("map1@PERMANENT")
  968. >>> check = map1.set_relative_time(2, None, "days")
  969. >>> maps.append(map1)
  970. >>> map2 = tgis.RasterDataset("map2@PERMANENT")
  971. >>> check = map2.set_relative_time(7, None, "days")
  972. >>> maps.append(map2)
  973. >>> grans = tgis.AbstractSpaceTimeDataset.resample_maplist_by_granularity(maps,0,16,2)
  974. >>> for map_list in grans:
  975. ... print(map_list[0].get_id(), map_list[0].get_temporal_extent_as_tuple())
  976. None (0, 2)
  977. map1@PERMANENT (2, 4)
  978. None (4, 6)
  979. map2@PERMANENT (6, 8)
  980. None (8, 10)
  981. None (10, 12)
  982. None (12, 14)
  983. None (14, 16)
  984. >>> maps = []
  985. >>> map1 = tgis.RasterDataset("map1@PERMANENT")
  986. >>> check = map1.set_absolute_time(datetime(2000, 4,1), datetime(2000, 6, 1))
  987. >>> maps.append(map1)
  988. >>> map2 = tgis.RasterDataset("map2@PERMANENT")
  989. >>> check = map2.set_absolute_time(datetime(2000, 8,1), datetime(2000, 12, 1))
  990. >>> maps.append(map2)
  991. >>> grans = tgis.AbstractSpaceTimeDataset.resample_maplist_by_granularity(maps,datetime(2000,1,1),datetime(2001,4,1),"1 month")
  992. >>> for map_list in grans:
  993. ... print(map_list[0].get_id(), map_list[0].get_temporal_extent_as_tuple())
  994. None (datetime.datetime(2000, 1, 1, 0, 0), datetime.datetime(2000, 2, 1, 0, 0))
  995. None (datetime.datetime(2000, 2, 1, 0, 0), datetime.datetime(2000, 3, 1, 0, 0))
  996. None (datetime.datetime(2000, 3, 1, 0, 0), datetime.datetime(2000, 4, 1, 0, 0))
  997. map1@PERMANENT (datetime.datetime(2000, 4, 1, 0, 0), datetime.datetime(2000, 5, 1, 0, 0))
  998. map1@PERMANENT (datetime.datetime(2000, 5, 1, 0, 0), datetime.datetime(2000, 6, 1, 0, 0))
  999. None (datetime.datetime(2000, 6, 1, 0, 0), datetime.datetime(2000, 7, 1, 0, 0))
  1000. None (datetime.datetime(2000, 7, 1, 0, 0), datetime.datetime(2000, 8, 1, 0, 0))
  1001. map2@PERMANENT (datetime.datetime(2000, 8, 1, 0, 0), datetime.datetime(2000, 9, 1, 0, 0))
  1002. map2@PERMANENT (datetime.datetime(2000, 9, 1, 0, 0), datetime.datetime(2000, 10, 1, 0, 0))
  1003. map2@PERMANENT (datetime.datetime(2000, 10, 1, 0, 0), datetime.datetime(2000, 11, 1, 0, 0))
  1004. map2@PERMANENT (datetime.datetime(2000, 11, 1, 0, 0), datetime.datetime(2000, 12, 1, 0, 0))
  1005. None (datetime.datetime(2000, 12, 1, 0, 0), datetime.datetime(2001, 1, 1, 0, 0))
  1006. None (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2001, 2, 1, 0, 0))
  1007. None (datetime.datetime(2001, 2, 1, 0, 0), datetime.datetime(2001, 3, 1, 0, 0))
  1008. None (datetime.datetime(2001, 3, 1, 0, 0), datetime.datetime(2001, 4, 1, 0, 0))
  1009. """
  1010. if not maps:
  1011. return None
  1012. first = maps[0]
  1013. # Build the gaplist
  1014. gap_list = []
  1015. while start < end:
  1016. if first.is_time_absolute():
  1017. next = increment_datetime_by_string(start, gran)
  1018. else:
  1019. next = start + gran
  1020. map = first.get_new_instance(None)
  1021. map.set_spatial_extent_from_values(0, 0, 0, 0, 0, 0)
  1022. if first.is_time_absolute():
  1023. map.set_absolute_time(start, next)
  1024. else:
  1025. map.set_relative_time(start, next, first.get_relative_time_unit())
  1026. gap_list.append(copy.copy(map))
  1027. start = next
  1028. tb = SpatioTemporalTopologyBuilder()
  1029. tb.build(gap_list, maps)
  1030. relations_order = ["EQUAL", "DURING", "OVERLAPS", "OVERLAPPED", "CONTAINS"]
  1031. gran_list = []
  1032. for gap in gap_list:
  1033. # If not temporal relations then gap
  1034. if not gap.get_temporal_relations():
  1035. gran_list.append(
  1036. [
  1037. gap,
  1038. ]
  1039. )
  1040. else:
  1041. relations = gap.get_temporal_relations()
  1042. map_list = []
  1043. for relation in relations_order:
  1044. if relation in relations:
  1045. map_list += relations[relation]
  1046. break
  1047. if map_list:
  1048. new_maps = []
  1049. for map in map_list:
  1050. new_map = map.get_new_instance(map.get_id())
  1051. new_map.set_temporal_extent(gap.get_temporal_extent())
  1052. new_map.set_spatial_extent(map.get_spatial_extent())
  1053. new_maps.append(new_map)
  1054. gran_list.append(new_maps)
  1055. else:
  1056. gran_list.append(
  1057. [
  1058. gap,
  1059. ]
  1060. )
  1061. if gran_list:
  1062. return gran_list
  1063. return None
  1064. def get_registered_maps_as_objects_with_gaps(self, where=None, dbif=None):
  1065. """Return all or a subset of the registered maps as
  1066. ordered (by start_time) object list with
  1067. "gap" map objects (id==None) for spatio-temporal topological
  1068. operations that require the spatio-temporal extent only.
  1069. Gaps between maps are identified as maps with id==None
  1070. The objects are initialized with their id's' and the spatio-temporal
  1071. extent (temporal type, start time, end time, west, east, south,
  1072. north, bottom and top).
  1073. In case more map information are needed, use the select()
  1074. method for each listed object.
  1075. :param where: The SQL where statement to select a
  1076. subset of the registered maps without "WHERE"
  1077. :param dbif: The database interface to be used
  1078. :return: ordered object list, in case nothing found None is returned
  1079. """
  1080. dbif, connection_state_changed = init_dbif(dbif)
  1081. obj_list = []
  1082. maps = self.get_registered_maps_as_objects(where, "start_time", dbif)
  1083. if maps is not None and len(maps) > 0:
  1084. for i in range(len(maps)):
  1085. obj_list.append(maps[i])
  1086. # Detect and insert gaps
  1087. if i < len(maps) - 1:
  1088. relation = maps[i + 1].temporal_relation(maps[i])
  1089. if relation == "after":
  1090. start1, end1 = maps[i].get_temporal_extent_as_tuple()
  1091. start2, end2 = maps[i + 1].get_temporal_extent_as_tuple()
  1092. end = start2
  1093. if end1 is not None:
  1094. start = end1
  1095. else:
  1096. start = start1
  1097. map = self.get_new_map_instance(None)
  1098. if self.is_time_absolute():
  1099. map.set_absolute_time(start, end)
  1100. elif self.is_time_relative():
  1101. map.set_relative_time(
  1102. start, end, self.get_relative_time_unit()
  1103. )
  1104. map.set_spatial_extent_from_values(0, 0, 0, 0, 0, 0)
  1105. obj_list.append(copy.copy(map))
  1106. if connection_state_changed:
  1107. dbif.close()
  1108. return obj_list
  1109. def get_registered_maps_as_objects_with_temporal_topology(
  1110. self, where=None, order="start_time", dbif=None
  1111. ):
  1112. """Return all or a subset of the registered maps as ordered object
  1113. list with spatio-temporal topological relationship information.
  1114. The objects are initialized with their id's' and the spatio-temporal
  1115. extent (temporal type, start time, end time, west, east, south,
  1116. north, bottom and top).
  1117. In case more map information are needed, use the select()
  1118. method for each listed object.
  1119. :param where: The SQL where statement to select a subset of
  1120. the registered maps without "WHERE"
  1121. :param order: The SQL order statement to be used to order the
  1122. objects in the list without "ORDER BY"
  1123. :param dbif: The database interface to be used
  1124. :return: The ordered map object list,
  1125. In case nothing found None is returned
  1126. """
  1127. dbif, connection_state_changed = init_dbif(dbif)
  1128. obj_list = self.get_registered_maps_as_objects(where, order, dbif)
  1129. tb = SpatioTemporalTopologyBuilder()
  1130. tb.build(obj_list)
  1131. if connection_state_changed:
  1132. dbif.close()
  1133. return obj_list
  1134. def get_registered_maps_as_objects(self, where=None, order="start_time", dbif=None):
  1135. """Return all or a subset of the registered maps as ordered object
  1136. list for spatio-temporal topological operations that require the
  1137. spatio-temporal extent only
  1138. The objects are initialized with their id's' and the spatio-temporal
  1139. extent (temporal type, start time, end time, west, east, south,
  1140. north, bottom and top).
  1141. In case more map information are needed, use the select()
  1142. method for each listed object.
  1143. :param where: The SQL where statement to select a subset of
  1144. the registered maps without "WHERE"
  1145. :param order: The SQL order statement to be used to order the
  1146. objects in the list without "ORDER BY"
  1147. :param dbif: The database interface to be used
  1148. :return: The ordered map object list,
  1149. In case nothing found None is returned
  1150. """
  1151. dbif, connection_state_changed = init_dbif(dbif)
  1152. obj_list = []
  1153. # Older temporal databases have no bottom and top columns
  1154. # in their views so we need a work around to set the full
  1155. # spatial extent as well
  1156. rows = get_tgis_metadata(dbif)
  1157. db_version = 0
  1158. if rows:
  1159. for row in rows:
  1160. if row["key"] == "tgis_db_version":
  1161. db_version = int(float(row["value"]))
  1162. if db_version >= 1:
  1163. has_bt_columns = True
  1164. columns = "id,start_time,end_time, west,east,south,north,bottom,top"
  1165. else:
  1166. has_bt_columns = False
  1167. columns = "id,start_time,end_time, west,east,south,north"
  1168. rows = self.get_registered_maps(columns, where, order, dbif)
  1169. if rows is not None:
  1170. for row in rows:
  1171. map = self.get_new_map_instance(row["id"])
  1172. if self.is_time_absolute():
  1173. map.set_absolute_time(row["start_time"], row["end_time"])
  1174. elif self.is_time_relative():
  1175. map.set_relative_time(
  1176. row["start_time"],
  1177. row["end_time"],
  1178. self.get_relative_time_unit(),
  1179. )
  1180. # The fast way
  1181. if has_bt_columns:
  1182. map.set_spatial_extent_from_values(
  1183. west=row["west"],
  1184. east=row["east"],
  1185. south=row["south"],
  1186. top=row["top"],
  1187. north=row["north"],
  1188. bottom=row["bottom"],
  1189. )
  1190. # The slow work around
  1191. else:
  1192. map.spatial_extent.select(dbif)
  1193. obj_list.append(copy.copy(map))
  1194. if connection_state_changed:
  1195. dbif.close()
  1196. return obj_list
  1197. def _update_where_statement_by_band_reference(self, where):
  1198. """Update given SQL WHERE statement by band reference.
  1199. Call this method only when self.band_reference is defined.
  1200. :param str where: SQL WHERE statement to be updated
  1201. :return: updated SQL WHERE statement
  1202. """
  1203. def leading_zero(value):
  1204. try:
  1205. if value.startswith("0"):
  1206. return value.lstrip("0")
  1207. else:
  1208. return "{0:02d}".format(int(value))
  1209. except ValueError:
  1210. return value
  1211. return None
  1212. # initialized WHERE statement
  1213. if where:
  1214. where += " AND "
  1215. else:
  1216. where = ""
  1217. # be case-insensitive
  1218. if "_" in self.band_reference:
  1219. # fully-qualified band reference
  1220. where += "band_reference IN ('{}'".format(self.band_reference.upper())
  1221. # be zero-padding less sensitive
  1222. shortcut, identifier = self.band_reference.split("_", -1)
  1223. identifier_zp = leading_zero(identifier)
  1224. if identifier_zp:
  1225. where += ", '{fl}_{zp}'".format(
  1226. fl=shortcut.upper(), zp=identifier_zp.upper()
  1227. )
  1228. # close WHERE statement
  1229. where += ")"
  1230. else:
  1231. # shortcut or band identifier given
  1232. shortcut_identifier = leading_zero(self.band_reference)
  1233. if shortcut_identifier:
  1234. where += (
  1235. "{br} LIKE '{si}\_%' {esc} OR {br} LIKE '%\_{si}' {esc} OR "
  1236. "{br} LIKE '{orig}\_%' {esc} OR {br} LIKE '%\_{orig}' {esc}".format(
  1237. br="band_reference",
  1238. si=shortcut_identifier,
  1239. orig=self.band_reference.upper(),
  1240. esc="ESCAPE '\\'",
  1241. )
  1242. )
  1243. else:
  1244. where += "band_reference = '{}'".format(self.band_reference)
  1245. return where
  1246. def get_registered_maps(self, columns=None, where=None, order=None, dbif=None):
  1247. """Return SQL rows of all registered maps.
  1248. In case columns are not specified, each row includes all columns
  1249. specified in the datatype specific view.
  1250. :param columns: Columns to be selected as SQL compliant string
  1251. :param where: The SQL where statement to select a subset
  1252. of the registered maps without "WHERE"
  1253. :param order: The SQL order statement to be used to order the
  1254. objects in the list without "ORDER BY"
  1255. :param dbif: The database interface to be used
  1256. :return: SQL rows of all registered maps,
  1257. In case nothing found None is returned
  1258. """
  1259. dbif, connection_state_changed = init_dbif(dbif)
  1260. rows = None
  1261. if self.get_map_register() is not None:
  1262. # Use the correct temporal table
  1263. if self.get_temporal_type() == "absolute":
  1264. map_view = self.get_new_map_instance(None).get_type() + "_view_abs_time"
  1265. else:
  1266. map_view = self.get_new_map_instance(None).get_type() + "_view_rel_time"
  1267. if columns is not None and columns != "":
  1268. sql = "SELECT %s FROM %s WHERE %s.id IN (SELECT id FROM %s)" % (
  1269. columns,
  1270. map_view,
  1271. map_view,
  1272. self.get_map_register(),
  1273. )
  1274. else:
  1275. sql = "SELECT * FROM %s WHERE %s.id IN (SELECT id FROM %s)" % (
  1276. map_view,
  1277. map_view,
  1278. self.get_map_register(),
  1279. )
  1280. # filter by band reference identifier
  1281. if self.band_reference:
  1282. where = self._update_where_statement_by_band_reference(where)
  1283. if where is not None and where != "":
  1284. sql += " AND (%s)" % (where.split(";")[0])
  1285. if order is not None and order != "":
  1286. sql += " ORDER BY %s" % (order.split(";")[0])
  1287. try:
  1288. dbif.execute(sql, mapset=self.base.mapset)
  1289. rows = dbif.fetchall(mapset=self.base.mapset)
  1290. except:
  1291. if connection_state_changed:
  1292. dbif.close()
  1293. self.msgr.error(
  1294. _("Unable to get map ids from register table " "<%s>")
  1295. % (self.get_map_register())
  1296. )
  1297. raise
  1298. if connection_state_changed:
  1299. dbif.close()
  1300. return rows
  1301. @staticmethod
  1302. def shift_map_list(maps, gran):
  1303. """Temporally shift each map in the list with the provided granularity
  1304. This method does not perform any temporal database operations.
  1305. :param maps: A list of maps with initialized temporal extent
  1306. :param gran: The granularity to be used for shifting
  1307. :return: The modified map list, None if nothing to shift or wrong
  1308. granularity
  1309. .. code-block:: python
  1310. >>> import grass.temporal as tgis
  1311. >>> maps = []
  1312. >>> for i in range(5):
  1313. ... map = tgis.RasterDataset(None)
  1314. ... if i%2 == 0:
  1315. ... check = map.set_relative_time(i, i + 1, 'years')
  1316. ... else:
  1317. ... check = map.set_relative_time(i, None, 'years')
  1318. ... maps.append(map)
  1319. >>> for map in maps:
  1320. ... map.temporal_extent.print_info()
  1321. +-------------------- Relative time -----------------------------------------+
  1322. | Start time:................. 0
  1323. | End time:................... 1
  1324. | Relative time unit:......... years
  1325. +-------------------- Relative time -----------------------------------------+
  1326. | Start time:................. 1
  1327. | End time:................... None
  1328. | Relative time unit:......... years
  1329. +-------------------- Relative time -----------------------------------------+
  1330. | Start time:................. 2
  1331. | End time:................... 3
  1332. | Relative time unit:......... years
  1333. +-------------------- Relative time -----------------------------------------+
  1334. | Start time:................. 3
  1335. | End time:................... None
  1336. | Relative time unit:......... years
  1337. +-------------------- Relative time -----------------------------------------+
  1338. | Start time:................. 4
  1339. | End time:................... 5
  1340. | Relative time unit:......... years
  1341. >>> maps = tgis.AbstractSpaceTimeDataset.shift_map_list(maps, 5)
  1342. >>> for map in maps:
  1343. ... map.temporal_extent.print_info()
  1344. +-------------------- Relative time -----------------------------------------+
  1345. | Start time:................. 5
  1346. | End time:................... 6
  1347. | Relative time unit:......... years
  1348. +-------------------- Relative time -----------------------------------------+
  1349. | Start time:................. 6
  1350. | End time:................... None
  1351. | Relative time unit:......... years
  1352. +-------------------- Relative time -----------------------------------------+
  1353. | Start time:................. 7
  1354. | End time:................... 8
  1355. | Relative time unit:......... years
  1356. +-------------------- Relative time -----------------------------------------+
  1357. | Start time:................. 8
  1358. | End time:................... None
  1359. | Relative time unit:......... years
  1360. +-------------------- Relative time -----------------------------------------+
  1361. | Start time:................. 9
  1362. | End time:................... 10
  1363. | Relative time unit:......... years
  1364. """
  1365. if maps is None:
  1366. return None
  1367. if not check_granularity_string(gran, maps[-1].get_temporal_type()):
  1368. return None
  1369. for map in maps:
  1370. start, end = map.get_temporal_extent_as_tuple()
  1371. if map.is_time_absolute():
  1372. start = increment_datetime_by_string(start, gran)
  1373. if end is not None:
  1374. end = increment_datetime_by_string(end, gran)
  1375. map.set_absolute_time(start, end)
  1376. elif map.is_time_relative():
  1377. start = start + int(gran)
  1378. if end is not None:
  1379. end = end + int(gran)
  1380. map.set_relative_time(start, end, map.get_relative_time_unit())
  1381. return maps
  1382. def shift(self, gran, dbif=None):
  1383. """Temporally shift each registered map with the provided granularity
  1384. :param gran: The granularity to be used for shifting
  1385. :param dbif: The database interface to be used
  1386. :return: True something to shift, False if nothing to shift or wrong
  1387. granularity
  1388. """
  1389. if (
  1390. get_enable_mapset_check() is True
  1391. and self.get_mapset() != get_current_mapset()
  1392. ):
  1393. self.msgr.fatal(
  1394. _(
  1395. "Unable to shift dataset <%(ds)s> of type "
  1396. "%(type)s in the temporal database. The mapset "
  1397. "of the dataset does not match the current "
  1398. "mapset"
  1399. )
  1400. % ({"ds": self.get_id()}, {"type": self.get_type()})
  1401. )
  1402. if not check_granularity_string(gran, self.get_temporal_type()):
  1403. self.msgr.error(_("Wrong granularity format: %s" % (gran)))
  1404. return False
  1405. dbif, connection_state_changed = init_dbif(dbif)
  1406. maps = self.get_registered_maps_as_objects(dbif=dbif)
  1407. if maps is None:
  1408. return False
  1409. date_list = []
  1410. # We need to make a dry run to avoid a break
  1411. # in the middle of the update process when the increment
  1412. # results in wrong number of days in a month
  1413. for map in maps:
  1414. start, end = map.get_temporal_extent_as_tuple()
  1415. if self.is_time_absolute():
  1416. start = increment_datetime_by_string(start, gran)
  1417. if end is not None:
  1418. end = increment_datetime_by_string(end, gran)
  1419. elif self.is_time_relative():
  1420. start = start + int(gran)
  1421. if end is not None:
  1422. end = end + int(gran)
  1423. date_list.append((start, end))
  1424. self._update_map_timestamps(maps, date_list, dbif)
  1425. if connection_state_changed:
  1426. dbif.close()
  1427. @staticmethod
  1428. def snap_map_list(maps):
  1429. """For each map in the list snap the end time to the start time of its
  1430. temporal nearest neighbor in the future.
  1431. Maps with equal time stamps are not snapped.
  1432. The granularity of the map list will be used to create the end time
  1433. of the last map in case it has a time instance as timestamp.
  1434. This method does not perform any temporal database operations.
  1435. :param maps: A list of maps with initialized temporal extent
  1436. :return: The modified map list, None nothing to shift or wrong
  1437. granularity
  1438. Usage:
  1439. .. code-block:: python
  1440. >>> import grass.temporal as tgis
  1441. >>> maps = []
  1442. >>> for i in range(5):
  1443. ... map = tgis.RasterDataset(None)
  1444. ... if i%2 == 0:
  1445. ... check = map.set_relative_time(i, i + 1, 'years')
  1446. ... else:
  1447. ... check = map.set_relative_time(i, None, 'years')
  1448. ... maps.append(map)
  1449. >>> for map in maps:
  1450. ... map.temporal_extent.print_info()
  1451. +-------------------- Relative time -----------------------------------------+
  1452. | Start time:................. 0
  1453. | End time:................... 1
  1454. | Relative time unit:......... years
  1455. +-------------------- Relative time -----------------------------------------+
  1456. | Start time:................. 1
  1457. | End time:................... None
  1458. | Relative time unit:......... years
  1459. +-------------------- Relative time -----------------------------------------+
  1460. | Start time:................. 2
  1461. | End time:................... 3
  1462. | Relative time unit:......... years
  1463. +-------------------- Relative time -----------------------------------------+
  1464. | Start time:................. 3
  1465. | End time:................... None
  1466. | Relative time unit:......... years
  1467. +-------------------- Relative time -----------------------------------------+
  1468. | Start time:................. 4
  1469. | End time:................... 5
  1470. | Relative time unit:......... years
  1471. >>> maps = tgis.AbstractSpaceTimeDataset.snap_map_list(maps)
  1472. >>> for map in maps:
  1473. ... map.temporal_extent.print_info()
  1474. +-------------------- Relative time -----------------------------------------+
  1475. | Start time:................. 0
  1476. | End time:................... 1
  1477. | Relative time unit:......... years
  1478. +-------------------- Relative time -----------------------------------------+
  1479. | Start time:................. 1
  1480. | End time:................... 2
  1481. | Relative time unit:......... years
  1482. +-------------------- Relative time -----------------------------------------+
  1483. | Start time:................. 2
  1484. | End time:................... 3
  1485. | Relative time unit:......... years
  1486. +-------------------- Relative time -----------------------------------------+
  1487. | Start time:................. 3
  1488. | End time:................... 4
  1489. | Relative time unit:......... years
  1490. +-------------------- Relative time -----------------------------------------+
  1491. | Start time:................. 4
  1492. | End time:................... 5
  1493. | Relative time unit:......... years
  1494. """
  1495. if maps is None or len(maps) == 0:
  1496. return None
  1497. # We need to sort the maps temporally by start time
  1498. maps = sorted(maps, key=AbstractDatasetComparisonKeyStartTime)
  1499. for i in range(len(maps) - 1):
  1500. start, end = maps[i].get_temporal_extent_as_tuple()
  1501. start_next, end = maps[i + 1].get_temporal_extent_as_tuple()
  1502. # Maps with equal time stamps can not be snapped
  1503. if start != start_next:
  1504. if maps[i].is_time_absolute():
  1505. maps[i].set_absolute_time(start, start_next)
  1506. elif maps[i].is_time_relative():
  1507. maps[i].set_relative_time(
  1508. start, start_next, maps[i].get_relative_time_unit()
  1509. )
  1510. else:
  1511. if maps[i].is_time_absolute():
  1512. maps[i].set_absolute_time(start, end)
  1513. elif maps[i].is_time_relative():
  1514. maps[i].set_relative_time(
  1515. start, end, maps[i].get_relative_time_unit()
  1516. )
  1517. # Last map
  1518. start, end = maps[-1].get_temporal_extent_as_tuple()
  1519. # We increment the start time with the dataset
  1520. # granularity if the end time is None
  1521. if end is None:
  1522. if maps[-1].is_time_absolute():
  1523. gran = compute_absolute_time_granularity(maps)
  1524. end = increment_datetime_by_string(start, gran)
  1525. maps[-1].set_absolute_time(start, end)
  1526. elif maps[-1].is_time_relative():
  1527. gran = compute_relative_time_granularity(maps)
  1528. end = start + gran
  1529. maps[-1].set_relative_time(
  1530. start, end, maps[-1].get_relative_time_unit()
  1531. )
  1532. return maps
  1533. def snap(self, dbif=None):
  1534. """For each registered map snap the end time to the start time of
  1535. its temporal nearest neighbor in the future
  1536. Maps with equal time stamps are not snapped
  1537. :param dbif: The database interface to be used
  1538. """
  1539. if (
  1540. get_enable_mapset_check() is True
  1541. and self.get_mapset() != get_current_mapset()
  1542. ):
  1543. self.msgr.fatal(
  1544. _(
  1545. "Unable to snap dataset <%(ds)s> of type "
  1546. "%(type)s in the temporal database. The mapset "
  1547. "of the dataset does not match the current "
  1548. "mapset"
  1549. )
  1550. % ({"ds": self.get_id()}, {"type": self.get_type()})
  1551. )
  1552. dbif, connection_state_changed = init_dbif(dbif)
  1553. maps = self.get_registered_maps_as_objects(dbif=dbif)
  1554. if maps is None:
  1555. return
  1556. date_list = []
  1557. for i in range(len(maps) - 1):
  1558. start, end = maps[i].get_temporal_extent_as_tuple()
  1559. start_next, end = maps[i + 1].get_temporal_extent_as_tuple()
  1560. # Maps with equal time stamps can not be snapped
  1561. if start != start_next:
  1562. date_list.append((start, start_next))
  1563. else:
  1564. # Keep the original time stamps
  1565. date_list.append((start, end))
  1566. # Last map
  1567. start, end = maps[-1].get_temporal_extent_as_tuple()
  1568. # We increment the start time with the dataset
  1569. # granularity if the end time is None
  1570. if end is None:
  1571. if self.is_time_absolute():
  1572. end = increment_datetime_by_string(start, self.get_granularity())
  1573. elif self.is_time_relative():
  1574. end = start + self.get_granularity()
  1575. date_list.append((start, end))
  1576. self._update_map_timestamps(maps, date_list, dbif)
  1577. if connection_state_changed:
  1578. dbif.close()
  1579. def _update_map_timestamps(self, maps, date_list, dbif):
  1580. """Update the timestamps of maps with the start and end time
  1581. stored in the date_list.
  1582. The number of dates in the list must be equal to the number
  1583. of maps.
  1584. :param maps: A list of map objects
  1585. :param date_list: A list with date tuples (start_time, end_time)
  1586. :param dbif: The database interface to be used
  1587. """
  1588. datatsets_to_modify = {}
  1589. # Now update the maps
  1590. count = 0
  1591. for map in maps:
  1592. start = date_list[count][0]
  1593. end = date_list[count][1]
  1594. map.select(dbif)
  1595. count += 1
  1596. if self.is_time_absolute():
  1597. map.update_absolute_time(start_time=start, end_time=end, dbif=dbif)
  1598. elif self.is_time_relative():
  1599. map.update_relative_time(
  1600. start_time=start,
  1601. end_time=end,
  1602. unit=self.get_relative_time_unit(),
  1603. dbif=dbif,
  1604. )
  1605. # Save the datasets that must be updated
  1606. datasets = map.get_registered_stds(dbif)
  1607. if datasets:
  1608. for dataset in datasets:
  1609. datatsets_to_modify[dataset] = dataset
  1610. self.update_from_registered_maps(dbif)
  1611. # Update affected datasets
  1612. if datatsets_to_modify:
  1613. for dataset in datatsets_to_modify:
  1614. if dataset != self.get_id():
  1615. ds = self.get_new_instance(ident=dataset)
  1616. ds.select(dbif)
  1617. ds.update_from_registered_maps(dbif)
  1618. def rename(self, ident, dbif=None):
  1619. """Rename the space time dataset
  1620. This method renames the space time dataset, the map register table
  1621. and updates the entries in registered maps stds register.
  1622. Renaming does not work with Postgresql yet.
  1623. :param ident: The new identifier "name@mapset"
  1624. :param dbif: The database interface to be used
  1625. """
  1626. if (
  1627. get_enable_mapset_check() is True
  1628. and self.get_mapset() != get_current_mapset()
  1629. ):
  1630. self.msgr.fatal(
  1631. _(
  1632. "Unable to rename dataset <%(ds)s> of type "
  1633. "%(type)s in the temporal database. The mapset "
  1634. "of the dataset does not match the current "
  1635. "mapset"
  1636. )
  1637. % ({"ds": self.get_id()}, {"type": self.get_type()})
  1638. )
  1639. dbif, connection_state_changed = init_dbif(dbif)
  1640. if dbif.get_dbmi().__name__ != "sqlite3":
  1641. self.msgr.fatal(
  1642. _("Renaming of space time datasets is not " "supported for PostgreSQL.")
  1643. )
  1644. # SELECT all needed information from the database
  1645. self.select(dbif)
  1646. # We need to select the registered maps here
  1647. maps = self.get_registered_maps_as_objects(None, "start_time", dbif)
  1648. # Safe old identifier
  1649. old_ident = self.get_id()
  1650. # We need to rename the old table
  1651. old_map_register_table = self.get_map_register()
  1652. # Set new identifier
  1653. self.set_id(ident)
  1654. # Create map register table name from new identifier
  1655. new_map_register_table = self.create_map_register_name()
  1656. # Set new map register table name
  1657. self.set_map_register(new_map_register_table)
  1658. # Get the update statement, we update the table entry of the old
  1659. # identifier
  1660. statement = self.update(dbif, execute=False, ident=old_ident)
  1661. # We need to rename the raster register table
  1662. statement += 'ALTER TABLE %s RENAME TO "%s";\n' % (
  1663. old_map_register_table,
  1664. new_map_register_table,
  1665. )
  1666. # We need to take care of the stds index in the sqlite3 database
  1667. if dbif.get_dbmi().__name__ == "sqlite3":
  1668. statement += "DROP INDEX %s_index;\n" % (old_map_register_table)
  1669. statement += "CREATE INDEX %s_index ON %s (id);" % (
  1670. new_map_register_table,
  1671. new_map_register_table,
  1672. )
  1673. # We need to rename the space time dataset in the maps register table
  1674. if maps:
  1675. for map in maps:
  1676. map.remove_stds_from_register(stds_id=old_ident, dbif=dbif)
  1677. map.add_stds_to_register(stds_id=ident, dbif=dbif)
  1678. # Execute the accumulated statements
  1679. dbif.execute_transaction(statement)
  1680. if connection_state_changed:
  1681. dbif.close()
  1682. def delete(self, dbif=None, execute=True):
  1683. """Delete a space time dataset from the temporal database
  1684. This method removes the space time dataset from the temporal
  1685. database and drops its map register table
  1686. :param dbif: The database interface to be used
  1687. :param execute: If True the SQL DELETE and DROP table
  1688. statements will be executed.
  1689. If False the prepared SQL statements are returned
  1690. and must be executed by the caller.
  1691. :return: The SQL statements if execute == False, else an empty
  1692. string
  1693. """
  1694. # First we need to check if maps are registered in this dataset and
  1695. # unregister them
  1696. self.msgr.verbose(
  1697. _("Delete space time %s dataset <%s> from temporal " "database")
  1698. % (self.get_new_map_instance(ident=None).get_type(), self.get_id())
  1699. )
  1700. if (
  1701. get_enable_mapset_check() is True
  1702. and self.get_mapset() != get_current_mapset()
  1703. ):
  1704. self.msgr.fatal(
  1705. _(
  1706. "Unable to delete dataset <%(ds)s> of type "
  1707. "%(type)s from the temporal database. The mapset"
  1708. " of the dataset does not match the current "
  1709. "mapset"
  1710. )
  1711. % {"ds": self.get_id(), "type": self.get_type()}
  1712. )
  1713. statement = ""
  1714. dbif, connection_state_changed = init_dbif(dbif)
  1715. # SELECT all needed information from the database
  1716. self.metadata.select(dbif)
  1717. if self.get_map_register() is not None:
  1718. self.msgr.debug(
  1719. 1, _("Drop map register table: %s") % (self.get_map_register())
  1720. )
  1721. rows = self.get_registered_maps("id", None, None, dbif)
  1722. # Unregister each registered map in the table
  1723. if rows is not None:
  1724. for row in rows:
  1725. # Unregister map
  1726. map = self.get_new_map_instance(row["id"])
  1727. statement += self.unregister_map(map=map, dbif=dbif, execute=False)
  1728. # Safe the DROP table statement
  1729. statement += "DROP TABLE IF EXISTS " + self.get_map_register() + ";\n"
  1730. # Remove the primary key, the foreign keys will be removed by trigger
  1731. statement += self.base.get_delete_statement()
  1732. if execute:
  1733. dbif.execute_transaction(statement)
  1734. self.reset(None)
  1735. if connection_state_changed:
  1736. dbif.close()
  1737. if execute:
  1738. return ""
  1739. return statement
  1740. def is_map_registered(self, map_id, dbif=None):
  1741. """Check if a map is registered in the space time dataset
  1742. :param map_id: The map id
  1743. :param dbif: The database interface to be used
  1744. :return: True if success, False otherwise
  1745. """
  1746. stds_register_table = self.get_map_register()
  1747. dbif, connection_state_changed = init_dbif(dbif)
  1748. is_registered = False
  1749. # Check if map is already registered
  1750. if stds_register_table is not None:
  1751. if dbif.get_dbmi().paramstyle == "qmark":
  1752. sql = "SELECT id FROM " + stds_register_table + " WHERE id = (?)"
  1753. else:
  1754. sql = "SELECT id FROM " + stds_register_table + " WHERE id = (%s)"
  1755. try:
  1756. dbif.execute(sql, (map_id,), mapset=self.base.mapset)
  1757. row = dbif.fetchone(mapset=self.base.mapset)
  1758. except:
  1759. self.msgr.warning(_("Error in register table request"))
  1760. raise
  1761. if row is not None and row[0] == map_id:
  1762. is_registered = True
  1763. if connection_state_changed is True:
  1764. dbif.close()
  1765. return is_registered
  1766. def register_map(self, map, dbif=None):
  1767. """Register a map in the space time dataset.
  1768. This method takes care of the registration of a map
  1769. in a space time dataset.
  1770. In case the map is already registered this function
  1771. will break with a warning and return False.
  1772. This method raises a FatalError exception in case of a fatal error
  1773. :param map: The AbstractMapDataset object that should be registered
  1774. :param dbif: The database interface to be used
  1775. :return: True if success, False otherwise
  1776. """
  1777. if (
  1778. get_enable_mapset_check() is True
  1779. and self.get_mapset() != get_current_mapset()
  1780. ):
  1781. self.msgr.fatal(
  1782. _(
  1783. "Unable to register map in dataset <%(ds)s> of "
  1784. "type %(type)s. The mapset of the dataset does "
  1785. "not match the current mapset"
  1786. )
  1787. % {"ds": self.get_id(), "type": self.get_type()}
  1788. )
  1789. dbif, connection_state_changed = init_dbif(dbif)
  1790. if map.is_in_db(dbif) is False:
  1791. dbif.close()
  1792. self.msgr.fatal(
  1793. _(
  1794. "Only a map that was inserted in the temporal "
  1795. "database can be registered in a space time "
  1796. "dataset"
  1797. )
  1798. )
  1799. if map.get_layer():
  1800. self.msgr.debug(
  1801. 1,
  1802. "Register %s map <%s> with layer %s in space "
  1803. "time %s dataset <%s>"
  1804. % (
  1805. map.get_type(),
  1806. map.get_map_id(),
  1807. map.get_layer(),
  1808. map.get_type(),
  1809. self.get_id(),
  1810. ),
  1811. )
  1812. else:
  1813. self.msgr.debug(
  1814. 1,
  1815. "Register %s map <%s> in space time %s "
  1816. "dataset <%s>"
  1817. % (map.get_type(), map.get_map_id(), map.get_type(), self.get_id()),
  1818. )
  1819. # First select all data from the database
  1820. map.select(dbif)
  1821. if not map.check_for_correct_time():
  1822. if map.get_layer():
  1823. self.msgr.fatal(
  1824. _("Map <%(id)s> with layer %(l)s has invalid " "time")
  1825. % {"id": map.get_map_id(), "l": map.get_layer()}
  1826. )
  1827. else:
  1828. self.msgr.fatal(_("Map <%s> has invalid time") % (map.get_map_id()))
  1829. # Get basic info
  1830. map_id = map.base.get_id()
  1831. map_mapset = map.base.get_mapset()
  1832. map_rel_time_unit = map.get_relative_time_unit()
  1833. map_ttype = map.get_temporal_type()
  1834. stds_mapset = self.base.get_mapset()
  1835. stds_register_table = self.get_map_register()
  1836. stds_ttype = self.get_temporal_type()
  1837. # The gathered SQL statemets are stroed here
  1838. statement = ""
  1839. # Check temporal types
  1840. if stds_ttype != map_ttype:
  1841. if map.get_layer():
  1842. self.msgr.fatal(
  1843. _(
  1844. "Temporal type of space time dataset "
  1845. "<%(id)s> and map <%(map)s> with layer %(l)s"
  1846. " are different"
  1847. )
  1848. % {
  1849. "id": self.get_id(),
  1850. "map": map.get_map_id(),
  1851. "l": map.get_layer(),
  1852. }
  1853. )
  1854. else:
  1855. self.msgr.fatal(
  1856. _(
  1857. "Temporal type of space time dataset "
  1858. "<%(id)s> and map <%(map)s> are different"
  1859. )
  1860. % {"id": self.get_id(), "map": map.get_map_id()}
  1861. )
  1862. # In case no map has been registered yet, set the
  1863. # relative time unit from the first map
  1864. if (
  1865. (
  1866. self.metadata.get_number_of_maps() is None
  1867. or self.metadata.get_number_of_maps() == 0
  1868. )
  1869. and self.map_counter == 0
  1870. and self.is_time_relative()
  1871. ):
  1872. self.set_relative_time_unit(map_rel_time_unit)
  1873. statement += self.relative_time.get_update_all_statement_mogrified(dbif)
  1874. self.msgr.debug(
  1875. 1,
  1876. _("Set temporal unit for space time %s dataset " "<%s> to %s")
  1877. % (map.get_type(), self.get_id(), map_rel_time_unit),
  1878. )
  1879. stds_rel_time_unit = self.get_relative_time_unit()
  1880. # Check the relative time unit
  1881. if self.is_time_relative() and (stds_rel_time_unit != map_rel_time_unit):
  1882. if map.get_layer():
  1883. self.msgr.fatal(
  1884. _(
  1885. "Relative time units of space time dataset "
  1886. "<%(id)s> and map <%(map)s> with layer %(l)s"
  1887. " are different"
  1888. )
  1889. % {
  1890. "id": self.get_id(),
  1891. "map": map.get_map_id(),
  1892. "l": map.get_layer(),
  1893. }
  1894. )
  1895. else:
  1896. self.msgr.fatal(
  1897. _(
  1898. "Relative time units of space time dataset "
  1899. "<%(id)s> and map <%(map)s> are different"
  1900. )
  1901. % {"id": self.get_id(), "map": map.get_map_id()}
  1902. )
  1903. if get_enable_mapset_check() is True and stds_mapset != map_mapset:
  1904. dbif.close()
  1905. self.msgr.fatal(_("Only maps from the same mapset can be registered"))
  1906. # Check if map is already registered
  1907. if self.is_map_registered(map_id, dbif=dbif):
  1908. if map.get_layer() is not None:
  1909. self.msgr.warning(
  1910. _("Map <%(map)s> with layer %(l)s is already" " registered.")
  1911. % {"map": map.get_map_id(), "l": map.get_layer()}
  1912. )
  1913. else:
  1914. self.msgr.warning(
  1915. _("Map <%s> is already registered.") % (map.get_map_id())
  1916. )
  1917. return False
  1918. # Register the stds in the map stds register table column
  1919. statement += map.add_stds_to_register(
  1920. stds_id=self.base.get_id(), dbif=dbif, execute=False
  1921. )
  1922. # Now put the raster name in the stds map register table
  1923. if dbif.get_dbmi().paramstyle == "qmark":
  1924. sql = "INSERT INTO " + stds_register_table + " (id) " + "VALUES (?);\n"
  1925. else:
  1926. sql = "INSERT INTO " + stds_register_table + " (id) " + "VALUES (%s);\n"
  1927. statement += dbif.mogrify_sql_statement((sql, (map_id,)))
  1928. # Now execute the insert transaction
  1929. dbif.execute_transaction(statement)
  1930. if connection_state_changed:
  1931. dbif.close()
  1932. # increase the counter
  1933. self.map_counter += 1
  1934. return True
  1935. def unregister_map(self, map, dbif=None, execute=True):
  1936. """Unregister a map from the space time dataset.
  1937. This method takes care of the un-registration of a map
  1938. from a space time dataset.
  1939. :param map: The map object to unregister
  1940. :param dbif: The database interface to be used
  1941. :param execute: If True the SQL DELETE and DROP table
  1942. statements will be executed.
  1943. If False the prepared SQL statements are
  1944. returned and must be executed by the caller.
  1945. :return: The SQL statements if execute == False, else an empty
  1946. string, None in case of a failure
  1947. """
  1948. if (
  1949. get_enable_mapset_check() is True
  1950. and self.get_mapset() != get_current_mapset()
  1951. ):
  1952. self.msgr.fatal(
  1953. _(
  1954. "Unable to unregister map from dataset <%(ds)s>"
  1955. " of type %(type)s in the temporal database."
  1956. " The mapset of the dataset does not match the"
  1957. " current mapset"
  1958. )
  1959. % {"ds": self.get_id(), "type": self.get_type()}
  1960. )
  1961. statement = ""
  1962. dbif, connection_state_changed = init_dbif(dbif)
  1963. # Check if the map is registered in the space time raster dataset
  1964. if self.is_map_registered(map.get_id(), dbif) is False:
  1965. if map.get_layer() is not None:
  1966. self.msgr.warning(
  1967. _(
  1968. "Map <%(map)s> with layer %(l)s is not "
  1969. "registered in space time dataset "
  1970. "<%(base)s>"
  1971. )
  1972. % {
  1973. "map": map.get_map_id(),
  1974. "l": map.get_layer(),
  1975. "base": self.base.get_id(),
  1976. }
  1977. )
  1978. else:
  1979. self.msgr.warning(
  1980. _(
  1981. "Map <%(map)s> is not registered in space "
  1982. "time dataset <%(base)s>"
  1983. )
  1984. % {"map": map.get_map_id(), "base": self.base.get_id()}
  1985. )
  1986. if connection_state_changed is True:
  1987. dbif.close()
  1988. return ""
  1989. # Remove the space time dataset from the dataset register
  1990. # We need to execute the statement here, otherwise the space time
  1991. # dataset will not be removed correctly
  1992. map.remove_stds_from_register(self.base.get_id(), dbif=dbif, execute=True)
  1993. # Remove the map from the space time dataset register
  1994. stds_register_table = self.get_map_register()
  1995. if stds_register_table is not None:
  1996. if dbif.get_dbmi().paramstyle == "qmark":
  1997. sql = "DELETE FROM " + stds_register_table + " WHERE id = ?;\n"
  1998. else:
  1999. sql = "DELETE FROM " + stds_register_table + " WHERE id = %s;\n"
  2000. statement += dbif.mogrify_sql_statement((sql, (map.get_id(),)))
  2001. if execute:
  2002. dbif.execute_transaction(statement)
  2003. statement = ""
  2004. if connection_state_changed:
  2005. dbif.close()
  2006. # decrease the counter
  2007. self.map_counter -= 1
  2008. return statement
  2009. def update_from_registered_maps(self, dbif=None):
  2010. """This methods updates the modification time, the spatial and
  2011. temporal extent as well as type specific metadata. It should always
  2012. been called after maps are registered or unregistered/deleted from
  2013. the space time dataset.
  2014. The update of the temporal extent checks if the end time is set
  2015. correctly.
  2016. In case the registered maps have no valid end time (None) the
  2017. maximum start time
  2018. will be used. If the end time is earlier than the maximum start
  2019. time, it will be replaced by the maximum start time.
  2020. :param dbif: The database interface to be used
  2021. """
  2022. if (
  2023. get_enable_mapset_check() is True
  2024. and self.get_mapset() != get_current_mapset()
  2025. ):
  2026. self.msgr.fatal(
  2027. _(
  2028. "Unable to update dataset <%(ds)s> of type "
  2029. "%(type)s in the temporal database. The mapset"
  2030. " of the dataset does not match the current "
  2031. "mapset"
  2032. )
  2033. % {"ds": self.get_id(), "type": self.get_type()}
  2034. )
  2035. self.msgr.verbose(
  2036. _(
  2037. "Update metadata, spatial and temporal extent from"
  2038. " all registered maps of <%s>"
  2039. )
  2040. % (self.get_id())
  2041. )
  2042. # Nothing to do if the map register is not present
  2043. if not self.get_map_register():
  2044. return
  2045. dbif, connection_state_changed = init_dbif(dbif)
  2046. map_time = None
  2047. use_start_time = False
  2048. # Get basic info
  2049. stds_name = self.base.get_name()
  2050. stds_mapset = self.base.get_mapset()
  2051. sql_path = get_sql_template_path()
  2052. stds_register_table = self.get_map_register()
  2053. # We create a transaction
  2054. sql_script = ""
  2055. # Update the spatial and temporal extent from registered maps
  2056. # Read the SQL template
  2057. sql = open(
  2058. os.path.join(sql_path, "update_stds_spatial_temporal_extent_template.sql"),
  2059. "r",
  2060. ).read()
  2061. sql = sql.replace("GRASS_MAP", self.get_new_map_instance(None).get_type())
  2062. sql = sql.replace("SPACETIME_REGISTER_TABLE", stds_register_table)
  2063. sql = sql.replace("SPACETIME_ID", self.base.get_id())
  2064. sql = sql.replace("STDS", self.get_type())
  2065. sql_script += sql
  2066. sql_script += "\n"
  2067. # Update type specific metadata
  2068. sql = open(
  2069. os.path.join(
  2070. sql_path, "update_" + self.get_type() + "_metadata_template.sql"
  2071. ),
  2072. "r",
  2073. ).read()
  2074. sql = sql.replace("SPACETIME_REGISTER_TABLE", stds_register_table)
  2075. sql = sql.replace("SPACETIME_ID", self.base.get_id())
  2076. sql_script += sql
  2077. sql_script += "\n"
  2078. dbif.execute_transaction(sql_script)
  2079. # Read and validate the selected end time
  2080. self.select(dbif)
  2081. if self.is_time_absolute():
  2082. start_time, end_time = self.get_absolute_time()
  2083. else:
  2084. start_time, end_time, unit = self.get_relative_time()
  2085. # In case no end time is set, use the maximum start time of
  2086. # all registered maps as end time
  2087. if end_time is None:
  2088. use_start_time = True
  2089. else:
  2090. # Check if the end time is smaller than the maximum start time
  2091. if self.is_time_absolute():
  2092. sql = """SELECT max(start_time) FROM GRASS_MAP_absolute_time
  2093. WHERE GRASS_MAP_absolute_time.id IN
  2094. (SELECT id FROM SPACETIME_REGISTER_TABLE);"""
  2095. sql = sql.replace(
  2096. "GRASS_MAP", self.get_new_map_instance(None).get_type()
  2097. )
  2098. sql = sql.replace("SPACETIME_REGISTER_TABLE", stds_register_table)
  2099. else:
  2100. sql = """SELECT max(start_time) FROM GRASS_MAP_relative_time
  2101. WHERE GRASS_MAP_relative_time.id IN
  2102. (SELECT id FROM SPACETIME_REGISTER_TABLE);"""
  2103. sql = sql.replace(
  2104. "GRASS_MAP", self.get_new_map_instance(None).get_type()
  2105. )
  2106. sql = sql.replace("SPACETIME_REGISTER_TABLE", stds_register_table)
  2107. dbif.execute(sql, mapset=self.base.mapset)
  2108. row = dbif.fetchone(mapset=self.base.mapset)
  2109. if row is not None:
  2110. # This seems to be a bug in sqlite3 Python driver
  2111. if dbif.get_dbmi().__name__ == "sqlite3":
  2112. tstring = row[0]
  2113. # Convert the unicode string into the datetime format
  2114. if self.is_time_absolute():
  2115. max_start_time = string_to_datetime(tstring)
  2116. if max_start_time is None:
  2117. max_start_time = end_time
  2118. else:
  2119. max_start_time = row[0]
  2120. else:
  2121. max_start_time = row[0]
  2122. if end_time < max_start_time:
  2123. use_start_time = True
  2124. # Set the maximum start time as end time
  2125. if use_start_time:
  2126. if self.is_time_absolute():
  2127. sql = """UPDATE STDS_absolute_time SET end_time =
  2128. (SELECT max(start_time) FROM GRASS_MAP_absolute_time WHERE
  2129. GRASS_MAP_absolute_time.id IN
  2130. (SELECT id FROM SPACETIME_REGISTER_TABLE)
  2131. ) WHERE id = 'SPACETIME_ID';"""
  2132. sql = sql.replace(
  2133. "GRASS_MAP", self.get_new_map_instance(None).get_type()
  2134. )
  2135. sql = sql.replace("SPACETIME_REGISTER_TABLE", stds_register_table)
  2136. sql = sql.replace("SPACETIME_ID", self.base.get_id())
  2137. sql = sql.replace("STDS", self.get_type())
  2138. elif self.is_time_relative():
  2139. sql = """UPDATE STDS_relative_time SET end_time =
  2140. (SELECT max(start_time) FROM GRASS_MAP_relative_time WHERE
  2141. GRASS_MAP_relative_time.id IN
  2142. (SELECT id FROM SPACETIME_REGISTER_TABLE)
  2143. ) WHERE id = 'SPACETIME_ID';"""
  2144. sql = sql.replace(
  2145. "GRASS_MAP", self.get_new_map_instance(None).get_type()
  2146. )
  2147. sql = sql.replace("SPACETIME_REGISTER_TABLE", stds_register_table)
  2148. sql = sql.replace("SPACETIME_ID", self.base.get_id())
  2149. sql = sql.replace("STDS", self.get_type())
  2150. dbif.execute_transaction(sql)
  2151. # Count the temporal map types
  2152. maps = self.get_registered_maps_as_objects(dbif=dbif)
  2153. tlist = self.count_temporal_types(maps)
  2154. if tlist["interval"] > 0 and tlist["point"] == 0 and tlist["invalid"] == 0:
  2155. map_time = "interval"
  2156. elif tlist["interval"] == 0 and tlist["point"] > 0 and tlist["invalid"] == 0:
  2157. map_time = "point"
  2158. elif tlist["interval"] > 0 and tlist["point"] > 0 and tlist["invalid"] == 0:
  2159. map_time = "mixed"
  2160. else:
  2161. map_time = "invalid"
  2162. # Compute the granularity
  2163. if map_time != "invalid":
  2164. # Smallest supported temporal resolution
  2165. if self.is_time_absolute():
  2166. gran = compute_absolute_time_granularity(maps)
  2167. elif self.is_time_relative():
  2168. gran = compute_relative_time_granularity(maps)
  2169. else:
  2170. gran = None
  2171. # Set the map time type and update the time objects
  2172. self.temporal_extent.select(dbif)
  2173. self.metadata.select(dbif)
  2174. if self.metadata.get_number_of_maps() > 0:
  2175. self.temporal_extent.set_map_time(map_time)
  2176. self.temporal_extent.set_granularity(gran)
  2177. else:
  2178. self.temporal_extent.set_map_time(None)
  2179. self.temporal_extent.set_granularity(None)
  2180. self.temporal_extent.update_all(dbif)
  2181. # Set the modification time
  2182. self.base.set_mtime(datetime.now())
  2183. self.base.update(dbif)
  2184. if connection_state_changed:
  2185. dbif.close()
  2186. ###############################################################################
  2187. if __name__ == "__main__":
  2188. import doctest
  2189. doctest.testmod()