geometry.py 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Wed Jul 18 10:46:25 2012
  4. @author: pietro
  5. """
  6. import ctypes
  7. import re
  8. import numpy as np
  9. import grass.lib.gis as libgis
  10. import grass.lib.vector as libvect
  11. from grass.pygrass.errors import GrassError
  12. from basic import Ilist, Bbox, Cats
  13. import sql
  14. WKT = {'POINT\((.*)\)': 'point', # 'POINT\(\s*([+-]*\d+\.*\d*)+\s*\)'
  15. 'LINESTRING\((.*)\)': 'line'}
  16. def read_WKT(string):
  17. """Read the string and return a geometry object
  18. WKT:
  19. POINT(0 0)
  20. LINESTRING(0 0,1 1,1 2)
  21. POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1, 2 1, 2 2, 1 2,1 1))
  22. MULTIPOINT(0 0,1 2)
  23. MULTILINESTRING((0 0,1 1,1 2),(2 3,3 2,5 4))
  24. MULTIPOLYGON(((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1)),
  25. ((-1 -1,-1 -2,-2 -2,-2 -1,-1 -1)))
  26. GEOMETRYCOLLECTION(POINT(2 3),LINESTRING(2 3,3 4))
  27. EWKT:
  28. POINT(0 0 0) -- XYZ
  29. SRID=32632;POINT(0 0) -- XY with SRID
  30. POINTM(0 0 0) -- XYM
  31. POINT(0 0 0 0) -- XYZM
  32. SRID=4326;MULTIPOINTM(0 0 0,1 2 1) -- XYM with SRID
  33. MULTILINESTRING((0 0 0,1 1 0,1 2 1),(2 3 1,3 2 1,5 4 1))
  34. POLYGON((0 0 0,4 0 0,4 4 0,0 4 0,0 0 0),(1 1 0,2 1 0,2 2 0,1 2 0,1 1 0))
  35. MULTIPOLYGON(((0 0 0,4 0 0,4 4 0,0 4 0,0 0 0),
  36. (1 1 0,2 1 0,2 2 0,1 2 0,1 1 0)),
  37. ((-1 -1 0,-1 -2 0,-2 -2 0,-2 -1 0,-1 -1 0)))
  38. GEOMETRYCOLLECTIONM( POINTM(2 3 9), LINESTRINGM(2 3 4, 3 4 5) )
  39. MULTICURVE( (0 0, 5 5), CIRCULARSTRING(4 0, 4 4, 8 4) )
  40. POLYHEDRALSURFACE( ((0 0 0, 0 0 1, 0 1 1, 0 1 0, 0 0 0)),
  41. ((0 0 0, 0 1 0, 1 1 0, 1 0 0, 0 0 0)),
  42. ((0 0 0, 1 0 0, 1 0 1, 0 0 1, 0 0 0)),
  43. ((1 1 0, 1 1 1, 1 0 1, 1 0 0, 1 1 0)),
  44. ((0 1 0, 0 1 1, 1 1 1, 1 1 0, 0 1 0)),
  45. ((0 0 1, 1 0 1, 1 1 1, 0 1 1, 0 0 1)) )
  46. TRIANGLE ((0 0, 0 9, 9 0, 0 0))
  47. TIN( ((0 0 0, 0 0 1, 0 1 0, 0 0 0)), ((0 0 0, 0 1 0, 1 1 0, 0 0 0)) )
  48. """
  49. for regexp, obj in WKT.items():
  50. if re.match(regexp, string):
  51. geo = 10
  52. return obj(geo)
  53. def read_WKB(buff):
  54. """Read the binary buffer and return a geometry object"""
  55. pass
  56. #=============================================
  57. # GEOMETRY
  58. #=============================================
  59. def get_xyz(pnt):
  60. """Return a tuple with: x, y, z. ::
  61. >>> pnt = Point(0, 0)
  62. >>> get_xyz(pnt)
  63. (0.0, 0.0, 0.0)
  64. >>> get_xyz((1, 1))
  65. (1, 1, 0.0)
  66. >>> get_xyz((1, 1, 2))
  67. (1, 1, 2)
  68. >>> get_xyz((1, 1, 2, 2)) #doctest: +ELLIPSIS
  69. Traceback (most recent call last):
  70. ...
  71. ValueError: The the format of the point is not supported: (1, 1, 2, 2)
  72. ..
  73. """
  74. if isinstance(pnt, Point):
  75. if pnt.is2D:
  76. x, y = pnt.x, pnt.y
  77. z = 0.
  78. else:
  79. x, y, z = pnt.x, pnt.y, pnt.z
  80. else:
  81. if len(pnt) == 2:
  82. x, y = pnt
  83. z = 0.
  84. elif len(pnt) == 3:
  85. x, y, z = pnt
  86. else:
  87. str_error = "The the format of the point is not supported: {0!r}"
  88. raise ValueError(str_error.format(pnt))
  89. return x, y, z
  90. class Attrs(object):
  91. def __init__(self, line, table, writable=False):
  92. self.line = line
  93. self.table = table
  94. self.cond = "%s=%d" % (self.table.key, self.line)
  95. self.writable = writable
  96. def __getitem__(self, *args):
  97. """Return the value stored in the attribute table. ::
  98. >>> attrs = Attrs(v_id, table)
  99. >>> attrs['LABEL']
  100. .
  101. .."""
  102. #SELECT {cols} FROM {tname} WHERE {condition};
  103. cols = args if isinstance(args[0], str) else args[0]
  104. cur = self.table.execute(sql.SELECT_WHERE.format(cols=','.join(cols),
  105. tname=self.table.name,
  106. condition=self.cond))
  107. results = cur.fetchone()
  108. return results[0] if len(cols) == 1 else results
  109. def __setitem__(self, key, value):
  110. """Set value of a given column of a table attribute. ::
  111. >>> attrs = Attrs(v_id, table)
  112. >>> attrs['LABEL'] = 'New Label'
  113. .."""
  114. if self.writable:
  115. #UPDATE {tname} SET {new_col} = {old_col} WHERE {condition}
  116. values = '%s=%r' % (key, value)
  117. self.table.execute(sql.UPDATE_WHERE.format(tname=self.table.name,
  118. values=values,
  119. condition=self.cond))
  120. #self.table.conn.commit()
  121. else:
  122. str_err = "You can only read the attributes if the map is \
  123. in another mapset"
  124. raise GrassError(str_err)
  125. def __dict__(self):
  126. """Reurn a dict of the attribute table row."""
  127. dic = {}
  128. for key, val in zip(self.keys(), self.values()):
  129. dic[key] = val
  130. return dic
  131. def values(self):
  132. """Return the values of the attribute table row."""
  133. #SELECT {cols} FROM {tname} WHERE {condition}
  134. cur = self.table.execute(sql.SELECT_WHERE.format(cols='*',
  135. tname=self.table.name,
  136. condition=self.cond))
  137. return cur.fetchone()
  138. def keys(self):
  139. """Return the column name of the attribute table."""
  140. return self.table.columns.names()
  141. def commit(self):
  142. """Save the changes"""
  143. self.table.conn.commit()
  144. class Geo(object):
  145. """
  146. >>> geo0 = Geo()
  147. >>> points = ctypes.pointer(libvect.line_pnts())
  148. >>> cats = ctypes.pointer(libvect.line_cats())
  149. >>> geo1 = Geo(c_points=points, c_cats=cats)
  150. """
  151. def __init__(self, v_id=None, c_mapinfo=None, c_points=None, c_cats=None,
  152. table=None, writable=False):
  153. self.id = v_id # vector id
  154. self.line = self.id
  155. self.c_mapinfo = c_mapinfo
  156. # set c_points
  157. if c_points is None:
  158. self.c_points = ctypes.pointer(libvect.line_pnts())
  159. else:
  160. self.c_points = c_points
  161. # set c_cats
  162. if c_cats is None:
  163. self.c_cats = ctypes.pointer(libvect.line_cats())
  164. else:
  165. self.c_cats = c_cats
  166. # set the attributes
  167. self.attrs = None
  168. if table is not None and self.line:
  169. self.attrs = Attrs(self.line, table, writable)
  170. def is_with_topology(self):
  171. if self.c_mapinfo is not None:
  172. return self.c_mapinfo.contents.level == 2
  173. else:
  174. return False
  175. def read(self):
  176. """Read and set the coordinates of the centroid from the vector map,
  177. using the centroid_id and calling the Vect_read_line C function"""
  178. libvect.Vect_read_line(self.c_mapinfo, self.c_points,
  179. self.c_cats, self.id)
  180. class Point(Geo):
  181. """Instantiate a Point object that could be 2 or 3D, default
  182. parameters are 0.
  183. ::
  184. >>> pnt = Point()
  185. >>> pnt.x
  186. 0.0
  187. >>> pnt.y
  188. 0.0
  189. >>> pnt.z
  190. >>> pnt.is2D
  191. True
  192. >>> pnt
  193. Point(0.000000, 0.000000)
  194. >>> pnt.z = 0
  195. >>> pnt.is2D
  196. False
  197. >>> pnt
  198. Point(0.000000, 0.000000, 0.000000)
  199. >>> print pnt
  200. POINT(0.000000, 0.000000, 0.000000)
  201. ..
  202. """
  203. def __init__(self, x=0, y=0, z=None, is2D=True, **kargs):
  204. super(Point, self).__init__(**kargs)
  205. if self.id is not None:
  206. self.read()
  207. self.is2D = is2D
  208. else:
  209. self.is2D = True if z is None else False
  210. z = z if z is not None else 0
  211. libvect.Vect_append_point(self.c_points, x, y, z)
  212. # geometry type
  213. self.gtype = libvect.GV_POINT
  214. def _get_x(self):
  215. return self.c_points.contents.x[0]
  216. def _set_x(self, value):
  217. self.c_points.contents.x[0] = value
  218. x = property(fget=_get_x, fset=_set_x)
  219. def _get_y(self):
  220. return self.c_points.contents.y[0]
  221. def _set_y(self, value):
  222. self.c_points.contents.y[0] = value
  223. y = property(fget=_get_y, fset=_set_y)
  224. def _get_z(self):
  225. if self.is2D:
  226. return None
  227. return self.c_points.contents.z[0]
  228. def _set_z(self, value):
  229. if value is None:
  230. self.is2D = True
  231. self.c_points.contents.z[0] = 0
  232. else:
  233. self.c_points.contents.z[0] = value
  234. self.is2D = False
  235. z = property(fget=_get_z, fset=_set_z)
  236. def __str__(self):
  237. return self.get_wkt()
  238. def __repr__(self):
  239. return "Point(%s)" % ', '.join(['%f' % coor for coor in self.coords()])
  240. def __eq__(self, pnt):
  241. if isinstance(pnt, Point):
  242. return pnt.coords() == self.coords()
  243. return Point(*pnt).coords() == self.coords()
  244. def coords(self):
  245. """Return a tuple with the point coordinates. ::
  246. >>> pnt = Point(10, 100)
  247. >>> pnt.coords()
  248. (10.0, 100.0)
  249. If the point is 2D return a x, y tuple. But if we change the ``z``
  250. the Point object become a 3D point, therefore the method return a
  251. x, y, z tuple. ::
  252. >>> pnt.z = 1000.
  253. >>> pnt.coords()
  254. (10.0, 100.0, 1000.0)
  255. ..
  256. """
  257. if self.is2D:
  258. return self.x, self.y
  259. else:
  260. return self.x, self.y, self.z
  261. def get_wkt(self):
  262. """Return a "well know text" (WKT) geometry string. ::
  263. >>> pnt = Point(10, 100)
  264. >>> pnt.get_wkt()
  265. 'POINT(10.000000, 100.000000)'
  266. .. warning::
  267. Only ``POINT`` (2/3D) are supported, ``POINTM`` and ``POINT`` with:
  268. ``XYZM`` are not supported yet.
  269. """
  270. return "POINT(%s)" % ', '.join(['%f' % coord
  271. for coord in self.coords()])
  272. def get_wkb(self):
  273. """Return a "well know binary" (WKB) geometry buffer
  274. .. warning::
  275. Not implemented yet.
  276. """
  277. pass
  278. def distance(self, pnt):
  279. """Calculate distance of 2 points, using the Vect_points_distance
  280. C function, If one of the point have z == None, return the 2D distance.
  281. ::
  282. >>> pnt0 = Point(0, 0, 0)
  283. >>> pnt1 = Point(1, 0)
  284. >>> pnt0.distance(pnt1)
  285. 1.0
  286. >>> pnt1.z = 1
  287. >>> pnt1
  288. Point(1.000000, 0.000000, 1.000000)
  289. >>> pnt0.distance(pnt1)
  290. 1.4142135623730951
  291. The distance method require a :class:Point or a tuple with
  292. the coordinates.
  293. """
  294. if self.is2D or pnt.is2D:
  295. return libvect.Vect_points_distance(self.x, self.y, 0,
  296. pnt.x, pnt.y, 0, 0)
  297. else:
  298. return libvect.Vect_points_distance(self.x, self.y, self.z,
  299. pnt.x, pnt.y, pnt.z, 1)
  300. def buffer(self, dist=None, dist_x=None, dist_y=None, angle=0,
  301. round_=True, tol=0.1):
  302. """Return the buffer area around the point, using the
  303. ``Vect_point_buffer2`` C function.
  304. Parameters
  305. ----------
  306. dist : numeric
  307. The distance around the line.
  308. dist_x: numeric, optional
  309. The distance along x
  310. dist_y: numeric, optional
  311. The distance along y
  312. angle: numeric, optional
  313. The angle between 0x and major axis
  314. round_: bool, optional
  315. To make corners round
  316. tol: float, optional
  317. Fix the maximum distance between theoretical arc
  318. and output segments
  319. Returns
  320. -------
  321. buffer : Area
  322. The buffer area around the line.
  323. Example
  324. ---------
  325. ::
  326. >>> pnt = Point(0, 0)
  327. >>> area = point.buffer(10)
  328. >>> area.boundary #doctest: +ELLIPSIS
  329. Line([Point(-10.000000, 0.000000),...Point(-10.000000, 0.000000)])
  330. >>> area.centroid
  331. Point(0.000000, 0.000000)
  332. >>> area.isles
  333. []
  334. """
  335. if dist is not None:
  336. dist_x = dist
  337. dist_y = dist
  338. elif not dist_x or not dist_y:
  339. raise TypeError('TypeError: buffer expected 1 arguments, got 0')
  340. bound = Line()
  341. p_points = ctypes.pointer(bound.c_points)
  342. libvect.Vect_point_buffer2(self.x, self.y,
  343. dist_x, dist_y,
  344. angle, int(round_), tol,
  345. p_points)
  346. return Area(boundary=bound, centroid=self)
  347. class Line(Geo):
  348. """Instantiate a new Line with a list of tuple, or with a list of Point. ::
  349. >>> line = Line([(0, 0), (1, 1), (2, 0), (1, -1)])
  350. >>> line #doctest: +NORMALIZE_WHITESPACE
  351. Line([Point(0.000000, 0.000000),
  352. Point(1.000000, 1.000000),
  353. Point(2.000000, 0.000000),
  354. Point(1.000000, -1.000000)])
  355. ..
  356. """
  357. def __init__(self, points=None, is2D=True, **kargs):
  358. super(Line, self).__init__(**kargs)
  359. if points is not None:
  360. for pnt in points:
  361. self.append(pnt)
  362. self.is2D = is2D
  363. # geometry type
  364. self.gtype = libvect.GV_LINE
  365. def __getitem__(self, key):
  366. """Get line point of given index, slice allowed. ::
  367. >>> line = Line([(0, 0), (1, 1), (2, 2), (3, 3)])
  368. >>> line[1]
  369. Point(1.000000, 1.000000)
  370. >>> line[-1]
  371. Point(3.000000, 3.000000)
  372. >>> line[:2]
  373. [Point(0.000000, 0.000000), Point(1.000000, 1.000000)]
  374. ..
  375. """
  376. #TODO:
  377. # line[0].x = 10 is not working
  378. #pnt.c_px = ctypes.pointer(self.c_points.contents.x[indx])
  379. # pnt.c_px = ctypes.cast(id(self.c_points.contents.x[indx]),
  380. # ctypes.POINTER(ctypes.c_double))
  381. if isinstance(key, slice):
  382. #import pdb; pdb.set_trace()
  383. #Get the start, stop, and step from the slice
  384. return [Point(self.c_points.contents.x[indx],
  385. self.c_points.contents.y[indx],
  386. None if self.is2D else self.c_points.contents.z[indx])
  387. for indx in xrange(*key.indices(len(self)))]
  388. elif isinstance(key, int):
  389. if key < 0: # Handle negative indices
  390. key += self.c_points.contents.n_points
  391. if key >= self.c_points.contents.n_points:
  392. raise IndexError('Index out of range')
  393. return Point(self.c_points.contents.x[key],
  394. self.c_points.contents.y[key],
  395. None if self.is2D else self.c_points.contents.z[key])
  396. else:
  397. raise ValueError("Invalid argument type: %r." % key)
  398. def __setitem__(self, indx, pnt):
  399. """Change the coordinate of point. ::
  400. >>> line = Line([(0, 0), (1, 1)])
  401. >>> line[0] = (2, 2)
  402. >>> line
  403. Line([Point(2.000000, 2.000000), Point(1.000000, 1.000000)])
  404. ..
  405. """
  406. x, y, z = get_xyz(pnt)
  407. self.c_points.contents.x[indx] = x
  408. self.c_points.contents.y[indx] = y
  409. self.c_points.contents.z[indx] = z
  410. def __iter__(self):
  411. """Return a Point generator of the Line"""
  412. return (self.__getitem__(i) for i in range(self.__len__()))
  413. def __len__(self):
  414. """Return the number of points of the line."""
  415. return self.c_points.contents.n_points
  416. def __str__(self):
  417. return self.get_wkt()
  418. def __repr__(self):
  419. return "Line([%s])" % ', '.join([repr(pnt) for pnt in self.__iter__()])
  420. def get_pnt(self, distance, angle=0, slope=0):
  421. """Return a Point object on line in the specified distance, using the
  422. `Vect_point_on_line` C function.
  423. Raise a ValueError If the distance exceed the Line length. ::
  424. >>> line = Line([(0, 0), (1, 1)])
  425. >>> line.get_pnt(5) #doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
  426. Traceback (most recent call last):
  427. ...
  428. ValueError: The distance exceed the lenght of the line,
  429. that is: 1.414214
  430. >>> line.get_pnt(1)
  431. Point(0.707107, 0.707107)
  432. ..
  433. """
  434. # instantiate an empty Point object
  435. maxdist = self.length()
  436. if distance > maxdist:
  437. str_err = "The distance exceed the lenght of the line, that is: %f"
  438. raise ValueError(str_err % maxdist)
  439. pnt = Point(0, 0, -9999)
  440. libvect.Vect_point_on_line(self.c_points, distance,
  441. pnt.c_points.contents.x,
  442. pnt.c_points.contents.y,
  443. pnt.c_points.contents.z,
  444. angle, slope)
  445. pnt.is2D = self.is2D
  446. return pnt
  447. def append(self, pnt):
  448. """Appends one point to the end of a line, using the
  449. ``Vect_append_point`` C function. ::
  450. >>> line = Line()
  451. >>> line.append((10, 100))
  452. >>> line
  453. Line([Point(10.000000, 100.000000)])
  454. >>> line.append((20, 200))
  455. >>> line
  456. Line([Point(10.000000, 100.000000), Point(20.000000, 200.000000)])
  457. Like python list.
  458. """
  459. x, y, z = get_xyz(pnt)
  460. libvect.Vect_append_point(self.c_points, x, y, z)
  461. def bbox(self):
  462. """Return the bounding box of the line, using ``Vect_line_box``
  463. C function. ::
  464. >>> line = Line([(0, 0), (0, 1), (2, 1), (2, 0)])
  465. >>> bbox = line.bbox()
  466. >>> bbox
  467. Bbox(1.0, 0.0, 2.0, 0.0)
  468. ..
  469. """
  470. bbox = Bbox()
  471. libvect.Vect_line_box(self.c_points, bbox.c_bbox)
  472. return bbox
  473. def extend(self, line, forward=True):
  474. """Appends points to the end of a line.
  475. It is possible to extend a line, give a list of points, or directly
  476. with a line_pnts struct.
  477. If forward is True the line is extend forward otherwise is extend
  478. backward. The method use the `Vect_append_points` C function. ::
  479. >>> line = Line([(0, 0), (1, 1)])
  480. >>> line.extend( Line([(2, 2), (3, 3)]) )
  481. >>> line #doctest: +NORMALIZE_WHITESPACE
  482. Line([Point(0.000000, 0.000000),
  483. Point(1.000000, 1.000000),
  484. Point(2.000000, 2.000000),
  485. Point(3.000000, 3.000000)])
  486. Like python list, it is possible to extend a line, with another line
  487. or with a list of points.
  488. """
  489. # set direction
  490. if forward:
  491. direction = libvect.GV_FORWARD
  492. else:
  493. direction = libvect.GV_BACKWARD
  494. # check if is a Line object
  495. if isinstance(line, Line):
  496. c_points = line.c_points
  497. else:
  498. # instantiate a Line object
  499. lin = Line()
  500. for pnt in line:
  501. # add the points to the line
  502. lin.append(pnt)
  503. c_points = lin.c_points
  504. libvect.Vect_append_points(self.c_points, c_points, direction)
  505. def insert(self, indx, pnt):
  506. """Insert new point at index position and move all old points at
  507. that position and above up, using ``Vect_line_insert_point``
  508. C function. ::
  509. >>> line = Line([(0, 0), (1, 1)])
  510. >>> line.insert(0, Point(1.000000, -1.000000) )
  511. >>> line #doctest: +NORMALIZE_WHITESPACE
  512. Line([Point(1.000000, -1.000000),
  513. Point(0.000000, 0.000000),
  514. Point(1.000000, 1.000000)])
  515. ..
  516. """
  517. if indx < 0: # Handle negative indices
  518. indx += self.c_points.contents.n_points
  519. if indx >= self.c_points.contents.n_points:
  520. raise IndexError('Index out of range')
  521. x, y, z = get_xyz(pnt)
  522. libvect.Vect_line_insert_point(self.c_points, indx, x, y, z)
  523. def length(self):
  524. """Calculate line length, 3D-length in case of 3D vector line, using
  525. `Vect_line_length` C function. ::
  526. >>> line = Line([(0, 0), (1, 1), (0, 1)])
  527. >>> line.length()
  528. 2.414213562373095
  529. ..
  530. """
  531. return libvect.Vect_line_length(self.c_points)
  532. def length_geodesic(self):
  533. """Calculate line length, usig `Vect_line_geodesic_length` C function.
  534. ::
  535. >>> line = Line([(0, 0), (1, 1), (0, 1)])
  536. >>> line.length_geodesic()
  537. 2.414213562373095
  538. ..
  539. """
  540. return libvect.Vect_line_geodesic_length(self.c_points)
  541. def distance(self, pnt):
  542. """Return a tuple with:
  543. * the closest point on the line,
  544. * the distance between these two points,
  545. * distance of point from segment beginning
  546. * distance of point from line
  547. The distance is compute using the ``Vect_line_distance`` C function.
  548. Example
  549. ---------
  550. ::
  551. >>> line = Line([(0, 0), (0, 2)])
  552. >>> line.distance(Point(1, 1))
  553. (Point(0.000000, 1.000000), 1.0, 1.0, 1.0)
  554. ..
  555. """
  556. # instantite outputs
  557. cx = ctypes.c_double(0)
  558. cy = ctypes.c_double(0)
  559. cz = ctypes.c_double(0)
  560. dist = ctypes.c_double(0)
  561. sp_dist = ctypes.c_double(0)
  562. lp_dist = ctypes.c_double(0)
  563. libvect.Vect_line_distance(self.c_points,
  564. pnt.x, pnt.y, 0 if pnt.is2D else pnt.z,
  565. 0 if self.is2D else 1,
  566. ctypes.byref(cx), ctypes.byref(cy),
  567. ctypes.byref(cz), ctypes.byref(dist),
  568. ctypes.byref(sp_dist),
  569. ctypes.byref(lp_dist))
  570. # instantiate the Point class
  571. point = Point(cx.value, cy.value, cz.value)
  572. point.is2D = self.is2D
  573. return point, dist.value, sp_dist.value, lp_dist.value
  574. def get_first_cat(self):
  575. """Fetches FIRST category number for given vector line and field, using
  576. the ``Vect_get_line_cat`` C function.
  577. .. warning::
  578. Not implemented yet.
  579. """
  580. # TODO: add this method.
  581. libvect.Vect_get_line_cat(self.map, self.id, self.field)
  582. pass
  583. def pop(self, indx):
  584. """Return the point in the index position and remove from the Line. ::
  585. >>> line = Line([(0, 0), (1, 1), (2, 2)])
  586. >>> midle_pnt = line.pop(1)
  587. >>> midle_pnt
  588. Point(1.000000, 1.000000)
  589. >>> line
  590. Line([Point(0.000000, 0.000000), Point(2.000000, 2.000000)])
  591. ..
  592. """
  593. if indx < 0: # Handle negative indices
  594. indx += self.c_points.contents.n_points
  595. if indx >= self.c_points.contents.n_points:
  596. raise IndexError('Index out of range')
  597. pnt = self.__getitem__(indx)
  598. libvect.Vect_line_delete_point(self.c_points, indx)
  599. return pnt
  600. def delete(self, indx):
  601. """Remove the point in the index position. ::
  602. >>> line = Line([(0, 0), (1, 1), (2, 2)])
  603. >>> line.delete(-1)
  604. >>> line
  605. Line([Point(0.000000, 0.000000), Point(1.000000, 1.000000)])
  606. ..
  607. """
  608. if indx < 0: # Handle negative indices
  609. indx += self.c_points.contents.n_points
  610. if indx >= self.c_points.contents.n_points:
  611. raise IndexError('Index out of range')
  612. libvect.Vect_line_delete_point(self.c_points, indx)
  613. def prune(self):
  614. """Remove duplicate points, i.e. zero length segments, using
  615. `Vect_line_prune` C function. ::
  616. >>> line = Line([(0, 0), (1, 1), (1, 1), (2, 2)])
  617. >>> line.prune()
  618. >>> line #doctest: +NORMALIZE_WHITESPACE
  619. Line([Point(0.000000, 0.000000),
  620. Point(1.000000, 1.000000),
  621. Point(2.000000, 2.000000)])
  622. ..
  623. """
  624. libvect.Vect_line_prune(self.c_points)
  625. def prune_thresh(self, threshold):
  626. """Remove points in threshold, using the ``Vect_line_prune_thresh``
  627. C funtion. ::
  628. >>> line = Line([(0, 0), (1.0, 1.0), (1.2, 0.9), (2, 2)])
  629. >>> line.prune_thresh(0.5)
  630. >>> line #doctest: +SKIP +NORMALIZE_WHITESPACE
  631. Line([Point(0.000000, 0.000000),
  632. Point(1.000000, 1.000000),
  633. Point(2.000000, 2.000000)])
  634. .. warning ::
  635. prune_thresh is not working yet.
  636. """
  637. libvect.Vect_line_prune(self.c_points, ctypes.c_double(threshold))
  638. def remove(self, pnt):
  639. """Delete point at given index and move all points above down, using
  640. `Vect_line_delete_point` C function. ::
  641. >>> line = Line([(0, 0), (1, 1), (2, 2)])
  642. >>> line.remove((2, 2))
  643. >>> line[-1]
  644. Point(1.000000, 1.000000)
  645. ..
  646. """
  647. for indx, point in enumerate(self.__iter__()):
  648. if pnt == point:
  649. libvect.Vect_line_delete_point(self.c_points, indx)
  650. return
  651. raise ValueError('list.remove(x): x not in list')
  652. def reverse(self):
  653. """Reverse the order of vertices, using `Vect_line_reverse`
  654. C function. ::
  655. >>> line = Line([(0, 0), (1, 1), (2, 2)])
  656. >>> line.reverse()
  657. >>> line #doctest: +NORMALIZE_WHITESPACE
  658. Line([Point(2.000000, 2.000000),
  659. Point(1.000000, 1.000000),
  660. Point(0.000000, 0.000000)])
  661. ..
  662. """
  663. libvect.Vect_line_reverse(self.c_points)
  664. def segment(self, start, end):
  665. """Create line segment. using the ``Vect_line_segment`` C function."""
  666. line = Line()
  667. libvect.Vect_line_segment(self.c_points, start, end, line.c_points)
  668. return line
  669. def tolist(self):
  670. """Return a list of tuple. ::
  671. >>> line = Line([(0, 0), (1, 1), (2, 0), (1, -1)])
  672. >>> line.tolist()
  673. [(0.0, 0.0), (1.0, 1.0), (2.0, 0.0), (1.0, -1.0)]
  674. ..
  675. """
  676. return [pnt.coords() for pnt in self.__iter__()]
  677. def toarray(self):
  678. """Return an array of coordinates. ::
  679. >>> line = Line([(0, 0), (1, 1), (2, 0), (1, -1)])
  680. >>> line.toarray() #doctest: +NORMALIZE_WHITESPACE
  681. array([[ 0., 0.],
  682. [ 1., 1.],
  683. [ 2., 0.],
  684. [ 1., -1.]])
  685. ..
  686. """
  687. return np.array(self.tolist())
  688. def get_wkt(self):
  689. """Return a Well Known Text string of the line. ::
  690. >>> line = Line([(0, 0), (1, 1), (1, 2)])
  691. >>> line.get_wkt() #doctest: +ELLIPSIS
  692. 'LINESTRING(0.000000 0.000000, ..., 1.000000 2.000000)'
  693. ..
  694. """
  695. return "LINESTRING(%s)" % ', '.join([
  696. ' '.join(['%f' % coord for coord in pnt.coords()])
  697. for pnt in self.__iter__()])
  698. def from_wkt(self, wkt):
  699. """Read a WKT string. ::
  700. >>> line = Line()
  701. >>> line.from_wkt("LINESTRING(0 0,1 1,1 2)")
  702. >>> line #doctest: +NORMALIZE_WHITESPACE
  703. Line([Point(0.000000, 0.000000),
  704. Point(1.000000, 1.000000),
  705. Point(1.000000, 2.000000)])
  706. ..
  707. """
  708. match = re.match('LINESTRING\((.*)\)', wkt)
  709. if match:
  710. self.reset()
  711. for coord in match.groups()[0].strip().split(','):
  712. self.append(tuple([float(e) for e in coord.split(' ')]))
  713. else:
  714. return None
  715. def get_wkb(self):
  716. """Return a WKB buffer.
  717. .. warning::
  718. Not implemented yet.
  719. """
  720. pass
  721. def buffer(self, dist=None, dist_x=None, dist_y=None,
  722. angle=0, round_=True, caps=True, tol=0.1):
  723. """Return the buffer area around the line, using the
  724. ``Vect_line_buffer2`` C function.
  725. Parameters
  726. ----------
  727. dist : numeric
  728. The distance around the line.
  729. dist_x: numeric, optional
  730. The distance along x
  731. dist_y: numeric, optional
  732. The distance along y
  733. angle: numeric, optional
  734. The angle between 0x and major axis
  735. round_: bool, optional
  736. To make corners round
  737. caps: bool, optional
  738. To add caps at line ends
  739. tol: float, optional
  740. Fix the maximum distance between theoretical arc
  741. and output segments
  742. Returns
  743. -------
  744. buffer : Area
  745. The buffer area around the line.
  746. Example
  747. ---------
  748. ::
  749. >>> line = Line([(0, 0), (0, 2)])
  750. >>> area = line.buffer(10)
  751. >>> area.boundary #doctest: +ELLIPSIS
  752. Line([Point(-10.000000, 0.000000),...Point(-10.000000, 0.000000)])
  753. >>> area.centroid
  754. Point(0.000000, 0.000000)
  755. >>> area.isles
  756. []
  757. ..
  758. """
  759. if dist is not None:
  760. dist_x = dist
  761. dist_y = dist
  762. elif not dist_x or not dist_y:
  763. raise TypeError('TypeError: buffer expected 1 arguments, got 0')
  764. p_bound = ctypes.pointer(ctypes.pointer(libvect.line_pnts()))
  765. pp_isle = ctypes.pointer(ctypes.pointer(
  766. ctypes.pointer(libvect.line_pnts())))
  767. n_isles = ctypes.pointer(ctypes.c_int())
  768. libvect.Vect_line_buffer2(self.c_points,
  769. dist_x, dist_y, angle,
  770. int(round_), int(caps), tol,
  771. p_bound, pp_isle, n_isles)
  772. return Area(boundary=Line(c_points=p_bound.contents),
  773. centroid=self[0],
  774. isles=[Line(c_points=pp_isle[i].contents)
  775. for i in xrange(n_isles.contents.value)])
  776. def reset(self):
  777. """Reset line, using `Vect_reset_line` C function. ::
  778. >>> line = Line([(0, 0), (1, 1), (2, 0), (1, -1)])
  779. >>> len(line)
  780. 4
  781. >>> line.reset()
  782. >>> len(line)
  783. 0
  784. >>> line
  785. Line([])
  786. ..
  787. """
  788. libvect.Vect_reset_line(self.c_points)
  789. class Node(object):
  790. pass
  791. class Boundary(Line):
  792. """
  793. """
  794. def __init__(self, area_id=None, lines=None, left=None, right=None,
  795. **kargs):
  796. super(Boundary, self).__init__(**kargs)
  797. self.area_id = area_id
  798. self.ilist = Ilist()
  799. self.lines = lines
  800. if lines:
  801. if len(lines) != len(left) or len(lines) != len(right):
  802. str_err = "Left and right must have the same length of lines"
  803. raise ValueError(str_err)
  804. self.left = Ilist()
  805. self.right = Ilist()
  806. # geometry type
  807. self.gtype = libvect.GV_BOUNDARY
  808. def __repr__(self):
  809. return "Boundary(v_id=%r)" % self.id
  810. def boundaries(self):
  811. """Returna Ilist object with the line id"""
  812. bounds = Ilist()
  813. libvect.Vect_get_area_boundaries(self.c_mapinfo, self.area_id,
  814. bounds.c_ilist)
  815. return bounds
  816. def get_left_right(self):
  817. """Return left and right value"""
  818. left = ctypes.poiter(ctypes.c_int())
  819. right = ctypes.poiter(ctypes.c_int())
  820. libvect.Vect_get_line_areas(self.c_mapinfo, self.id,
  821. left, right)
  822. return left.contents.value, right.contents.value
  823. def area(self):
  824. """Return the area of the polygon.
  825. ::
  826. >>> bound = Boundary([(0, 0), (0, 2), (2, 2), (2, 0), (0, 0)])
  827. >>> bound.area()
  828. 4
  829. .."""
  830. libgis.G_begin_polygon_area_calculations()
  831. return libgis.G_area_of_polygon(self.c_points.contents.x,
  832. self.c_points.contents.y,
  833. self.c_points.contents.n_points)
  834. class Centroid(Point):
  835. """The Centroid class inherit from the Point class.
  836. Centroid contains an attribute with the C Map_info struct, and attributes
  837. with the id of the Area. ::
  838. >>> centroid = Centroid(x=0, y=10)
  839. >>> centroid
  840. Centoid(0.000000, 10.000000)
  841. >>> from grass.pygrass.vector import VectorTopo
  842. >>> geo = VectorTopo('geology')
  843. >>> geo.open()
  844. >>> centroid = Centroid(v_id=1, c_mapinfo=mun.c_mapinfo)
  845. >>> centroid
  846. Centoid(893202.874416, 297339.312795)
  847. ..
  848. """
  849. def __init__(self, area_id=None, **kargs):
  850. super(Centroid, self).__init__(**kargs)
  851. self.area_id = area_id
  852. if self.id and self.c_mapinfo and self.area_id is None:
  853. self.area_id = self.get_area_id()
  854. elif self.c_mapinfo and self.area_id and self.id is None:
  855. self.id = self.get_centroid_id()
  856. if self.area_id is not None:
  857. self.read()
  858. # geometry type
  859. self.gtype = libvect.GV_CENTROID
  860. #self.c_pline = ctypes.pointer(libvect.P_line()) if topology else None
  861. def __repr__(self):
  862. return "Centoid(%s)" % ', '.join(['%f' % co for co in self.coords()])
  863. def get_centroid_id(self):
  864. """Return the centroid_id, using the c_mapinfo and an area_id
  865. attributes of the class, and calling the Vect_get_area_centroid
  866. C function, if no centroid_id were found return None"""
  867. centroid_id = libvect.Vect_get_area_centroid(self.c_mapinfo,
  868. self.area_id)
  869. return centroid_id if centroid_id != 0 else None
  870. def get_area_id(self):
  871. """Return the area_id, using the c_mapinfo and an centroid_id
  872. attributes of the class, and calling the Vect_get_centroid_area
  873. C function, if no area_id were found return None"""
  874. area_id = libvect.Vect_get_centroid_area(self.c_mapinfo,
  875. self.id)
  876. return area_id if area_id != 0 else None
  877. class Isle(Geo):
  878. """An Isle is an area contained by another area.
  879. """
  880. def __init__(self, **kargs):
  881. super(Isle, self).__init__(**kargs)
  882. #self.area_id = area_id
  883. def __repr__(self):
  884. return "Isle(%d)" % (self.id)
  885. def boundaries(self):
  886. ilist = Ilist()
  887. libvect.Vect_get_isle_boundaries(self.c_mapinfo, self.id,
  888. ilist.c_ilist)
  889. return ilist
  890. def bbox(self):
  891. bbox = Bbox()
  892. libvect.Vect_get_isle_box(self.c_mapinfo, self.id, bbox.c_bbox)
  893. return bbox
  894. def points(self):
  895. """Return a Line object with the outer ring points"""
  896. line = Line()
  897. libvect.Vect_get_isle_points(self.c_mapinfo, self.id, line.c_points)
  898. return line
  899. def points_geos(self):
  900. """Return a Line object with the outer ring points
  901. """
  902. return libvect.Vect_get_isle_points_geos(self.c_mapinfo, self.id)
  903. def area_id(self):
  904. """Returns area id for isle."""
  905. return libvect.Vect_get_isle_area(self.c_mapinfo, self.id)
  906. def alive(self):
  907. """Check if isle is alive or dead (topology required)"""
  908. return bool(libvect.Vect_isle_alive(self.c_mapinfo, self.id))
  909. def contain_pnt(self, pnt):
  910. """Check if point is in area."""
  911. bbox = self.bbox()
  912. return bool(libvect.Vect_point_in_island(pnt.x, pnt.y,
  913. self.c_mapinfo, self.id,
  914. bbox.c_bbox.contents))
  915. def area(self):
  916. """Return the area value of an Isle"""
  917. border = self.points()
  918. return libgis.G_area_of_polygon(border.c_points.contents.x,
  919. border.c_points.contents.y,
  920. border.c_points.contents.n_points)
  921. def perimeter(self):
  922. """Return the perimeter value of an Isle.
  923. ::
  924. double Vect_area_perimeter()
  925. """
  926. border = self.points()
  927. return libvect.Vect_area_perimeter(border.c_points)
  928. class Isles(object):
  929. def __init__(self, c_mapinfo, area_id):
  930. self.c_mapinfo = c_mapinfo
  931. self.area_id = area_id
  932. self._isles_id = self.get_isles_id()
  933. self._isles = self.get_isles()
  934. def __len__(self):
  935. return libvect.Vect_get_area_num_isles(self.c_mapinfo, self.area_id)
  936. def __repr__(self):
  937. return "Isles(%r)" % self._isles
  938. def __getitem__(self, key):
  939. return self._isles[key]
  940. def get_isles_id(self):
  941. return [libvect.Vect_get_area_isle(self.c_mapinfo, self.area_id, i)
  942. for i in range(self.__len__())]
  943. def get_isles(self):
  944. return [Isle(v_id=isle_id, c_mapinfo=self.c_mapinfo)
  945. for isle_id in self._isles_id]
  946. def select_by_bbox(self, bbox):
  947. """Vect_select_isles_by_box"""
  948. pass
  949. class Area(Geo):
  950. """
  951. 'Vect_build_line_area',
  952. 'Vect_find_area',
  953. 'Vect_get_area_box',
  954. 'Vect_get_area_points_geos',
  955. 'Vect_get_centroid_area',
  956. 'Vect_get_isle_area',
  957. 'Vect_get_line_areas',
  958. 'Vect_get_num_areas',
  959. 'Vect_get_point_in_area',
  960. 'Vect_isle_find_area',
  961. 'Vect_point_in_area',
  962. 'Vect_point_in_area_outer_ring',
  963. 'Vect_read_area_geos',
  964. 'Vect_remove_small_areas',
  965. 'Vect_select_areas_by_box',
  966. 'Vect_select_areas_by_polygon']
  967. """
  968. def __init__(self, boundary=None, centroid=None, isles=[], **kargs):
  969. super(Area, self).__init__(**kargs)
  970. if self.id is not None and self.c_mapinfo:
  971. self.boundary = self.points()
  972. self.centroid = self.centroid()
  973. self.isles = self.get_isles()
  974. libvect.Vect_read_line(self.c_mapinfo, None, self.c_cats,
  975. self.centroid.id)
  976. self.line = self.c_cats.contents.cat.contents.value
  977. elif boundary and centroid:
  978. self.boundary = boundary
  979. self.centroid = centroid
  980. self.isles = isles
  981. else:
  982. str_err = "To instantiate an Area you need at least: Boundary and Centroid"
  983. raise GrassError(str_err)
  984. # set the attributes
  985. if self.attrs and self.line:
  986. self.attrs = Attrs(self.line,
  987. self.attrs.table, self.attrs.writable)
  988. # geometry type
  989. self.gtype = libvect.GV_AREA
  990. def __repr__(self):
  991. return "Area(%d)" % self.id if self.id else "Area( )"
  992. def init_from_id(self, area_id=None):
  993. """Return an Area object"""
  994. if area_id is None and self.id is None:
  995. raise ValueError("You need to give or set the area_id")
  996. self.id = area_id if area_id is not None else self.id
  997. # get boundary
  998. self.get_boundary()
  999. # get isles
  1000. self.get_isles()
  1001. pass
  1002. def points(self):
  1003. """Return a Line object with the outer ring"""
  1004. line = Line()
  1005. libvect.Vect_get_area_points(self.c_mapinfo, self.id, line.c_points)
  1006. return line
  1007. def centroid(self):
  1008. centroid_id = libvect.Vect_get_area_centroid(self.c_mapinfo, self.id)
  1009. #import pdb; pdb.set_trace()
  1010. return Centroid(v_id=centroid_id, c_mapinfo=self.c_mapinfo,
  1011. area_id=self.id)
  1012. def num_isles(self):
  1013. return libvect.Vect_get_area_num_isles(self.c_mapinfo, self.id)
  1014. def get_isles(self):
  1015. """Instantiate the boundary attribute reading area_id"""
  1016. return Isles(self.c_mapinfo, self.id)
  1017. def area(self):
  1018. """Returns area of area without areas of isles.
  1019. double Vect_get_area_area (const struct Map_info *Map, int area)
  1020. """
  1021. return libvect.Vect_get_area_area(self.c_mapinfo, self.id)
  1022. def alive(self):
  1023. """Check if area is alive or dead (topology required)
  1024. """
  1025. return bool(libvect.Vect_area_alive(self.c_mapinfo, self.id))
  1026. def bbox(self):
  1027. """
  1028. Vect_get_area_box
  1029. """
  1030. bbox = Bbox()
  1031. libvect.Vect_get_area_box(self.c_mapinfo, self.id, bbox.c_bbox)
  1032. return bbox
  1033. def buffer(self, dist=None, dist_x=None, dist_y=None,
  1034. angle=0, round_=True, caps=True, tol=0.1):
  1035. """Return the buffer area around the area, using the
  1036. ``Vect_area_buffer2`` C function.
  1037. Parameters
  1038. ----------
  1039. dist : numeric
  1040. The distance around the line.
  1041. dist_x: numeric, optional
  1042. The distance along x
  1043. dist_y: numeric, optional
  1044. The distance along y
  1045. angle: numeric, optional
  1046. The angle between 0x and major axis
  1047. round_: bool, optional
  1048. To make corners round
  1049. caps: bool, optional
  1050. To add caps at line ends
  1051. tol: float, optional
  1052. Fix the maximum distance between theoretical arc
  1053. and output segments
  1054. Returns
  1055. -------
  1056. buffer : Area
  1057. The buffer area around the line.
  1058. """
  1059. if dist is not None:
  1060. dist_x = dist
  1061. dist_y = dist
  1062. elif not dist_x or not dist_y:
  1063. raise TypeError('TypeError: buffer expected 1 arguments, got 0')
  1064. p_bound = ctypes.pointer(ctypes.pointer(libvect.line_pnts()))
  1065. pp_isle = ctypes.pointer(ctypes.pointer(
  1066. ctypes.pointer(libvect.line_pnts())))
  1067. n_isles = ctypes.pointer(ctypes.c_int())
  1068. libvect.Vect_area_buffer2(self.c_mapinfo, self.id,
  1069. dist_x, dist_y, angle,
  1070. int(round_), int(caps), tol,
  1071. p_bound, pp_isle, n_isles)
  1072. return Area(boundary=Line(c_points=p_bound.contents),
  1073. centroid=self.centroid,
  1074. isles=[Line(c_points=pp_isle[i].contents)
  1075. for i in xrange(n_isles.contents.value)])
  1076. def boundaries(self):
  1077. """Creates list of boundaries for given area.
  1078. int Vect_get_area_boundaries(const struct Map_info *Map,
  1079. int area, struct ilist *List)
  1080. """
  1081. ilist = Ilist()
  1082. libvect.Vect_get_area_boundaries(self.c_mapinfo, self.id,
  1083. ilist.c_ilist)
  1084. return ilist
  1085. def cats(self):
  1086. """Get area categories.
  1087. int Vect_get_area_cats (const struct Map_info *Map,
  1088. int area, struct line_cats *Cats)
  1089. """
  1090. cats = Cats()
  1091. libvect.Vect_get_area_cats(self.c_mapinfo, self.id, cats.c_cats)
  1092. return cats
  1093. def get_first_cat(self):
  1094. """Find FIRST category of given field and area.
  1095. int Vect_get_area_cat(const struct Map_info *Map, int area, int field)
  1096. """
  1097. pass
  1098. def contain_pnt(self, pnt):
  1099. """Check if point is in area.
  1100. int Vect_point_in_area(double x, double y,
  1101. const struct Map_info *Map,
  1102. int area, struct bound_box box)
  1103. """
  1104. bbox = self.bbox()
  1105. libvect.Vect_point_in_area(pnt.x, pnt.y, self.c_mapinfo, self.id,
  1106. bbox.c_bbox)
  1107. return bbox
  1108. def perimeter(self):
  1109. """Calculate area perimeter.
  1110. double Vect_area_perimeter (const struct line_pnts *Points)
  1111. """
  1112. border = self.points()
  1113. return libvect.Vect_area_perimeter(border.c_points)