abstract.py 8.7 KB

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