c_libraries_interface.py 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300
  1. # -*- coding: utf-8 -*-
  2. """
  3. Fast and exit-safe interface to GRASS C-library functions
  4. using ctypes and multiprocessing
  5. (C) 2013 by the GRASS Development Team
  6. This program is free software under the GNU General Public
  7. License (>=v2). Read the file COPYING that comes with GRASS
  8. for details.
  9. :authors: Soeren Gebbert
  10. """
  11. import sys
  12. from multiprocessing import Process, Lock, Pipe
  13. import logging
  14. from ctypes import *
  15. from core import *
  16. import grass.lib.gis as libgis
  17. import grass.lib.raster as libraster
  18. import grass.lib.vector as libvector
  19. import grass.lib.date as libdate
  20. import grass.lib.raster3d as libraster3d
  21. import grass.lib.temporal as libtgis
  22. ###############################################################################
  23. class RPCDefs(object):
  24. # Function identifier and index
  25. STOP = 0
  26. HAS_TIMESTAMP = 1
  27. WRITE_TIMESTAMP = 2
  28. READ_TIMESTAMP = 3
  29. REMOVE_TIMESTAMP = 4
  30. READ_MAP_INFO = 5
  31. MAP_EXISTS = 6
  32. READ_MAP_INFO = 7
  33. AVAILABLE_MAPSETS = 8
  34. GET_DRIVER_NAME = 9
  35. GET_DATABASE_NAME = 10
  36. G_MAPSET = 11
  37. G_LOCATION = 12
  38. G_GISDBASE = 13
  39. G_FATAL_ERROR = 14
  40. TYPE_RASTER = 0
  41. TYPE_RASTER3D = 1
  42. TYPE_VECTOR = 2
  43. ###############################################################################
  44. def _fatal_error(lock, conn, data):
  45. """Calls G_fatal_error()"""
  46. libgis.G_fatal_error("Fatal Error in C library server")
  47. def _get_mapset(lock, conn, data):
  48. """Return the current mapset
  49. :param lock: A multiprocessing.Lock instance
  50. :param conn: A multiprocessing.Pipe instance used to send True or False
  51. :param data: The mapset as list entry 1 [function_id]
  52. :returns: Name of the current mapset
  53. """
  54. mapset = libgis.G_mapset()
  55. conn.send(mapset)
  56. def _get_location(lock, conn, data):
  57. """Return the current location
  58. :param lock: A multiprocessing.Lock instance
  59. :param conn: A multiprocessing.Pipe instance used to send True or False
  60. :param data: The mapset as list entry 1 [function_id]
  61. :returns: Name of the location
  62. """
  63. location = libgis.G_location()
  64. conn.send(location)
  65. def _get_gisdbase(lock, conn, data):
  66. """Return the current gisdatabase
  67. :param lock: A multiprocessing.Lock instance
  68. :param conn: A multiprocessing.Pipe instance used to send True or False
  69. :param data: The mapset as list entry 1 [function_id]
  70. :returns: Name of the gisdatabase
  71. """
  72. gisdbase = libgis.G_gisdbase()
  73. conn.send(gisdbase)
  74. def _get_driver_name(lock, conn, data):
  75. """Return the temporal database driver of a specific mapset
  76. :param lock: A multiprocessing.Lock instance
  77. :param conn: A multiprocessing.Pipe instance used to send True or False
  78. :param data: The mapset as list entry 1 [function_id, mapset]
  79. :returns: Name of the driver or None if no temporal database present
  80. """
  81. mapset = data[1]
  82. if not mapset:
  83. mapset = libgis.G_mapset()
  84. drstring = libtgis.tgis_get_mapset_driver_name(mapset)
  85. conn.send(drstring)
  86. ###############################################################################
  87. def _get_database_name(lock, conn, data):
  88. """Return the temporal database name of a specific mapset
  89. :param lock: A multiprocessing.Lock instance
  90. :param conn: A multiprocessing.Pipe instance used to send True or False
  91. :param data: The mapset as list entry 1 [function_id, mapset]
  92. :returns: Name of the database or None if no temporal database present
  93. """
  94. mapset = data[1]
  95. if not mapset:
  96. mapset = libgis.G_mapset()
  97. dbstring = libtgis.tgis_get_mapset_database_name(mapset)
  98. if dbstring:
  99. # We substitute GRASS variables if they are located in the database string
  100. # This behavior is in conjunction with db.connect
  101. dbstring = dbstring.replace("$GISDBASE", libgis.G_gisdbase())
  102. dbstring = dbstring.replace("$LOCATION_NAME", libgis.G_location())
  103. dbstring = dbstring.replace("$MAPSET", mapset)
  104. conn.send(dbstring)
  105. ###############################################################################
  106. def _available_mapsets(lock, conn, data):
  107. """Return all available mapsets the user can access as a list of strings
  108. :param lock: A multiprocessing.Lock instance
  109. :param conn: A multiprocessing.Pipe instance used to send True or False
  110. :param data: The list of data entries [function_id]
  111. :returns: Names of available mapsets as list of strings
  112. """
  113. count = 0
  114. mapset_list = []
  115. try:
  116. # Initilaize the accessable mapset list, this is bad C design!!!
  117. libgis.G_get_mapset_name(0)
  118. mapsets = libgis.G_get_available_mapsets()
  119. while mapsets[count]:
  120. char_list = ""
  121. mapset = mapsets[count]
  122. if libgis.G_mapset_permissions(mapset) == 1 and libgis.G_is_mapset_in_search_path(mapset) == 1:
  123. c = 0
  124. while mapset[c] != "\x00":
  125. char_list += mapset[c]
  126. c += 1
  127. mapset_list.append(char_list)
  128. count += 1
  129. # We need to sort the mapset list, but the first one should be
  130. # the current mapset
  131. current_mapset = libgis.G_mapset()
  132. if current_mapset in mapset_list:
  133. mapset_list.remove(current_mapset)
  134. mapset_list.sort()
  135. mapset_list.reverse()
  136. mapset_list.append(current_mapset)
  137. mapset_list.reverse()
  138. except:
  139. raise
  140. finally:
  141. conn.send(mapset_list)
  142. def _has_timestamp(lock, conn, data):
  143. """Check if the file based GRASS timestamp is present and send
  144. True or False using the provided pipe.
  145. :param lock: A multiprocessing.Lock instance
  146. :param conn: A multiprocessing.Pipe instance used to send True or False
  147. :param data: The list of data entries [function_id, maptype, name,
  148. mapset, layer]
  149. """
  150. maptype = data[1]
  151. name = data[2]
  152. mapset = data[3]
  153. layer = data[4]
  154. check = False
  155. if maptype == RPCDefs.TYPE_RASTER:
  156. if libgis.G_has_raster_timestamp(name, mapset) == 1:
  157. check = True
  158. elif maptype == RPCDefs.TYPE_VECTOR:
  159. if libgis.G_has_vector_timestamp(name, layer, mapset) == 1:
  160. check = True
  161. elif maptype == RPCDefs.TYPE_RASTER3D:
  162. if libgis.G_has_raster3d_timestamp(name, mapset) == 1:
  163. check = True
  164. conn.send(check)
  165. ###############################################################################
  166. def _read_timestamp(lock, conn, data):
  167. """Read the file based GRASS timestamp and send
  168. the result using the provided pipe.
  169. The tuple to be send via pipe: (return value of G_read_*_timestamp,
  170. timestamps).
  171. Please have a look at the documentation of G_read_raster_timestamp,
  172. G_read_vector_timestamp and G_read_raster3d_timestamp for the return
  173. values description.
  174. The timestamps to be send are tuples of values:
  175. - relative time (start, end, unit), start and end are of type
  176. integer, unit is of type string.
  177. - absolute time (start, end), start and end are of type datetime
  178. The end time may be None in case of a time instance.
  179. :param lock: A multiprocessing.Lock instance
  180. :param conn: A multiprocessing.Pipe instance used to send the result
  181. :param data: The list of data entries [function_id, maptype, name,
  182. mapset, layer]
  183. """
  184. maptype = data[1]
  185. name = data[2]
  186. mapset = data[3]
  187. layer = data[4]
  188. check = False
  189. ts = libgis.TimeStamp()
  190. if maptype == RPCDefs.TYPE_RASTER:
  191. check = libgis.G_read_raster_timestamp(name, mapset, byref(ts))
  192. elif maptype == RPCDefs.TYPE_VECTOR:
  193. check = libgis.G_read_vector_timestamp(name, layer, mapset, byref(ts))
  194. elif maptype == RPCDefs.TYPE_RASTER3D:
  195. check = libgis.G_read_raster3d_timestamp(name, mapset, byref(ts))
  196. dates = _convert_timestamp_from_grass(ts)
  197. conn.send((check, dates))
  198. ###############################################################################
  199. def _write_timestamp(lock, conn, data):
  200. """Write the file based GRASS timestamp
  201. the return values of the called C-functions using the provided pipe.
  202. The value to be send via pipe is the return value of G_write_*_timestamp.
  203. Please have a look at the documentation of G_write_raster_timestamp,
  204. G_write_vector_timestamp and G_write_raster3d_timestamp for the return
  205. values description.
  206. :param lock: A multiprocessing.Lock instance
  207. :param conn: A multiprocessing.Pipe instance used to send True or False
  208. :param data: The list of data entries [function_id, maptype, name,
  209. mapset, layer, timestring]
  210. """
  211. maptype = data[1]
  212. name = data[2]
  213. mapset = data[3]
  214. layer = data[4]
  215. timestring = data[5]
  216. check = -3
  217. ts = libgis.TimeStamp()
  218. check = libgis.G_scan_timestamp(byref(ts), timestring)
  219. if check != 1:
  220. logging.error("Unable to convert the timestamp: " + timestring)
  221. return -2
  222. if maptype == RPCDefs.TYPE_RASTER:
  223. check = libgis.G_write_raster_timestamp(name, byref(ts))
  224. elif maptype == RPCDefs.TYPE_VECTOR:
  225. check = libgis.G_write_vector_timestamp(name, layer, byref(ts))
  226. elif maptype == RPCDefs.TYPE_RASTER3D:
  227. check = libgis.G_write_raster3d_timestamp(name, byref(ts))
  228. conn.send(check)
  229. ###############################################################################
  230. def _remove_timestamp(lock, conn, data):
  231. """Remove the file based GRASS timestamp
  232. the return values of the called C-functions using the provided pipe.
  233. The value to be send via pipe is the return value of G_remove_*_timestamp.
  234. Please have a look at the documentation of G_remove_raster_timestamp,
  235. G_remove_vector_timestamp and G_remove_raster3d_timestamp for the return
  236. values description.
  237. :param lock: A multiprocessing.Lock instance
  238. :param conn: A multiprocessing.Pipe instance used to send True or False
  239. :param data: The list of data entries [function_id, maptype, name,
  240. mapset, layer]
  241. """
  242. maptype = data[1]
  243. name = data[2]
  244. mapset = data[3]
  245. layer = data[4]
  246. check = False
  247. if maptype == RPCDefs.TYPE_RASTER:
  248. check = libgis.G_remove_raster_timestamp(name, mapset)
  249. elif maptype == RPCDefs.TYPE_VECTOR:
  250. check = libgis.G_remove_vector_timestamp(name, layer, mapset)
  251. elif maptype == RPCDefs.TYPE_RASTER3D:
  252. check = libgis.G_remove_raster3d_timestamp(name, mapset)
  253. conn.send(check)
  254. ###############################################################################
  255. def _map_exists(lock, conn, data):
  256. """Check if a map exists in the spatial database
  257. The value to be send via pipe is True in case the map exists and False
  258. if not.
  259. :param lock: A multiprocessing.Lock instance
  260. :param conn: A multiprocessing.Pipe instance used to send True or False
  261. :param data: The list of data entries [function_id, maptype, name, mapset]
  262. """
  263. maptype = data[1]
  264. name = data[2]
  265. mapset = data[3]
  266. check = False
  267. if maptype == RPCDefs.TYPE_RASTER:
  268. mapset = libgis.G_find_raster(name, mapset)
  269. elif maptype == RPCDefs.TYPE_VECTOR:
  270. mapset = libgis.G_find_vector(name, mapset)
  271. elif maptype == RPCDefs.TYPE_RASTER3D:
  272. mapset = libgis.G_find_raster3d(name, mapset)
  273. if mapset:
  274. check = True
  275. conn.send(check)
  276. ###############################################################################
  277. def _read_map_info(lock, conn, data):
  278. """Read map specific metadata from the spatial database using C-library
  279. functions
  280. :param lock: A multiprocessing.Lock instance
  281. :param conn: A multiprocessing.Pipe instance used to send True or False
  282. :param data: The list of data entries [function_id, maptype, name, mapset]
  283. """
  284. maptype = data[1]
  285. name = data[2]
  286. mapset = data[3]
  287. if maptype == RPCDefs.TYPE_RASTER:
  288. kvp = _read_raster_info(name, mapset)
  289. elif maptype == RPCDefs.TYPE_VECTOR:
  290. kvp = _read_vector_info(name, mapset)
  291. elif maptype == RPCDefs.TYPE_RASTER3D:
  292. kvp = _read_raster3d_info(name, mapset)
  293. conn.send(kvp)
  294. ###############################################################################
  295. def _read_raster_info(name, mapset):
  296. """Read the raster map info from the file system and store the content
  297. into a dictionary
  298. This method uses the ctypes interface to the gis and raster libraries
  299. to read the map metadata information
  300. :param name: The name of the map
  301. :param mapset: The mapset of the map
  302. :returns: The key value pairs of the map specific metadata, or None in
  303. case of an error
  304. """
  305. kvp = {}
  306. if not libgis.G_find_raster(name, mapset):
  307. return None
  308. # Read the region information
  309. region = libgis.Cell_head()
  310. libraster.Rast_get_cellhd(name, mapset, byref(region))
  311. kvp["north"] = region.north
  312. kvp["south"] = region.south
  313. kvp["east"] = region.east
  314. kvp["west"] = region.west
  315. kvp["nsres"] = region.ns_res
  316. kvp["ewres"] = region.ew_res
  317. kvp["rows"] = region.cols
  318. kvp["cols"] = region.rows
  319. maptype = libraster.Rast_map_type(name, mapset)
  320. if maptype == libraster.DCELL_TYPE:
  321. kvp["datatype"] = "DCELL"
  322. elif maptype == libraster.FCELL_TYPE:
  323. kvp["datatype"] = "FCELL"
  324. elif maptype == libraster.CELL_TYPE:
  325. kvp["datatype"] = "CELL"
  326. # Read range
  327. if libraster.Rast_map_is_fp(name, mapset):
  328. range = libraster.FPRange()
  329. libraster.Rast_init_fp_range(byref(range))
  330. ret = libraster.Rast_read_fp_range(name, mapset, byref(range))
  331. if ret < 0:
  332. logging.error(_("Unable to read range file"))
  333. return None
  334. if ret == 2:
  335. kvp["min"] = None
  336. kvp["max"] = None
  337. else:
  338. min = libgis.DCELL()
  339. max = libgis.DCELL()
  340. libraster.Rast_get_fp_range_min_max(
  341. byref(range), byref(min), byref(max))
  342. kvp["min"] = min.value
  343. kvp["max"] = max.value
  344. else:
  345. range = libraster.Range()
  346. libraster.Rast_init_range(byref(range))
  347. ret = libraster.Rast_read_range(name, mapset, byref(range))
  348. if ret < 0:
  349. logging.error(_("Unable to read range file"))
  350. return None
  351. if ret == 2:
  352. kvp["min"] = None
  353. kvp["max"] = None
  354. else:
  355. min = libgis.CELL()
  356. max = libgis.CELL()
  357. libraster.Rast_get_range_min_max(
  358. byref(range), byref(min), byref(max))
  359. kvp["min"] = min.value
  360. kvp["max"] = max.value
  361. return kvp
  362. ###############################################################################
  363. def _read_raster3d_info(name, mapset):
  364. """Read the 3D raster map info from the file system and store the content
  365. into a dictionary
  366. This method uses the ctypes interface to the gis and raster3d libraries
  367. to read the map metadata information
  368. :param name: The name of the map
  369. :param mapset: The mapset of the map
  370. :returns: The key value pairs of the map specific metadata, or None in
  371. case of an error
  372. """
  373. kvp = {}
  374. if not libgis.G_find_raster3d(name, mapset):
  375. return None
  376. # Read the region information
  377. region = libraster3d.RASTER3D_Region()
  378. libraster3d.Rast3d_read_region_map(name, mapset, byref(region))
  379. kvp["north"] = region.north
  380. kvp["south"] = region.south
  381. kvp["east"] = region.east
  382. kvp["west"] = region.west
  383. kvp["nsres"] = region.ns_res
  384. kvp["ewres"] = region.ew_res
  385. kvp["tbres"] = region.tb_res
  386. kvp["rows"] = region.cols
  387. kvp["cols"] = region.rows
  388. kvp["depths"] = region.depths
  389. kvp["top"] = region.top
  390. kvp["bottom"] = region.bottom
  391. # We need to open the map, this function returns a void pointer
  392. # but we may need the correct type which is RASTER3D_Map, hence
  393. # the casting
  394. g3map = cast(libraster3d.Rast3d_open_cell_old(name, mapset,
  395. libraster3d.RASTER3D_DEFAULT_WINDOW,
  396. libraster3d.RASTER3D_TILE_SAME_AS_FILE,
  397. libraster3d.RASTER3D_NO_CACHE),
  398. POINTER(libraster3d.RASTER3D_Map))
  399. if not g3map:
  400. logging.error(_("Unable to open 3D raster map <%s>" % (name)))
  401. return None
  402. maptype = libraster3d.Rast3d_file_type_map(g3map)
  403. if maptype == libraster.DCELL_TYPE:
  404. kvp["datatype"] = "DCELL"
  405. elif maptype == libraster.FCELL_TYPE:
  406. kvp["datatype"] = "FCELL"
  407. # Read range
  408. min = libgis.DCELL()
  409. max = libgis.DCELL()
  410. ret = libraster3d.Rast3d_range_load(g3map)
  411. if not ret:
  412. logging.error(_("Unable to load range of 3D raster map <%s>" %
  413. (name)))
  414. return None
  415. libraster3d.Rast3d_range_min_max(g3map, byref(min), byref(max))
  416. if min.value != min.value:
  417. kvp["min"] = None
  418. else:
  419. kvp["min"] = float(min.value)
  420. if max.value != max.value:
  421. kvp["max"] = None
  422. else:
  423. kvp["max"] = float(max.value)
  424. if not libraster3d.Rast3d_close(g3map):
  425. logging.error(_("Unable to close 3D raster map <%s>" % (name)))
  426. return None
  427. return kvp
  428. ###############################################################################
  429. def _read_vector_info(name, mapset):
  430. """Read the vector map info from the file system and store the content
  431. into a dictionary
  432. This method uses the ctypes interface to the vector libraries
  433. to read the map metadata information
  434. :param name: The name of the map
  435. :param mapset: The mapset of the map
  436. :returns: The key value pairs of the map specific metadata, or None in
  437. case of an error
  438. """
  439. kvp = {}
  440. if not libgis.G_find_vector(name, mapset):
  441. return None
  442. # The vector map structure
  443. Map = libvector.Map_info()
  444. # We open the maps always in topology mode first
  445. libvector.Vect_set_open_level(2)
  446. with_topo = True
  447. # Code lend from v.info main.c
  448. if libvector.Vect_open_old_head2(byref(Map), name, mapset, "1") < 2:
  449. # force level 1, open fully
  450. # NOTE: number of points, lines, boundaries, centroids,
  451. # faces, kernels is still available
  452. libvector.Vect_set_open_level(1) # no topology
  453. with_topo = False
  454. if libvector.Vect_open_old2(byref(Map), name, mapset, "1") < 1:
  455. logging.error(_("Unable to open vector map <%s>" %
  456. (libvector.Vect_get_full_name(byref(Map)))))
  457. return None
  458. # Release the vector spatial index memory when closed
  459. libvector.Vect_set_release_support(byref(Map))
  460. # Read the extent information
  461. bbox = libvector.bound_box()
  462. libvector.Vect_get_map_box(byref(Map), byref(bbox))
  463. kvp["north"] = bbox.N
  464. kvp["south"] = bbox.S
  465. kvp["east"] = bbox.E
  466. kvp["west"] = bbox.W
  467. kvp["top"] = bbox.T
  468. kvp["bottom"] = bbox.B
  469. kvp["map3d"] = bool(libvector.Vect_is_3d(byref(Map)))
  470. # Read number of features
  471. if with_topo:
  472. kvp["points"] = libvector.Vect_get_num_primitives(
  473. byref(Map), libvector.GV_POINT)
  474. kvp["lines"] = libvector.Vect_get_num_primitives(
  475. byref(Map), libvector.GV_LINE)
  476. kvp["boundaries"] = libvector.Vect_get_num_primitives(
  477. byref(Map), libvector.GV_BOUNDARY)
  478. kvp["centroids"] = libvector.Vect_get_num_primitives(
  479. byref(Map), libvector.GV_CENTROID)
  480. kvp["faces"] = libvector.Vect_get_num_primitives(
  481. byref(Map), libvector.GV_FACE)
  482. kvp["kernels"] = libvector.Vect_get_num_primitives(
  483. byref(Map), libvector.GV_KERNEL)
  484. # Summarize the primitives
  485. kvp["primitives"] = kvp["points"] + kvp["lines"] + \
  486. kvp["boundaries"] + kvp["centroids"]
  487. if kvp["map3d"]:
  488. kvp["primitives"] += kvp["faces"] + kvp["kernels"]
  489. # Read topology information
  490. kvp["nodes"] = libvector.Vect_get_num_nodes(byref(Map))
  491. kvp["areas"] = libvector.Vect_get_num_areas(byref(Map))
  492. kvp["islands"] = libvector.Vect_get_num_islands(byref(Map))
  493. kvp["holes"] = libvector.Vect_get_num_holes(byref(Map))
  494. kvp["volumes"] = libvector.Vect_get_num_primitives(
  495. byref(Map), libvector.GV_VOLUME)
  496. else:
  497. kvp["points"] = None
  498. kvp["lines"] = None
  499. kvp["boundaries"] = None
  500. kvp["centroids"] = None
  501. kvp["faces"] = None
  502. kvp["kernels"] = None
  503. kvp["primitives"] = None
  504. kvp["nodes"] = None
  505. kvp["areas"] = None
  506. kvp["islands"] = None
  507. kvp["holes"] = None
  508. kvp["volumes"] = None
  509. libvector.Vect_close(byref(Map))
  510. return kvp
  511. ###############################################################################
  512. def _convert_timestamp_from_grass(ts):
  513. """Convert a GRASS file based timestamp into the temporal framework
  514. format datetime or integer.
  515. A tuple of two datetime objects (start, end) is returned in case of
  516. absolute time.
  517. In case of relative time a tuple with start time, end time and the
  518. relative unit (start, end, unit) will be returned.
  519. Note:
  520. The end time will be set to None in case of a time instance.
  521. :param ts grass.lib.gis.TimeStamp object created by G_read_*_timestamp
  522. """
  523. dt1 = libgis.DateTime()
  524. dt2 = libgis.DateTime()
  525. count = c_int()
  526. libgis.G_get_timestamps(byref(ts),
  527. byref(dt1),
  528. byref(dt2),
  529. byref(count))
  530. if dt1.mode == libdate.DATETIME_ABSOLUTE:
  531. pdt1 = None
  532. pdt2 = None
  533. if count.value >= 1:
  534. pdt1 = datetime(int(dt1.year), int(dt1.month), int(dt1.day),
  535. int(dt1.hour), int(dt1.minute),
  536. int(dt1.second))
  537. if count.value == 2:
  538. pdt2 = datetime(int(dt2.year), int(dt2.month), int(dt2.day),
  539. int(dt2.hour), int(dt2.minute),
  540. int(dt2.second))
  541. # ATTENTION: We ignore the time zone
  542. # TODO: Write time zone support
  543. return (pdt1, pdt2)
  544. else:
  545. unit = None
  546. start = None
  547. end = None
  548. if count.value >= 1:
  549. if dt1.year > 0:
  550. unit = "years"
  551. start = dt1.year
  552. elif dt1.month > 0:
  553. unit = "months"
  554. start = dt1.month
  555. elif dt1.day > 0:
  556. unit = "days"
  557. start = dt1.day
  558. elif dt1.hour > 0:
  559. unit = "hours"
  560. start = dt1.hour
  561. elif dt1.minute > 0:
  562. unit = "minutes"
  563. start = dt1.minute
  564. elif dt1.second > 0:
  565. unit = "seconds"
  566. start = dt1.second
  567. if count.value == 2:
  568. if dt2.year > 0:
  569. end = dt2.year
  570. elif dt2.month > 0:
  571. end = dt2.month
  572. elif dt2.day > 0:
  573. end = dt2.day
  574. elif dt2.hour > 0:
  575. end = dt2.hour
  576. elif dt2.minute > 0:
  577. end = dt2.minute
  578. elif dt2.second > 0:
  579. end = dt2.second
  580. return (start, end, unit)
  581. ###############################################################################
  582. def _stop(lock, conn, data):
  583. libgis.G_debug(1, "Stop C-interface server")
  584. conn.close()
  585. lock.release()
  586. sys.exit()
  587. ###############################################################################
  588. # Global server connection
  589. server_connection = None
  590. server_lock = None
  591. def c_library_server(lock, conn):
  592. """The GRASS C-libraries server function designed to be a target for
  593. multiprocessing.Process
  594. :param lock: A multiprocessing.Lock
  595. :param conn: A multiprocessing.Pipe
  596. """
  597. # Crerate the function array
  598. functions = [0]*15
  599. functions[RPCDefs.STOP] = _stop
  600. functions[RPCDefs.HAS_TIMESTAMP] = _has_timestamp
  601. functions[RPCDefs.WRITE_TIMESTAMP] = _write_timestamp
  602. functions[RPCDefs.READ_TIMESTAMP] = _read_timestamp
  603. functions[RPCDefs.REMOVE_TIMESTAMP] = _remove_timestamp
  604. functions[RPCDefs.READ_MAP_INFO] = _read_map_info
  605. functions[RPCDefs.MAP_EXISTS] = _map_exists
  606. functions[RPCDefs.AVAILABLE_MAPSETS] = _available_mapsets
  607. functions[RPCDefs.GET_DRIVER_NAME] = _get_driver_name
  608. functions[RPCDefs.GET_DATABASE_NAME] = _get_database_name
  609. functions[RPCDefs.G_MAPSET] = _get_mapset
  610. functions[RPCDefs.G_LOCATION] = _get_location
  611. functions[RPCDefs.G_GISDBASE] = _get_gisdbase
  612. functions[RPCDefs.G_FATAL_ERROR] = _fatal_error
  613. libgis.G_gisinit("c_library_server")
  614. libgis.G_debug(1, "Start C-interface server")
  615. while True:
  616. # Avoid busy waiting
  617. conn.poll(4)
  618. data = conn.recv()
  619. lock.acquire()
  620. functions[data[0]](lock, conn, data)
  621. lock.release()
  622. class CLibrariesInterface(object):
  623. """Fast and exit-safe interface to GRASS C-libraries functions
  624. This class implements a fast and exit-safe interface to the GRASS
  625. gis, raster, 3D raster and vector C-libraries functions.
  626. The C-libraries functions are called via ctypes in a subprocess
  627. using a pipe (multiprocessing.Pipe) to transfer the text messages.
  628. Hence, the process that uses the CLibrariesInterface will not be
  629. exited, if a G_fatal_error() was invoked in the subprocess.
  630. In this case the CLibrariesInterface object will simply start a
  631. new subprocess and restarts the pipeline.
  632. Usage:
  633. .. code-block:: python
  634. >>> import grass.script as gscript
  635. >>> import grass.temporal as tgis
  636. >>> gscript.use_temp_region()
  637. >>> gscript.run_command("g.region", n=80.0, s=0.0, e=120.0, w=0.0,
  638. ... t=1.0, b=0.0, res=10.0, res3=10.0)
  639. 0
  640. >>> tgis.init()
  641. >>> gscript.run_command("r.mapcalc", expression="test = 1", overwrite=True, quiet=True)
  642. 0
  643. >>> gscript.run_command("r3.mapcalc", expression="test = 1", overwrite=True, quiet=True)
  644. 0
  645. >>> gscript.run_command("v.random", output="test", n=10, overwrite=True, quiet=True)
  646. 0
  647. >>> gscript.run_command("r.timestamp", map="test", date='12 Mar 1995 10:34:40', overwrite=True, quiet=True)
  648. 0
  649. >>> gscript.run_command("r3.timestamp", map="test", date='12 Mar 1995 10:34:40', overwrite=True, quiet=True)
  650. 0
  651. >>> gscript.run_command("v.timestamp", map="test", date='12 Mar 1995 10:34:40', overwrite=True, quiet=True)
  652. 0
  653. # Check mapsets
  654. >>> ciface = tgis.CLibrariesInterface()
  655. >>> mapsets = ciface.available_mapsets()
  656. >>> mapsets[0] == tgis.get_current_mapset()
  657. True
  658. # Raster map
  659. >>> ciface = tgis.CLibrariesInterface()
  660. >>> check = ciface.raster_map_exists("test", tgis.get_current_mapset())
  661. >>> print check
  662. True
  663. >>> ciface.read_raster_info("test", tgis.get_current_mapset())
  664. {'rows': 12, 'north': 80.0, 'min': 1, 'datatype': 'CELL', 'max': 1, 'ewres': 10.0, 'cols': 8, 'west': 0.0, 'east': 120.0, 'nsres': 10.0, 'south': 0.0}
  665. >>> check = ciface.has_raster_timestamp("test", tgis.get_current_mapset())
  666. >>> print check
  667. True
  668. >>> if check:
  669. ... res = ciface.read_raster_timestamp("test", tgis.get_current_mapset())
  670. ... if res[0]:
  671. ... print str(res[1][0]), str(res[1][0])
  672. ... ciface.remove_raster_timestamp("test", tgis.get_current_mapset())
  673. 1995-03-12 10:34:40 1995-03-12 10:34:40
  674. 1
  675. >>> ciface.has_raster_timestamp("test", tgis.get_current_mapset())
  676. False
  677. >>> ciface.write_raster_timestamp("test", tgis.get_current_mapset(), "13 Jan 1999 14:30:05")
  678. 1
  679. >>> ciface.has_raster_timestamp("test", tgis.get_current_mapset())
  680. True
  681. # 3D raster map
  682. >>> check = ciface.raster3d_map_exists("test", tgis.get_current_mapset())
  683. >>> print check
  684. True
  685. >>> ciface.read_raster3d_info("test", tgis.get_current_mapset())
  686. {'tbres': 1.0, 'rows': 12, 'north': 80.0, 'bottom': 0.0, 'datatype': 'DCELL', 'max': 1.0, 'top': 1.0, 'min': 1.0, 'cols': 8, 'depths': 1, 'west': 0.0, 'ewres': 10.0, 'east': 120.0, 'nsres': 10.0, 'south': 0.0}
  687. >>> check = ciface.has_raster3d_timestamp("test", tgis.get_current_mapset())
  688. >>> print check
  689. True
  690. >>> if check:
  691. ... res = ciface.read_raster3d_timestamp("test", tgis.get_current_mapset())
  692. ... if res[0]:
  693. ... print str(res[1][0]), str(res[1][0])
  694. ... ciface.remove_raster3d_timestamp("test", tgis.get_current_mapset())
  695. 1995-03-12 10:34:40 1995-03-12 10:34:40
  696. 1
  697. >>> ciface.has_raster3d_timestamp("test", tgis.get_current_mapset())
  698. False
  699. >>> ciface.write_raster3d_timestamp("test", tgis.get_current_mapset(), "13 Jan 1999 14:30:05")
  700. 1
  701. >>> ciface.has_raster3d_timestamp("test", tgis.get_current_mapset())
  702. True
  703. # Vector map
  704. >>> check = ciface.vector_map_exists("test", tgis.get_current_mapset())
  705. >>> print check
  706. True
  707. >>> kvp = ciface.read_vector_info("test", tgis.get_current_mapset())
  708. >>> print kvp['points']
  709. 10
  710. >>> check = ciface.has_vector_timestamp("test", tgis.get_current_mapset(), None)
  711. >>> print check
  712. True
  713. >>> if check:
  714. ... res = ciface.read_vector_timestamp("test", tgis.get_current_mapset())
  715. ... if res[0]:
  716. ... print str(res[1][0]), str(res[1][0])
  717. ... ciface.remove_vector_timestamp("test", tgis.get_current_mapset())
  718. 1995-03-12 10:34:40 1995-03-12 10:34:40
  719. 1
  720. >>> ciface.has_vector_timestamp("test", tgis.get_current_mapset())
  721. False
  722. >>> ciface.write_vector_timestamp("test", tgis.get_current_mapset(), "13 Jan 1999 14:30:05")
  723. 1
  724. >>> ciface.has_vector_timestamp("test", tgis.get_current_mapset())
  725. True
  726. >>> ciface.get_driver_name()
  727. 'sqlite'
  728. >>> ciface.get_database_name().split("/")[-1]
  729. 'sqlite.db'
  730. >>> mapset = ciface.get_mapset()
  731. >>> location = ciface.get_location()
  732. >>> gisdbase = ciface.get_gisdbase()
  733. >>> gscript.del_temp_region()
  734. """
  735. def __init__(self):
  736. self.client_conn = None
  737. self.server_conn = None
  738. self.queue = None
  739. self.server = None
  740. self.start_server()
  741. def start_server(self):
  742. self.client_conn, self.server_conn = Pipe(True)
  743. self.lock = Lock()
  744. self.server = Process(target=c_library_server, args=(self.lock,
  745. self.server_conn))
  746. self.server.daemon = True
  747. self.server.start()
  748. def _check_restart_server(self):
  749. """Restart the server if it was terminated
  750. """
  751. if self.server.is_alive() is True:
  752. return
  753. self.client_conn.close()
  754. self.server_conn.close()
  755. self.start_server()
  756. logging.warning("Needed to restart the libgis server")
  757. def raster_map_exists(self, name, mapset):
  758. """Check if a raster map exists in the spatial database
  759. :param name: The name of the map
  760. :param mapset: The mapset of the map
  761. :returns: True if exists, False if not
  762. """
  763. self._check_restart_server()
  764. self.client_conn.send([RPCDefs.MAP_EXISTS, RPCDefs.TYPE_RASTER,
  765. name, mapset, None])
  766. return self.client_conn.recv()
  767. def read_raster_info(self, name, mapset):
  768. """Read the raster map info from the file system and store the content
  769. into a dictionary
  770. :param name: The name of the map
  771. :param mapset: The mapset of the map
  772. :returns: The key value pairs of the map specific metadata,
  773. or None in case of an error
  774. """
  775. self._check_restart_server()
  776. self.client_conn.send([RPCDefs.READ_MAP_INFO, RPCDefs.TYPE_RASTER,
  777. name, mapset, None])
  778. return self.client_conn.recv()
  779. def has_raster_timestamp(self, name, mapset):
  780. """Check if a file based raster timetamp exists
  781. :param name: The name of the map
  782. :param mapset: The mapset of the map
  783. :returns: True if exists, False if not
  784. """
  785. self._check_restart_server()
  786. self.client_conn.send([RPCDefs.HAS_TIMESTAMP, RPCDefs.TYPE_RASTER,
  787. name, mapset, None])
  788. return self.client_conn.recv()
  789. def remove_raster_timestamp(self, name, mapset):
  790. """Remove a file based raster timetamp
  791. Please have a look at the documentation G_remove_raster_timestamp
  792. for the return values description.
  793. :param name: The name of the map
  794. :param mapset: The mapset of the map
  795. :returns: The return value of G_remove_raster_timestamp
  796. """
  797. self._check_restart_server()
  798. self.client_conn.send([RPCDefs.REMOVE_TIMESTAMP, RPCDefs.TYPE_RASTER,
  799. name, mapset, None])
  800. return self.client_conn.recv()
  801. def read_raster_timestamp(self, name, mapset):
  802. """Read a file based raster timetamp
  803. Please have a look at the documentation G_read_raster_timestamp
  804. for the return values description.
  805. The timestamps to be send are tuples of values:
  806. - relative time (start, end, unit), start and end are of type
  807. integer, unit is of type string.
  808. - absolute time (start, end), start and end are of type datetime
  809. The end time may be None in case of a time instance.
  810. :param name: The name of the map
  811. :param mapset: The mapset of the map
  812. :returns: The return value of G_read_raster_timestamp
  813. """
  814. self._check_restart_server()
  815. self.client_conn.send([RPCDefs.READ_TIMESTAMP, RPCDefs.TYPE_RASTER,
  816. name, mapset, None])
  817. return self.client_conn.recv()
  818. def write_raster_timestamp(self, name, mapset, timestring):
  819. """Write a file based raster timetamp
  820. Please have a look at the documentation G_write_raster_timestamp
  821. for the return values description.
  822. Note:
  823. Only timestamps of maps from the current mapset can written.
  824. :param name: The name of the map
  825. :param mapset: The mapset of the map
  826. :param timestring: A GRASS datetime C-library compatible string
  827. :returns: The return value of G_write_raster_timestamp
  828. """
  829. self._check_restart_server()
  830. self.client_conn.send([RPCDefs.WRITE_TIMESTAMP, RPCDefs.TYPE_RASTER,
  831. name, mapset, None, timestring])
  832. return self.client_conn.recv()
  833. def raster3d_map_exists(self, name, mapset):
  834. """Check if a 3D raster map exists in the spatial database
  835. :param name: The name of the map
  836. :param mapset: The mapset of the map
  837. :returns: True if exists, False if not
  838. """
  839. self._check_restart_server()
  840. self.client_conn.send([RPCDefs.MAP_EXISTS, RPCDefs.TYPE_RASTER3D,
  841. name, mapset, None])
  842. return self.client_conn.recv()
  843. def read_raster3d_info(self, name, mapset):
  844. """Read the 3D raster map info from the file system and store the content
  845. into a dictionary
  846. :param name: The name of the map
  847. :param mapset: The mapset of the map
  848. :returns: The key value pairs of the map specific metadata,
  849. or None in case of an error
  850. """
  851. self._check_restart_server()
  852. self.client_conn.send([RPCDefs.READ_MAP_INFO, RPCDefs.TYPE_RASTER3D,
  853. name, mapset, None])
  854. return self.client_conn.recv()
  855. def has_raster3d_timestamp(self, name, mapset):
  856. """Check if a file based 3D raster timetamp exists
  857. :param name: The name of the map
  858. :param mapset: The mapset of the map
  859. :returns: True if exists, False if not
  860. """
  861. self._check_restart_server()
  862. self.client_conn.send([RPCDefs.HAS_TIMESTAMP, RPCDefs.TYPE_RASTER3D,
  863. name, mapset, None])
  864. return self.client_conn.recv()
  865. def remove_raster3d_timestamp(self, name, mapset):
  866. """Remove a file based 3D raster timetamp
  867. Please have a look at the documentation G_remove_raster3d_timestamp
  868. for the return values description.
  869. :param name: The name of the map
  870. :param mapset: The mapset of the map
  871. :returns: The return value of G_remove_raster3d_timestamp
  872. """
  873. self._check_restart_server()
  874. self.client_conn.send([RPCDefs.REMOVE_TIMESTAMP, RPCDefs.TYPE_RASTER3D,
  875. name, mapset, None])
  876. return self.client_conn.recv()
  877. def read_raster3d_timestamp(self, name, mapset):
  878. """Read a file based 3D raster timetamp
  879. Please have a look at the documentation G_read_raster3d_timestamp
  880. for the return values description.
  881. The timestamps to be send are tuples of values:
  882. - relative time (start, end, unit), start and end are of type
  883. integer, unit is of type string.
  884. - absolute time (start, end), start and end are of type datetime
  885. The end time may be None in case of a time instance.
  886. :param name: The name of the map
  887. :param mapset: The mapset of the map
  888. :returns: The return value of G_read_raster3d_timestamp
  889. """
  890. self._check_restart_server()
  891. self.client_conn.send([RPCDefs.READ_TIMESTAMP, RPCDefs.TYPE_RASTER3D,
  892. name, mapset, None])
  893. return self.client_conn.recv()
  894. def write_raster3d_timestamp(self, name, mapset, timestring):
  895. """Write a file based 3D raster timetamp
  896. Please have a look at the documentation G_write_raster3d_timestamp
  897. for the return values description.
  898. Note:
  899. Only timestamps of maps from the current mapset can written.
  900. :param name: The name of the map
  901. :param mapset: The mapset of the map
  902. :param timestring: A GRASS datetime C-library compatible string
  903. :returns: The return value of G_write_raster3d_timestamp
  904. """
  905. self._check_restart_server()
  906. self.client_conn.send([RPCDefs.WRITE_TIMESTAMP, RPCDefs.TYPE_RASTER3D,
  907. name, mapset, None, timestring])
  908. return self.client_conn.recv()
  909. def vector_map_exists(self, name, mapset):
  910. """Check if a vector map exists in the spatial database
  911. :param name: The name of the map
  912. :param mapset: The mapset of the map
  913. :returns: True if exists, False if not
  914. """
  915. self._check_restart_server()
  916. self.client_conn.send([RPCDefs.MAP_EXISTS, RPCDefs.TYPE_VECTOR,
  917. name, mapset, None])
  918. return self.client_conn.recv()
  919. def read_vector_info(self, name, mapset):
  920. """Read the vector map info from the file system and store the content
  921. into a dictionary
  922. :param name: The name of the map
  923. :param mapset: The mapset of the map
  924. :returns: The key value pairs of the map specific metadata,
  925. or None in case of an error
  926. """
  927. self._check_restart_server()
  928. self.client_conn.send([RPCDefs.READ_MAP_INFO, RPCDefs.TYPE_VECTOR,
  929. name, mapset, None])
  930. return self.client_conn.recv()
  931. def has_vector_timestamp(self, name, mapset, layer=None):
  932. """Check if a file based vector timetamp exists
  933. :param name: The name of the map
  934. :param mapset: The mapset of the map
  935. :param layer: The layer of the vector map
  936. :returns: True if exists, False if not
  937. """
  938. self._check_restart_server()
  939. self.client_conn.send([RPCDefs.HAS_TIMESTAMP, RPCDefs.TYPE_VECTOR,
  940. name, mapset, layer])
  941. return self.client_conn.recv()
  942. def remove_vector_timestamp(self, name, mapset, layer=None):
  943. """Remove a file based vector timetamp
  944. Please have a look at the documentation G_remove_vector_timestamp
  945. for the return values description.
  946. :param name: The name of the map
  947. :param mapset: The mapset of the map
  948. :param layer: The layer of the vector map
  949. :returns: The return value of G_remove_vector_timestamp
  950. """
  951. self._check_restart_server()
  952. self.client_conn.send([RPCDefs.REMOVE_TIMESTAMP, RPCDefs.TYPE_VECTOR,
  953. name, mapset, layer])
  954. return self.client_conn.recv()
  955. def read_vector_timestamp(self, name, mapset, layer=None):
  956. """Read a file based vector timetamp
  957. Please have a look at the documentation G_read_vector_timestamp
  958. for the return values description.
  959. The timestamps to be send are tuples of values:
  960. - relative time (start, end, unit), start and end are of type
  961. integer, unit is of type string.
  962. - absolute time (start, end), start and end are of type datetime
  963. The end time may be None in case of a time instance.
  964. :param name: The name of the map
  965. :param mapset: The mapset of the map
  966. :param layer: The layer of the vector map
  967. :returns: The return value ofG_read_vector_timestamp and the timestamps
  968. """
  969. self._check_restart_server()
  970. self.client_conn.send([RPCDefs.READ_TIMESTAMP, RPCDefs.TYPE_VECTOR,
  971. name, mapset, layer])
  972. return self.client_conn.recv()
  973. def write_vector_timestamp(self, name, mapset, timestring, layer=None):
  974. """Write a file based vector timestamp
  975. Please have a look at the documentation G_write_vector_timestamp
  976. for the return values description.
  977. Note:
  978. Only timestamps pf maps from the current mapset can written.
  979. :param name: The name of the map
  980. :param mapset: The mapset of the map
  981. :param timestring: A GRASS datetime C-library compatible string
  982. :param layer: The layer of the vector map
  983. :returns: The return value of G_write_vector_timestamp
  984. """
  985. self._check_restart_server()
  986. self.client_conn.send([RPCDefs.WRITE_TIMESTAMP, RPCDefs.TYPE_VECTOR,
  987. name, mapset, layer, timestring])
  988. return self.client_conn.recv()
  989. def available_mapsets(self):
  990. """Return all available mapsets the user can access as a list of strings
  991. :returns: Names of available mapsets as list of strings
  992. """
  993. self._check_restart_server()
  994. self.client_conn.send([RPCDefs.AVAILABLE_MAPSETS, ])
  995. return self.client_conn.recv()
  996. def get_driver_name(self, mapset=None):
  997. """Return the temporal database driver of a specific mapset
  998. :param mapset: Name of the mapset
  999. :returns: Name of the driver or None if no temporal database present
  1000. """
  1001. self._check_restart_server()
  1002. self.client_conn.send([RPCDefs.GET_DRIVER_NAME, mapset])
  1003. return self.client_conn.recv()
  1004. def get_database_name(self, mapset=None):
  1005. """Return the temporal database name of a specific mapset
  1006. :param mapset: Name of the mapset
  1007. :returns: Name of the database or None if no temporal database present
  1008. """
  1009. self._check_restart_server()
  1010. self.client_conn.send([RPCDefs.GET_DATABASE_NAME, mapset])
  1011. return self.client_conn.recv()
  1012. def get_mapset(self):
  1013. """Return the current mapset
  1014. :returns: Name of the current mapset
  1015. """
  1016. self._check_restart_server()
  1017. self.client_conn.send([RPCDefs.G_MAPSET, ])
  1018. return self.client_conn.recv()
  1019. def get_location(self):
  1020. """Return the location
  1021. :returns: Name of the location
  1022. """
  1023. self._check_restart_server()
  1024. self.client_conn.send([RPCDefs.G_LOCATION, ])
  1025. return self.client_conn.recv()
  1026. def get_gisdbase(self):
  1027. """Return the gisdatabase
  1028. :returns: Name of the gisdatabase
  1029. """
  1030. self._check_restart_server()
  1031. self.client_conn.send([RPCDefs.G_GISDBASE, ])
  1032. return self.client_conn.recv()
  1033. def fatal_error(self, mapset=None):
  1034. """Return the temporal database name of a specific mapset
  1035. :param mapset: Name of the mapset
  1036. :returns: Name of the database or None if no temporal database present
  1037. """
  1038. self._check_restart_server()
  1039. self.client_conn.send([RPCDefs.G_FATAL_ERROR])
  1040. def stop(self):
  1041. """Stop the messenger server and close the pipe
  1042. This method should be called at exit using the package atexit
  1043. """
  1044. if self.server is not None and self.server.is_alive():
  1045. self.client_conn.send([0, ])
  1046. self.server.join(5)
  1047. self.server.terminate()
  1048. if self.client_conn is not None:
  1049. self.client_conn.close()
  1050. if __name__ == "__main__":
  1051. import doctest
  1052. doctest.testmod()