c_libraries_interface.py 39 KB

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