c_libraries_interface.py 45 KB

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