space_time_datasets.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  1. """!@package grass.temporal
  2. @brief GRASS Python scripting module (temporal GIS functions)
  3. Temporal GIS related functions to be used in Python scripts.
  4. Usage:
  5. @code
  6. import grass.temporal as tgis
  7. strds = tgis.space_time_raster_dataset("soils_1950_2010")
  8. ...
  9. @endcode
  10. (C) 2008-2011 by the GRASS Development Team
  11. This program is free software under the GNU General Public
  12. License (>=v2). Read the file COPYING that comes with GRASS
  13. for details.
  14. @author Soeren Gebbert
  15. """
  16. import getpass
  17. from ctypes import *
  18. import grass.lib.gis as libgis
  19. import grass.lib.raster as libraster
  20. import grass.lib.vector as libvector
  21. import grass.lib.raster3d as libraster3d
  22. from datetime_math import *
  23. from abstract_map_dataset import *
  24. from abstract_space_time_dataset import *
  25. ###############################################################################
  26. class raster_dataset(abstract_map_dataset):
  27. """!Raster dataset class
  28. This class provides functions to select, update, insert or delete raster
  29. map information and valid time stamps into the SQL temporal database.
  30. """
  31. def __init__(self, ident):
  32. abstract_map_dataset.__init__(self)
  33. self.reset(ident)
  34. def get_type(self):
  35. return "raster"
  36. def get_new_instance(self, ident):
  37. """!Return a new instance with the type of this class"""
  38. return raster_dataset(ident)
  39. def get_new_stds_instance(self, ident):
  40. """!Return a new space time dataset instance in which maps are stored with the type of this class"""
  41. return space_time_raster_dataset(ident)
  42. def get_stds_register(self):
  43. """!Return the space time dataset register table name in which stds are listed in which this map is registered"""
  44. return self.metadata.get_strds_register()
  45. def set_stds_register(self, name):
  46. """!Set the space time dataset register table name in which stds are listed in which this map is registered"""
  47. self.metadata.set_strds_register(name)
  48. def spatial_overlapping(self, dataset):
  49. """!Return True if the spatial extents 2d overlap"""
  50. return self.spatial_extent.overlapping_2d(dataset.spatial_extent)
  51. def spatial_relation(self, dataset):
  52. """Return the two dimensional spatial relation"""
  53. return self.spatial_extent.spatial_relation_2d(dataset.spatial_extent)
  54. def reset(self, ident):
  55. """!Reset the internal structure and set the identifier"""
  56. self.ident = ident
  57. self.base = raster_base(ident=ident)
  58. self.absolute_time = raster_absolute_time(ident=ident)
  59. self.relative_time = raster_relative_time(ident=ident)
  60. self.spatial_extent = raster_spatial_extent(ident=ident)
  61. self.metadata = raster_metadata(ident=ident)
  62. def has_grass_timestamp(self):
  63. """!Check if a grass file bsased time stamp exists for this map.
  64. """
  65. if G_has_raster_timestamp(self.get_name(), self.get_mapset()):
  66. return True
  67. else:
  68. return False
  69. def write_timestamp_to_grass(self):
  70. """!Write the timestamp of this map into the map metadata in the grass file system based spatial
  71. database.
  72. Internally the libgis API functions are used for writing
  73. """
  74. ts = libgis.TimeStamp()
  75. libgis.G_scan_timestamp(byref(ts), self._convert_timestamp())
  76. check = libgis.G_write_raster_timestamp(self.get_name(), byref(ts))
  77. if check == -1:
  78. core.error(_("Unable to create timestamp file for raster map <%s>"%(self.get_map_id())))
  79. if check == -2:
  80. core.error(_("Invalid datetime in timestamp for raster map <%s>"%(self.get_map_id())))
  81. def remove_timestamp_from_grass(self):
  82. """!Remove the timestamp from the grass file system based spatial database
  83. Internally the libgis API functions are used for removal
  84. """
  85. check = libgis.G_remove_raster_timestamp(self.get_name())
  86. if check == -1:
  87. core.error(_("Unable to remove timestamp for raster map <%s>"%(self.get_name())))
  88. def map_exists(self):
  89. """!Return True in case the map exists in the grass spatial database
  90. @return True if map exists, False otherwise
  91. """
  92. mapset = libgis.G_find_raster(self.get_name(), self.get_mapset())
  93. if not mapset:
  94. return False
  95. return True
  96. def read_info(self):
  97. """!Read the raster map info from the file system and store the content
  98. into a dictionary
  99. This method uses the ctypes interface to the gis and raster libraries
  100. to read the map metadata information
  101. """
  102. kvp = {}
  103. name = self.get_name()
  104. mapset = self.get_mapset()
  105. if not self.map_exists():
  106. core.fatal(_("Raster map <%s> not found" % name))
  107. # Read the region information
  108. region = libgis.Cell_head()
  109. libraster.Rast_get_cellhd(name, mapset, byref(region))
  110. kvp["north"] = region.north
  111. kvp["south"] = region.south
  112. kvp["east"] = region.east
  113. kvp["west"] = region.west
  114. kvp["nsres"] = region.ns_res
  115. kvp["ewres"] = region.ew_res
  116. kvp["rows"] = region.cols
  117. kvp["cols"] = region.rows
  118. maptype = libraster.Rast_map_type(name, mapset)
  119. if maptype == libraster.DCELL_TYPE:
  120. kvp["datatype"] = "DCELL"
  121. elif maptype == libraster.FCELL_TYPE:
  122. kvp["datatype"] = "FCELL"
  123. elif maptype == libraster.CELL_TYPE:
  124. kvp["datatype"] = "CELL"
  125. # Read range
  126. if libraster.Rast_map_is_fp(name, mapset):
  127. range = libraster.FPRange()
  128. libraster.Rast_init_fp_range (byref(range))
  129. ret = libraster.Rast_read_fp_range(name, mapset, byref(range))
  130. if ret < 0:
  131. core.fatal(_("Unable to read range file"))
  132. if ret == 2:
  133. kvp["min"] = None
  134. kvp["max"] = None
  135. else:
  136. min = libgis.DCELL()
  137. max = libgis.DCELL()
  138. libraster.Rast_get_fp_range_min_max(byref(range), byref(min), byref(max))
  139. kvp["min"] = min.value
  140. kvp["max"] = max.value
  141. else:
  142. range = libraster.Range()
  143. libraster.Rast_init_range (byref(range))
  144. ret = libraster.Rast_read_range(name, mapset, byref(range))
  145. if ret < 0:
  146. core.fatal(_("Unable to read range file"))
  147. if ret == 2:
  148. kvp["min"] = None
  149. kvp["max"] = None
  150. else:
  151. min = libgis.CELL()
  152. max = libgis.CELL()
  153. libraster.Rast_get_range_min_max(byref(range), byref(min), byref(max))
  154. kvp["min"] = min.value
  155. kvp["max"] = max.value
  156. return kvp
  157. def load(self):
  158. """!Load all info from an existing raster map into the internal structure"""
  159. # Fill base information
  160. self.base.set_name(self.ident.split("@")[0])
  161. self.base.set_mapset(self.ident.split("@")[1])
  162. self.base.set_creator(str(getpass.getuser()))
  163. # Get the data from an existing raster map
  164. kvp = self.read_info()
  165. # Fill spatial extent
  166. self.set_spatial_extent(north=kvp["north"], south=kvp["south"], \
  167. east=kvp["east"], west=kvp["west"])
  168. # Fill metadata
  169. self.metadata.set_nsres(kvp["nsres"])
  170. self.metadata.set_ewres(kvp["ewres"])
  171. self.metadata.set_datatype(kvp["datatype"])
  172. self.metadata.set_min(kvp["min"])
  173. self.metadata.set_max(kvp["max"])
  174. rows = kvp["rows"]
  175. cols = kvp["cols"]
  176. ncells = cols * rows
  177. self.metadata.set_cols(cols)
  178. self.metadata.set_rows(rows)
  179. self.metadata.set_number_of_cells(ncells)
  180. ###############################################################################
  181. class raster3d_dataset(abstract_map_dataset):
  182. """!Raster3d dataset class
  183. This class provides functions to select, update, insert or delete raster3d
  184. map information and valid time stamps into the SQL temporal database.
  185. """
  186. def __init__(self, ident):
  187. abstract_map_dataset.__init__(self)
  188. self.reset(ident)
  189. def get_type(self):
  190. return "raster3d"
  191. def get_new_instance(self, ident):
  192. """!Return a new instance with the type of this class"""
  193. return raster3d_dataset(ident)
  194. def get_new_stds_instance(self, ident):
  195. """!Return a new space time dataset instance in which maps are stored with the type of this class"""
  196. return space_time_raster3d_dataset(ident)
  197. def get_stds_register(self):
  198. """!Return the space time dataset register table name in which stds are listed in which this map is registered"""
  199. return self.metadata.get_str3ds_register()
  200. def set_stds_register(self, name):
  201. """!Set the space time dataset register table name in which stds are listed in which this map is registered"""
  202. self.metadata.set_str3ds_register(name)
  203. def spatial_overlapping(self, dataset):
  204. """!Return True if the spatial extents overlap"""
  205. if self.get_type() == dataset.get_type() or dataset.get_type() == "str3ds":
  206. return self.spatial_extent.overlapping(dataset.spatial_extent)
  207. else:
  208. return self.spatial_extent.overlapping_2d(dataset.spatial_extent)
  209. def spatial_relation(self, dataset):
  210. """!Return the two or three dimensional spatial relation"""
  211. if self.get_type() == dataset.get_type() or dataset.get_type() == "str3ds":
  212. return self.spatial_extent.spatial_relation(dataset.spatial_extent)
  213. else:
  214. return self.spatial_extent.spatial_relation_2d(dataset.spatial_extent)
  215. def reset(self, ident):
  216. """!Reset the internal structure and set the identifier"""
  217. self.ident = ident
  218. self.base = raster3d_base(ident=ident)
  219. self.absolute_time = raster3d_absolute_time(ident=ident)
  220. self.relative_time = raster3d_relative_time(ident=ident)
  221. self.spatial_extent = raster3d_spatial_extent(ident=ident)
  222. self.metadata = raster3d_metadata(ident=ident)
  223. def has_grass_timestamp(self):
  224. """!Check if a grass file bsased time stamp exists for this map.
  225. """
  226. if G_has_raster3d_timestamp(self.get_name(), self.get_mapset()):
  227. return True
  228. else:
  229. return False
  230. def write_timestamp_to_grass(self):
  231. """!Write the timestamp of this map into the map metadata in the grass file system based spatial
  232. database.
  233. Internally the libgis API functions are used for writing
  234. """
  235. ts = libgis.TimeStamp()
  236. libgis.G_scan_timestamp(byref(ts), self._convert_timestamp())
  237. check = libgis.G_write_raster3d_timestamp(self.get_name(), byref(ts))
  238. if check == -1:
  239. core.error(_("Unable to create timestamp file for raster3d map <%s>"%(self.get_map_id())))
  240. if check == -2:
  241. core.error(_("Invalid datetime in timestamp for raster3d map <%s>"%(self.get_map_id())))
  242. def remove_timestamp_from_grass(self):
  243. """!Remove the timestamp from the grass file system based spatial database
  244. Internally the libgis API functions are used for removal
  245. """
  246. check = libgis.G_remove_raster3d_timestamp(self.get_name())
  247. if check == -1:
  248. core.error(_("Unable to remove timestamp for raster3d map <%s>"%(self.get_name())))
  249. def map_exists(self):
  250. """!Return True in case the map exists in the grass spatial database
  251. @return True if map exists, False otherwise
  252. """
  253. mapset = libgis.G_find_raster3d(self.get_name(), self.get_mapset())
  254. if not mapset:
  255. return False
  256. return True
  257. def read_info(self):
  258. """!Read the raster3d map info from the file system and store the content
  259. into a dictionary
  260. This method uses the ctypes interface to the gis and raster3d libraries
  261. to read the map metadata information
  262. """
  263. kvp = {}
  264. name = self.get_name()
  265. mapset = self.get_mapset()
  266. if not self.map_exists():
  267. core.fatal(_("Raster3d map <%s> not found" % name))
  268. # Read the region information
  269. region = libraster3d.RASTER3D_Region()
  270. libraster3d.Rast3d_read_region_map(name, mapset, byref(region))
  271. kvp["north"] = region.north
  272. kvp["south"] = region.south
  273. kvp["east"] = region.east
  274. kvp["west"] = region.west
  275. kvp["nsres"] = region.ns_res
  276. kvp["ewres"] = region.ew_res
  277. kvp["tbres"] = region.tb_res
  278. kvp["rows"] = region.cols
  279. kvp["cols"] = region.rows
  280. kvp["depths"] = region.depths
  281. kvp["top"] = region.top
  282. kvp["bottom"] = region.bottom
  283. # We need to open the map, this function returns a void pointer
  284. # but we may need the correct type which is RASTER3D_Map, hence the casting
  285. g3map = cast(libraster3d.Rast3d_open_cell_old(name, mapset, \
  286. libraster3d.RASTER3D_DEFAULT_WINDOW, libraster3d.RASTER3D_TILE_SAME_AS_FILE, \
  287. libraster3d.RASTER3D_NO_CACHE), POINTER(libraster3d.RASTER3D_Map))
  288. if not g3map:
  289. core.fatal(_("Unable to open 3D raster map <%s>"%(name)));
  290. maptype = libraster3d.Rast3d_file_type_map(g3map)
  291. if maptype == libraster.DCELL_TYPE:
  292. kvp["datatype"] = "DCELL"
  293. elif maptype == libraster.FCELL_TYPE:
  294. kvp["datatype"] = "FCELL"
  295. # Read range
  296. min = libgis.DCELL()
  297. max = libgis.DCELL()
  298. ret = libraster3d.Rast3d_range_load(g3map)
  299. if not ret:
  300. core.fatal(_("Unable to load range of 3D raster map <%s>"%(name)));
  301. libraster3d.Rast3d_range_min_max(g3map, byref(min), byref(max))
  302. if min.value != min.value:
  303. kvp["min"] = None
  304. else:
  305. kvp["min"] = float(min.value)
  306. if max.value != max.value:
  307. kvp["max"] = None
  308. else:
  309. kvp["max"] = float(max.value)
  310. if not libraster3d.Rast3d_close(g3map):
  311. G_fatal_error(_("Unable to close 3D raster map <%s>"%(name)))
  312. return kvp
  313. def load(self):
  314. """!Load all info from an existing raster3d map into the internal structure"""
  315. # Fill base information
  316. self.base.set_name(self.ident.split("@")[0])
  317. self.base.set_mapset(self.ident.split("@")[1])
  318. self.base.set_creator(str(getpass.getuser()))
  319. # Fill spatial extent
  320. # Get the data from an existing raster map
  321. kvp = self.read_info()
  322. self.set_spatial_extent(north=kvp["north"], south=kvp["south"], \
  323. east=kvp["east"], west=kvp["west"],\
  324. top=kvp["top"], bottom=kvp["bottom"])
  325. # Fill metadata
  326. self.metadata.set_nsres(kvp["nsres"])
  327. self.metadata.set_ewres(kvp["ewres"])
  328. self.metadata.set_tbres(kvp["tbres"])
  329. self.metadata.set_datatype(kvp["datatype"])
  330. self.metadata.set_min(kvp["min"])
  331. self.metadata.set_max(kvp["max"])
  332. rows = kvp["rows"]
  333. cols = kvp["cols"]
  334. depths = kvp["depths"]
  335. ncells = cols * rows
  336. ncells = cols * rows * depths
  337. self.metadata.set_cols(cols)
  338. self.metadata.set_rows(rows)
  339. self.metadata.set_depths(depths)
  340. self.metadata.set_number_of_cells(ncells)
  341. ###############################################################################
  342. class vector_dataset(abstract_map_dataset):
  343. """!Vector dataset class
  344. This class provides functions to select, update, insert or delete vector
  345. map information and valid time stamps into the SQL temporal database.
  346. """
  347. def __init__(self, ident):
  348. abstract_map_dataset.__init__(self)
  349. self.reset(ident)
  350. def get_type(self):
  351. return "vector"
  352. def get_new_instance(self, ident):
  353. """!Return a new instance with the type of this class"""
  354. return vector_dataset(ident)
  355. def get_new_stds_instance(self, ident):
  356. """!Return a new space time dataset instance in which maps are stored with the type of this class"""
  357. return space_time_vector_dataset(ident)
  358. def get_stds_register(self):
  359. """!Return the space time dataset register table name in which stds are listed in which this map is registered"""
  360. return self.metadata.get_stvds_register()
  361. def set_stds_register(self, name):
  362. """!Set the space time dataset register table name in which stds are listed in which this map is registered"""
  363. self.metadata.set_stvds_register(name)
  364. def get_layer(self):
  365. """!Return the layer"""
  366. return self.base.get_layer()
  367. def spatial_overlapping(self, dataset):
  368. """!Return True if the spatial extents 2d overlap"""
  369. return self.spatial_extent.overlapping_2d(dataset.spatial_extent)
  370. def spatial_relation(self, dataset):
  371. """!Return the two dimensional spatial relation"""
  372. return self.spatial_extent.spatial_relation_2d(dataset.spatial_extent)
  373. def reset(self, ident):
  374. """!Reset the internal structure and set the identifier"""
  375. self.ident = ident
  376. self.base = vector_base(ident=ident)
  377. self.absolute_time = vector_absolute_time(ident=ident)
  378. self.relative_time = vector_relative_time(ident=ident)
  379. self.spatial_extent = vector_spatial_extent(ident=ident)
  380. self.metadata = vector_metadata(ident=ident)
  381. def has_grass_timestamp(self):
  382. """!Check if a grass file bsased time stamp exists for this map.
  383. """
  384. if G_has_raster_timestamp(self.get_name(), self.get_layer(), self.get_mapset()):
  385. return True
  386. else:
  387. return False
  388. def write_timestamp_to_grass(self):
  389. """!Write the timestamp of this map into the map metadata in the grass file system based spatial
  390. database.
  391. Internally the libgis API functions are used for writing
  392. """
  393. ts = libgis.TimeStamp()
  394. libgis.G_scan_timestamp(byref(ts), self._convert_timestamp())
  395. check = libgis.G_write_vector_timestamp(self.get_name(), self.get_layer(), byref(ts))
  396. if check == -1:
  397. core.error(_("Unable to create timestamp file for vector map <%s>"%(self.get_map_id())))
  398. if check == -2:
  399. core.error(_("Invalid datetime in timestamp for vector map <%s>"%(self.get_map_id())))
  400. def remove_timestamp_from_grass(self):
  401. """!Remove the timestamp from the grass file system based spatial database
  402. Internally the libgis API functions are used for removal
  403. """
  404. check = libgis.G_remove_vector_timestamp(self.get_name(), self.get_layer())
  405. if check == -1:
  406. core.error(_("Unable to remove timestamp for vector map <%s>"%(self.get_name())))
  407. def map_exists(self):
  408. """!Return True in case the map exists in the grass spatial database
  409. @return True if map exists, False otherwise
  410. """
  411. mapset = libgis.G_find_vector(self.get_name(), self.get_mapset())
  412. if not mapset:
  413. return False
  414. return True
  415. def read_info(self):
  416. """!Read the vector map info from the file system and store the content
  417. into a dictionary
  418. This method uses the ctypes interface to the vector libraries
  419. to read the map metadata information
  420. """
  421. kvp = {}
  422. name = self.get_name()
  423. mapset = self.get_mapset()
  424. if not self.map_exists():
  425. core.fatal(_("Vector map <%s> not found" % name))
  426. # The vector map structure
  427. Map = libvector.Map_info()
  428. # We open the maps always in topology mode first
  429. libvector.Vect_set_open_level(2)
  430. with_topo = True
  431. # Code lend from v.info main.c
  432. if libvector.Vect_open_old_head2(byref(Map), name, mapset, "1") < 2:
  433. # force level 1, open fully
  434. # NOTE: number of points, lines, boundaries, centroids, faces, kernels is still available
  435. libvector.Vect_set_open_level(1) # no topology
  436. with_topo = False
  437. core.message(_("Open map without topology support"))
  438. if libvector.Vect_open_old2(byref(Map), name, mapset, "1") < 1:
  439. core.fatal(_("Unable to open vector map <%s>"%(libvector.Vect_get_full_name(byref(Map)))))
  440. # Read the extent information
  441. bbox = libvector.bound_box()
  442. libvector.Vect_get_map_box(byref(Map), byref(bbox));
  443. kvp["north"] = bbox.N
  444. kvp["south"] = bbox.S
  445. kvp["east"] = bbox.E
  446. kvp["west"] = bbox.W
  447. kvp["top"] = bbox.T
  448. kvp["bottom"] = bbox.B
  449. kvp["is_3d"] = bool(libvector.Vect_is_3d(byref(Map)))
  450. # Read number of features
  451. if with_topo:
  452. kvp["points"] = libvector.Vect_get_num_primitives(byref(Map), libvector.GV_POINT)
  453. kvp["lines"] = libvector.Vect_get_num_primitives(byref(Map), libvector.GV_LINE)
  454. kvp["boundaries"] = libvector.Vect_get_num_primitives(byref(Map), libvector.GV_BOUNDARY)
  455. kvp["centroids"] = libvector.Vect_get_num_primitives(byref(Map), libvector.GV_CENTROID)
  456. kvp["faces"] = libvector.Vect_get_num_primitives(byref(Map), libvector.GV_FACE)
  457. kvp["kernels"] = libvector.Vect_get_num_primitives(byref(Map), libvector.GV_KERNEL)
  458. # Summarize the primitives
  459. kvp["primitives"] = kvp["points"] + kvp["lines"] + kvp["boundaries"] + kvp["centroids"]
  460. if kvp["is_3d"]:
  461. kvp["primitives"] += kvp["faces"] + kvp["kernels"]
  462. # Read topology information
  463. kvp["nodes"] = libvector.Vect_get_num_nodes(byref(Map))
  464. kvp["areas"] = libvector.Vect_get_num_areas(byref(Map))
  465. kvp["islands"] = libvector.Vect_get_num_islands(byref(Map))
  466. kvp["holes"] = libvector.Vect_get_num_holes(byref(Map))
  467. kvp["volumes"] = libvector.Vect_get_num_primitives(byref(Map), libvector.GV_VOLUME)
  468. else:
  469. kvp["points"] = None
  470. kvp["lines"] = None
  471. kvp["boundaries"] = None
  472. kvp["centroids"] = None
  473. kvp["faces"] = None
  474. kvp["kernels"] = None
  475. kvp["primitives"] = None
  476. kvp["nodes"] = None
  477. kvp["areas"] = None
  478. kvp["islands"] = None
  479. kvp["holes"] = None
  480. kvp["volumes"] = None
  481. libvector.Vect_close(byref(Map))
  482. return kvp
  483. def load(self):
  484. """!Load all info from an existing vector map into the internal structure"""
  485. # Fill base information
  486. if self.ident.find(":") >= 0:
  487. self.base.set_name(self.ident.split("@")[0].split(":")[0])
  488. self.base.set_layer(self.ident.split("@")[0].split(":")[1])
  489. else:
  490. self.base.set_name(self.ident.split("@")[0])
  491. self.base.set_mapset(self.ident.split("@")[1])
  492. self.base.set_creator(str(getpass.getuser()))
  493. # Get the data from an existing raster map
  494. kvp = self.read_info()
  495. # Fill spatial extent
  496. self.set_spatial_extent(north=kvp["north"], south=kvp["south"], \
  497. east=kvp["east"], west=kvp["west"],\
  498. top=kvp["top"], bottom=kvp["bottom"])
  499. # Fill metadata
  500. self.metadata.set_3d_info(kvp["is_3d"])
  501. self.metadata.set_points(kvp["points"])
  502. self.metadata.set_lines(kvp["lines"])
  503. self.metadata.set_boundaries(kvp["boundaries"])
  504. self.metadata.set_centroids(kvp["centroids"])
  505. self.metadata.set_faces(kvp["faces"])
  506. self.metadata.set_kernels(kvp["kernels"])
  507. self.metadata.set_primitives(kvp["primitives"])
  508. self.metadata.set_nodes(kvp["nodes"])
  509. self.metadata.set_areas(kvp["areas"])
  510. self.metadata.set_islands(kvp["islands"])
  511. self.metadata.set_holes(kvp["holes"])
  512. self.metadata.set_volumes(kvp["volumes"])
  513. ###############################################################################
  514. class space_time_raster_dataset(abstract_space_time_dataset):
  515. """!Space time raster dataset class
  516. """
  517. def __init__(self, ident):
  518. abstract_space_time_dataset.__init__(self, ident)
  519. def get_type(self):
  520. return "strds"
  521. def get_new_instance(self, ident):
  522. """!Return a new instance with the type of this class"""
  523. return space_time_raster_dataset(ident)
  524. def get_new_map_instance(self, ident):
  525. """!Return a new instance of a map dataset which is associated with the type of this class"""
  526. return raster_dataset(ident)
  527. def get_map_register(self):
  528. """!Return the name of the map register table"""
  529. return self.metadata.get_raster_register()
  530. def set_map_register(self, name):
  531. """!Set the name of the map register table"""
  532. self.metadata.set_raster_register(name)
  533. def spatial_overlapping(self, dataset):
  534. """!Return True if the spatial extents 2d overlap"""
  535. return self.spatial_extent.overlapping_2d(dataset.spatial_extent)
  536. def spatial_relation(self, dataset):
  537. """!Return the two dimensional spatial relation"""
  538. return self.spatial_extent.spatial_relation_2d(dataset.spatial_extent)
  539. def reset(self, ident):
  540. """!Reset the internal structure and set the identifier"""
  541. self.ident = ident
  542. self.base = strds_base(ident=ident)
  543. if ident != None:
  544. self.base.set_name(self.ident.split("@")[0])
  545. self.base.set_mapset(self.ident.split("@")[1])
  546. self.base.set_creator(str(getpass.getuser()))
  547. self.absolute_time = strds_absolute_time(ident=ident)
  548. self.relative_time = strds_relative_time(ident=ident)
  549. self.spatial_extent = strds_spatial_extent(ident=ident)
  550. self.metadata = strds_metadata(ident=ident)
  551. ###############################################################################
  552. class space_time_raster3d_dataset(abstract_space_time_dataset):
  553. """!Space time raster3d dataset class
  554. """
  555. def __init__(self, ident):
  556. abstract_space_time_dataset.__init__(self, ident)
  557. def get_type(self):
  558. return "str3ds"
  559. def get_new_instance(self, ident):
  560. """!Return a new instance with the type of this class"""
  561. return space_time_raster3d_dataset(ident)
  562. def get_new_map_instance(self, ident):
  563. """!Return a new instance of a map dataset which is associated with the type of this class"""
  564. return raster3d_dataset(ident)
  565. def get_map_register(self):
  566. """!Return the name of the map register table"""
  567. return self.metadata.get_raster3d_register()
  568. def set_map_register(self, name):
  569. """!Set the name of the map register table"""
  570. self.metadata.set_raster3d_register(name)
  571. def spatial_overlapping(self, dataset):
  572. """!Return True if the spatial extents overlap"""
  573. if self.get_type() == dataset.get_type() or dataset.get_type() == "str3ds":
  574. return self.spatial_extent.overlapping(dataset.spatial_extent)
  575. else:
  576. return self.spatial_extent.overlapping_2d(dataset.spatial_extent)
  577. def spatial_relation(self, dataset):
  578. """!Return the two or three dimensional spatial relation"""
  579. if self.get_type() == dataset.get_type() or dataset.get_type() == "str3ds":
  580. return self.spatial_extent.spatial_relation(dataset.spatial_extent)
  581. else:
  582. return self.spatial_extent.spatial_relation_2d(dataset.spatial_extent)
  583. def reset(self, ident):
  584. """!Reset the internal structure and set the identifier"""
  585. self.ident = ident
  586. self.base = str3ds_base(ident=ident)
  587. if ident != None:
  588. self.base.set_name(self.ident.split("@")[0])
  589. self.base.set_mapset(self.ident.split("@")[1])
  590. self.base.set_creator(str(getpass.getuser()))
  591. self.absolute_time = str3ds_absolute_time(ident=ident)
  592. self.relative_time = str3ds_relative_time(ident=ident)
  593. self.spatial_extent = str3ds_spatial_extent(ident=ident)
  594. self.metadata = str3ds_metadata(ident=ident)
  595. ###############################################################################
  596. class space_time_vector_dataset(abstract_space_time_dataset):
  597. """!Space time vector dataset class
  598. """
  599. def __init__(self, ident):
  600. abstract_space_time_dataset.__init__(self, ident)
  601. def get_type(self):
  602. return "stvds"
  603. def get_new_instance(self, ident):
  604. """!Return a new instance with the type of this class"""
  605. return space_time_vector_dataset(ident)
  606. def get_new_map_instance(self, ident):
  607. """!Return a new instance of a map dataset which is associated with the type of this class"""
  608. return vector_dataset(ident)
  609. def get_map_register(self):
  610. """!Return the name of the map register table"""
  611. return self.metadata.get_vector_register()
  612. def set_map_register(self, name):
  613. """!Set the name of the map register table"""
  614. self.metadata.set_vector_register(name)
  615. def spatial_overlapping(self, dataset):
  616. """!Return True if the spatial extents 2d overlap"""
  617. return self.spatial_extent.overlapping_2d(dataset.spatial_extent)
  618. def spatial_relation(self, dataset):
  619. """!Return the two dimensional spatial relation"""
  620. return self.spatial_extent.spatial_relation_2d(dataset.spatial_extent)
  621. def reset(self, ident):
  622. """!Reset the internal structure and set the identifier"""
  623. self.ident = ident
  624. self.base = stvds_base(ident=ident)
  625. if ident != None:
  626. self.base.set_name(self.ident.split("@")[0])
  627. self.base.set_mapset(self.ident.split("@")[1])
  628. self.base.set_creator(str(getpass.getuser()))
  629. self.absolute_time = stvds_absolute_time(ident=ident)
  630. self.relative_time = stvds_relative_time(ident=ident)
  631. self.spatial_extent = stvds_spatial_extent(ident=ident)
  632. self.metadata = stvds_metadata(ident=ident)