test_geos.py 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  1. import ctypes
  2. import random
  3. import unittest
  4. from django.contrib.gis.geos import *
  5. from django.contrib.gis.geos.base import gdal, numpy, GEOSBase
  6. from django.contrib.gis.geos.libgeos import GEOS_PREPARE
  7. from django.contrib.gis.geometry.test_data import TestDataMixin
  8. class GEOSTest(unittest.TestCase, TestDataMixin):
  9. @property
  10. def null_srid(self):
  11. """
  12. Returns the proper null SRID depending on the GEOS version.
  13. See the comments in `test15_srid` for more details.
  14. """
  15. info = geos_version_info()
  16. if info['version'] == '3.0.0' and info['release_candidate']:
  17. return -1
  18. else:
  19. return None
  20. def test00_base(self):
  21. "Tests out the GEOSBase class."
  22. # Testing out GEOSBase class, which provides a `ptr` property
  23. # that abstracts out access to underlying C pointers.
  24. class FakeGeom1(GEOSBase):
  25. pass
  26. # This one only accepts pointers to floats
  27. c_float_p = ctypes.POINTER(ctypes.c_float)
  28. class FakeGeom2(GEOSBase):
  29. ptr_type = c_float_p
  30. # Default ptr_type is `c_void_p`.
  31. fg1 = FakeGeom1()
  32. # Default ptr_type is C float pointer
  33. fg2 = FakeGeom2()
  34. # These assignments are OK -- None is allowed because
  35. # it's equivalent to the NULL pointer.
  36. fg1.ptr = ctypes.c_void_p()
  37. fg1.ptr = None
  38. fg2.ptr = c_float_p(ctypes.c_float(5.23))
  39. fg2.ptr = None
  40. # Because pointers have been set to NULL, an exception should be
  41. # raised when we try to access it. Raising an exception is
  42. # preferrable to a segmentation fault that commonly occurs when
  43. # a C method is given a NULL memory reference.
  44. for fg in (fg1, fg2):
  45. # Equivalent to `fg.ptr`
  46. self.assertRaises(GEOSException, fg._get_ptr)
  47. # Anything that is either not None or the acceptable pointer type will
  48. # result in a TypeError when trying to assign it to the `ptr` property.
  49. # Thus, memmory addresses (integers) and pointers of the incorrect type
  50. # (in `bad_ptrs`) will not be allowed.
  51. bad_ptrs = (5, ctypes.c_char_p('foobar'))
  52. for bad_ptr in bad_ptrs:
  53. # Equivalent to `fg.ptr = bad_ptr`
  54. self.assertRaises(TypeError, fg1._set_ptr, bad_ptr)
  55. self.assertRaises(TypeError, fg2._set_ptr, bad_ptr)
  56. def test01a_wkt(self):
  57. "Testing WKT output."
  58. for g in self.geometries.wkt_out:
  59. geom = fromstr(g.wkt)
  60. self.assertEqual(g.ewkt, geom.wkt)
  61. def test01b_hex(self):
  62. "Testing HEX output."
  63. for g in self.geometries.hex_wkt:
  64. geom = fromstr(g.wkt)
  65. self.assertEqual(g.hex, geom.hex)
  66. def test01b_hexewkb(self):
  67. "Testing (HEX)EWKB output."
  68. from binascii import a2b_hex
  69. # For testing HEX(EWKB).
  70. ogc_hex = '01010000000000000000000000000000000000F03F'
  71. # `SELECT ST_AsHEXEWKB(ST_GeomFromText('POINT(0 1)', 4326));`
  72. hexewkb_2d = '0101000020E61000000000000000000000000000000000F03F'
  73. # `SELECT ST_AsHEXEWKB(ST_GeomFromEWKT('SRID=4326;POINT(0 1 2)'));`
  74. hexewkb_3d = '01010000A0E61000000000000000000000000000000000F03F0000000000000040'
  75. pnt_2d = Point(0, 1, srid=4326)
  76. pnt_3d = Point(0, 1, 2, srid=4326)
  77. # OGC-compliant HEX will not have SRID nor Z value.
  78. self.assertEqual(ogc_hex, pnt_2d.hex)
  79. self.assertEqual(ogc_hex, pnt_3d.hex)
  80. # HEXEWKB should be appropriate for its dimension -- have to use an
  81. # a WKBWriter w/dimension set accordingly, else GEOS will insert
  82. # garbage into 3D coordinate if there is none. Also, GEOS has a
  83. # a bug in versions prior to 3.1 that puts the X coordinate in
  84. # place of Z; an exception should be raised on those versions.
  85. self.assertEqual(hexewkb_2d, pnt_2d.hexewkb)
  86. if GEOS_PREPARE:
  87. self.assertEqual(hexewkb_3d, pnt_3d.hexewkb)
  88. self.assertEqual(True, GEOSGeometry(hexewkb_3d).hasz)
  89. else:
  90. try:
  91. hexewkb = pnt_3d.hexewkb
  92. except GEOSException:
  93. pass
  94. else:
  95. self.fail('Should have raised GEOSException.')
  96. # Same for EWKB.
  97. self.assertEqual(buffer(a2b_hex(hexewkb_2d)), pnt_2d.ewkb)
  98. if GEOS_PREPARE:
  99. self.assertEqual(buffer(a2b_hex(hexewkb_3d)), pnt_3d.ewkb)
  100. else:
  101. try:
  102. ewkb = pnt_3d.ewkb
  103. except GEOSException:
  104. pass
  105. else:
  106. self.fail('Should have raised GEOSException')
  107. # Redundant sanity check.
  108. self.assertEqual(4326, GEOSGeometry(hexewkb_2d).srid)
  109. def test01c_kml(self):
  110. "Testing KML output."
  111. for tg in self.geometries.wkt_out:
  112. geom = fromstr(tg.wkt)
  113. kml = getattr(tg, 'kml', False)
  114. if kml: self.assertEqual(kml, geom.kml)
  115. def test01d_errors(self):
  116. "Testing the Error handlers."
  117. # string-based
  118. print("\nBEGIN - expecting GEOS_ERROR; safe to ignore.\n")
  119. for err in self.geometries.errors:
  120. try:
  121. g = fromstr(err.wkt)
  122. except (GEOSException, ValueError):
  123. pass
  124. # Bad WKB
  125. self.assertRaises(GEOSException, GEOSGeometry, buffer('0'))
  126. print("\nEND - expecting GEOS_ERROR; safe to ignore.\n")
  127. class NotAGeometry(object):
  128. pass
  129. # Some other object
  130. self.assertRaises(TypeError, GEOSGeometry, NotAGeometry())
  131. # None
  132. self.assertRaises(TypeError, GEOSGeometry, None)
  133. def test01e_wkb(self):
  134. "Testing WKB output."
  135. from binascii import b2a_hex
  136. for g in self.geometries.hex_wkt:
  137. geom = fromstr(g.wkt)
  138. wkb = geom.wkb
  139. self.assertEqual(b2a_hex(wkb).upper(), g.hex)
  140. def test01f_create_hex(self):
  141. "Testing creation from HEX."
  142. for g in self.geometries.hex_wkt:
  143. geom_h = GEOSGeometry(g.hex)
  144. # we need to do this so decimal places get normalised
  145. geom_t = fromstr(g.wkt)
  146. self.assertEqual(geom_t.wkt, geom_h.wkt)
  147. def test01g_create_wkb(self):
  148. "Testing creation from WKB."
  149. from binascii import a2b_hex
  150. for g in self.geometries.hex_wkt:
  151. wkb = buffer(a2b_hex(g.hex))
  152. geom_h = GEOSGeometry(wkb)
  153. # we need to do this so decimal places get normalised
  154. geom_t = fromstr(g.wkt)
  155. self.assertEqual(geom_t.wkt, geom_h.wkt)
  156. def test01h_ewkt(self):
  157. "Testing EWKT."
  158. srids = (-1, 32140)
  159. for srid in srids:
  160. for p in self.geometries.polygons:
  161. ewkt = 'SRID=%d;%s' % (srid, p.wkt)
  162. poly = fromstr(ewkt)
  163. self.assertEqual(srid, poly.srid)
  164. self.assertEqual(srid, poly.shell.srid)
  165. self.assertEqual(srid, fromstr(poly.ewkt).srid) # Checking export
  166. def test01i_json(self):
  167. "Testing GeoJSON input/output (via GDAL)."
  168. if not gdal or not gdal.GEOJSON: return
  169. for g in self.geometries.json_geoms:
  170. geom = GEOSGeometry(g.wkt)
  171. if not hasattr(g, 'not_equal'):
  172. self.assertEqual(g.json, geom.json)
  173. self.assertEqual(g.json, geom.geojson)
  174. self.assertEqual(GEOSGeometry(g.wkt), GEOSGeometry(geom.json))
  175. def test01k_fromfile(self):
  176. "Testing the fromfile() factory."
  177. from io import BytesIO
  178. ref_pnt = GEOSGeometry('POINT(5 23)')
  179. wkt_f = BytesIO()
  180. wkt_f.write(ref_pnt.wkt)
  181. wkb_f = BytesIO()
  182. wkb_f.write(str(ref_pnt.wkb))
  183. # Other tests use `fromfile()` on string filenames so those
  184. # aren't tested here.
  185. for fh in (wkt_f, wkb_f):
  186. fh.seek(0)
  187. pnt = fromfile(fh)
  188. self.assertEqual(ref_pnt, pnt)
  189. def test01k_eq(self):
  190. "Testing equivalence."
  191. p = fromstr('POINT(5 23)')
  192. self.assertEqual(p, p.wkt)
  193. self.assertNotEqual(p, 'foo')
  194. ls = fromstr('LINESTRING(0 0, 1 1, 5 5)')
  195. self.assertEqual(ls, ls.wkt)
  196. self.assertNotEqual(p, 'bar')
  197. # Error shouldn't be raise on equivalence testing with
  198. # an invalid type.
  199. for g in (p, ls):
  200. self.assertNotEqual(g, None)
  201. self.assertNotEqual(g, {'foo' : 'bar'})
  202. self.assertNotEqual(g, False)
  203. def test02a_points(self):
  204. "Testing Point objects."
  205. prev = fromstr('POINT(0 0)')
  206. for p in self.geometries.points:
  207. # Creating the point from the WKT
  208. pnt = fromstr(p.wkt)
  209. self.assertEqual(pnt.geom_type, 'Point')
  210. self.assertEqual(pnt.geom_typeid, 0)
  211. self.assertEqual(p.x, pnt.x)
  212. self.assertEqual(p.y, pnt.y)
  213. self.assertEqual(True, pnt == fromstr(p.wkt))
  214. self.assertEqual(False, pnt == prev)
  215. # Making sure that the point's X, Y components are what we expect
  216. self.assertAlmostEqual(p.x, pnt.tuple[0], 9)
  217. self.assertAlmostEqual(p.y, pnt.tuple[1], 9)
  218. # Testing the third dimension, and getting the tuple arguments
  219. if hasattr(p, 'z'):
  220. self.assertEqual(True, pnt.hasz)
  221. self.assertEqual(p.z, pnt.z)
  222. self.assertEqual(p.z, pnt.tuple[2], 9)
  223. tup_args = (p.x, p.y, p.z)
  224. set_tup1 = (2.71, 3.14, 5.23)
  225. set_tup2 = (5.23, 2.71, 3.14)
  226. else:
  227. self.assertEqual(False, pnt.hasz)
  228. self.assertEqual(None, pnt.z)
  229. tup_args = (p.x, p.y)
  230. set_tup1 = (2.71, 3.14)
  231. set_tup2 = (3.14, 2.71)
  232. # Centroid operation on point should be point itself
  233. self.assertEqual(p.centroid, pnt.centroid.tuple)
  234. # Now testing the different constructors
  235. pnt2 = Point(tup_args) # e.g., Point((1, 2))
  236. pnt3 = Point(*tup_args) # e.g., Point(1, 2)
  237. self.assertEqual(True, pnt == pnt2)
  238. self.assertEqual(True, pnt == pnt3)
  239. # Now testing setting the x and y
  240. pnt.y = 3.14
  241. pnt.x = 2.71
  242. self.assertEqual(3.14, pnt.y)
  243. self.assertEqual(2.71, pnt.x)
  244. # Setting via the tuple/coords property
  245. pnt.tuple = set_tup1
  246. self.assertEqual(set_tup1, pnt.tuple)
  247. pnt.coords = set_tup2
  248. self.assertEqual(set_tup2, pnt.coords)
  249. prev = pnt # setting the previous geometry
  250. def test02b_multipoints(self):
  251. "Testing MultiPoint objects."
  252. for mp in self.geometries.multipoints:
  253. mpnt = fromstr(mp.wkt)
  254. self.assertEqual(mpnt.geom_type, 'MultiPoint')
  255. self.assertEqual(mpnt.geom_typeid, 4)
  256. self.assertAlmostEqual(mp.centroid[0], mpnt.centroid.tuple[0], 9)
  257. self.assertAlmostEqual(mp.centroid[1], mpnt.centroid.tuple[1], 9)
  258. self.assertRaises(GEOSIndexError, mpnt.__getitem__, len(mpnt))
  259. self.assertEqual(mp.centroid, mpnt.centroid.tuple)
  260. self.assertEqual(mp.coords, tuple(m.tuple for m in mpnt))
  261. for p in mpnt:
  262. self.assertEqual(p.geom_type, 'Point')
  263. self.assertEqual(p.geom_typeid, 0)
  264. self.assertEqual(p.empty, False)
  265. self.assertEqual(p.valid, True)
  266. def test03a_linestring(self):
  267. "Testing LineString objects."
  268. prev = fromstr('POINT(0 0)')
  269. for l in self.geometries.linestrings:
  270. ls = fromstr(l.wkt)
  271. self.assertEqual(ls.geom_type, 'LineString')
  272. self.assertEqual(ls.geom_typeid, 1)
  273. self.assertEqual(ls.empty, False)
  274. self.assertEqual(ls.ring, False)
  275. if hasattr(l, 'centroid'):
  276. self.assertEqual(l.centroid, ls.centroid.tuple)
  277. if hasattr(l, 'tup'):
  278. self.assertEqual(l.tup, ls.tuple)
  279. self.assertEqual(True, ls == fromstr(l.wkt))
  280. self.assertEqual(False, ls == prev)
  281. self.assertRaises(GEOSIndexError, ls.__getitem__, len(ls))
  282. prev = ls
  283. # Creating a LineString from a tuple, list, and numpy array
  284. self.assertEqual(ls, LineString(ls.tuple)) # tuple
  285. self.assertEqual(ls, LineString(*ls.tuple)) # as individual arguments
  286. self.assertEqual(ls, LineString([list(tup) for tup in ls.tuple])) # as list
  287. self.assertEqual(ls.wkt, LineString(*tuple(Point(tup) for tup in ls.tuple)).wkt) # Point individual arguments
  288. if numpy: self.assertEqual(ls, LineString(numpy.array(ls.tuple))) # as numpy array
  289. def test03b_multilinestring(self):
  290. "Testing MultiLineString objects."
  291. prev = fromstr('POINT(0 0)')
  292. for l in self.geometries.multilinestrings:
  293. ml = fromstr(l.wkt)
  294. self.assertEqual(ml.geom_type, 'MultiLineString')
  295. self.assertEqual(ml.geom_typeid, 5)
  296. self.assertAlmostEqual(l.centroid[0], ml.centroid.x, 9)
  297. self.assertAlmostEqual(l.centroid[1], ml.centroid.y, 9)
  298. self.assertEqual(True, ml == fromstr(l.wkt))
  299. self.assertEqual(False, ml == prev)
  300. prev = ml
  301. for ls in ml:
  302. self.assertEqual(ls.geom_type, 'LineString')
  303. self.assertEqual(ls.geom_typeid, 1)
  304. self.assertEqual(ls.empty, False)
  305. self.assertRaises(GEOSIndexError, ml.__getitem__, len(ml))
  306. self.assertEqual(ml.wkt, MultiLineString(*tuple(s.clone() for s in ml)).wkt)
  307. self.assertEqual(ml, MultiLineString(*tuple(LineString(s.tuple) for s in ml)))
  308. def test04_linearring(self):
  309. "Testing LinearRing objects."
  310. for rr in self.geometries.linearrings:
  311. lr = fromstr(rr.wkt)
  312. self.assertEqual(lr.geom_type, 'LinearRing')
  313. self.assertEqual(lr.geom_typeid, 2)
  314. self.assertEqual(rr.n_p, len(lr))
  315. self.assertEqual(True, lr.valid)
  316. self.assertEqual(False, lr.empty)
  317. # Creating a LinearRing from a tuple, list, and numpy array
  318. self.assertEqual(lr, LinearRing(lr.tuple))
  319. self.assertEqual(lr, LinearRing(*lr.tuple))
  320. self.assertEqual(lr, LinearRing([list(tup) for tup in lr.tuple]))
  321. if numpy: self.assertEqual(lr, LinearRing(numpy.array(lr.tuple)))
  322. def test05a_polygons(self):
  323. "Testing Polygon objects."
  324. # Testing `from_bbox` class method
  325. bbox = (-180, -90, 180, 90)
  326. p = Polygon.from_bbox( bbox )
  327. self.assertEqual(bbox, p.extent)
  328. prev = fromstr('POINT(0 0)')
  329. for p in self.geometries.polygons:
  330. # Creating the Polygon, testing its properties.
  331. poly = fromstr(p.wkt)
  332. self.assertEqual(poly.geom_type, 'Polygon')
  333. self.assertEqual(poly.geom_typeid, 3)
  334. self.assertEqual(poly.empty, False)
  335. self.assertEqual(poly.ring, False)
  336. self.assertEqual(p.n_i, poly.num_interior_rings)
  337. self.assertEqual(p.n_i + 1, len(poly)) # Testing __len__
  338. self.assertEqual(p.n_p, poly.num_points)
  339. # Area & Centroid
  340. self.assertAlmostEqual(p.area, poly.area, 9)
  341. self.assertAlmostEqual(p.centroid[0], poly.centroid.tuple[0], 9)
  342. self.assertAlmostEqual(p.centroid[1], poly.centroid.tuple[1], 9)
  343. # Testing the geometry equivalence
  344. self.assertEqual(True, poly == fromstr(p.wkt))
  345. self.assertEqual(False, poly == prev) # Should not be equal to previous geometry
  346. self.assertEqual(True, poly != prev)
  347. # Testing the exterior ring
  348. ring = poly.exterior_ring
  349. self.assertEqual(ring.geom_type, 'LinearRing')
  350. self.assertEqual(ring.geom_typeid, 2)
  351. if p.ext_ring_cs:
  352. self.assertEqual(p.ext_ring_cs, ring.tuple)
  353. self.assertEqual(p.ext_ring_cs, poly[0].tuple) # Testing __getitem__
  354. # Testing __getitem__ and __setitem__ on invalid indices
  355. self.assertRaises(GEOSIndexError, poly.__getitem__, len(poly))
  356. self.assertRaises(GEOSIndexError, poly.__setitem__, len(poly), False)
  357. self.assertRaises(GEOSIndexError, poly.__getitem__, -1 * len(poly) - 1)
  358. # Testing __iter__
  359. for r in poly:
  360. self.assertEqual(r.geom_type, 'LinearRing')
  361. self.assertEqual(r.geom_typeid, 2)
  362. # Testing polygon construction.
  363. self.assertRaises(TypeError, Polygon.__init__, 0, [1, 2, 3])
  364. self.assertRaises(TypeError, Polygon.__init__, 'foo')
  365. # Polygon(shell, (hole1, ... holeN))
  366. rings = tuple(r for r in poly)
  367. self.assertEqual(poly, Polygon(rings[0], rings[1:]))
  368. # Polygon(shell_tuple, hole_tuple1, ... , hole_tupleN)
  369. ring_tuples = tuple(r.tuple for r in poly)
  370. self.assertEqual(poly, Polygon(*ring_tuples))
  371. # Constructing with tuples of LinearRings.
  372. self.assertEqual(poly.wkt, Polygon(*tuple(r for r in poly)).wkt)
  373. self.assertEqual(poly.wkt, Polygon(*tuple(LinearRing(r.tuple) for r in poly)).wkt)
  374. def test05b_multipolygons(self):
  375. "Testing MultiPolygon objects."
  376. print("\nBEGIN - expecting GEOS_NOTICE; safe to ignore.\n")
  377. prev = fromstr('POINT (0 0)')
  378. for mp in self.geometries.multipolygons:
  379. mpoly = fromstr(mp.wkt)
  380. self.assertEqual(mpoly.geom_type, 'MultiPolygon')
  381. self.assertEqual(mpoly.geom_typeid, 6)
  382. self.assertEqual(mp.valid, mpoly.valid)
  383. if mp.valid:
  384. self.assertEqual(mp.num_geom, mpoly.num_geom)
  385. self.assertEqual(mp.n_p, mpoly.num_coords)
  386. self.assertEqual(mp.num_geom, len(mpoly))
  387. self.assertRaises(GEOSIndexError, mpoly.__getitem__, len(mpoly))
  388. for p in mpoly:
  389. self.assertEqual(p.geom_type, 'Polygon')
  390. self.assertEqual(p.geom_typeid, 3)
  391. self.assertEqual(p.valid, True)
  392. self.assertEqual(mpoly.wkt, MultiPolygon(*tuple(poly.clone() for poly in mpoly)).wkt)
  393. print("\nEND - expecting GEOS_NOTICE; safe to ignore.\n")
  394. def test06a_memory_hijinks(self):
  395. "Testing Geometry __del__() on rings and polygons."
  396. #### Memory issues with rings and polygons
  397. # These tests are needed to ensure sanity with writable geometries.
  398. # Getting a polygon with interior rings, and pulling out the interior rings
  399. poly = fromstr(self.geometries.polygons[1].wkt)
  400. ring1 = poly[0]
  401. ring2 = poly[1]
  402. # These deletes should be 'harmless' since they are done on child geometries
  403. del ring1
  404. del ring2
  405. ring1 = poly[0]
  406. ring2 = poly[1]
  407. # Deleting the polygon
  408. del poly
  409. # Access to these rings is OK since they are clones.
  410. s1, s2 = str(ring1), str(ring2)
  411. def test08_coord_seq(self):
  412. "Testing Coordinate Sequence objects."
  413. for p in self.geometries.polygons:
  414. if p.ext_ring_cs:
  415. # Constructing the polygon and getting the coordinate sequence
  416. poly = fromstr(p.wkt)
  417. cs = poly.exterior_ring.coord_seq
  418. self.assertEqual(p.ext_ring_cs, cs.tuple) # done in the Polygon test too.
  419. self.assertEqual(len(p.ext_ring_cs), len(cs)) # Making sure __len__ works
  420. # Checks __getitem__ and __setitem__
  421. for i in xrange(len(p.ext_ring_cs)):
  422. c1 = p.ext_ring_cs[i] # Expected value
  423. c2 = cs[i] # Value from coordseq
  424. self.assertEqual(c1, c2)
  425. # Constructing the test value to set the coordinate sequence with
  426. if len(c1) == 2: tset = (5, 23)
  427. else: tset = (5, 23, 8)
  428. cs[i] = tset
  429. # Making sure every set point matches what we expect
  430. for j in range(len(tset)):
  431. cs[i] = tset
  432. self.assertEqual(tset[j], cs[i][j])
  433. def test09_relate_pattern(self):
  434. "Testing relate() and relate_pattern()."
  435. g = fromstr('POINT (0 0)')
  436. self.assertRaises(GEOSException, g.relate_pattern, 0, 'invalid pattern, yo')
  437. for rg in self.geometries.relate_geoms:
  438. a = fromstr(rg.wkt_a)
  439. b = fromstr(rg.wkt_b)
  440. self.assertEqual(rg.result, a.relate_pattern(b, rg.pattern))
  441. self.assertEqual(rg.pattern, a.relate(b))
  442. def test10_intersection(self):
  443. "Testing intersects() and intersection()."
  444. for i in xrange(len(self.geometries.topology_geoms)):
  445. a = fromstr(self.geometries.topology_geoms[i].wkt_a)
  446. b = fromstr(self.geometries.topology_geoms[i].wkt_b)
  447. i1 = fromstr(self.geometries.intersect_geoms[i].wkt)
  448. self.assertEqual(True, a.intersects(b))
  449. i2 = a.intersection(b)
  450. self.assertEqual(i1, i2)
  451. self.assertEqual(i1, a & b) # __and__ is intersection operator
  452. a &= b # testing __iand__
  453. self.assertEqual(i1, a)
  454. def test11_union(self):
  455. "Testing union()."
  456. for i in xrange(len(self.geometries.topology_geoms)):
  457. a = fromstr(self.geometries.topology_geoms[i].wkt_a)
  458. b = fromstr(self.geometries.topology_geoms[i].wkt_b)
  459. u1 = fromstr(self.geometries.union_geoms[i].wkt)
  460. u2 = a.union(b)
  461. self.assertEqual(u1, u2)
  462. self.assertEqual(u1, a | b) # __or__ is union operator
  463. a |= b # testing __ior__
  464. self.assertEqual(u1, a)
  465. def test12_difference(self):
  466. "Testing difference()."
  467. for i in xrange(len(self.geometries.topology_geoms)):
  468. a = fromstr(self.geometries.topology_geoms[i].wkt_a)
  469. b = fromstr(self.geometries.topology_geoms[i].wkt_b)
  470. d1 = fromstr(self.geometries.diff_geoms[i].wkt)
  471. d2 = a.difference(b)
  472. self.assertEqual(d1, d2)
  473. self.assertEqual(d1, a - b) # __sub__ is difference operator
  474. a -= b # testing __isub__
  475. self.assertEqual(d1, a)
  476. def test13_symdifference(self):
  477. "Testing sym_difference()."
  478. for i in xrange(len(self.geometries.topology_geoms)):
  479. a = fromstr(self.geometries.topology_geoms[i].wkt_a)
  480. b = fromstr(self.geometries.topology_geoms[i].wkt_b)
  481. d1 = fromstr(self.geometries.sdiff_geoms[i].wkt)
  482. d2 = a.sym_difference(b)
  483. self.assertEqual(d1, d2)
  484. self.assertEqual(d1, a ^ b) # __xor__ is symmetric difference operator
  485. a ^= b # testing __ixor__
  486. self.assertEqual(d1, a)
  487. def test14_buffer(self):
  488. "Testing buffer()."
  489. for bg in self.geometries.buffer_geoms:
  490. g = fromstr(bg.wkt)
  491. # The buffer we expect
  492. exp_buf = fromstr(bg.buffer_wkt)
  493. quadsegs = bg.quadsegs
  494. width = bg.width
  495. # Can't use a floating-point for the number of quadsegs.
  496. self.assertRaises(ctypes.ArgumentError, g.buffer, width, float(quadsegs))
  497. # Constructing our buffer
  498. buf = g.buffer(width, quadsegs)
  499. self.assertEqual(exp_buf.num_coords, buf.num_coords)
  500. self.assertEqual(len(exp_buf), len(buf))
  501. # Now assuring that each point in the buffer is almost equal
  502. for j in xrange(len(exp_buf)):
  503. exp_ring = exp_buf[j]
  504. buf_ring = buf[j]
  505. self.assertEqual(len(exp_ring), len(buf_ring))
  506. for k in xrange(len(exp_ring)):
  507. # Asserting the X, Y of each point are almost equal (due to floating point imprecision)
  508. self.assertAlmostEqual(exp_ring[k][0], buf_ring[k][0], 9)
  509. self.assertAlmostEqual(exp_ring[k][1], buf_ring[k][1], 9)
  510. def test15_srid(self):
  511. "Testing the SRID property and keyword."
  512. # Testing SRID keyword on Point
  513. pnt = Point(5, 23, srid=4326)
  514. self.assertEqual(4326, pnt.srid)
  515. pnt.srid = 3084
  516. self.assertEqual(3084, pnt.srid)
  517. self.assertRaises(ctypes.ArgumentError, pnt.set_srid, '4326')
  518. # Testing SRID keyword on fromstr(), and on Polygon rings.
  519. poly = fromstr(self.geometries.polygons[1].wkt, srid=4269)
  520. self.assertEqual(4269, poly.srid)
  521. for ring in poly: self.assertEqual(4269, ring.srid)
  522. poly.srid = 4326
  523. self.assertEqual(4326, poly.shell.srid)
  524. # Testing SRID keyword on GeometryCollection
  525. gc = GeometryCollection(Point(5, 23), LineString((0, 0), (1.5, 1.5), (3, 3)), srid=32021)
  526. self.assertEqual(32021, gc.srid)
  527. for i in range(len(gc)): self.assertEqual(32021, gc[i].srid)
  528. # GEOS may get the SRID from HEXEWKB
  529. # 'POINT(5 23)' at SRID=4326 in hex form -- obtained from PostGIS
  530. # using `SELECT GeomFromText('POINT (5 23)', 4326);`.
  531. hex = '0101000020E610000000000000000014400000000000003740'
  532. p1 = fromstr(hex)
  533. self.assertEqual(4326, p1.srid)
  534. # In GEOS 3.0.0rc1-4 when the EWKB and/or HEXEWKB is exported,
  535. # the SRID information is lost and set to -1 -- this is not a
  536. # problem on the 3.0.0 version (another reason to upgrade).
  537. exp_srid = self.null_srid
  538. p2 = fromstr(p1.hex)
  539. self.assertEqual(exp_srid, p2.srid)
  540. p3 = fromstr(p1.hex, srid=-1) # -1 is intended.
  541. self.assertEqual(-1, p3.srid)
  542. def test16_mutable_geometries(self):
  543. "Testing the mutability of Polygons and Geometry Collections."
  544. ### Testing the mutability of Polygons ###
  545. for p in self.geometries.polygons:
  546. poly = fromstr(p.wkt)
  547. # Should only be able to use __setitem__ with LinearRing geometries.
  548. self.assertRaises(TypeError, poly.__setitem__, 0, LineString((1, 1), (2, 2)))
  549. # Constructing the new shell by adding 500 to every point in the old shell.
  550. shell_tup = poly.shell.tuple
  551. new_coords = []
  552. for point in shell_tup: new_coords.append((point[0] + 500., point[1] + 500.))
  553. new_shell = LinearRing(*tuple(new_coords))
  554. # Assigning polygon's exterior ring w/the new shell
  555. poly.exterior_ring = new_shell
  556. s = str(new_shell) # new shell is still accessible
  557. self.assertEqual(poly.exterior_ring, new_shell)
  558. self.assertEqual(poly[0], new_shell)
  559. ### Testing the mutability of Geometry Collections
  560. for tg in self.geometries.multipoints:
  561. mp = fromstr(tg.wkt)
  562. for i in range(len(mp)):
  563. # Creating a random point.
  564. pnt = mp[i]
  565. new = Point(random.randint(1, 100), random.randint(1, 100))
  566. # Testing the assignment
  567. mp[i] = new
  568. s = str(new) # what was used for the assignment is still accessible
  569. self.assertEqual(mp[i], new)
  570. self.assertEqual(mp[i].wkt, new.wkt)
  571. self.assertNotEqual(pnt, mp[i])
  572. # MultiPolygons involve much more memory management because each
  573. # Polygon w/in the collection has its own rings.
  574. for tg in self.geometries.multipolygons:
  575. mpoly = fromstr(tg.wkt)
  576. for i in xrange(len(mpoly)):
  577. poly = mpoly[i]
  578. old_poly = mpoly[i]
  579. # Offsetting the each ring in the polygon by 500.
  580. for j in xrange(len(poly)):
  581. r = poly[j]
  582. for k in xrange(len(r)): r[k] = (r[k][0] + 500., r[k][1] + 500.)
  583. poly[j] = r
  584. self.assertNotEqual(mpoly[i], poly)
  585. # Testing the assignment
  586. mpoly[i] = poly
  587. s = str(poly) # Still accessible
  588. self.assertEqual(mpoly[i], poly)
  589. self.assertNotEqual(mpoly[i], old_poly)
  590. # Extreme (!!) __setitem__ -- no longer works, have to detect
  591. # in the first object that __setitem__ is called in the subsequent
  592. # objects -- maybe mpoly[0, 0, 0] = (3.14, 2.71)?
  593. #mpoly[0][0][0] = (3.14, 2.71)
  594. #self.assertEqual((3.14, 2.71), mpoly[0][0][0])
  595. # Doing it more slowly..
  596. #self.assertEqual((3.14, 2.71), mpoly[0].shell[0])
  597. #del mpoly
  598. def test17_threed(self):
  599. "Testing three-dimensional geometries."
  600. # Testing a 3D Point
  601. pnt = Point(2, 3, 8)
  602. self.assertEqual((2.,3.,8.), pnt.coords)
  603. self.assertRaises(TypeError, pnt.set_coords, (1.,2.))
  604. pnt.coords = (1.,2.,3.)
  605. self.assertEqual((1.,2.,3.), pnt.coords)
  606. # Testing a 3D LineString
  607. ls = LineString((2., 3., 8.), (50., 250., -117.))
  608. self.assertEqual(((2.,3.,8.), (50.,250.,-117.)), ls.tuple)
  609. self.assertRaises(TypeError, ls.__setitem__, 0, (1.,2.))
  610. ls[0] = (1.,2.,3.)
  611. self.assertEqual((1.,2.,3.), ls[0])
  612. def test18_distance(self):
  613. "Testing the distance() function."
  614. # Distance to self should be 0.
  615. pnt = Point(0, 0)
  616. self.assertEqual(0.0, pnt.distance(Point(0, 0)))
  617. # Distance should be 1
  618. self.assertEqual(1.0, pnt.distance(Point(0, 1)))
  619. # Distance should be ~ sqrt(2)
  620. self.assertAlmostEqual(1.41421356237, pnt.distance(Point(1, 1)), 11)
  621. # Distances are from the closest vertex in each geometry --
  622. # should be 3 (distance from (2, 2) to (5, 2)).
  623. ls1 = LineString((0, 0), (1, 1), (2, 2))
  624. ls2 = LineString((5, 2), (6, 1), (7, 0))
  625. self.assertEqual(3, ls1.distance(ls2))
  626. def test19_length(self):
  627. "Testing the length property."
  628. # Points have 0 length.
  629. pnt = Point(0, 0)
  630. self.assertEqual(0.0, pnt.length)
  631. # Should be ~ sqrt(2)
  632. ls = LineString((0, 0), (1, 1))
  633. self.assertAlmostEqual(1.41421356237, ls.length, 11)
  634. # Should be circumfrence of Polygon
  635. poly = Polygon(LinearRing((0, 0), (0, 1), (1, 1), (1, 0), (0, 0)))
  636. self.assertEqual(4.0, poly.length)
  637. # Should be sum of each element's length in collection.
  638. mpoly = MultiPolygon(poly.clone(), poly)
  639. self.assertEqual(8.0, mpoly.length)
  640. def test20a_emptyCollections(self):
  641. "Testing empty geometries and collections."
  642. gc1 = GeometryCollection([])
  643. gc2 = fromstr('GEOMETRYCOLLECTION EMPTY')
  644. pnt = fromstr('POINT EMPTY')
  645. ls = fromstr('LINESTRING EMPTY')
  646. poly = fromstr('POLYGON EMPTY')
  647. mls = fromstr('MULTILINESTRING EMPTY')
  648. mpoly1 = fromstr('MULTIPOLYGON EMPTY')
  649. mpoly2 = MultiPolygon(())
  650. for g in [gc1, gc2, pnt, ls, poly, mls, mpoly1, mpoly2]:
  651. self.assertEqual(True, g.empty)
  652. # Testing len() and num_geom.
  653. if isinstance(g, Polygon):
  654. self.assertEqual(1, len(g)) # Has one empty linear ring
  655. self.assertEqual(1, g.num_geom)
  656. self.assertEqual(0, len(g[0]))
  657. elif isinstance(g, (Point, LineString)):
  658. self.assertEqual(1, g.num_geom)
  659. self.assertEqual(0, len(g))
  660. else:
  661. self.assertEqual(0, g.num_geom)
  662. self.assertEqual(0, len(g))
  663. # Testing __getitem__ (doesn't work on Point or Polygon)
  664. if isinstance(g, Point):
  665. self.assertRaises(GEOSIndexError, g.get_x)
  666. elif isinstance(g, Polygon):
  667. lr = g.shell
  668. self.assertEqual('LINEARRING EMPTY', lr.wkt)
  669. self.assertEqual(0, len(lr))
  670. self.assertEqual(True, lr.empty)
  671. self.assertRaises(GEOSIndexError, lr.__getitem__, 0)
  672. else:
  673. self.assertRaises(GEOSIndexError, g.__getitem__, 0)
  674. def test20b_collections_of_collections(self):
  675. "Testing GeometryCollection handling of other collections."
  676. # Creating a GeometryCollection WKT string composed of other
  677. # collections and polygons.
  678. coll = [mp.wkt for mp in self.geometries.multipolygons if mp.valid]
  679. coll.extend([mls.wkt for mls in self.geometries.multilinestrings])
  680. coll.extend([p.wkt for p in self.geometries.polygons])
  681. coll.extend([mp.wkt for mp in self.geometries.multipoints])
  682. gc_wkt = 'GEOMETRYCOLLECTION(%s)' % ','.join(coll)
  683. # Should construct ok from WKT
  684. gc1 = GEOSGeometry(gc_wkt)
  685. # Should also construct ok from individual geometry arguments.
  686. gc2 = GeometryCollection(*tuple(g for g in gc1))
  687. # And, they should be equal.
  688. self.assertEqual(gc1, gc2)
  689. def test21_test_gdal(self):
  690. "Testing `ogr` and `srs` properties."
  691. if not gdal.HAS_GDAL: return
  692. g1 = fromstr('POINT(5 23)')
  693. self.assertEqual(True, isinstance(g1.ogr, gdal.OGRGeometry))
  694. self.assertEqual(g1.srs, None)
  695. g2 = fromstr('LINESTRING(0 0, 5 5, 23 23)', srid=4326)
  696. self.assertEqual(True, isinstance(g2.ogr, gdal.OGRGeometry))
  697. self.assertEqual(True, isinstance(g2.srs, gdal.SpatialReference))
  698. self.assertEqual(g2.hex, g2.ogr.hex)
  699. self.assertEqual('WGS 84', g2.srs.name)
  700. def test22_copy(self):
  701. "Testing use with the Python `copy` module."
  702. import copy
  703. poly = GEOSGeometry('POLYGON((0 0, 0 23, 23 23, 23 0, 0 0), (5 5, 5 10, 10 10, 10 5, 5 5))')
  704. cpy1 = copy.copy(poly)
  705. cpy2 = copy.deepcopy(poly)
  706. self.assertNotEqual(poly._ptr, cpy1._ptr)
  707. self.assertNotEqual(poly._ptr, cpy2._ptr)
  708. def test23_transform(self):
  709. "Testing `transform` method."
  710. if not gdal.HAS_GDAL: return
  711. orig = GEOSGeometry('POINT (-104.609 38.255)', 4326)
  712. trans = GEOSGeometry('POINT (992385.4472045 481455.4944650)', 2774)
  713. # Using a srid, a SpatialReference object, and a CoordTransform object
  714. # for transformations.
  715. t1, t2, t3 = orig.clone(), orig.clone(), orig.clone()
  716. t1.transform(trans.srid)
  717. t2.transform(gdal.SpatialReference('EPSG:2774'))
  718. ct = gdal.CoordTransform(gdal.SpatialReference('WGS84'), gdal.SpatialReference(2774))
  719. t3.transform(ct)
  720. # Testing use of the `clone` keyword.
  721. k1 = orig.clone()
  722. k2 = k1.transform(trans.srid, clone=True)
  723. self.assertEqual(k1, orig)
  724. self.assertNotEqual(k1, k2)
  725. prec = 3
  726. for p in (t1, t2, t3, k2):
  727. self.assertAlmostEqual(trans.x, p.x, prec)
  728. self.assertAlmostEqual(trans.y, p.y, prec)
  729. def test23_transform_noop(self):
  730. """ Testing `transform` method (SRID match) """
  731. # transform() should no-op if source & dest SRIDs match,
  732. # regardless of whether GDAL is available.
  733. if gdal.HAS_GDAL:
  734. g = GEOSGeometry('POINT (-104.609 38.255)', 4326)
  735. gt = g.tuple
  736. g.transform(4326)
  737. self.assertEqual(g.tuple, gt)
  738. self.assertEqual(g.srid, 4326)
  739. g = GEOSGeometry('POINT (-104.609 38.255)', 4326)
  740. g1 = g.transform(4326, clone=True)
  741. self.assertEqual(g1.tuple, g.tuple)
  742. self.assertEqual(g1.srid, 4326)
  743. self.assertTrue(g1 is not g, "Clone didn't happen")
  744. old_has_gdal = gdal.HAS_GDAL
  745. try:
  746. gdal.HAS_GDAL = False
  747. g = GEOSGeometry('POINT (-104.609 38.255)', 4326)
  748. gt = g.tuple
  749. g.transform(4326)
  750. self.assertEqual(g.tuple, gt)
  751. self.assertEqual(g.srid, 4326)
  752. g = GEOSGeometry('POINT (-104.609 38.255)', 4326)
  753. g1 = g.transform(4326, clone=True)
  754. self.assertEqual(g1.tuple, g.tuple)
  755. self.assertEqual(g1.srid, 4326)
  756. self.assertTrue(g1 is not g, "Clone didn't happen")
  757. finally:
  758. gdal.HAS_GDAL = old_has_gdal
  759. def test23_transform_nosrid(self):
  760. """ Testing `transform` method (no SRID or negative SRID) """
  761. g = GEOSGeometry('POINT (-104.609 38.255)', srid=None)
  762. self.assertRaises(GEOSException, g.transform, 2774)
  763. g = GEOSGeometry('POINT (-104.609 38.255)', srid=None)
  764. self.assertRaises(GEOSException, g.transform, 2774, clone=True)
  765. g = GEOSGeometry('POINT (-104.609 38.255)', srid=-1)
  766. self.assertRaises(GEOSException, g.transform, 2774)
  767. g = GEOSGeometry('POINT (-104.609 38.255)', srid=-1)
  768. self.assertRaises(GEOSException, g.transform, 2774, clone=True)
  769. def test23_transform_nogdal(self):
  770. """ Testing `transform` method (GDAL not available) """
  771. old_has_gdal = gdal.HAS_GDAL
  772. try:
  773. gdal.HAS_GDAL = False
  774. g = GEOSGeometry('POINT (-104.609 38.255)', 4326)
  775. self.assertRaises(GEOSException, g.transform, 2774)
  776. g = GEOSGeometry('POINT (-104.609 38.255)', 4326)
  777. self.assertRaises(GEOSException, g.transform, 2774, clone=True)
  778. finally:
  779. gdal.HAS_GDAL = old_has_gdal
  780. def test24_extent(self):
  781. "Testing `extent` method."
  782. # The xmin, ymin, xmax, ymax of the MultiPoint should be returned.
  783. mp = MultiPoint(Point(5, 23), Point(0, 0), Point(10, 50))
  784. self.assertEqual((0.0, 0.0, 10.0, 50.0), mp.extent)
  785. pnt = Point(5.23, 17.8)
  786. # Extent of points is just the point itself repeated.
  787. self.assertEqual((5.23, 17.8, 5.23, 17.8), pnt.extent)
  788. # Testing on the 'real world' Polygon.
  789. poly = fromstr(self.geometries.polygons[3].wkt)
  790. ring = poly.shell
  791. x, y = ring.x, ring.y
  792. xmin, ymin = min(x), min(y)
  793. xmax, ymax = max(x), max(y)
  794. self.assertEqual((xmin, ymin, xmax, ymax), poly.extent)
  795. def test25_pickle(self):
  796. "Testing pickling and unpickling support."
  797. # Using both pickle and cPickle -- just 'cause.
  798. import pickle, cPickle
  799. # Creating a list of test geometries for pickling,
  800. # and setting the SRID on some of them.
  801. def get_geoms(lst, srid=None):
  802. return [GEOSGeometry(tg.wkt, srid) for tg in lst]
  803. tgeoms = get_geoms(self.geometries.points)
  804. tgeoms.extend(get_geoms(self.geometries.multilinestrings, 4326))
  805. tgeoms.extend(get_geoms(self.geometries.polygons, 3084))
  806. tgeoms.extend(get_geoms(self.geometries.multipolygons, 900913))
  807. # The SRID won't be exported in GEOS 3.0 release candidates.
  808. no_srid = self.null_srid == -1
  809. for geom in tgeoms:
  810. s1, s2 = cPickle.dumps(geom), pickle.dumps(geom)
  811. g1, g2 = cPickle.loads(s1), pickle.loads(s2)
  812. for tmpg in (g1, g2):
  813. self.assertEqual(geom, tmpg)
  814. if not no_srid: self.assertEqual(geom.srid, tmpg.srid)
  815. def test26_prepared(self):
  816. "Testing PreparedGeometry support."
  817. if not GEOS_PREPARE: return
  818. # Creating a simple multipolygon and getting a prepared version.
  819. mpoly = GEOSGeometry('MULTIPOLYGON(((0 0,0 5,5 5,5 0,0 0)),((5 5,5 10,10 10,10 5,5 5)))')
  820. prep = mpoly.prepared
  821. # A set of test points.
  822. pnts = [Point(5, 5), Point(7.5, 7.5), Point(2.5, 7.5)]
  823. covers = [True, True, False] # No `covers` op for regular GEOS geoms.
  824. for pnt, c in zip(pnts, covers):
  825. # Results should be the same (but faster)
  826. self.assertEqual(mpoly.contains(pnt), prep.contains(pnt))
  827. self.assertEqual(mpoly.intersects(pnt), prep.intersects(pnt))
  828. self.assertEqual(c, prep.covers(pnt))
  829. def test26_line_merge(self):
  830. "Testing line merge support"
  831. ref_geoms = (fromstr('LINESTRING(1 1, 1 1, 3 3)'),
  832. fromstr('MULTILINESTRING((1 1, 3 3), (3 3, 4 2))'),
  833. )
  834. ref_merged = (fromstr('LINESTRING(1 1, 3 3)'),
  835. fromstr('LINESTRING (1 1, 3 3, 4 2)'),
  836. )
  837. for geom, merged in zip(ref_geoms, ref_merged):
  838. self.assertEqual(merged, geom.merged)
  839. def test27_valid_reason(self):
  840. "Testing IsValidReason support"
  841. # Skipping tests if GEOS < v3.1.
  842. if not GEOS_PREPARE: return
  843. g = GEOSGeometry("POINT(0 0)")
  844. self.assertTrue(g.valid)
  845. self.assertTrue(isinstance(g.valid_reason, basestring))
  846. self.assertEqual(g.valid_reason, "Valid Geometry")
  847. print("\nBEGIN - expecting GEOS_NOTICE; safe to ignore.\n")
  848. g = GEOSGeometry("LINESTRING(0 0, 0 0)")
  849. self.assertTrue(not g.valid)
  850. self.assertTrue(isinstance(g.valid_reason, basestring))
  851. self.assertTrue(g.valid_reason.startswith("Too few points in geometry component"))
  852. print("\nEND - expecting GEOS_NOTICE; safe to ignore.\n")
  853. def test28_geos_version(self):
  854. "Testing the GEOS version regular expression."
  855. from django.contrib.gis.geos.libgeos import version_regex
  856. versions = [ ('3.0.0rc4-CAPI-1.3.3', '3.0.0'),
  857. ('3.0.0-CAPI-1.4.1', '3.0.0'),
  858. ('3.4.0dev-CAPI-1.8.0', '3.4.0') ]
  859. for v, expected in versions:
  860. m = version_regex.match(v)
  861. self.assertTrue(m)
  862. self.assertEqual(m.group('version'), expected)
  863. def suite():
  864. s = unittest.TestSuite()
  865. s.addTest(unittest.makeSuite(GEOSTest))
  866. return s
  867. def run(verbosity=2):
  868. unittest.TextTestRunner(verbosity=verbosity).run(suite())