c_libraries_interface.py 39 KB

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