geometry.py 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982
  1. """
  2. Created on Wed Jul 18 10:46:25 2012
  3. @author: pietro
  4. """
  5. import ctypes
  6. import re
  7. from collections import namedtuple
  8. import numpy as np
  9. import grass.lib.gis as libgis
  10. import grass.lib.vector as libvect
  11. from grass.pygrass.utils import decode
  12. from grass.pygrass.errors import GrassError, mapinfo_must_be_set
  13. from grass.pygrass.vector.basic import Ilist, Bbox, Cats
  14. from grass.pygrass.vector import sql
  15. # For test purposes
  16. test_vector_name = "geometry_doctest_map"
  17. LineDist = namedtuple("LineDist", "point dist spdist sldist")
  18. WKT = {
  19. "POINT\((.*)\)": "point", # 'POINT\(\s*([+-]*\d+\.*\d*)+\s*\)'
  20. "LINESTRING\((.*)\)": "line",
  21. }
  22. def read_WKT(string):
  23. """Read the string and return a geometry object
  24. **WKT**:
  25. ::
  26. POINT(0 0)
  27. LINESTRING(0 0,1 1,1 2)
  28. POLYGON((0 0,4 0,4 4,0 4,0 0),(1 1, 2 1, 2 2, 1 2,1 1))
  29. MULTIPOINT(0 0,1 2)
  30. MULTILINESTRING((0 0,1 1,1 2),(2 3,3 2,5 4))
  31. MULTIPOLYGON(((0 0,4 0,4 4,0 4,0 0),(1 1,2 1,2 2,1 2,1 1)),
  32. ((-1 -1,-1 -2,-2 -2,-2 -1,-1 -1)))
  33. GEOMETRYCOLLECTION(POINT(2 3),LINESTRING(2 3,3 4))
  34. **EWKT**:
  35. ::
  36. POINT(0 0 0) -- XYZ
  37. SRID=32632;POINT(0 0) -- XY with SRID
  38. POINTM(0 0 0) -- XYM
  39. POINT(0 0 0 0) -- XYZM
  40. SRID=4326;MULTIPOINTM(0 0 0,1 2 1) -- XYM with SRID
  41. MULTILINESTRING((0 0 0,1 1 0,1 2 1),(2 3 1,3 2 1,5 4 1))
  42. 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))
  43. MULTIPOLYGON(((0 0 0,4 0 0,4 4 0,0 4 0,0 0 0),
  44. (1 1 0,2 1 0,2 2 0,1 2 0,1 1 0)),
  45. ((-1 -1 0,-1 -2 0,-2 -2 0,-2 -1 0,-1 -1 0)))
  46. GEOMETRYCOLLECTIONM( POINTM(2 3 9), LINESTRINGM(2 3 4, 3 4 5) )
  47. MULTICURVE( (0 0, 5 5), CIRCULARSTRING(4 0, 4 4, 8 4) )
  48. POLYHEDRALSURFACE( ((0 0 0, 0 0 1, 0 1 1, 0 1 0, 0 0 0)),
  49. ((0 0 0, 0 1 0, 1 1 0, 1 0 0, 0 0 0)),
  50. ((0 0 0, 1 0 0, 1 0 1, 0 0 1, 0 0 0)),
  51. ((1 1 0, 1 1 1, 1 0 1, 1 0 0, 1 1 0)),
  52. ((0 1 0, 0 1 1, 1 1 1, 1 1 0, 0 1 0)),
  53. ((0 0 1, 1 0 1, 1 1 1, 0 1 1, 0 0 1)) )
  54. TRIANGLE ((0 0, 0 9, 9 0, 0 0))
  55. 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)) )
  56. """
  57. for regexp, obj in WKT.items():
  58. if re.match(regexp, string):
  59. geo = 10
  60. return obj(geo)
  61. def read_WKB(buff):
  62. """Read the binary buffer and return a geometry object"""
  63. pass
  64. def intersects(lineA, lineB, with_z=False):
  65. """Return a list of points
  66. >>> lineA = Line([(0, 0), (4, 0)])
  67. >>> lineB = Line([(2, 2), (2, -2)])
  68. >>> intersects(lineA, lineB)
  69. Line([Point(2.000000, 0.000000)])
  70. """
  71. line = Line()
  72. if libvect.Vect_line_get_intersections(
  73. lineA.c_points, lineB.c_points, line.c_points, int(with_z)
  74. ):
  75. return line
  76. else:
  77. return []
  78. # =============================================
  79. # GEOMETRY
  80. # =============================================
  81. def get_xyz(pnt):
  82. """Return a tuple with: x, y, z.
  83. >>> pnt = Point(0, 0)
  84. >>> get_xyz(pnt)
  85. (0.0, 0.0, 0.0)
  86. >>> get_xyz((1, 1))
  87. (1, 1, 0.0)
  88. >>> get_xyz((1, 1, 2))
  89. (1, 1, 2)
  90. >>> get_xyz((1, 1, 2, 2)) #doctest: +ELLIPSIS
  91. Traceback (most recent call last):
  92. ...
  93. ValueError: The the format of the point is not supported: (1, 1, 2, 2)
  94. """
  95. if isinstance(pnt, Point):
  96. if pnt.is2D:
  97. x, y = pnt.x, pnt.y
  98. z = 0.0
  99. else:
  100. x, y, z = pnt.x, pnt.y, pnt.z
  101. else:
  102. if len(pnt) == 2:
  103. x, y = pnt
  104. z = 0.0
  105. elif len(pnt) == 3:
  106. x, y, z = pnt
  107. else:
  108. str_error = "The the format of the point is not supported: {0!r}"
  109. raise ValueError(str_error.format(pnt))
  110. return x, y, z
  111. class Attrs(object):
  112. def __init__(self, cat, table, writeable=False):
  113. self._cat = None
  114. self.cond = ""
  115. self.table = table
  116. self.cat = cat
  117. self.writeable = writeable
  118. def _get_cat(self):
  119. return self._cat
  120. def _set_cat(self, value):
  121. self._cat = value
  122. if value:
  123. # update condition
  124. self.cond = "%s=%d" % (self.table.key, value)
  125. cat = property(fget=_get_cat, fset=_set_cat, doc="Set and obtain cat value")
  126. def __getitem__(self, keys):
  127. """Return the value stored in the attribute table.
  128. >>> from grass.pygrass.vector import VectorTopo
  129. >>> test_vect = VectorTopo(test_vector_name)
  130. >>> test_vect.open('r')
  131. >>> v1 = test_vect[1]
  132. >>> v1.attrs['name']
  133. 'point'
  134. >>> v1.attrs['name', 'value']
  135. ('point', 1.0)
  136. >>> test_vect.close()
  137. """
  138. sqlcode = sql.SELECT_WHERE.format(
  139. cols=(keys if np.isscalar(keys) else ", ".join(keys)),
  140. tname=self.table.name,
  141. condition=self.cond,
  142. )
  143. cur = self.table.execute(sqlcode)
  144. results = cur.fetchone()
  145. if results is not None:
  146. return results[0] if len(results) == 1 else results
  147. def __setitem__(self, keys, values):
  148. """Set value of a given column of a table attribute.
  149. >>> from grass.pygrass.vector import VectorTopo
  150. >>> test_vect = VectorTopo(test_vector_name)
  151. >>> test_vect.open('r')
  152. >>> v1 = test_vect[1]
  153. >>> v1.attrs['name']
  154. 'point'
  155. >>> v1.attrs['name'] = "new_point_1"
  156. >>> v1.attrs['name']
  157. 'new_point_1'
  158. >>> v1.attrs['name', 'value'] = "new_point_2", 100.
  159. >>> v1.attrs['name', 'value']
  160. ('new_point_2', 100.0)
  161. >>> v1.attrs['name', 'value'] = "point", 1.
  162. >>> v1.attrs.table.conn.commit()
  163. >>> test_vect.close()
  164. """
  165. if self.writeable:
  166. if np.isscalar(keys):
  167. keys, values = (keys,), (values,)
  168. # check if key is a column of the table or not
  169. for key in keys:
  170. if key not in self.table.columns:
  171. raise KeyError("Column: %s not in table" % key)
  172. # prepare the string using as paramstyle: qmark
  173. vals = ",".join(["%s=?" % k for k in keys])
  174. # "UPDATE {tname} SET {values} WHERE {condition};"
  175. sqlcode = sql.UPDATE_WHERE.format(
  176. tname=self.table.name, values=vals, condition=self.cond
  177. )
  178. self.table.execute(sqlcode, values=values)
  179. # self.table.conn.commit()
  180. else:
  181. str_err = "You can only read the attributes if the map is in another mapset"
  182. raise GrassError(str_err)
  183. def __dict__(self):
  184. """Return a dict of the attribute table row."""
  185. dic = {}
  186. for key, val in zip(self.keys(), self.values()):
  187. dic[key] = val
  188. return dic
  189. def values(self):
  190. """Return the values of the attribute table row.
  191. >>> from grass.pygrass.vector import VectorTopo
  192. >>> test_vect = VectorTopo(test_vector_name)
  193. >>> test_vect.open('r')
  194. >>> v1 = test_vect[1]
  195. >>> v1.attrs.values()
  196. (1, 'point', 1.0)
  197. >>> test_vect.close()
  198. """
  199. # SELECT {cols} FROM {tname} WHERE {condition}
  200. cur = self.table.execute(
  201. sql.SELECT_WHERE.format(
  202. cols="*", tname=self.table.name, condition=self.cond
  203. )
  204. )
  205. return cur.fetchone()
  206. def keys(self):
  207. """Return the column name of the attribute table.
  208. >>> from grass.pygrass.vector import VectorTopo
  209. >>> test_vect = VectorTopo(test_vector_name)
  210. >>> test_vect.open('r')
  211. >>> v1 = test_vect[1]
  212. >>> v1.attrs.keys()
  213. ['cat', 'name', 'value']
  214. >>> test_vect.close()
  215. """
  216. return self.table.columns.names()
  217. def commit(self):
  218. """Save the changes"""
  219. self.table.conn.commit()
  220. class Geo(object):
  221. """
  222. Base object for different feature types
  223. """
  224. gtype = None
  225. def __init__(
  226. self,
  227. v_id=0,
  228. c_mapinfo=None,
  229. c_points=None,
  230. c_cats=None,
  231. table=None,
  232. writeable=False,
  233. is2D=True,
  234. free_points=False,
  235. free_cats=False,
  236. ):
  237. """Constructor of a geometry object
  238. :param v_id: The vector feature id
  239. :param c_mapinfo: A pointer to the vector mapinfo structure
  240. :param c_points: A pointer to a libvect.line_pnts structure, this
  241. is optional, if not set an internal structure will
  242. be allocated and free'd at object destruction
  243. :param c_cats: A pointer to a libvect.line_cats structure, this
  244. is optional, if not set an internal structure will
  245. be allocated and free'd at object destruction
  246. :param table: The attribute table to select attributes for
  247. this feature
  248. :param writeable: Not sure what this is for?
  249. :param is2D: If True this feature has two dimensions, False if
  250. this feature has three dimensions
  251. :param free_points: Set this True if the provided c_points structure
  252. should be free'd at object destruction, be aware
  253. that no other object should free them, otherwise
  254. you can expect a double free corruption segfault
  255. :param free_cats: Set this True if the provided c_cats structure
  256. should be free'd at object destruction, be aware
  257. that no other object should free them, otherwise
  258. you can expect a double free corruption segfault
  259. """
  260. self.id = v_id # vector id
  261. self.c_mapinfo = c_mapinfo
  262. self.is2D = (
  263. is2D if is2D is not None else bool(libvect.Vect_is_3d(self.c_mapinfo) != 1)
  264. )
  265. # Set True if cats and points are allocated by this object
  266. # to free the cats and points structures on destruction
  267. self._free_points = False
  268. self._free_cats = False
  269. read = False
  270. # set c_points
  271. if c_points is None:
  272. self.c_points = ctypes.pointer(libvect.line_pnts())
  273. self._free_points = True
  274. read = True
  275. else:
  276. self.c_points = c_points
  277. self._free_points = free_points
  278. # set c_cats
  279. if c_cats is None:
  280. self.c_cats = ctypes.pointer(libvect.line_cats())
  281. self._free_cats = free_cats
  282. read = True
  283. else:
  284. self.c_cats = c_cats
  285. self._free_cats = True
  286. if self.id and self.c_mapinfo is not None and read:
  287. self.read()
  288. # set the attributes as last thing to do
  289. self.attrs = None
  290. if table is not None and self.cat is not None:
  291. self.attrs = Attrs(self.cat, table, writeable)
  292. def __del__(self):
  293. """Take care of the allocated line_pnts and line_cats allocation"""
  294. if self._free_points is True and self.c_points:
  295. if self.c_points.contents.alloc_points > 0:
  296. # print("G_free(points) [%i]"%(self.c_points.contents.alloc_points))
  297. libgis.G_free(self.c_points.contents.x)
  298. libgis.G_free(self.c_points.contents.y)
  299. if self.c_points.contents.z:
  300. libgis.G_free(self.c_points.contents.z)
  301. if self._free_cats is True and self.c_cats:
  302. if self.c_cats.contents.alloc_cats > 0:
  303. # print("G_free(cats) [%i]"%(self.c_cats.contents.alloc_cats))
  304. libgis.G_free(self.c_cats.contents.cat)
  305. @property
  306. def cat(self):
  307. if self.c_cats.contents.cat:
  308. return self.c_cats.contents.cat.contents.value
  309. def has_topology(self):
  310. if self.c_mapinfo is not None:
  311. return self.c_mapinfo.contents.level == 2
  312. else:
  313. return False
  314. @mapinfo_must_be_set
  315. def read(self):
  316. """Read and set the coordinates of the centroid from the vector map,
  317. using the centroid_id and calling the Vect_read_line C function"""
  318. self.id, ftype, c_points, c_cats = c_read_line(
  319. self.id, self.c_mapinfo, self.c_points, self.c_cats
  320. )
  321. def to_wkt(self):
  322. """Return a "well know text" (WKT) geometry string, this method uses
  323. the GEOS implementation in the vector library. ::
  324. >>> pnt = Point(10, 100)
  325. >>> pnt.to_wkt()
  326. 'POINT (10.0000000000000000 100.0000000000000000)'
  327. """
  328. return decode(
  329. libvect.Vect_line_to_wkt(self.c_points, self.gtype, not self.is2D)
  330. )
  331. def to_wkb(self):
  332. """Return a "well know binary" (WKB) geometry byte array, this method uses
  333. the GEOS implementation in the vector library. ::
  334. >>> pnt = Point(10, 100)
  335. >>> wkb = pnt.to_wkb()
  336. >>> len(wkb)
  337. 21
  338. """
  339. size = ctypes.c_size_t()
  340. barray = libvect.Vect_line_to_wkb(
  341. self.c_points, self.gtype, not self.is2D, ctypes.byref(size)
  342. )
  343. return ctypes.string_at(barray, size.value)
  344. class Point(Geo):
  345. """Instantiate a Point object that could be 2 or 3D, default
  346. parameters are 0.
  347. ::
  348. >>> pnt = Point()
  349. >>> pnt.x
  350. 0.0
  351. >>> pnt.y
  352. 0.0
  353. >>> pnt.z
  354. >>> pnt.is2D
  355. True
  356. >>> pnt
  357. Point(0.000000, 0.000000)
  358. >>> pnt.z = 0
  359. >>> pnt.is2D
  360. False
  361. >>> pnt
  362. Point(0.000000, 0.000000, 0.000000)
  363. >>> print(pnt)
  364. POINT Z (0.0000000000000000 0.0000000000000000 0.0000000000000000)
  365. >>> c_points = ctypes.pointer(libvect.line_pnts())
  366. >>> c_cats = ctypes.pointer(libvect.line_cats())
  367. >>> p = Point(c_points = c_points, c_cats=c_cats)
  368. >>> del p
  369. >>> c_points = ctypes.pointer(libvect.line_pnts())
  370. >>> c_cats = ctypes.pointer(libvect.line_cats())
  371. >>> p = Point(c_points=c_points, c_cats=c_cats, free_points=True,
  372. ... free_cats=True)
  373. >>> del p
  374. ..
  375. """
  376. # geometry type
  377. gtype = libvect.GV_POINT
  378. def __init__(self, x=0, y=0, z=None, **kargs):
  379. super(Point, self).__init__(**kargs)
  380. if self.id and self.c_mapinfo:
  381. self.read()
  382. else:
  383. self.is2D = True if z is None else False
  384. z = z if z is not None else 0
  385. libvect.Vect_append_point(self.c_points, x, y, z)
  386. def _get_x(self):
  387. return self.c_points.contents.x[0]
  388. def _set_x(self, value):
  389. self.c_points.contents.x[0] = value
  390. x = property(fget=_get_x, fset=_set_x, doc="Set and obtain x coordinate")
  391. def _get_y(self):
  392. return self.c_points.contents.y[0]
  393. def _set_y(self, value):
  394. self.c_points.contents.y[0] = value
  395. y = property(fget=_get_y, fset=_set_y, doc="Set and obtain y coordinate")
  396. def _get_z(self):
  397. if self.is2D:
  398. return None
  399. return self.c_points.contents.z[0]
  400. def _set_z(self, value):
  401. if value is None:
  402. self.is2D = True
  403. self.c_points.contents.z[0] = 0
  404. else:
  405. self.c_points.contents.z[0] = value
  406. self.is2D = False
  407. z = property(fget=_get_z, fset=_set_z, doc="Set and obtain z coordinate")
  408. def __str__(self):
  409. return self.to_wkt()
  410. def __repr__(self):
  411. return "Point(%s)" % ", ".join(["%f" % coor for coor in self.coords()])
  412. def __eq__(self, pnt):
  413. """Return True if the coordinates are the same.
  414. >>> p0 = Point()
  415. >>> p1 = Point()
  416. >>> p2 = Point(1, 1)
  417. >>> p0 == p1
  418. True
  419. >>> p1 == p2
  420. False
  421. """
  422. if isinstance(pnt, Point):
  423. return pnt.coords() == self.coords()
  424. return Point(*pnt).coords() == self.coords()
  425. def __ne__(self, other):
  426. return not self == other
  427. # Restore Python 2 hashing beaviour on Python 3
  428. __hash__ = object.__hash__
  429. def coords(self):
  430. """Return a tuple with the point coordinates. ::
  431. >>> pnt = Point(10, 100)
  432. >>> pnt.coords()
  433. (10.0, 100.0)
  434. If the point is 2D return a x, y tuple. But if we change the ``z``
  435. the Point object become a 3D point, therefore the method return a
  436. x, y, z tuple. ::
  437. >>> pnt.z = 1000.
  438. >>> pnt.coords()
  439. (10.0, 100.0, 1000.0)
  440. ..
  441. """
  442. if self.is2D:
  443. return self.x, self.y
  444. else:
  445. return self.x, self.y, self.z
  446. def to_wkt_p(self):
  447. """Return a "well know text" (WKT) geometry string Python implementation. ::
  448. >>> pnt = Point(10, 100)
  449. >>> pnt.to_wkt_p()
  450. 'POINT(10.000000 100.000000)'
  451. .. warning::
  452. Only ``POINT`` (2/3D) are supported, ``POINTM`` and ``POINT`` with:
  453. ``XYZM`` are not supported yet.
  454. """
  455. return "POINT(%s)" % " ".join(["%f" % coord for coord in self.coords()])
  456. def distance(self, pnt):
  457. """Calculate distance of 2 points, using the Vect_points_distance
  458. C function, If one of the point have z == None, return the 2D distance.
  459. :param pnt: the point for calculate the distance
  460. :type pnt: a Point object or a tuple with the coordinates
  461. >>> pnt0 = Point(0, 0, 0)
  462. >>> pnt1 = Point(1, 0)
  463. >>> pnt0.distance(pnt1)
  464. 1.0
  465. >>> pnt1.z = 1
  466. >>> pnt1
  467. Point(1.000000, 0.000000, 1.000000)
  468. >>> pnt0.distance(pnt1)
  469. 1.4142135623730951
  470. """
  471. if self.is2D or pnt.is2D:
  472. return libvect.Vect_points_distance(self.x, self.y, 0, pnt.x, pnt.y, 0, 0)
  473. else:
  474. return libvect.Vect_points_distance(
  475. self.x, self.y, self.z, pnt.x, pnt.y, pnt.z, 1
  476. )
  477. def buffer(
  478. self, dist=None, dist_x=None, dist_y=None, angle=0, round_=True, tol=0.1
  479. ):
  480. """Return the buffer area around the point, using the
  481. ``Vect_point_buffer2`` C function.
  482. :param dist: the distance around the point
  483. :type dist: num
  484. :param dist_x: the distance along x
  485. :type dist_x: num
  486. :param dist_y: the distance along y
  487. :type dist_y: num
  488. :param angle: the angle between 0x and major axis
  489. :type angle: num
  490. :param round_: to make corners round
  491. :type round_: bool
  492. :param tol: fix the maximum distance between theoretical arc and
  493. output segments
  494. :type tol: float
  495. :returns: the buffer as Area object
  496. >>> pnt = Point(0, 0)
  497. >>> boundary, centroid = pnt.buffer(10)
  498. >>> boundary #doctest: +ELLIPSIS
  499. Line([Point(10.000000, 0.000000),...Point(10.000000, 0.000000)])
  500. >>> centroid
  501. Point(0.000000, 0.000000)
  502. """
  503. if dist is not None:
  504. dist_x = dist
  505. dist_y = dist
  506. elif not dist_x or not dist_y:
  507. raise TypeError("TypeError: buffer expected 1 arguments, got 0")
  508. bound = Line()
  509. p_points = ctypes.pointer(bound.c_points)
  510. libvect.Vect_point_buffer2(
  511. self.x, self.y, dist_x, dist_y, angle, int(round_), tol, p_points
  512. )
  513. return (bound, self)
  514. class Line(Geo):
  515. """Instantiate a new Line with a list of tuple, or with a list of Point. ::
  516. >>> line = Line([(0, 0), (1, 1), (2, 0), (1, -1)])
  517. >>> line #doctest: +NORMALIZE_WHITESPACE
  518. Line([Point(0.000000, 0.000000),
  519. Point(1.000000, 1.000000),
  520. Point(2.000000, 0.000000),
  521. Point(1.000000, -1.000000)])
  522. ..
  523. """
  524. # geometry type
  525. gtype = libvect.GV_LINE
  526. def __init__(self, points=None, **kargs):
  527. super(Line, self).__init__(**kargs)
  528. if points is not None:
  529. for pnt in points:
  530. self.append(pnt)
  531. def __getitem__(self, key):
  532. """Get line point of given index, slice allowed. ::
  533. >>> line = Line([(0, 0), (1, 1), (2, 2), (3, 3)])
  534. >>> line[1]
  535. Point(1.000000, 1.000000)
  536. >>> line[-1]
  537. Point(3.000000, 3.000000)
  538. >>> line[:2]
  539. [Point(0.000000, 0.000000), Point(1.000000, 1.000000)]
  540. ..
  541. """
  542. # TODO:
  543. # line[0].x = 10 is not working
  544. # pnt.c_px = ctypes.pointer(self.c_points.contents.x[indx])
  545. # pnt.c_px = ctypes.cast(id(self.c_points.contents.x[indx]),
  546. # ctypes.POINTER(ctypes.c_double))
  547. if isinstance(key, slice):
  548. # import pdb; pdb.set_trace()
  549. # Get the start, stop, and step from the slice
  550. return [
  551. Point(
  552. self.c_points.contents.x[indx],
  553. self.c_points.contents.y[indx],
  554. None if self.is2D else self.c_points.contents.z[indx],
  555. )
  556. for indx in range(*key.indices(len(self)))
  557. ]
  558. elif isinstance(key, int):
  559. if key < 0: # Handle negative indices
  560. key += self.c_points.contents.n_points
  561. if key >= self.c_points.contents.n_points:
  562. raise IndexError("Index out of range")
  563. return Point(
  564. self.c_points.contents.x[key],
  565. self.c_points.contents.y[key],
  566. None if self.is2D else self.c_points.contents.z[key],
  567. )
  568. else:
  569. raise ValueError("Invalid argument type: %r." % key)
  570. def __setitem__(self, indx, pnt):
  571. """Change the coordinate of point. ::
  572. >>> line = Line([(0, 0), (1, 1)])
  573. >>> line[0] = (2, 2)
  574. >>> line
  575. Line([Point(2.000000, 2.000000), Point(1.000000, 1.000000)])
  576. ..
  577. """
  578. x, y, z = get_xyz(pnt)
  579. self.c_points.contents.x[indx] = x
  580. self.c_points.contents.y[indx] = y
  581. self.c_points.contents.z[indx] = z
  582. def __iter__(self):
  583. """Return a Point generator of the Line"""
  584. return (self.__getitem__(i) for i in range(self.__len__()))
  585. def __len__(self):
  586. """Return the number of points of the line."""
  587. return self.c_points.contents.n_points
  588. def __str__(self):
  589. return self.to_wkt()
  590. def __repr__(self):
  591. return "Line([%s])" % ", ".join([repr(pnt) for pnt in self.__iter__()])
  592. def point_on_line(self, distance, angle=0, slope=0):
  593. """Return a Point object on line in the specified distance, using the
  594. `Vect_point_on_line` C function.
  595. Raise a ValueError If the distance exceed the Line length. ::
  596. >>> line = Line([(0, 0), (1, 1)])
  597. >>> line.point_on_line(5) #doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
  598. Traceback (most recent call last):
  599. ...
  600. ValueError: The distance exceed the length of the line,
  601. that is: 1.414214
  602. >>> line.point_on_line(1)
  603. Point(0.707107, 0.707107)
  604. ..
  605. """
  606. # instantiate an empty Point object
  607. maxdist = self.length()
  608. if distance > maxdist:
  609. str_err = "The distance exceed the length of the line, that is: %f"
  610. raise ValueError(str_err % maxdist)
  611. pnt = Point(0, 0, -9999)
  612. if not libvect.Vect_point_on_line(
  613. self.c_points,
  614. distance,
  615. pnt.c_points.contents.x,
  616. pnt.c_points.contents.y,
  617. pnt.c_points.contents.z,
  618. ctypes.pointer(ctypes.c_double(angle)),
  619. ctypes.pointer(ctypes.c_double(slope)),
  620. ):
  621. raise ValueError("Vect_point_on_line give an error.")
  622. pnt.is2D = self.is2D
  623. return pnt
  624. @mapinfo_must_be_set
  625. def alive(self):
  626. """Return True if this line is alive or False if this line is
  627. dead or its index is out of range.
  628. """
  629. return bool(libvect.Vect_line_alive(self.c_mapinfo, self.id))
  630. def append(self, pnt):
  631. """Appends one point to the end of a line, using the
  632. ``Vect_append_point`` C function.
  633. :param pnt: the point to add to line
  634. :type pnt: a Point object or a tuple with the coordinates
  635. >>> line = Line()
  636. >>> line.append((10, 100))
  637. >>> line
  638. Line([Point(10.000000, 100.000000)])
  639. >>> line.append((20, 200))
  640. >>> line
  641. Line([Point(10.000000, 100.000000), Point(20.000000, 200.000000)])
  642. Like python list.
  643. """
  644. x, y, z = get_xyz(pnt)
  645. libvect.Vect_append_point(self.c_points, x, y, z)
  646. def bbox(self, bbox=None):
  647. """Return the bounding box of the line, using ``Vect_line_box``
  648. C function. ::
  649. >>> line = Line([(0, 0), (0, 1), (2, 1), (2, 0)])
  650. >>> bbox = line.bbox()
  651. >>> bbox
  652. Bbox(1.0, 0.0, 2.0, 0.0)
  653. ..
  654. """
  655. bbox = bbox if bbox else Bbox()
  656. libvect.Vect_line_box(self.c_points, bbox.c_bbox)
  657. return bbox
  658. def extend(self, line, forward=True):
  659. """Appends points to the end of a line.
  660. :param line: it is possible to extend a line, give a list of points,
  661. or directly with a line_pnts struct.
  662. :type line: Line object ot list of points
  663. :param forward: if forward is True the line is extend forward otherwise
  664. is extend backward. The method use the
  665. `Vect_append_points` C function.
  666. :type forward: bool
  667. >>> line = Line([(0, 0), (1, 1)])
  668. >>> line.extend( Line([(2, 2), (3, 3)]) )
  669. >>> line #doctest: +NORMALIZE_WHITESPACE
  670. Line([Point(0.000000, 0.000000),
  671. Point(1.000000, 1.000000),
  672. Point(2.000000, 2.000000),
  673. Point(3.000000, 3.000000)])
  674. """
  675. # set direction
  676. if forward:
  677. direction = libvect.GV_FORWARD
  678. else:
  679. direction = libvect.GV_BACKWARD
  680. # check if is a Line object
  681. if isinstance(line, Line):
  682. c_points = line.c_points
  683. else:
  684. # instantiate a Line object
  685. lin = Line()
  686. for pnt in line:
  687. # add the points to the line
  688. lin.append(pnt)
  689. c_points = lin.c_points
  690. libvect.Vect_append_points(self.c_points, c_points, direction)
  691. def insert(self, indx, pnt):
  692. """Insert new point at index position and move all old points at
  693. that position and above up, using ``Vect_line_insert_point``
  694. C function.
  695. :param indx: the index where add new point
  696. :type indx: int
  697. :param pnt: the point to add
  698. :type pnt: a Point object
  699. >>> line = Line([(0, 0), (1, 1)])
  700. >>> line.insert(0, Point(1.000000, -1.000000) )
  701. >>> line #doctest: +NORMALIZE_WHITESPACE
  702. Line([Point(1.000000, -1.000000),
  703. Point(0.000000, 0.000000),
  704. Point(1.000000, 1.000000)])
  705. """
  706. if indx < 0: # Handle negative indices
  707. indx += self.c_points.contents.n_points
  708. if indx >= self.c_points.contents.n_points:
  709. raise IndexError("Index out of range")
  710. x, y, z = get_xyz(pnt)
  711. libvect.Vect_line_insert_point(self.c_points, indx, x, y, z)
  712. def length(self):
  713. """Calculate line length, 3D-length in case of 3D vector line, using
  714. `Vect_line_length` C function. ::
  715. >>> line = Line([(0, 0), (1, 1), (0, 1)])
  716. >>> line.length()
  717. 2.414213562373095
  718. ..
  719. """
  720. return libvect.Vect_line_length(self.c_points)
  721. def length_geodesic(self):
  722. """Calculate line length, usig `Vect_line_geodesic_length` C function.
  723. ::
  724. >>> line = Line([(0, 0), (1, 1), (0, 1)])
  725. >>> line.length_geodesic()
  726. 2.414213562373095
  727. ..
  728. """
  729. return libvect.Vect_line_geodesic_length(self.c_points)
  730. def distance(self, pnt):
  731. """Calculate the distance between line and a point.
  732. :param pnt: the point to calculate distance
  733. :type pnt: a Point object or a tuple with the coordinates
  734. Return a namedtuple with:
  735. * point: the closest point on the line,
  736. * dist: the distance between these two points,
  737. * spdist: distance to point on line from segment beginning
  738. * sldist: distance to point on line form line beginning along line
  739. The distance is compute using the ``Vect_line_distance`` C function.
  740. >>> point = Point(2.3, 0.5)
  741. >>> line = Line([(0, 0), (2, 0), (3, 0)])
  742. >>> line.distance(point) #doctest: +NORMALIZE_WHITESPACE
  743. LineDist(point=Point(2.300000, 0.000000),
  744. dist=0.5, spdist=0.2999999999999998, sldist=2.3)
  745. """
  746. # instantite outputs
  747. cx = ctypes.c_double(0)
  748. cy = ctypes.c_double(0)
  749. cz = ctypes.c_double(0)
  750. dist = ctypes.c_double(0)
  751. sp_dist = ctypes.c_double(0)
  752. lp_dist = ctypes.c_double(0)
  753. libvect.Vect_line_distance(
  754. self.c_points,
  755. pnt.x,
  756. pnt.y,
  757. 0 if pnt.is2D else pnt.z,
  758. 0 if self.is2D else 1,
  759. ctypes.byref(cx),
  760. ctypes.byref(cy),
  761. ctypes.byref(cz),
  762. ctypes.byref(dist),
  763. ctypes.byref(sp_dist),
  764. ctypes.byref(lp_dist),
  765. )
  766. # instantiate the Point class
  767. point = Point(cx.value, cy.value, cz.value)
  768. point.is2D = self.is2D
  769. return LineDist(point, dist.value, sp_dist.value, lp_dist.value)
  770. @mapinfo_must_be_set
  771. def first_cat(self):
  772. """Fetches FIRST category number for given vector line and field, using
  773. the ``Vect_get_line_cat`` C function.
  774. .. warning::
  775. Not implemented yet.
  776. """
  777. # TODO: add this method.
  778. # libvect.Vect_get_line_cat(self.c_mapinfo, self.id, self.field)
  779. pass
  780. def pop(self, indx):
  781. """Return the point in the index position and remove from the Line.
  782. :param indx: the index where add new point
  783. :type indx: int
  784. >>> line = Line([(0, 0), (1, 1), (2, 2)])
  785. >>> midle_pnt = line.pop(1)
  786. >>> midle_pnt #doctest: +NORMALIZE_WHITESPACE
  787. Point(1.000000, 1.000000)
  788. >>> line #doctest: +NORMALIZE_WHITESPACE
  789. Line([Point(0.000000, 0.000000), Point(2.000000, 2.000000)])
  790. """
  791. if indx < 0: # Handle negative indices
  792. indx += self.c_points.contents.n_points
  793. if indx >= self.c_points.contents.n_points:
  794. raise IndexError("Index out of range")
  795. pnt = self.__getitem__(indx)
  796. libvect.Vect_line_delete_point(self.c_points, indx)
  797. return pnt
  798. def delete(self, indx):
  799. """Remove the point in the index position.
  800. :param indx: the index where add new point
  801. :type indx: int
  802. >>> line = Line([(0, 0), (1, 1), (2, 2)])
  803. >>> line.delete(-1)
  804. >>> line #doctest: +NORMALIZE_WHITESPACE
  805. Line([Point(0.000000, 0.000000), Point(1.000000, 1.000000)])
  806. """
  807. if indx < 0: # Handle negative indices
  808. indx += self.c_points.contents.n_points
  809. if indx >= self.c_points.contents.n_points:
  810. raise IndexError("Index out of range")
  811. libvect.Vect_line_delete_point(self.c_points, indx)
  812. def prune(self):
  813. """Remove duplicate points, i.e. zero length segments, using
  814. `Vect_line_prune` C function. ::
  815. >>> line = Line([(0, 0), (1, 1), (1, 1), (2, 2)])
  816. >>> line.prune()
  817. >>> line #doctest: +NORMALIZE_WHITESPACE
  818. Line([Point(0.000000, 0.000000),
  819. Point(1.000000, 1.000000),
  820. Point(2.000000, 2.000000)])
  821. ..
  822. """
  823. libvect.Vect_line_prune(self.c_points)
  824. def prune_thresh(self, threshold):
  825. """Remove points in threshold, using the ``Vect_line_prune_thresh``
  826. C function.
  827. :param threshold: the threshold value where prune points
  828. :type threshold: num
  829. >>> line = Line([(0, 0), (1.0, 1.0), (1.2, 0.9), (2, 2)])
  830. >>> line.prune_thresh(0.5)
  831. >>> line #doctest: +SKIP +NORMALIZE_WHITESPACE
  832. Line([Point(0.000000, 0.000000),
  833. Point(1.000000, 1.000000),
  834. Point(2.000000, 2.000000)])
  835. .. warning ::
  836. prune_thresh is not working yet.
  837. """
  838. libvect.Vect_line_prune(self.c_points, ctypes.c_double(threshold))
  839. def remove(self, pnt):
  840. """Delete point at given index and move all points above down, using
  841. `Vect_line_delete_point` C function.
  842. :param pnt: the point to remove
  843. :type pnt: a Point object or a tuple with the coordinates
  844. >>> line = Line([(0, 0), (1, 1), (2, 2)])
  845. >>> line.remove((2, 2))
  846. >>> line[-1] #doctest: +NORMALIZE_WHITESPACE
  847. Point(1.000000, 1.000000)
  848. ..
  849. """
  850. for indx, point in enumerate(self.__iter__()):
  851. if pnt == point:
  852. libvect.Vect_line_delete_point(self.c_points, indx)
  853. return
  854. raise ValueError("list.remove(x): x not in list")
  855. def reverse(self):
  856. """Reverse the order of vertices, using `Vect_line_reverse`
  857. C function. ::
  858. >>> line = Line([(0, 0), (1, 1), (2, 2)])
  859. >>> line.reverse()
  860. >>> line #doctest: +NORMALIZE_WHITESPACE
  861. Line([Point(2.000000, 2.000000),
  862. Point(1.000000, 1.000000),
  863. Point(0.000000, 0.000000)])
  864. ..
  865. """
  866. libvect.Vect_line_reverse(self.c_points)
  867. def segment(self, start, end):
  868. """Create line segment. using the ``Vect_line_segment`` C function.
  869. :param start: distance from the beginning of the line where
  870. the segment start
  871. :type start: float
  872. :param end: distance from the beginning of the line where
  873. the segment end
  874. :type end: float
  875. ::
  876. # x (1, 1)
  877. # |
  878. # |-
  879. # |
  880. # x--------x (1, 0)
  881. # (0, 0) ^
  882. >>> line = Line([(0, 0), (1, 0), (1, 1)])
  883. >>> line.segment(0.5, 1.5) #doctest: +NORMALIZE_WHITESPACE
  884. Line([Point(0.500000, 0.000000),
  885. Point(1.000000, 0.000000),
  886. Point(1.000000, 0.500000)])
  887. """
  888. line = Line()
  889. libvect.Vect_line_segment(self.c_points, start, end, line.c_points)
  890. return line
  891. def to_list(self):
  892. """Return a list of tuple. ::
  893. >>> line = Line([(0, 0), (1, 1), (2, 0), (1, -1)])
  894. >>> line.to_list()
  895. [(0.0, 0.0), (1.0, 1.0), (2.0, 0.0), (1.0, -1.0)]
  896. ..
  897. """
  898. return [pnt.coords() for pnt in self.__iter__()]
  899. def to_array(self):
  900. """Return an array of coordinates. ::
  901. >>> line = Line([(0, 0), (1, 1), (2, 0), (1, -1)])
  902. >>> line.to_array() #doctest: +NORMALIZE_WHITESPACE
  903. array([[ 0., 0.],
  904. [ 1., 1.],
  905. [ 2., 0.],
  906. [ 1., -1.]])
  907. ..
  908. """
  909. return np.array(self.to_list())
  910. def to_wkt_p(self):
  911. """Return a Well Known Text string of the line. ::
  912. >>> line = Line([(0, 0), (1, 1), (1, 2)])
  913. >>> line.to_wkt_p() #doctest: +ELLIPSIS
  914. 'LINESTRING(0.000000 0.000000, ..., 1.000000 2.000000)'
  915. ..
  916. """
  917. return "LINESTRING(%s)" % ", ".join(
  918. [
  919. " ".join(["%f" % coord for coord in pnt.coords()])
  920. for pnt in self.__iter__()
  921. ]
  922. )
  923. def from_wkt(self, wkt):
  924. """Create a line reading a WKT string.
  925. :param wkt: the WKT string containing the LINESTRING
  926. :type wkt: str
  927. >>> line = Line()
  928. >>> line.from_wkt("LINESTRING(0 0,1 1,1 2)")
  929. >>> line #doctest: +NORMALIZE_WHITESPACE
  930. Line([Point(0.000000, 0.000000),
  931. Point(1.000000, 1.000000),
  932. Point(1.000000, 2.000000)])
  933. ..
  934. """
  935. match = re.match("LINESTRING\((.*)\)", wkt)
  936. if match:
  937. self.reset()
  938. for coord in match.groups()[0].strip().split(","):
  939. self.append(tuple([float(e) for e in coord.split(" ")]))
  940. else:
  941. return None
  942. def buffer(
  943. self,
  944. dist=None,
  945. dist_x=None,
  946. dist_y=None,
  947. angle=0,
  948. round_=True,
  949. caps=True,
  950. tol=0.1,
  951. ):
  952. """Return the buffer area around the line, using the
  953. ``Vect_line_buffer2`` C function.
  954. :param dist: the distance around the line
  955. :type dist: num
  956. :param dist_x: the distance along x
  957. :type dist_x: num
  958. :param dist_y: the distance along y
  959. :type dist_y: num
  960. :param angle: the angle between 0x and major axis
  961. :type angle: num
  962. :param round_: to make corners round
  963. :type round_: bool
  964. :param tol: fix the maximum distance between theoretical arc and
  965. output segments
  966. :type tol: float
  967. :returns: the buffer as Area object
  968. >>> line = Line([(0, 0), (0, 2)])
  969. >>> boundary, centroid, isles = line.buffer(10)
  970. >>> boundary #doctest: +ELLIPSIS
  971. Line([Point(-10.000000, 0.000000),...Point(-10.000000, 0.000000)])
  972. >>> centroid #doctest: +NORMALIZE_WHITESPACE
  973. Point(0.000000, 0.000000)
  974. >>> isles
  975. []
  976. ..
  977. """
  978. if dist is not None:
  979. dist_x = dist
  980. dist_y = dist
  981. elif not dist_x or not dist_y:
  982. raise TypeError("TypeError: buffer expected 1 arguments, got 0")
  983. p_bound = ctypes.pointer(ctypes.pointer(libvect.line_pnts()))
  984. pp_isle = ctypes.pointer(ctypes.pointer(ctypes.pointer(libvect.line_pnts())))
  985. n_isles = ctypes.pointer(ctypes.c_int())
  986. libvect.Vect_line_buffer2(
  987. self.c_points,
  988. dist_x,
  989. dist_y,
  990. angle,
  991. int(round_),
  992. int(caps),
  993. tol,
  994. p_bound,
  995. pp_isle,
  996. n_isles,
  997. )
  998. boundary = Line(c_points=p_bound.contents)
  999. isles = [
  1000. Line(c_points=pp_isle[i].contents)
  1001. for i in range(n_isles.contents.value)
  1002. if pp_isle[i]
  1003. ]
  1004. return (boundary, self[0], isles)
  1005. def reset(self):
  1006. """Reset line, using `Vect_reset_line` C function. ::
  1007. >>> line = Line([(0, 0), (1, 1), (2, 0), (1, -1)])
  1008. >>> len(line)
  1009. 4
  1010. >>> line.reset()
  1011. >>> len(line)
  1012. 0
  1013. >>> line
  1014. Line([])
  1015. ..
  1016. """
  1017. libvect.Vect_reset_line(self.c_points)
  1018. @mapinfo_must_be_set
  1019. def nodes(self):
  1020. """Return the start and end nodes of the line
  1021. This method requires topology build.
  1022. return: A tuple of Node objects that represent the
  1023. start and end point of this line.
  1024. """
  1025. if self.has_topology():
  1026. n1 = ctypes.c_int()
  1027. n2 = ctypes.c_int()
  1028. libvect.Vect_get_line_nodes(
  1029. self.c_mapinfo, self.id, ctypes.byref(n1), ctypes.byref(n2)
  1030. )
  1031. return (Node(n1.value, self.c_mapinfo), Node(n2.value, self.c_mapinfo))
  1032. class Node(object):
  1033. """Node class for topological analysis of line neighbors.
  1034. Objects of this class will be returned by the node() function
  1035. of a Line object.
  1036. All methods in this class require a proper setup of the Node
  1037. objects. Hence, the correct id and a valid pointer to a mapinfo
  1038. object must be provided in the constructions. Otherwise a segfault
  1039. may happen.
  1040. """
  1041. def __init__(self, v_id, c_mapinfo, **kwords):
  1042. """Construct a Node object
  1043. param v_id: The unique node id
  1044. param c_mapinfo: A valid pointer to the mapinfo object
  1045. param **kwords: Ignored
  1046. """
  1047. self.id = v_id # vector id
  1048. self.c_mapinfo = c_mapinfo
  1049. self._setup()
  1050. @mapinfo_must_be_set
  1051. def _setup(self):
  1052. self.is2D = bool(libvect.Vect_is_3d(self.c_mapinfo) != 1)
  1053. self.nlines = libvect.Vect_get_node_n_lines(self.c_mapinfo, self.id)
  1054. def __len__(self):
  1055. return self.nlines
  1056. def __iter__(self):
  1057. return self.ilines()
  1058. def __repr__(self):
  1059. return "Node(%d)" % self.id
  1060. @mapinfo_must_be_set
  1061. def alive(self):
  1062. """Return True if this node is alive or False if this node is
  1063. dead or its index is out of range.
  1064. """
  1065. return bool(libvect.Vect_node_alive(self.c_mapinfo, self.id))
  1066. @mapinfo_must_be_set
  1067. def coords(self):
  1068. """Return a tuple with the node coordinates."""
  1069. x = ctypes.c_double()
  1070. y = ctypes.c_double()
  1071. z = ctypes.c_double()
  1072. libvect.Vect_get_node_coor(
  1073. self.c_mapinfo, self.id, ctypes.byref(x), ctypes.byref(y), ctypes.byref(z)
  1074. )
  1075. return (x.value, y.value) if self.is2D else (x.value, y.value, z.value)
  1076. def to_wkt(self):
  1077. """Return a "well know text" (WKT) geometry string. ::"""
  1078. return "POINT(%s)" % " ".join(["%f" % coord for coord in self.coords()])
  1079. def to_wkb(self):
  1080. """Return a "well know binary" (WKB) geometry array. ::
  1081. TODO: Must be implemented
  1082. """
  1083. raise Exception("Not implemented")
  1084. def ilines(self, only_in=False, only_out=False):
  1085. """Return a generator with all lines id connected to a node.
  1086. The line id is negative if line is ending on the node and positive if
  1087. starting from the node.
  1088. :param only_in: Return only the lines that are ending in the node
  1089. :type only_in: bool
  1090. :param only_out: Return only the lines that are starting in the node
  1091. :type only_out: bool
  1092. """
  1093. for iline in range(self.nlines):
  1094. lid = libvect.Vect_get_node_line(self.c_mapinfo, self.id, iline)
  1095. if (not only_in and lid > 0) or (not only_out and lid < 0):
  1096. yield lid
  1097. @mapinfo_must_be_set
  1098. def lines(self, only_in=False, only_out=False):
  1099. """Return a generator with all lines connected to a node.
  1100. :param only_in: Return only the lines that are ending in the node
  1101. :type only_in: bool
  1102. :param only_out: Return only the lines that are starting in the node
  1103. :type only_out: bool
  1104. """
  1105. for iline in self.ilines(only_in, only_out):
  1106. yield Line(v_id=abs(iline), c_mapinfo=self.c_mapinfo)
  1107. @mapinfo_must_be_set
  1108. def angles(self):
  1109. """Return a generator with all lines angles in a node."""
  1110. for iline in range(self.nlines):
  1111. yield libvect.Vect_get_node_line_angle(self.c_mapinfo, self.id, iline)
  1112. class Boundary(Line):
  1113. """"""
  1114. # geometry type
  1115. gtype = libvect.GV_BOUNDARY
  1116. def __init__(self, **kargs):
  1117. super(Boundary, self).__init__(**kargs)
  1118. v_id = kargs.get("v_id", 0)
  1119. # not sure what it means that v_id is None
  1120. v_id = 0 if v_id is None else v_id
  1121. self.dir = libvect.GV_FORWARD if v_id > 0 else libvect.GV_BACKWARD
  1122. self.c_left = ctypes.pointer(ctypes.c_int())
  1123. self.c_right = ctypes.pointer(ctypes.c_int())
  1124. @property
  1125. def left_area_id(self):
  1126. """Left side area id, only available after read_area_ids() was called"""
  1127. return self.c_left.contents.value
  1128. @property
  1129. def right_area_id(self):
  1130. """Right side area id, only available after read_area_ids() was called"""
  1131. return self.c_right.contents.value
  1132. def __repr__(self):
  1133. return "Boundary([%s])" % ", ".join([repr(pnt) for pnt in self.__iter__()])
  1134. @mapinfo_must_be_set
  1135. def _centroid(self, side, idonly=False):
  1136. if side > 0:
  1137. v_id = libvect.Vect_get_area_centroid(self.c_mapinfo, side)
  1138. v_id = v_id if v_id else None
  1139. if idonly:
  1140. return v_id
  1141. else:
  1142. cntr = Centroid(v_id=v_id, c_mapinfo=self.c_mapinfo)
  1143. return cntr
  1144. def left_centroid(self, idonly=False):
  1145. """Return left centroid
  1146. :param idonly: True to return only the cat of feature
  1147. :type idonly: bool
  1148. """
  1149. return self._centroid(self.c_left.contents.value, idonly)
  1150. def right_centroid(self, idonly=False):
  1151. """Return right centroid
  1152. :param idonly: True to return only the cat of feature
  1153. :type idonly: bool
  1154. """
  1155. return self._centroid(self.c_right.contents.value, idonly)
  1156. @mapinfo_must_be_set
  1157. def read_area_ids(self):
  1158. """Read and return left and right area ids of the boundary"""
  1159. libvect.Vect_get_line_areas(self.c_mapinfo, self.id, self.c_left, self.c_right)
  1160. return self.c_left.contents.value, self.c_right.contents.value
  1161. def area(self):
  1162. """Return the area of the polygon.
  1163. >>> bound = Boundary(points=[(0, 0), (0, 2), (2, 2), (2, 0),
  1164. ... (0, 0)])
  1165. >>> bound.area()
  1166. 4.0
  1167. """
  1168. libgis.G_begin_polygon_area_calculations()
  1169. return libgis.G_area_of_polygon(
  1170. self.c_points.contents.x,
  1171. self.c_points.contents.y,
  1172. self.c_points.contents.n_points,
  1173. )
  1174. class Centroid(Point):
  1175. """The Centroid class inherit from the Point class.
  1176. Centroid contains an attribute with the C Map_info struct, and attributes
  1177. with the id of the Area. ::
  1178. >>> centroid = Centroid(x=0, y=10)
  1179. >>> centroid
  1180. Centroid(0.000000, 10.000000)
  1181. >>> from grass.pygrass.vector import VectorTopo
  1182. >>> test_vect = VectorTopo(test_vector_name)
  1183. >>> test_vect.open(mode='r')
  1184. >>> centroid = Centroid(v_id=18, c_mapinfo=test_vect.c_mapinfo)
  1185. >>> centroid
  1186. Centroid(3.500000, 3.500000)
  1187. >>> test_vect.close()
  1188. ..
  1189. """
  1190. # geometry type
  1191. gtype = libvect.GV_CENTROID
  1192. def __init__(self, area_id=None, **kargs):
  1193. super(Centroid, self).__init__(**kargs)
  1194. self.area_id = area_id
  1195. if self.id and self.c_mapinfo and self.area_id is None:
  1196. self.area_id = self._area_id()
  1197. elif self.c_mapinfo and self.area_id and self.id is None:
  1198. self.id = self._centroid_id()
  1199. if self.area_id is not None:
  1200. self.read()
  1201. # self.c_pline = ctypes.pointer(libvect.P_line()) if topology else None
  1202. def __repr__(self):
  1203. return "Centroid(%s)" % ", ".join(["%f" % co for co in self.coords()])
  1204. @mapinfo_must_be_set
  1205. def _centroid_id(self):
  1206. """Return the centroid_id, using the c_mapinfo and an area_id
  1207. attributes of the class, and calling the Vect_get_area_centroid
  1208. C function, if no centroid_id were found return None"""
  1209. centroid_id = libvect.Vect_get_area_centroid(self.c_mapinfo, self.area_id)
  1210. return centroid_id if centroid_id != 0 else None
  1211. @mapinfo_must_be_set
  1212. def _area_id(self):
  1213. """Return the area_id, using the c_mapinfo and an centroid_id
  1214. attributes of the class, and calling the Vect_centroid_area
  1215. C function, if no area_id were found return None"""
  1216. area_id = libvect.Vect_get_centroid_area(self.c_mapinfo, self.id)
  1217. return area_id if area_id != 0 else None
  1218. class Isle(Geo):
  1219. """An Isle is an area contained by another area."""
  1220. def __init__(self, **kargs):
  1221. super(Isle, self).__init__(**kargs)
  1222. # self.area_id = area_id
  1223. def __repr__(self):
  1224. return "Isle(%d)" % (self.id)
  1225. @mapinfo_must_be_set
  1226. def boundaries(self):
  1227. """Return a list of boundaries"""
  1228. ilist = Ilist()
  1229. libvect.Vect_get_isle_boundaries(self.c_mapinfo, self.id, ilist.c_ilist)
  1230. return ilist
  1231. @mapinfo_must_be_set
  1232. def bbox(self, bbox=None):
  1233. """Return bounding box of Isle"""
  1234. bbox = bbox if bbox else Bbox()
  1235. libvect.Vect_get_isle_box(self.c_mapinfo, self.id, bbox.c_bbox)
  1236. return bbox
  1237. @mapinfo_must_be_set
  1238. def points(self):
  1239. """Return a Line object with the outer ring points"""
  1240. line = Line()
  1241. libvect.Vect_get_isle_points(self.c_mapinfo, self.id, line.c_points)
  1242. return line
  1243. def to_wkt(self):
  1244. """Return a Well Known Text string of the isle. ::
  1245. For now the outer ring is returned
  1246. TODO: Implement inner rings detected from isles
  1247. """
  1248. line = self.points()
  1249. return "Polygon((%s))" % ", ".join(
  1250. [" ".join(["%f" % coord for coord in pnt]) for pnt in line.to_list()]
  1251. )
  1252. def to_wkb(self):
  1253. """Return a "well know text" (WKB) geometry array. ::"""
  1254. raise Exception("Not implemented")
  1255. @mapinfo_must_be_set
  1256. def points_geos(self):
  1257. """Return a Line object with the outer ring points"""
  1258. return libvect.Vect_get_isle_points_geos(self.c_mapinfo, self.id)
  1259. @mapinfo_must_be_set
  1260. def area_id(self):
  1261. """Returns area id for isle."""
  1262. return libvect.Vect_get_isle_area(self.c_mapinfo, self.id)
  1263. @mapinfo_must_be_set
  1264. def alive(self):
  1265. """Check if isle is alive or dead (topology required)"""
  1266. return bool(libvect.Vect_isle_alive(self.c_mapinfo, self.id))
  1267. @mapinfo_must_be_set
  1268. def contain_pnt(self, pnt):
  1269. """Check if point is in area.
  1270. :param pnt: the point to remove
  1271. :type pnt: a Point object or a tuple with the coordinates
  1272. """
  1273. bbox = self.bbox()
  1274. return bool(
  1275. libvect.Vect_point_in_island(
  1276. pnt.x, pnt.y, self.c_mapinfo, self.id, bbox.c_bbox.contents
  1277. )
  1278. )
  1279. def area(self):
  1280. """Return the area value of an Isle"""
  1281. border = self.points()
  1282. return libgis.G_area_of_polygon(
  1283. border.c_points.contents.x,
  1284. border.c_points.contents.y,
  1285. border.c_points.contents.n_points,
  1286. )
  1287. def perimeter(self):
  1288. """Return the perimeter value of an Isle."""
  1289. border = self.points()
  1290. return libvect.Vect_line_geodesic_length(border.c_points)
  1291. class Isles(object):
  1292. def __init__(self, c_mapinfo, area_id=None):
  1293. self.c_mapinfo = c_mapinfo
  1294. self.area_id = area_id
  1295. self._isles_id = None
  1296. self._isles = None
  1297. if area_id:
  1298. self._isles_id = self.isles_ids()
  1299. self._isles = self.isles()
  1300. @mapinfo_must_be_set
  1301. def __len__(self):
  1302. return libvect.Vect_get_area_num_isles(self.c_mapinfo, self.area_id)
  1303. def __repr__(self):
  1304. return "Isles(%r)" % self.area_id
  1305. def __getitem__(self, key):
  1306. if self._isles is None:
  1307. self.isles()
  1308. return self._isles[key]
  1309. @mapinfo_must_be_set
  1310. def isles_ids(self):
  1311. """Return the id of isles"""
  1312. return [
  1313. libvect.Vect_get_area_isle(self.c_mapinfo, self.area_id, i)
  1314. for i in range(self.__len__())
  1315. ]
  1316. @mapinfo_must_be_set
  1317. def isles(self):
  1318. """Return isles"""
  1319. return [
  1320. Isle(v_id=isle_id, c_mapinfo=self.c_mapinfo) for isle_id in self._isles_id
  1321. ]
  1322. class Area(Geo):
  1323. """
  1324. Vect_build_line_area,
  1325. Vect_find_area,
  1326. Vect_get_area_box,
  1327. Vect_get_area_points_geos,
  1328. Vect_centroid_area,
  1329. Vect_get_isle_area,
  1330. Vect_get_line_areas,
  1331. Vect_get_num_areas,
  1332. Vect_get_point_in_area,
  1333. Vect_isle_find_area,
  1334. Vect_point_in_area,
  1335. Vect_point_in_area_outer_ring,
  1336. Vect_read_area_geos,
  1337. Vect_remove_small_areas,
  1338. Vect_select_areas_by_box,
  1339. Vect_select_areas_by_polygon
  1340. """
  1341. # geometry type
  1342. gtype = libvect.GV_AREA
  1343. def __init__(self, **kargs):
  1344. super(Area, self).__init__(**kargs)
  1345. # set the attributes
  1346. # if self.attrs and self.cat:
  1347. # self.attrs.cat = self.cat
  1348. def __repr__(self):
  1349. return "Area(%d)" % self.id if self.id else "Area( )"
  1350. @property
  1351. def cat(self):
  1352. centroid = self.centroid()
  1353. return centroid.cat if centroid else None
  1354. @mapinfo_must_be_set
  1355. def points(self, line=None):
  1356. """Return a Line object with the outer ring
  1357. :param line: a Line object to fill with info from points of area
  1358. :type line: a Line object
  1359. """
  1360. line = Line() if line is None else line
  1361. libvect.Vect_get_area_points(self.c_mapinfo, self.id, line.c_points)
  1362. return line
  1363. @mapinfo_must_be_set
  1364. def centroid(self):
  1365. """Return the centroid
  1366. :param centroid: a Centroid object to fill with info from centroid of area
  1367. :type centroid: a Centroid object
  1368. """
  1369. centroid_id = libvect.Vect_get_area_centroid(self.c_mapinfo, self.id)
  1370. if centroid_id:
  1371. return Centroid(v_id=centroid_id, c_mapinfo=self.c_mapinfo, area_id=self.id)
  1372. @mapinfo_must_be_set
  1373. def num_isles(self):
  1374. return libvect.Vect_get_area_num_isles(self.c_mapinfo, self.id)
  1375. @mapinfo_must_be_set
  1376. def isles(self, isles=None):
  1377. """Return a list of islands located in this area"""
  1378. if isles is not None:
  1379. isles.area_id = self.id
  1380. return isles
  1381. return Isles(self.c_mapinfo, self.id)
  1382. @mapinfo_must_be_set
  1383. def area(self):
  1384. """Returns area of area without areas of isles.
  1385. double Vect_get_area_area (const struct Map_info \*Map, int area)
  1386. """
  1387. return libvect.Vect_get_area_area(self.c_mapinfo, self.id)
  1388. @mapinfo_must_be_set
  1389. def alive(self):
  1390. """Check if area is alive or dead (topology required)"""
  1391. return bool(libvect.Vect_area_alive(self.c_mapinfo, self.id))
  1392. @mapinfo_must_be_set
  1393. def bbox(self, bbox=None):
  1394. """Return the Bbox of area
  1395. :param bbox: a Bbox object to fill with info from bounding box of area
  1396. :type bbox: a Bbox object
  1397. """
  1398. bbox = bbox if bbox else Bbox()
  1399. libvect.Vect_get_area_box(self.c_mapinfo, self.id, bbox.c_bbox)
  1400. return bbox
  1401. @mapinfo_must_be_set
  1402. def buffer(
  1403. self,
  1404. dist=None,
  1405. dist_x=None,
  1406. dist_y=None,
  1407. angle=0,
  1408. round_=True,
  1409. caps=True,
  1410. tol=0.1,
  1411. ):
  1412. """Return the buffer area around the area, using the
  1413. ``Vect_area_buffer2`` C function.
  1414. :param dist: the distance around the area
  1415. :type dist: num
  1416. :param dist_x: the distance along x
  1417. :type dist_x: num
  1418. :param dist_y: the distance along y
  1419. :type dist_y: num
  1420. :param angle: the angle between 0x and major axis
  1421. :type angle: num
  1422. :param round_: to make corners round
  1423. :type round_: bool
  1424. :param tol: fix the maximum distance between theoretical arc and
  1425. output segments
  1426. :type tol: float
  1427. :returns: the buffer as line, centroid, isles object tuple
  1428. """
  1429. if dist is not None:
  1430. dist_x = dist
  1431. dist_y = dist
  1432. elif not dist_x or not dist_y:
  1433. raise TypeError("TypeError: buffer expected 1 arguments, got 0")
  1434. p_bound = ctypes.pointer(ctypes.pointer(libvect.line_pnts()))
  1435. pp_isle = ctypes.pointer(ctypes.pointer(ctypes.pointer(libvect.line_pnts())))
  1436. n_isles = ctypes.pointer(ctypes.c_int())
  1437. libvect.Vect_area_buffer2(
  1438. self.c_mapinfo,
  1439. self.id,
  1440. dist_x,
  1441. dist_y,
  1442. angle,
  1443. int(round_),
  1444. int(caps),
  1445. tol,
  1446. p_bound,
  1447. pp_isle,
  1448. n_isles,
  1449. )
  1450. return (
  1451. Line(c_points=p_bound.contents),
  1452. self.centroid,
  1453. [Line(c_points=pp_isle[i].contents) for i in range(n_isles.contents.value)],
  1454. )
  1455. @mapinfo_must_be_set
  1456. def boundaries(self, ilist=False):
  1457. """Creates list of boundaries for given area.
  1458. int Vect_get_area_boundaries(const struct Map_info \*Map,
  1459. int area, struct ilist \*List)
  1460. """
  1461. ilst = Ilist()
  1462. libvect.Vect_get_area_boundaries(self.c_mapinfo, self.id, ilst.c_ilist)
  1463. if ilist:
  1464. return ilist
  1465. return [Boundary(v_id=abs(v_id), c_mapinfo=self.c_mapinfo) for v_id in ilst]
  1466. def to_wkt(self):
  1467. """Return a "well know text" (WKT) area string, this method uses
  1468. the GEOS implementation in the vector library. ::
  1469. """
  1470. return decode(libvect.Vect_read_area_to_wkt(self.c_mapinfo, self.id))
  1471. def to_wkb(self):
  1472. """Return a "well know binary" (WKB) area byte array, this method uses
  1473. the GEOS implementation in the vector library. ::
  1474. """
  1475. size = ctypes.c_size_t()
  1476. barray = libvect.Vect_read_area_to_wkb(
  1477. self.c_mapinfo, self.id, ctypes.byref(size)
  1478. )
  1479. return ctypes.string_at(barray, size.value)
  1480. @mapinfo_must_be_set
  1481. def cats(self, cats=None):
  1482. """Get area categories.
  1483. :param cats: a Cats object to fill with info with area categories
  1484. :type cats: a Cats object
  1485. """
  1486. cats = cats if cats else Cats()
  1487. libvect.Vect_get_area_cats(self.c_mapinfo, self.id, cats.c_cats)
  1488. return cats
  1489. def get_first_cat(self):
  1490. """Find FIRST category of given field and area.
  1491. int Vect_get_area_cat(const struct Map_info \*Map, int area, int field)
  1492. ..warning: Not implemented
  1493. """
  1494. pass
  1495. @mapinfo_must_be_set
  1496. def contains_point(self, point, bbox=None):
  1497. """Check if point is in area.
  1498. :param point: the point to analyze
  1499. :type point: a Point object or a tuple with the coordinates
  1500. :param bbox: the bounding box where run the analysis
  1501. :type bbox: a Bbox object
  1502. """
  1503. bbox = bbox if bbox else self.bbox()
  1504. return bool(
  1505. libvect.Vect_point_in_area(
  1506. point.x, point.y, self.c_mapinfo, self.id, bbox.c_bbox
  1507. )
  1508. )
  1509. @mapinfo_must_be_set
  1510. def perimeter(self):
  1511. """Calculate area perimeter.
  1512. :return: double Vect_area_perimeter (const struct line_pnts \*Points)
  1513. """
  1514. border = self.points()
  1515. return libvect.Vect_line_geodesic_length(border.c_points)
  1516. def read(self):
  1517. pass
  1518. #
  1519. # Define a dictionary to convert the feature type to name and or object
  1520. #
  1521. GV_TYPE = {
  1522. libvect.GV_POINT: {"label": "point", "obj": Point},
  1523. libvect.GV_LINE: {"label": "line", "obj": Line},
  1524. libvect.GV_BOUNDARY: {"label": "boundary", "obj": Boundary},
  1525. libvect.GV_CENTROID: {"label": "centroid", "obj": Centroid},
  1526. libvect.GV_FACE: {"label": "face", "obj": None},
  1527. libvect.GV_KERNEL: {"label": "kernel", "obj": None},
  1528. libvect.GV_AREA: {"label": "area", "obj": Area},
  1529. libvect.GV_VOLUME: {"label": "volume", "obj": None},
  1530. }
  1531. GEOOBJ = {
  1532. "areas": Area,
  1533. "dblinks": None,
  1534. "faces": None,
  1535. "holes": None,
  1536. "boundaries": Boundary,
  1537. "islands": Isle,
  1538. "kernels": None,
  1539. "line_points": None,
  1540. "points": Point,
  1541. "lines": Line,
  1542. "nodes": Node,
  1543. "volumes": None,
  1544. }
  1545. def c_read_next_line(c_mapinfo, c_points, c_cats):
  1546. v_id = c_mapinfo.contents.next_line
  1547. v_id = v_id if v_id != 0 else None
  1548. ftype = libvect.Vect_read_next_line(c_mapinfo, c_points, c_cats)
  1549. if ftype == -2:
  1550. raise StopIteration()
  1551. if ftype == -1:
  1552. raise
  1553. return ftype, v_id, c_points, c_cats
  1554. def read_next_line(
  1555. c_mapinfo, table=None, writeable=False, c_points=None, c_cats=None, is2D=True
  1556. ):
  1557. """Return the next geometry feature of a vector map."""
  1558. # Take care of good memory management
  1559. free_points = False
  1560. if c_points is None:
  1561. free_points = True
  1562. free_cats = False
  1563. if c_cats is None:
  1564. free_cats = True
  1565. c_points = c_points if c_points else ctypes.pointer(libvect.line_pnts())
  1566. c_cats = c_cats if c_cats else ctypes.pointer(libvect.line_cats())
  1567. ftype, v_id, c_points, c_cats = c_read_next_line(c_mapinfo, c_points, c_cats)
  1568. return GV_TYPE[ftype]["obj"](
  1569. v_id=v_id,
  1570. c_mapinfo=c_mapinfo,
  1571. c_points=c_points,
  1572. c_cats=c_cats,
  1573. table=table,
  1574. writeable=writeable,
  1575. is2D=is2D,
  1576. free_points=free_points,
  1577. free_cats=free_cats,
  1578. )
  1579. def c_read_line(feature_id, c_mapinfo, c_points, c_cats):
  1580. nmax = libvect.Vect_get_num_lines(c_mapinfo)
  1581. if feature_id < 0: # Handle negative indices
  1582. feature_id += nmax + 1
  1583. if feature_id > nmax:
  1584. raise IndexError("Index out of range")
  1585. if feature_id > 0:
  1586. ftype = libvect.Vect_read_line(c_mapinfo, c_points, c_cats, feature_id)
  1587. return feature_id, ftype, c_points, c_cats
  1588. else:
  1589. raise ValueError("The index must be >0, %r given." % feature_id)
  1590. def read_line(
  1591. feature_id,
  1592. c_mapinfo,
  1593. table=None,
  1594. writeable=False,
  1595. c_points=None,
  1596. c_cats=None,
  1597. is2D=True,
  1598. ):
  1599. """Return a geometry object given the feature id and the c_mapinfo."""
  1600. # Take care of good memory management
  1601. free_points = False
  1602. if c_points is None:
  1603. free_points = True
  1604. free_cats = False
  1605. if c_cats is None:
  1606. free_cats = True
  1607. c_points = c_points if c_points else ctypes.pointer(libvect.line_pnts())
  1608. c_cats = c_cats if c_cats else ctypes.pointer(libvect.line_cats())
  1609. feature_id, ftype, c_points, c_cats = c_read_line(
  1610. feature_id, c_mapinfo, c_points, c_cats
  1611. )
  1612. if GV_TYPE[ftype]["obj"] is not None:
  1613. return GV_TYPE[ftype]["obj"](
  1614. v_id=feature_id,
  1615. c_mapinfo=c_mapinfo,
  1616. c_points=c_points,
  1617. c_cats=c_cats,
  1618. table=table,
  1619. writeable=writeable,
  1620. is2D=is2D,
  1621. free_points=free_points,
  1622. free_cats=free_cats,
  1623. )
  1624. if __name__ == "__main__":
  1625. import doctest
  1626. from grass.pygrass import utils
  1627. utils.create_test_vector_map(test_vector_name)
  1628. doctest.testmod()
  1629. """Remove the generated vector map, if exist"""
  1630. from grass.pygrass.utils import get_mapset_vector
  1631. from grass.script.core import run_command
  1632. mset = get_mapset_vector(test_vector_name, mapset="")
  1633. if mset:
  1634. run_command("g.remove", flags="f", type="vector", name=test_vector_name)