abstract.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Fri Aug 17 17:24:03 2012
  4. @author: pietro
  5. """
  6. import ctypes
  7. import datetime
  8. import grass.lib.vector as libvect
  9. from vector_type import MAPTYPE
  10. import pygrass.env as env
  11. from pygrass.errors import GrassError, OpenError
  12. from table import DBlinks
  13. def must_be_open(method):
  14. def wrapper(self):
  15. if self.is_open():
  16. return method(self)
  17. else:
  18. print "You must open the map."
  19. return wrapper
  20. #=============================================
  21. # VECTOR ABSTRACT CLASS
  22. #=============================================
  23. class Info(object):
  24. """Basic vector info.
  25. To get access to the vector info the map must be opened. ::
  26. >>> municip = Info('boundary_municp', 'PERMANENT')
  27. >>> municip.full_name
  28. You must open the map.
  29. >>> municip.open()
  30. Then it is possible to read and write the following map attributes: ::
  31. >>> municip.organization
  32. 'NC OneMap'
  33. >>> municip.person
  34. 'helena'
  35. >>> municip.title
  36. 'North Carolina municipality boundaries (polygon map)'
  37. >>> municip.map_date
  38. datetime.datetime(2006, 11, 7, 0, 1, 27)
  39. >>> municip.date
  40. ''
  41. >>> municip.scale
  42. 1
  43. >>> municip.comment
  44. ''
  45. >>> municip.comment = "One useful comment!"
  46. >>> municip.comment
  47. 'One useful comment!'
  48. >>> municip.zone
  49. 0
  50. >>> municip.proj
  51. 99
  52. There are some read only attributes: ::
  53. >>> municip.full_name
  54. 'boundary_municp@PERMANENT'
  55. >>> municip.proj_name
  56. 'Lambert Conformal Conic'
  57. >>> municip.maptype
  58. 'native'
  59. And some basic methods: ::
  60. >>> municip.is_3D()
  61. False
  62. >>> municip.exist()
  63. True
  64. >>> municip.is_open()
  65. True
  66. >>> municip.close()
  67. """
  68. def __init__(self, name, mapset=''):
  69. # Set map name and mapset
  70. self._name = name
  71. self.mapset = mapset
  72. self.c_mapinfo = ctypes.pointer(libvect.Map_info())
  73. self._topo_level = 1
  74. self._class_name = 'Vector'
  75. self.overwrite = False
  76. self.date_fmt = '%a %b %d %H:%M:%S %Y'
  77. def _get_name(self):
  78. if self.exist() and self.is_open():
  79. return libvect.Vect_get_name(self.c_mapinfo)
  80. else:
  81. return self._name
  82. def _set_name(self, newname):
  83. self.rename(newname)
  84. name = property(fget=_get_name, fset=_set_name)
  85. # @property
  86. # def mapset(self):
  87. # return libvect.Vect_get_mapset(self.c_mapinfo)
  88. def _get_organization(self):
  89. return libvect.Vect_get_organization(self.c_mapinfo)
  90. def _set_organization(self, org):
  91. libvect.Vect_get_organization(self.c_mapinfo, ctypes.c_char_p(org))
  92. organization = property(fget=_get_organization, fset=_set_organization)
  93. def _get_date(self):
  94. return libvect.Vect_get_date(self.c_mapinfo)
  95. def _set_date(self, date):
  96. return libvect.Vect_set_date(self.c_mapinfo, ctypes.c_char_p(date))
  97. date = property(fget=_get_date, fset=_set_date)
  98. def _get_person(self):
  99. return libvect.Vect_get_person(self.c_mapinfo)
  100. def _set_person(self, person):
  101. libvect.Vect_set_person(self.c_mapinfo, ctypes.c_char_p(person))
  102. person = property(fget=_get_person, fset=_set_person)
  103. def _get_title(self):
  104. return libvect.Vect_get_map_name(self.c_mapinfo)
  105. def _set_title(self, title):
  106. libvect.Vect_set_map_name(self.c_mapinfo, ctypes.c_char_p(title))
  107. title = property(fget=_get_title, fset=_set_title)
  108. def _get_map_date(self):
  109. date_str = libvect.Vect_get_map_date(self.c_mapinfo)
  110. return datetime.datetime.strptime(date_str, self.date_fmt)
  111. def _set_map_date(self, datetimeobj):
  112. date_str = datetimeobj.strftime(self.date_fmt)
  113. libvect.Vect_set_map_date(self.c_mapinfo, ctypes.c_char_p(date_str))
  114. map_date = property(fget=_get_map_date, fset=_set_map_date)
  115. def _get_scale(self):
  116. return libvect.Vect_get_scale(self.c_mapinfo)
  117. def _set_scale(self, scale):
  118. return libvect.Vect_set_scale(self.c_mapinfo, ctypes.c_int(scale))
  119. scale = property(fget=_get_scale, fset=_set_scale)
  120. def _get_comment(self):
  121. return libvect.Vect_get_comment(self.c_mapinfo)
  122. def _set_comment(self, comm):
  123. return libvect.Vect_set_comment(self.c_mapinfo, ctypes.c_char_p(comm))
  124. comment = property(fget=_get_comment, fset=_set_comment)
  125. def _get_zone(self):
  126. return libvect.Vect_get_zone(self.c_mapinfo)
  127. def _set_zone(self, zone):
  128. return libvect.Vect_set_zone(self.c_mapinfo, ctypes.c_int(zone))
  129. zone = property(fget=_get_zone, fset=_set_zone)
  130. def _get_proj(self):
  131. return libvect.Vect_get_proj(self.c_mapinfo)
  132. def _set_proj(self, proj):
  133. libvect.Vect_set_proj(self.c_mapinfo, ctypes.c_int(proj))
  134. proj = property(fget=_get_proj, fset=_set_proj)
  135. def _get_thresh(self):
  136. return libvect.Vect_get_thresh(self.c_mapinfo)
  137. def _set_thresh(self, thresh):
  138. return libvect.Vect_set_thresh(self.c_mapinfo, ctypes.c_double(thresh))
  139. thresh = property(fget=_get_thresh, fset=_set_thresh)
  140. @property
  141. @must_be_open
  142. def full_name(self):
  143. return libvect.Vect_get_full_name(self.c_mapinfo)
  144. @property
  145. @must_be_open
  146. def maptype(self):
  147. return MAPTYPE[libvect.Vect_maptype(self.c_mapinfo)]
  148. @property
  149. @must_be_open
  150. def proj_name(self):
  151. return libvect.Vect_get_proj_name(self.c_mapinfo)
  152. def _write_header(self):
  153. libvect.Vect_write_header(self.c_mapinfo)
  154. def rename(self, newname):
  155. """Rename the map"""
  156. if self.exist():
  157. env.rename(self.name, newname, 'vect')
  158. self._name = newname
  159. def is_3D(self):
  160. return bool(libvect.Vect_is_3d(self.c_mapinfo))
  161. def exist(self):
  162. if self._name:
  163. self.mapset = env.get_mapset_vector(self._name, self.mapset)
  164. else:
  165. return False
  166. if self.mapset:
  167. return True
  168. else:
  169. return False
  170. def is_open(self):
  171. return (self.c_mapinfo.contents.open != 0 and
  172. self.c_mapinfo.contents.open != libvect.VECT_CLOSED_CODE)
  173. def open(self, mode='r', layer='0', overwrite=None):
  174. """::
  175. >>> mun = Info('boundary_municp_sqlite')
  176. >>> mun.open()
  177. >>> mun.is_open()
  178. True
  179. >>> mun.close()
  180. ..
  181. """
  182. # check if map exists or not
  183. if not self.exist() and mode != 'w':
  184. raise OpenError("Map <%s> not found." % self._name)
  185. if libvect.Vect_set_open_level(self._topo_level) != 0:
  186. raise OpenError("Invalid access level.")
  187. # update the overwrite attribute
  188. self.overwrite = overwrite if overwrite is not None else self.overwrite
  189. # check if the mode is valid
  190. if mode not in ('r', 'rw', 'w'):
  191. raise ValueError("Mode not supported. Use one of: 'r', 'rw', 'w'.")
  192. # check if the map exist
  193. if self.exist() and mode == 'r':
  194. openvect = libvect.Vect_open_old2(self.c_mapinfo, self.name,
  195. self.mapset, layer)
  196. # If it is opened in write mode
  197. if mode == 'w':
  198. #TODO: build topo if new
  199. openvect = libvect.Vect_open_new(self.c_mapinfo, self.name,
  200. libvect.WITHOUT_Z)
  201. elif mode == 'rw':
  202. openvect = libvect.Vect_open_update2(self.c_mapinfo, self.name,
  203. self.mapset, layer)
  204. # initialize the dblinks object
  205. self.dblinks = DBlinks(self.c_mapinfo)
  206. # check the C function result.
  207. if openvect == -1:
  208. str_err = "Not able to open the map, C function return %d."
  209. raise OpenError(str_err % openvect)
  210. def close(self):
  211. if self.is_open():
  212. if libvect.Vect_close(self.c_mapinfo) != 0:
  213. str_err = 'Error when trying to close the map with Vect_close'
  214. raise GrassError(str_err)
  215. def remove(self):
  216. """Remove vector map"""
  217. if self.is_open():
  218. self.close()
  219. env.remove(vect=self.name)
  220. def build(self):
  221. """Close the vector map and build vector Topology"""
  222. self.close()
  223. libvect.Vect_set_open_level(1)
  224. if libvect.Vect_open_old2(self.c_mapinfo, self.name,
  225. self.mapset, '0') != 1:
  226. str_err = 'Error when trying to open the vector map.'
  227. raise GrassError(str_err)
  228. # Vect_build returns 1 on success and 0 on error (bool approach)
  229. if libvect.Vect_build(self.c_mapinfo) != 1:
  230. str_err = 'Error when trying build topology with Vect_build'
  231. raise GrassError(str_err)
  232. libvect.Vect_close(self.c_mapinfo)