c_libraries_interface.py 46 KB

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