space_time_datasets.py 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166
  1. """!@package grass.temporal
  2. @brief GRASS Python scripting module (temporal GIS functions)
  3. Temporal GIS related functions to be used in Python scripts.
  4. (C) 2012-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. @author Soeren Gebbert
  9. """
  10. import getpass
  11. import logging
  12. from abstract_map_dataset import *
  13. from abstract_space_time_dataset import *
  14. ###############################################################################
  15. class RasterDataset(AbstractMapDataset):
  16. """!Raster dataset class
  17. This class provides functions to select, update, insert or delete raster
  18. map information and valid time stamps into the SQL temporal database.
  19. Usage:
  20. @code
  21. >>> import grass.script as grass
  22. >>> init()
  23. >>> grass.use_temp_region()
  24. >>> grass.run_command("g.region", n=80.0, s=0.0, e=120.0, w=0.0,
  25. ... t=1.0, b=0.0, res=10.0)
  26. 0
  27. >>> grass.run_command("r.mapcalc", overwrite=True, quiet=True,
  28. ... expression="strds_map_test_case = 1")
  29. 0
  30. >>> grass.run_command("r.timestamp", map="strds_map_test_case",
  31. ... date="15 jan 1999", quiet=True)
  32. 0
  33. >>> mapset = get_current_mapset()
  34. >>> name = "strds_map_test_case"
  35. >>> identifier = "%s@%s" % (name, mapset)
  36. >>> rmap = RasterDataset(identifier)
  37. >>> rmap.map_exists()
  38. True
  39. >>> rmap.read_timestamp_from_grass()
  40. True
  41. >>> rmap.get_temporal_extent_as_tuple()
  42. (datetime.datetime(1999, 1, 15, 0, 0), None)
  43. >>> rmap.load()
  44. >>> rmap.spatial_extent.print_info()
  45. +-------------------- Spatial extent ----------------------------------------+
  46. | North:...................... 80.0
  47. | South:...................... 0.0
  48. | East:.. .................... 120.0
  49. | West:....................... 0.0
  50. | Top:........................ 0.0
  51. | Bottom:..................... 0.0
  52. >>> rmap.absolute_time.print_info()
  53. +-------------------- Absolute time -----------------------------------------+
  54. | Start time:................. 1999-01-15 00:00:00
  55. | End time:................... None
  56. >>> rmap.metadata.print_info()
  57. +-------------------- Metadata information ----------------------------------+
  58. | Datatype:................... CELL
  59. | Number of columns:.......... 8
  60. | Number of rows:............. 12
  61. | Number of cells:............ 96
  62. | North-South resolution:..... 10.0
  63. | East-west resolution:....... 10.0
  64. | Minimum value:.............. 1.0
  65. | Maximum value:.............. 1.0
  66. >>> newmap = rmap.get_new_instance("new@PERMANENT")
  67. >>> isinstance(newmap, RasterDataset)
  68. True
  69. >>> newstrds = rmap.get_new_stds_instance("new@PERMANENT")
  70. >>> isinstance(newstrds, SpaceTimeRasterDataset)
  71. True
  72. >>> rmap.get_type()
  73. 'raster'
  74. >>> rmap.set_absolute_time(start_time=datetime(2001,1,1),
  75. ... end_time=datetime(2012,1,1))
  76. True
  77. >>> rmap.get_absolute_time()
  78. (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2012, 1, 1, 0, 0))
  79. >>> rmap.get_temporal_extent_as_tuple()
  80. (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2012, 1, 1, 0, 0))
  81. >>> rmap.get_name()
  82. 'strds_map_test_case'
  83. >>> rmap.get_mapset() == mapset
  84. True
  85. >>> rmap.get_temporal_type()
  86. 'absolute'
  87. >>> rmap.get_spatial_extent_as_tuple()
  88. (80.0, 0.0, 120.0, 0.0, 0.0, 0.0)
  89. >>> rmap.is_time_absolute()
  90. True
  91. >>> rmap.is_time_relative()
  92. False
  93. >>> grass.run_command("g.remove", rast=name, quiet=True)
  94. 0
  95. >>> grass.del_temp_region()
  96. @endcode
  97. """
  98. def __init__(self, ident):
  99. AbstractMapDataset.__init__(self)
  100. self.reset(ident)
  101. def is_stds(self):
  102. """!Return True if this class is a space time dataset
  103. @return True if this class is a space time dataset, False otherwise
  104. """
  105. return False
  106. def get_type(self):
  107. return 'raster'
  108. def get_new_instance(self, ident):
  109. """!Return a new instance with the type of this class"""
  110. return RasterDataset(ident)
  111. def get_new_stds_instance(self, ident):
  112. """!Return a new space time dataset instance in which maps
  113. are stored with the type of this class"""
  114. return SpaceTimeRasterDataset(ident)
  115. def spatial_overlapping(self, dataset):
  116. """!Return True if the spatial extents 2d overlap"""
  117. return self.spatial_extent.overlapping_2d(dataset.spatial_extent)
  118. def spatial_relation(self, dataset):
  119. """!Return the two dimensional spatial relation"""
  120. return self.spatial_extent.spatial_relation_2d(dataset.spatial_extent)
  121. def spatial_intersection(self, dataset):
  122. """!Return the two dimensional intersection as spatial_extent
  123. object or None in case no intersection was found.
  124. @param dataset The abstract dataset to intersect with
  125. @return The intersection spatial extent or None
  126. """
  127. return self.spatial_extent.intersect_2d(dataset.spatial_extent)
  128. def spatial_union(self, dataset):
  129. """!Return the two dimensional union as spatial_extent
  130. object or None in case the extents does not overlap or meet.
  131. @param dataset The abstract dataset to create a union with
  132. @return The union spatial extent or None
  133. """
  134. return self.spatial_extent.union_2d(dataset.spatial_extent)
  135. def spatial_disjoint_union(self, dataset):
  136. """!Return the two dimensional union as spatial_extent object.
  137. @param dataset The abstract dataset to create a union with
  138. @return The union spatial extent
  139. """
  140. return self.spatial_extent.disjoint_union_2d(dataset.spatial_extent)
  141. def get_np_array(self):
  142. """!Return this raster map as memmap numpy style array to access the raster
  143. values in numpy style without loading the whole map in the RAM.
  144. In case this raster map does exists in the grass spatial database,
  145. the map will be exported using r.out.bin to a temporary location
  146. and assigned to the memmap object that is returned by this function.
  147. In case the raster map does not exists, an empty temporary
  148. binary file will be created and assigned to the memap object.
  149. You need to call the write function to write the memmap
  150. array back into grass.
  151. """
  152. a = garray.array()
  153. if self.map_exists():
  154. a.read(self.get_map_id())
  155. return a
  156. def reset(self, ident):
  157. """!Reset the internal structure and set the identifier"""
  158. self.base = RasterBase(ident=ident)
  159. self.absolute_time = RasterAbsoluteTime(ident=ident)
  160. self.relative_time = RasterRelativeTime(ident=ident)
  161. self.spatial_extent = RasterSpatialExtent(ident=ident)
  162. self.metadata = RasterMetadata(ident=ident)
  163. self.stds_register = RasterSTDSRegister(ident=ident)
  164. def has_grass_timestamp(self):
  165. """!Check if a grass file bsased time stamp exists for this map.
  166. @return True if success, False on error
  167. """
  168. return self.ciface.has_raster_timestamp(self.get_name(),
  169. self.get_mapset())
  170. def read_timestamp_from_grass(self):
  171. """!Read the timestamp of this map from the map metadata
  172. in the grass file system based spatial database and
  173. set the internal time stamp that should be insert/updated
  174. in the temporal database.
  175. @return True if success, False on error
  176. """
  177. if not self.has_grass_timestamp():
  178. return False
  179. check, dates = self.ciface.read_raster_timestamp(self.get_name(),
  180. self.get_mapset(),)
  181. if check < 1:
  182. self.msgr.error(_("Unable to read timestamp file "
  183. "for raster map <%s>" % (self.get_map_id())))
  184. return False
  185. if len(dates) == 2:
  186. self.set_absolute_time(dates[0], dates[1])
  187. else:
  188. self.set_relative_time(dates[0], dates[1], dates[2])
  189. return True
  190. def write_timestamp_to_grass(self):
  191. """!Write the timestamp of this map into the map metadata in
  192. the grass file system based spatial database.
  193. Internally the libgis API functions are used for writing
  194. @return True if success, False on error
  195. """
  196. check = self.ciface.write_raster_timestamp(self.get_name(),
  197. self.get_mapset(),
  198. self._convert_timestamp())
  199. if check == -1:
  200. self.msgr.error(_("Unable to create timestamp file "
  201. "for raster map <%s>" % (self.get_map_id())))
  202. return False
  203. if check == -2:
  204. self.msgr.error(_("Invalid datetime in timestamp for raster map <%s>" %
  205. (self.get_map_id())))
  206. return False
  207. if check == -3:
  208. self.msgr.error(_("Internal error"))
  209. return False
  210. return True
  211. def remove_timestamp_from_grass(self):
  212. """!Remove the timestamp from the grass file system based
  213. spatial database
  214. Internally the libgis API functions are used for removal
  215. @return True if success, False on error
  216. """
  217. check = self.ciface.remove_raster_timestamp(self.get_name(),
  218. self.get_mapset())
  219. if check == -1:
  220. self.msgr.error(_("Unable to remove timestamp for raster map <%s>" %
  221. (self.get_name())))
  222. return False
  223. return True
  224. def map_exists(self):
  225. """!Return True in case the map exists in the grass spatial database
  226. @return True if map exists, False otherwise
  227. """
  228. return self.ciface.raster_map_exists(self.get_name(),
  229. self.get_mapset())
  230. def load(self):
  231. """!Load all info from an existing raster map into the internal s
  232. tructure"""
  233. # Fill base information
  234. self.base.set_creator(str(getpass.getuser()))
  235. kvp = self.ciface.read_raster_info(self.get_name(),
  236. self.get_mapset())
  237. # Fill spatial extent
  238. self.set_spatial_extent_from_values(north=kvp["north"],
  239. south=kvp["south"],
  240. east=kvp["east"],
  241. west=kvp["west"])
  242. # Fill metadata
  243. self.metadata.set_nsres(kvp["nsres"])
  244. self.metadata.set_ewres(kvp["ewres"])
  245. self.metadata.set_datatype(kvp["datatype"])
  246. self.metadata.set_min(kvp["min"])
  247. self.metadata.set_max(kvp["max"])
  248. rows = int(kvp["rows"])
  249. cols = int(kvp["cols"])
  250. ncells = cols * rows
  251. self.metadata.set_cols(cols)
  252. self.metadata.set_rows(rows)
  253. self.metadata.set_number_of_cells(ncells)
  254. ###############################################################################
  255. class Raster3DDataset(AbstractMapDataset):
  256. """!Raster3d dataset class
  257. This class provides functions to select, update, insert or delete raster3d
  258. map information and valid time stamps into the SQL temporal database.
  259. Usage:
  260. @code
  261. >>> import grass.script as grass
  262. >>> init()
  263. >>> grass.use_temp_region()
  264. >>> grass.run_command("g.region", n=80.0, s=0.0, e=120.0, w=0.0,
  265. ... t=100.0, b=0.0, res=10.0, res3=10.0)
  266. 0
  267. >>> grass.run_command("r3.mapcalc", overwrite=True, quiet=True,
  268. ... expression="str3ds_map_test_case = 1")
  269. 0
  270. >>> grass.run_command("r3.timestamp", map="str3ds_map_test_case",
  271. ... date="15 jan 1999", quiet=True)
  272. 0
  273. >>> mapset = get_current_mapset()
  274. >>> name = "str3ds_map_test_case"
  275. >>> identifier = "%s@%s" % (name, mapset)
  276. >>> r3map = Raster3DDataset(identifier)
  277. >>> r3map.map_exists()
  278. True
  279. >>> r3map.read_timestamp_from_grass()
  280. True
  281. >>> r3map.get_temporal_extent_as_tuple()
  282. (datetime.datetime(1999, 1, 15, 0, 0), None)
  283. >>> r3map.load()
  284. >>> r3map.spatial_extent.print_info()
  285. +-------------------- Spatial extent ----------------------------------------+
  286. | North:...................... 80.0
  287. | South:...................... 0.0
  288. | East:.. .................... 120.0
  289. | West:....................... 0.0
  290. | Top:........................ 100.0
  291. | Bottom:..................... 0.0
  292. >>> r3map.absolute_time.print_info()
  293. +-------------------- Absolute time -----------------------------------------+
  294. | Start time:................. 1999-01-15 00:00:00
  295. | End time:................... None
  296. >>> r3map.metadata.print_info()
  297. +-------------------- Metadata information ----------------------------------+
  298. | Datatype:................... DCELL
  299. | Number of columns:.......... 8
  300. | Number of rows:............. 12
  301. | Number of cells:............ 960
  302. | North-South resolution:..... 10.0
  303. | East-west resolution:....... 10.0
  304. | Minimum value:.............. 1.0
  305. | Maximum value:.............. 1.0
  306. | Number of depths:........... 10
  307. | Top-Bottom resolution:...... 10.0
  308. >>> newmap = r3map.get_new_instance("new@PERMANENT")
  309. >>> isinstance(newmap, Raster3DDataset)
  310. True
  311. >>> newstr3ds = r3map.get_new_stds_instance("new@PERMANENT")
  312. >>> isinstance(newstr3ds, SpaceTimeRaster3DDataset)
  313. True
  314. >>> r3map.get_type()
  315. 'raster3d'
  316. >>> r3map.set_absolute_time(start_time=datetime(2001,1,1),
  317. ... end_time=datetime(2012,1,1))
  318. True
  319. >>> r3map.get_absolute_time()
  320. (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2012, 1, 1, 0, 0))
  321. >>> r3map.get_temporal_extent_as_tuple()
  322. (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2012, 1, 1, 0, 0))
  323. >>> r3map.get_name()
  324. 'str3ds_map_test_case'
  325. >>> r3map.get_mapset() == mapset
  326. True
  327. >>> r3map.get_temporal_type()
  328. 'absolute'
  329. >>> r3map.get_spatial_extent_as_tuple()
  330. (80.0, 0.0, 120.0, 0.0, 100.0, 0.0)
  331. >>> r3map.is_time_absolute()
  332. True
  333. >>> r3map.is_time_relative()
  334. False
  335. >>> grass.run_command("g.remove", rast3d=name, quiet=True)
  336. 0
  337. >>> grass.del_temp_region()
  338. """
  339. def __init__(self, ident):
  340. AbstractMapDataset.__init__(self)
  341. self.reset(ident)
  342. def is_stds(self):
  343. """!Return True if this class is a space time dataset
  344. @return True if this class is a space time dataset, False otherwise
  345. """
  346. return False
  347. def get_type(self):
  348. return "raster3d"
  349. def get_new_instance(self, ident):
  350. """!Return a new instance with the type of this class"""
  351. return Raster3DDataset(ident)
  352. def get_new_stds_instance(self, ident):
  353. """!Return a new space time dataset instance in which maps
  354. are stored with the type of this class"""
  355. return SpaceTimeRaster3DDataset(ident)
  356. def spatial_overlapping(self, dataset):
  357. """!Return True if the spatial extents overlap"""
  358. if self.get_type() == dataset.get_type() or dataset.get_type() == "str3ds":
  359. return self.spatial_extent.overlapping(dataset.spatial_extent)
  360. else:
  361. return self.spatial_extent.overlapping_2d(dataset.spatial_extent)
  362. def spatial_relation(self, dataset):
  363. """!Return the two or three dimensional spatial relation"""
  364. if self.get_type() == dataset.get_type() or dataset.get_type() == "str3ds":
  365. return self.spatial_extent.spatial_relation(dataset.spatial_extent)
  366. else:
  367. return self.spatial_extent.spatial_relation_2d(dataset.spatial_extent)
  368. def spatial_intersection(self, dataset):
  369. """!Return the three or two dimensional intersection as spatial_extent
  370. object or None in case no intersection was found.
  371. @param dataset The abstract dataset to intersect with
  372. @return The intersection spatial extent or None
  373. """
  374. if self.get_type() == dataset.get_type() or dataset.get_type() == "str3ds":
  375. return self.spatial_extent.intersect(dataset.spatial_extent)
  376. else:
  377. return self.spatial_extent.intersect_2d(dataset.spatial_extent)
  378. def spatial_union(self, dataset):
  379. """!Return the three or two dimensional union as spatial_extent
  380. object or None in case the extents does not overlap or meet.
  381. @param dataset The abstract dataset to create a union with
  382. @return The union spatial extent or None
  383. """
  384. if self.get_type() == dataset.get_type() or dataset.get_type() == "str3ds":
  385. return self.spatial_extent.union(dataset.spatial_extent)
  386. else:
  387. return self.spatial_extent.union_2d(dataset.spatial_extent)
  388. def spatial_disjoint_union(self, dataset):
  389. """!Return the three or two dimensional union as spatial_extent object.
  390. @param dataset The abstract dataset to create a union with
  391. @return The union spatial extent
  392. """
  393. if self.get_type() == dataset.get_type() or dataset.get_type() == "str3ds":
  394. return self.spatial_extent.disjoint_union(dataset.spatial_extent)
  395. else:
  396. return self.spatial_extent.disjoint_union_2d(dataset.spatial_extent)
  397. def get_np_array(self):
  398. """!Return this 3D raster map as memmap numpy style array to access the 3D raster
  399. values in numpy style without loading the whole map in the RAM.
  400. In case this 3D raster map does exists in the grass spatial database,
  401. the map will be exported using r3.out.bin to a temporary location
  402. and assigned to the memmap object that is returned by this function.
  403. In case the 3D raster map does not exists, an empty temporary
  404. binary file will be created and assigned to the memap object.
  405. You need to call the write function to write the memmap
  406. array back into grass.
  407. """
  408. a = garray.array3d()
  409. if self.map_exists():
  410. a.read(self.get_map_id())
  411. return a
  412. def reset(self, ident):
  413. """!Reset the internal structure and set the identifier"""
  414. self.base = Raster3DBase(ident=ident)
  415. self.absolute_time = Raster3DAbsoluteTime(ident=ident)
  416. self.relative_time = Raster3DRelativeTime(ident=ident)
  417. self.spatial_extent = Raster3DSpatialExtent(ident=ident)
  418. self.metadata = Raster3DMetadata(ident=ident)
  419. self.stds_register = Raster3DSTDSRegister(ident=ident)
  420. def has_grass_timestamp(self):
  421. """!Check if a grass file bsased time stamp exists for this map.
  422. @return True if success, False on error
  423. """
  424. return self.ciface.has_raster3d_timestamp(self.get_name(),
  425. self.get_mapset())
  426. def read_timestamp_from_grass(self):
  427. """!Read the timestamp of this map from the map metadata
  428. in the grass file system based spatial database and
  429. set the internal time stamp that should be insert/updated
  430. in the temporal database.
  431. @return True if success, False on error
  432. """
  433. if not self.has_grass_timestamp():
  434. return False
  435. check, dates = self.ciface.read_raster3d_timestamp(self.get_name(),
  436. self.get_mapset(),)
  437. if check < 1:
  438. self.msgr.error(_("Unable to read timestamp file "
  439. "for 3D raster map <%s>" % (self.get_map_id())))
  440. return False
  441. if len(dates) == 2:
  442. self.set_absolute_time(dates[0], dates[1])
  443. else:
  444. self.set_relative_time(dates[0], dates[1], dates[2])
  445. return True
  446. def write_timestamp_to_grass(self):
  447. """!Write the timestamp of this map into the map metadata
  448. in the grass file system based spatial database.
  449. Internally the libgis API functions are used for writing
  450. @return True if success, False on error
  451. """
  452. check = self.ciface.write_raster3d_timestamp(self.get_name(),
  453. self.get_mapset(),
  454. self._convert_timestamp())
  455. if check == -1:
  456. self.msgr.error(_("Unable to create timestamp file "
  457. "for 3D raster map <%s>" % (self.get_map_id())))
  458. return False
  459. if check == -2:
  460. self.msgr.error(_("Invalid datetime in timestamp for 3D raster map <%s>" %
  461. (self.get_map_id())))
  462. return False
  463. if check == -3:
  464. self.msgr.error(_("Internal error"))
  465. return False
  466. return True
  467. def remove_timestamp_from_grass(self):
  468. """!Remove the timestamp from the grass file system based spatial database
  469. @return True if success, False on error
  470. """
  471. check = self.ciface.remove_raster3d_timestamp(self.get_name(),
  472. self.get_mapset())
  473. if check == -1:
  474. self.msgr.error(_("Unable to remove timestamp for raster map <%s>" %
  475. (self.get_name())))
  476. return False
  477. return True
  478. def map_exists(self):
  479. """!Return True in case the map exists in the grass spatial database
  480. @return True if map exists, False otherwise
  481. """
  482. return self.ciface.raster3d_map_exists(self.get_name(),
  483. self.get_mapset())
  484. def load(self):
  485. """!Load all info from an existing raster3d map into the internal structure"""
  486. # Fill base information
  487. self.base.set_creator(str(getpass.getuser()))
  488. # Fill spatial extent
  489. kvp = self.ciface.read_raster3d_info(self.get_name(),
  490. self.get_mapset())
  491. self.set_spatial_extent_from_values(north=kvp["north"], south=kvp["south"],
  492. east=kvp["east"], west=kvp["west"],
  493. top=kvp["top"], bottom=kvp["bottom"])
  494. # Fill metadata
  495. self.metadata.set_nsres(kvp["nsres"])
  496. self.metadata.set_ewres(kvp["ewres"])
  497. self.metadata.set_tbres(kvp["tbres"])
  498. self.metadata.set_datatype(kvp["datatype"])
  499. self.metadata.set_min(kvp["min"])
  500. self.metadata.set_max(kvp["max"])
  501. rows = int(kvp["rows"])
  502. cols = int(kvp["cols"])
  503. depths = int(kvp["depths"])
  504. ncells = cols * rows * depths
  505. self.metadata.set_cols(cols)
  506. self.metadata.set_rows(rows)
  507. self.metadata.set_depths(depths)
  508. self.metadata.set_number_of_cells(ncells)
  509. ###############################################################################
  510. class VectorDataset(AbstractMapDataset):
  511. """!Vector dataset class
  512. This class provides functions to select, update, insert or delete vector
  513. map information and valid time stamps into the SQL temporal database.
  514. Usage:
  515. @code
  516. >>> import grass.script as grass
  517. >>> init()
  518. >>> grass.use_temp_region()
  519. >>> grass.run_command("g.region", n=80.0, s=0.0, e=120.0, w=0.0,
  520. ... t=1.0, b=0.0, res=10.0)
  521. 0
  522. >>> grass.run_command("v.random", overwrite=True, output="stvds_map_test_case",
  523. ... n=100, zmin=0, zmax=100, flags="z", column="elevation", quiet=True)
  524. 0
  525. >>> grass.run_command("v.timestamp", map="stvds_map_test_case",
  526. ... date="15 jan 1999", quiet=True)
  527. 0
  528. >>> mapset = get_current_mapset()
  529. >>> name = "stvds_map_test_case"
  530. >>> identifier = "%s@%s" % (name, mapset)
  531. >>> vmap = VectorDataset(identifier)
  532. >>> vmap.map_exists()
  533. True
  534. >>> vmap.read_timestamp_from_grass()
  535. True
  536. >>> vmap.get_temporal_extent_as_tuple()
  537. (datetime.datetime(1999, 1, 15, 0, 0), None)
  538. >>> vmap.load()
  539. >>> vmap.absolute_time.print_info()
  540. +-------------------- Absolute time -----------------------------------------+
  541. | Start time:................. 1999-01-15 00:00:00
  542. | End time:................... None
  543. >>> vmap.metadata.print_info()
  544. +-------------------- Metadata information ----------------------------------+
  545. | Is map 3d .................. True
  546. | Number of points ........... 100
  547. | Number of lines ............ 0
  548. | Number of boundaries ....... 0
  549. | Number of centroids ........ 0
  550. | Number of faces ............ 0
  551. | Number of kernels .......... 0
  552. | Number of primitives ....... 100
  553. | Number of nodes ............ 0
  554. | Number of areas ............ 0
  555. | Number of islands .......... 0
  556. | Number of holes ............ 0
  557. | Number of volumes .......... 0
  558. >>> newmap = vmap.get_new_instance("new@PERMANENT")
  559. >>> isinstance(newmap, VectorDataset)
  560. True
  561. >>> newstvds = vmap.get_new_stds_instance("new@PERMANENT")
  562. >>> isinstance(newstvds, SpaceTimeVectorDataset)
  563. True
  564. >>> vmap.get_type()
  565. 'vector'
  566. >>> vmap.set_absolute_time(start_time=datetime(2001,1,1),
  567. ... end_time=datetime(2012,1,1))
  568. True
  569. >>> vmap.get_absolute_time()
  570. (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2012, 1, 1, 0, 0))
  571. >>> vmap.get_temporal_extent_as_tuple()
  572. (datetime.datetime(2001, 1, 1, 0, 0), datetime.datetime(2012, 1, 1, 0, 0))
  573. >>> vmap.get_name()
  574. 'stvds_map_test_case'
  575. >>> vmap.get_mapset() == mapset
  576. True
  577. >>> vmap.get_temporal_type()
  578. 'absolute'
  579. >>> vmap.is_time_absolute()
  580. True
  581. >>> vmap.is_time_relative()
  582. False
  583. >>> grass.run_command("g.remove", vect=name, quiet=True)
  584. 0
  585. >>> grass.del_temp_region()
  586. @endcode
  587. """
  588. def __init__(self, ident):
  589. AbstractMapDataset.__init__(self)
  590. self.reset(ident)
  591. def is_stds(self):
  592. """!Return True if this class is a space time dataset
  593. @return True if this class is a space time dataset, False otherwise
  594. """
  595. return False
  596. def get_type(self):
  597. return "vector"
  598. def get_new_instance(self, ident):
  599. """!Return a new instance with the type of this class"""
  600. return VectorDataset(ident)
  601. def get_new_stds_instance(self, ident):
  602. """!Return a new space time dataset instance in which maps
  603. are stored with the type of this class"""
  604. return SpaceTimeVectorDataset(ident)
  605. def get_layer(self):
  606. """!Return the layer"""
  607. return self.base.get_layer()
  608. def spatial_overlapping(self, dataset):
  609. """!Return True if the spatial extents 2d overlap"""
  610. return self.spatial_extent.overlapping_2d(dataset.spatial_extent)
  611. def spatial_relation(self, dataset):
  612. """!Return the two dimensional spatial relation"""
  613. return self.spatial_extent.spatial_relation_2d(dataset.spatial_extent)
  614. def spatial_intersection(self, dataset):
  615. """!Return the two dimensional intersection as spatial_extent
  616. object or None in case no intersection was found.
  617. @param dataset The abstract dataset to intersect with
  618. @return The intersection spatial extent or None
  619. """
  620. return self.spatial_extent.intersect_2d(dataset.spatial_extent)
  621. def spatial_union(self, dataset):
  622. """!Return the two dimensional union as spatial_extent
  623. object or None in case the extents does not overlap or meet.
  624. @param dataset The abstract dataset to create a union with
  625. @return The union spatial extent or None
  626. """
  627. return self.spatial_extent.union_2d(dataset.spatial_extent)
  628. def spatial_disjoint_union(self, dataset):
  629. """!Return the two dimensional union as spatial_extent object.
  630. @param dataset The abstract dataset to create a union with
  631. @return The union spatial extent
  632. """
  633. return self.spatial_extent.disjoint_union_2d(dataset.spatial_extent)
  634. def reset(self, ident):
  635. """!Reset the internal structure and set the identifier"""
  636. self.base = VectorBase(ident=ident)
  637. self.absolute_time = VectorAbsoluteTime(ident=ident)
  638. self.relative_time = VectorRelativeTime(ident=ident)
  639. self.spatial_extent = VectorSpatialExtent(ident=ident)
  640. self.metadata = VectorMetadata(ident=ident)
  641. self.stds_register = VectorSTDSRegister(ident=ident)
  642. def has_grass_timestamp(self):
  643. """!Check if a grass file bsased time stamp exists for this map.
  644. """
  645. return self.ciface.has_vector_timestamp(self.get_name(),
  646. self.get_mapset(),
  647. self.get_layer())
  648. def read_timestamp_from_grass(self):
  649. """!Read the timestamp of this map from the map metadata
  650. in the grass file system based spatial database and
  651. set the internal time stamp that should be insert/updated
  652. in the temporal database.
  653. """
  654. if not self.has_grass_timestamp():
  655. return False
  656. check, dates = self.ciface.read_vector_timestamp(self.get_name(),
  657. self.get_mapset(),)
  658. if check < 1:
  659. self.msgr.error(_("Unable to read timestamp file "
  660. "for vector map <%s>" % (self.get_map_id())))
  661. return False
  662. if len(dates) == 2:
  663. self.set_absolute_time(dates[0], dates[1])
  664. else:
  665. self.set_relative_time(dates[0], dates[1], dates[2])
  666. return True
  667. def write_timestamp_to_grass(self):
  668. """!Write the timestamp of this map into the map metadata in
  669. the grass file system based spatial database.
  670. Internally the libgis API functions are used for writing
  671. """
  672. check = self.ciface.write_vector_timestamp(self.get_name(),
  673. self.get_mapset(),
  674. self._convert_timestamp(),
  675. self.get_layer())
  676. if check == -1:
  677. self.msgr.error(_("Unable to create timestamp file "
  678. "for vector map <%s>" % (self.get_map_id())))
  679. return False
  680. if check == -2:
  681. self.msgr.error(_("Invalid datetime in timestamp for vector map <%s>" %
  682. (self.get_map_id())))
  683. return False
  684. return True
  685. def remove_timestamp_from_grass(self):
  686. """!Remove the timestamp from the grass file system based spatial
  687. database
  688. Internally the libgis API functions are used for removal
  689. """
  690. check = self.ciface.remove_vector_timestamp(self.get_name(),
  691. self.get_mapset())
  692. if check == -1:
  693. self.msgr.error(_("Unable to remove timestamp for vector map <%s>" %
  694. (self.get_name())))
  695. return False
  696. return True
  697. def map_exists(self):
  698. """!Return True in case the map exists in the grass spatial database
  699. @return True if map exists, False otherwise
  700. """
  701. return self.ciface.vector_map_exists(self.get_name(),
  702. self.get_mapset())
  703. def load(self):
  704. """!Load all info from an existing vector map into the internal
  705. structure"""
  706. # Fill base information
  707. self.base.set_creator(str(getpass.getuser()))
  708. # Get the data from an existing vector map
  709. kvp = self.ciface.read_vector_info(self.get_name(),
  710. self.get_mapset())
  711. # Fill spatial extent
  712. self.set_spatial_extent_from_values(north=kvp["north"], south=kvp["south"],
  713. east=kvp["east"], west=kvp["west"],
  714. top=kvp["top"], bottom=kvp["bottom"])
  715. # Fill metadata
  716. self.metadata.set_3d_info(kvp["map3d"])
  717. self.metadata.set_number_of_points(kvp["points"])
  718. self.metadata.set_number_of_lines(kvp["lines"])
  719. self.metadata.set_number_of_boundaries(kvp["boundaries"])
  720. self.metadata.set_number_of_centroids(kvp["centroids"])
  721. self.metadata.set_number_of_faces(kvp["faces"])
  722. self.metadata.set_number_of_kernels(kvp["kernels"])
  723. self.metadata.set_number_of_primitives(kvp["primitives"])
  724. self.metadata.set_number_of_nodes(kvp["nodes"])
  725. self.metadata.set_number_of_areas(kvp["areas"])
  726. self.metadata.set_number_of_islands(kvp["islands"])
  727. self.metadata.set_number_of_holes(kvp["holes"])
  728. self.metadata.set_number_of_volumes(kvp["volumes"])
  729. ###############################################################################
  730. class SpaceTimeRasterDataset(AbstractSpaceTimeDataset):
  731. """!Space time raster dataset class
  732. """
  733. def __init__(self, ident):
  734. AbstractSpaceTimeDataset.__init__(self, ident)
  735. def is_stds(self):
  736. """!Return True if this class is a space time dataset
  737. @return True if this class is a space time dataset, False otherwise
  738. """
  739. return True
  740. def get_type(self):
  741. return "strds"
  742. def get_new_instance(self, ident):
  743. """!Return a new instance with the type of this class"""
  744. return SpaceTimeRasterDataset(ident)
  745. def get_new_map_instance(self, ident):
  746. """!Return a new instance of a map dataset which is associated "
  747. "with the type of this class"""
  748. return RasterDataset(ident)
  749. def get_map_register(self):
  750. """!Return the name of the map register table"""
  751. return self.metadata.get_raster_register()
  752. def set_map_register(self, name):
  753. """!Set the name of the map register table"""
  754. self.metadata.set_raster_register(name)
  755. def spatial_overlapping(self, dataset):
  756. """!Return True if the spatial extents 2d overlap"""
  757. return self.spatial_extent.overlapping_2d(dataset.spatial_extent)
  758. def spatial_relation(self, dataset):
  759. """!Return the two dimensional spatial relation"""
  760. return self.spatial_extent.spatial_relation_2d(dataset.spatial_extent)
  761. def spatial_intersection(self, dataset):
  762. """!Return the two dimensional intersection as spatial_extent
  763. object or None in case no intersection was found.
  764. @param dataset The abstract dataset to intersect with
  765. @return The intersection spatial extent or None
  766. """
  767. return self.spatial_extent.intersect_2d(dataset.spatial_extent)
  768. def spatial_union(self, dataset):
  769. """!Return the two dimensional union as spatial_extent
  770. object or None in case the extents does not overlap or meet.
  771. @param dataset The abstract dataset to create a union with
  772. @return The union spatial extent or None
  773. """
  774. return self.spatial_extent.union_2d(dataset.spatial_extent)
  775. def spatial_disjoint_union(self, dataset):
  776. """!Return the two dimensional union as spatial_extent object.
  777. @param dataset The abstract dataset to create a union with
  778. @return The union spatial extent
  779. """
  780. return self.spatial_extent.disjoint_union_2d(dataset.spatial_extent)
  781. def reset(self, ident):
  782. """!Reset the internal structure and set the identifier"""
  783. self.base = STRDSBase(ident=ident)
  784. self.base.set_creator(str(getpass.getuser()))
  785. self.absolute_time = STRDSAbsoluteTime(ident=ident)
  786. self.relative_time = STRDSRelativeTime(ident=ident)
  787. self.spatial_extent = STRDSSpatialExtent(ident=ident)
  788. self.metadata = STRDSMetadata(ident=ident)
  789. ###############################################################################
  790. class SpaceTimeRaster3DDataset(AbstractSpaceTimeDataset):
  791. """!Space time raster3d dataset class
  792. """
  793. def __init__(self, ident):
  794. AbstractSpaceTimeDataset.__init__(self, ident)
  795. def is_stds(self):
  796. """!Return True if this class is a space time dataset
  797. @return True if this class is a space time dataset, False otherwise
  798. """
  799. return True
  800. def get_type(self):
  801. return "str3ds"
  802. def get_new_instance(self, ident):
  803. """!Return a new instance with the type of this class"""
  804. return SpaceTimeRaster3DDataset(ident)
  805. def get_new_map_instance(self, ident):
  806. """!Return a new instance of a map dataset which is associated
  807. with the type of this class"""
  808. return Raster3DDataset(ident)
  809. def get_map_register(self):
  810. """!Return the name of the map register table"""
  811. return self.metadata.get_raster3d_register()
  812. def set_map_register(self, name):
  813. """!Set the name of the map register table"""
  814. self.metadata.set_raster3d_register(name)
  815. def spatial_overlapping(self, dataset):
  816. """!Return True if the spatial extents overlap"""
  817. if self.get_type() == dataset.get_type() or dataset.get_type() == "str3ds":
  818. return self.spatial_extent.overlapping(dataset.spatial_extent)
  819. else:
  820. return self.spatial_extent.overlapping_2d(dataset.spatial_extent)
  821. def spatial_relation(self, dataset):
  822. """!Return the two or three dimensional spatial relation"""
  823. if self.get_type() == dataset.get_type() or \
  824. dataset.get_type() == "str3ds":
  825. return self.spatial_extent.spatial_relation(dataset.spatial_extent)
  826. else:
  827. return self.spatial_extent.spatial_relation_2d(dataset.spatial_extent)
  828. def spatial_intersection(self, dataset):
  829. """!Return the three or two dimensional intersection as spatial_extent
  830. object or None in case no intersection was found.
  831. @param dataset The abstract dataset to intersect with
  832. @return The intersection spatial extent or None
  833. """
  834. if self.get_type() == dataset.get_type() or dataset.get_type() == "raster3d":
  835. return self.spatial_extent.intersect(dataset.spatial_extent)
  836. else:
  837. return self.spatial_extent.intersect_2d(dataset.spatial_extent)
  838. def spatial_union(self, dataset):
  839. """!Return the three or two dimensional union as spatial_extent
  840. object or None in case the extents does not overlap or meet.
  841. @param dataset The abstract dataset to create a union with
  842. @return The union spatial extent or None
  843. """
  844. if self.get_type() == dataset.get_type() or dataset.get_type() == "raster3d":
  845. return self.spatial_extent.union(dataset.spatial_extent)
  846. else:
  847. return self.spatial_extent.union_2d(dataset.spatial_extent)
  848. def spatial_disjoint_union(self, dataset):
  849. """!Return the three or two dimensional union as spatial_extent object.
  850. @param dataset The abstract dataset to create a union with
  851. @return The union spatial extent
  852. """
  853. if self.get_type() == dataset.get_type() or dataset.get_type() == "raster3d":
  854. return self.spatial_extent.disjoint_union(dataset.spatial_extent)
  855. else:
  856. return self.spatial_extent.disjoint_union_2d(dataset.spatial_extent)
  857. def reset(self, ident):
  858. """!Reset the internal structure and set the identifier"""
  859. self.base = STR3DSBase(ident=ident)
  860. self.base.set_creator(str(getpass.getuser()))
  861. self.absolute_time = STR3DSAbsoluteTime(ident=ident)
  862. self.relative_time = STR3DSRelativeTime(ident=ident)
  863. self.spatial_extent = STR3DSSpatialExtent(ident=ident)
  864. self.metadata = STR3DSMetadata(ident=ident)
  865. ###############################################################################
  866. class SpaceTimeVectorDataset(AbstractSpaceTimeDataset):
  867. """!Space time vector dataset class
  868. """
  869. def __init__(self, ident):
  870. AbstractSpaceTimeDataset.__init__(self, ident)
  871. def is_stds(self):
  872. """!Return True if this class is a space time dataset
  873. @return True if this class is a space time dataset, False otherwise
  874. """
  875. return True
  876. def get_type(self):
  877. return "stvds"
  878. def get_new_instance(self, ident):
  879. """!Return a new instance with the type of this class"""
  880. return SpaceTimeVectorDataset(ident)
  881. def get_new_map_instance(self, ident):
  882. """!Return a new instance of a map dataset which is associated
  883. with the type of this class"""
  884. return VectorDataset(ident)
  885. def get_map_register(self):
  886. """!Return the name of the map register table"""
  887. return self.metadata.get_vector_register()
  888. def set_map_register(self, name):
  889. """!Set the name of the map register table"""
  890. self.metadata.set_vector_register(name)
  891. def spatial_overlapping(self, dataset):
  892. """!Return True if the spatial extents 2d overlap"""
  893. return self.spatial_extent.overlapping_2d(dataset.spatial_extent)
  894. def spatial_relation(self, dataset):
  895. """!Return the two dimensional spatial relation"""
  896. return self.spatial_extent.spatial_relation_2d(dataset.spatial_extent)
  897. def spatial_intersection(self, dataset):
  898. """!Return the two dimensional intersection as spatial_extent
  899. object or None in case no intersection was found.
  900. @param dataset The abstract dataset to intersect with
  901. @return The intersection spatial extent or None
  902. """
  903. return self.spatial_extent.intersect_2d(dataset.spatial_extent)
  904. def spatial_union(self, dataset):
  905. """!Return the two dimensional union as spatial_extent
  906. object or None in case the extents does not overlap or meet.
  907. @param dataset The abstract dataset to create a union with
  908. @return The union spatial extent or None
  909. """
  910. return self.spatial_extent.union_2d(dataset.spatial_extent)
  911. def spatial_disjoint_union(self, dataset):
  912. """!Return the two dimensional union as spatial_extent object.
  913. @param dataset The abstract dataset to create a union with
  914. @return The union spatial extent
  915. """
  916. return self.spatial_extent.disjoint_union_2d(dataset.spatial_extent)
  917. def reset(self, ident):
  918. """!Reset the internal structure and set the identifier"""
  919. self.base = STVDSBase(ident=ident)
  920. self.base.set_creator(str(getpass.getuser()))
  921. self.absolute_time = STVDSAbsoluteTime(ident=ident)
  922. self.relative_time = STVDSRelativeTime(ident=ident)
  923. self.spatial_extent = STVDSSpatialExtent(ident=ident)
  924. self.metadata = STVDSMetadata(ident=ident)
  925. ###############################################################################
  926. if __name__ == "__main__":
  927. import doctest
  928. doctest.testmod()