test_geom.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. import json
  2. import pickle
  3. from django.contrib.gis.gdal import (
  4. CoordTransform, GDALException, OGRGeometry, OGRGeomType, SpatialReference,
  5. )
  6. from django.template import Context
  7. from django.template.engine import Engine
  8. from django.test import SimpleTestCase
  9. from ..test_data import TestDataMixin
  10. class OGRGeomTest(SimpleTestCase, TestDataMixin):
  11. "This tests the OGR Geometry."
  12. def test_geomtype(self):
  13. "Testing OGRGeomType object."
  14. # OGRGeomType should initialize on all these inputs.
  15. OGRGeomType(1)
  16. OGRGeomType(7)
  17. OGRGeomType('point')
  18. OGRGeomType('GeometrycollectioN')
  19. OGRGeomType('LINearrING')
  20. OGRGeomType('Unknown')
  21. # Should throw TypeError on this input
  22. with self.assertRaises(GDALException):
  23. OGRGeomType(23)
  24. with self.assertRaises(GDALException):
  25. OGRGeomType('fooD')
  26. with self.assertRaises(GDALException):
  27. OGRGeomType(9)
  28. # Equivalence can take strings, ints, and other OGRGeomTypes
  29. self.assertEqual(OGRGeomType(1), OGRGeomType(1))
  30. self.assertEqual(OGRGeomType(7), 'GeometryCollection')
  31. self.assertEqual(OGRGeomType('point'), 'POINT')
  32. self.assertNotEqual(OGRGeomType('point'), 2)
  33. self.assertEqual(OGRGeomType('unknown'), 0)
  34. self.assertEqual(OGRGeomType(6), 'MULtiPolyGON')
  35. self.assertEqual(OGRGeomType(1), OGRGeomType('point'))
  36. self.assertNotEqual(OGRGeomType('POINT'), OGRGeomType(6))
  37. # Testing the Django field name equivalent property.
  38. self.assertEqual('PointField', OGRGeomType('Point').django)
  39. self.assertEqual('GeometryField', OGRGeomType('Geometry').django)
  40. self.assertEqual('GeometryField', OGRGeomType('Unknown').django)
  41. self.assertIsNone(OGRGeomType('none').django)
  42. # 'Geometry' initialization implies an unknown geometry type.
  43. gt = OGRGeomType('Geometry')
  44. self.assertEqual(0, gt.num)
  45. self.assertEqual('Unknown', gt.name)
  46. def test_geomtype_25d(self):
  47. "Testing OGRGeomType object with 25D types."
  48. wkb25bit = OGRGeomType.wkb25bit
  49. self.assertEqual(OGRGeomType(wkb25bit + 1), 'Point25D')
  50. self.assertEqual(OGRGeomType('MultiLineString25D'), (5 + wkb25bit))
  51. self.assertEqual('GeometryCollectionField', OGRGeomType('GeometryCollection25D').django)
  52. def test_wkt(self):
  53. "Testing WKT output."
  54. for g in self.geometries.wkt_out:
  55. geom = OGRGeometry(g.wkt)
  56. self.assertEqual(g.wkt, geom.wkt)
  57. def test_ewkt(self):
  58. "Testing EWKT input/output."
  59. for ewkt_val in ('POINT (1 2 3)', 'LINEARRING (0 0,1 1,2 1,0 0)'):
  60. # First with ewkt output when no SRID in EWKT
  61. self.assertEqual(ewkt_val, OGRGeometry(ewkt_val).ewkt)
  62. # No test consumption with an SRID specified.
  63. ewkt_val = 'SRID=4326;%s' % ewkt_val
  64. geom = OGRGeometry(ewkt_val)
  65. self.assertEqual(ewkt_val, geom.ewkt)
  66. self.assertEqual(4326, geom.srs.srid)
  67. def test_gml(self):
  68. "Testing GML output."
  69. for g in self.geometries.wkt_out:
  70. geom = OGRGeometry(g.wkt)
  71. exp_gml = g.gml
  72. self.assertEqual(exp_gml, geom.gml)
  73. def test_hex(self):
  74. "Testing HEX input/output."
  75. for g in self.geometries.hex_wkt:
  76. geom1 = OGRGeometry(g.wkt)
  77. self.assertEqual(g.hex.encode(), geom1.hex)
  78. # Constructing w/HEX
  79. geom2 = OGRGeometry(g.hex)
  80. self.assertEqual(geom1, geom2)
  81. def test_wkb(self):
  82. "Testing WKB input/output."
  83. for g in self.geometries.hex_wkt:
  84. geom1 = OGRGeometry(g.wkt)
  85. wkb = geom1.wkb
  86. self.assertEqual(wkb.hex().upper(), g.hex)
  87. # Constructing w/WKB.
  88. geom2 = OGRGeometry(wkb)
  89. self.assertEqual(geom1, geom2)
  90. def test_json(self):
  91. "Testing GeoJSON input/output."
  92. for g in self.geometries.json_geoms:
  93. geom = OGRGeometry(g.wkt)
  94. if not hasattr(g, 'not_equal'):
  95. # Loading jsons to prevent decimal differences
  96. self.assertEqual(json.loads(g.json), json.loads(geom.json))
  97. self.assertEqual(json.loads(g.json), json.loads(geom.geojson))
  98. self.assertEqual(OGRGeometry(g.wkt), OGRGeometry(geom.json))
  99. # Test input with some garbage content (but valid json) (#15529)
  100. geom = OGRGeometry('{"type": "Point", "coordinates": [ 100.0, 0.0 ], "other": "<test>"}')
  101. self.assertIsInstance(geom, OGRGeometry)
  102. def test_points(self):
  103. "Testing Point objects."
  104. OGRGeometry('POINT(0 0)')
  105. for p in self.geometries.points:
  106. if not hasattr(p, 'z'): # No 3D
  107. pnt = OGRGeometry(p.wkt)
  108. self.assertEqual(1, pnt.geom_type)
  109. self.assertEqual('POINT', pnt.geom_name)
  110. self.assertEqual(p.x, pnt.x)
  111. self.assertEqual(p.y, pnt.y)
  112. self.assertEqual((p.x, p.y), pnt.tuple)
  113. def test_multipoints(self):
  114. "Testing MultiPoint objects."
  115. for mp in self.geometries.multipoints:
  116. mgeom1 = OGRGeometry(mp.wkt) # First one from WKT
  117. self.assertEqual(4, mgeom1.geom_type)
  118. self.assertEqual('MULTIPOINT', mgeom1.geom_name)
  119. mgeom2 = OGRGeometry('MULTIPOINT') # Creating empty multipoint
  120. mgeom3 = OGRGeometry('MULTIPOINT')
  121. for g in mgeom1:
  122. mgeom2.add(g) # adding each point from the multipoints
  123. mgeom3.add(g.wkt) # should take WKT as well
  124. self.assertEqual(mgeom1, mgeom2) # they should equal
  125. self.assertEqual(mgeom1, mgeom3)
  126. self.assertEqual(mp.coords, mgeom2.coords)
  127. self.assertEqual(mp.n_p, mgeom2.point_count)
  128. def test_linestring(self):
  129. "Testing LineString objects."
  130. prev = OGRGeometry('POINT(0 0)')
  131. for ls in self.geometries.linestrings:
  132. linestr = OGRGeometry(ls.wkt)
  133. self.assertEqual(2, linestr.geom_type)
  134. self.assertEqual('LINESTRING', linestr.geom_name)
  135. self.assertEqual(ls.n_p, linestr.point_count)
  136. self.assertEqual(ls.coords, linestr.tuple)
  137. self.assertEqual(linestr, OGRGeometry(ls.wkt))
  138. self.assertNotEqual(linestr, prev)
  139. msg = 'Index out of range when accessing points of a line string: %s.'
  140. with self.assertRaisesMessage(IndexError, msg % len(linestr)):
  141. linestr.__getitem__(len(linestr))
  142. prev = linestr
  143. # Testing the x, y properties.
  144. x = [tmpx for tmpx, tmpy in ls.coords]
  145. y = [tmpy for tmpx, tmpy in ls.coords]
  146. self.assertEqual(x, linestr.x)
  147. self.assertEqual(y, linestr.y)
  148. def test_multilinestring(self):
  149. "Testing MultiLineString objects."
  150. prev = OGRGeometry('POINT(0 0)')
  151. for mls in self.geometries.multilinestrings:
  152. mlinestr = OGRGeometry(mls.wkt)
  153. self.assertEqual(5, mlinestr.geom_type)
  154. self.assertEqual('MULTILINESTRING', mlinestr.geom_name)
  155. self.assertEqual(mls.n_p, mlinestr.point_count)
  156. self.assertEqual(mls.coords, mlinestr.tuple)
  157. self.assertEqual(mlinestr, OGRGeometry(mls.wkt))
  158. self.assertNotEqual(mlinestr, prev)
  159. prev = mlinestr
  160. for ls in mlinestr:
  161. self.assertEqual(2, ls.geom_type)
  162. self.assertEqual('LINESTRING', ls.geom_name)
  163. msg = 'Index out of range when accessing geometry in a collection: %s.'
  164. with self.assertRaisesMessage(IndexError, msg % len(mlinestr)):
  165. mlinestr.__getitem__(len(mlinestr))
  166. def test_linearring(self):
  167. "Testing LinearRing objects."
  168. prev = OGRGeometry('POINT(0 0)')
  169. for rr in self.geometries.linearrings:
  170. lr = OGRGeometry(rr.wkt)
  171. # self.assertEqual(101, lr.geom_type.num)
  172. self.assertEqual('LINEARRING', lr.geom_name)
  173. self.assertEqual(rr.n_p, len(lr))
  174. self.assertEqual(lr, OGRGeometry(rr.wkt))
  175. self.assertNotEqual(lr, prev)
  176. prev = lr
  177. def test_polygons(self):
  178. "Testing Polygon objects."
  179. # Testing `from_bbox` class method
  180. bbox = (-180, -90, 180, 90)
  181. p = OGRGeometry.from_bbox(bbox)
  182. self.assertEqual(bbox, p.extent)
  183. prev = OGRGeometry('POINT(0 0)')
  184. for p in self.geometries.polygons:
  185. poly = OGRGeometry(p.wkt)
  186. self.assertEqual(3, poly.geom_type)
  187. self.assertEqual('POLYGON', poly.geom_name)
  188. self.assertEqual(p.n_p, poly.point_count)
  189. self.assertEqual(p.n_i + 1, len(poly))
  190. msg = 'Index out of range when accessing rings of a polygon: %s.'
  191. with self.assertRaisesMessage(IndexError, msg % len(poly)):
  192. poly.__getitem__(len(poly))
  193. # Testing area & centroid.
  194. self.assertAlmostEqual(p.area, poly.area, 9)
  195. x, y = poly.centroid.tuple
  196. self.assertAlmostEqual(p.centroid[0], x, 9)
  197. self.assertAlmostEqual(p.centroid[1], y, 9)
  198. # Testing equivalence
  199. self.assertEqual(poly, OGRGeometry(p.wkt))
  200. self.assertNotEqual(poly, prev)
  201. if p.ext_ring_cs:
  202. ring = poly[0]
  203. self.assertEqual(p.ext_ring_cs, ring.tuple)
  204. self.assertEqual(p.ext_ring_cs, poly[0].tuple)
  205. self.assertEqual(len(p.ext_ring_cs), ring.point_count)
  206. for r in poly:
  207. self.assertEqual('LINEARRING', r.geom_name)
  208. def test_polygons_templates(self):
  209. # Accessing Polygon attributes in templates should work.
  210. engine = Engine()
  211. template = engine.from_string('{{ polygons.0.wkt }}')
  212. polygons = [OGRGeometry(p.wkt) for p in self.geometries.multipolygons[:2]]
  213. content = template.render(Context({'polygons': polygons}))
  214. self.assertIn('MULTIPOLYGON (((100', content)
  215. def test_closepolygons(self):
  216. "Testing closing Polygon objects."
  217. # Both rings in this geometry are not closed.
  218. poly = OGRGeometry('POLYGON((0 0, 5 0, 5 5, 0 5), (1 1, 2 1, 2 2, 2 1))')
  219. self.assertEqual(8, poly.point_count)
  220. with self.assertRaises(GDALException):
  221. poly.centroid
  222. poly.close_rings()
  223. self.assertEqual(10, poly.point_count) # Two closing points should've been added
  224. self.assertEqual(OGRGeometry('POINT(2.5 2.5)'), poly.centroid)
  225. def test_multipolygons(self):
  226. "Testing MultiPolygon objects."
  227. OGRGeometry('POINT(0 0)')
  228. for mp in self.geometries.multipolygons:
  229. mpoly = OGRGeometry(mp.wkt)
  230. self.assertEqual(6, mpoly.geom_type)
  231. self.assertEqual('MULTIPOLYGON', mpoly.geom_name)
  232. if mp.valid:
  233. self.assertEqual(mp.n_p, mpoly.point_count)
  234. self.assertEqual(mp.num_geom, len(mpoly))
  235. msg = 'Index out of range when accessing geometry in a collection: %s.'
  236. with self.assertRaisesMessage(IndexError, msg % len(mpoly)):
  237. mpoly.__getitem__(len(mpoly))
  238. for p in mpoly:
  239. self.assertEqual('POLYGON', p.geom_name)
  240. self.assertEqual(3, p.geom_type)
  241. self.assertEqual(mpoly.wkt, OGRGeometry(mp.wkt).wkt)
  242. def test_srs(self):
  243. "Testing OGR Geometries with Spatial Reference objects."
  244. for mp in self.geometries.multipolygons:
  245. # Creating a geometry w/spatial reference
  246. sr = SpatialReference('WGS84')
  247. mpoly = OGRGeometry(mp.wkt, sr)
  248. self.assertEqual(sr.wkt, mpoly.srs.wkt)
  249. # Ensuring that SRS is propagated to clones.
  250. klone = mpoly.clone()
  251. self.assertEqual(sr.wkt, klone.srs.wkt)
  252. # Ensuring all children geometries (polygons and their rings) all
  253. # return the assigned spatial reference as well.
  254. for poly in mpoly:
  255. self.assertEqual(sr.wkt, poly.srs.wkt)
  256. for ring in poly:
  257. self.assertEqual(sr.wkt, ring.srs.wkt)
  258. # Ensuring SRS propagate in topological ops.
  259. a = OGRGeometry(self.geometries.topology_geoms[0].wkt_a, sr)
  260. b = OGRGeometry(self.geometries.topology_geoms[0].wkt_b, sr)
  261. diff = a.difference(b)
  262. union = a.union(b)
  263. self.assertEqual(sr.wkt, diff.srs.wkt)
  264. self.assertEqual(sr.srid, union.srs.srid)
  265. # Instantiating w/an integer SRID
  266. mpoly = OGRGeometry(mp.wkt, 4326)
  267. self.assertEqual(4326, mpoly.srid)
  268. mpoly.srs = SpatialReference(4269)
  269. self.assertEqual(4269, mpoly.srid)
  270. self.assertEqual('NAD83', mpoly.srs.name)
  271. # Incrementing through the multipolygon after the spatial reference
  272. # has been re-assigned.
  273. for poly in mpoly:
  274. self.assertEqual(mpoly.srs.wkt, poly.srs.wkt)
  275. poly.srs = 32140
  276. for ring in poly:
  277. # Changing each ring in the polygon
  278. self.assertEqual(32140, ring.srs.srid)
  279. self.assertEqual('NAD83 / Texas South Central', ring.srs.name)
  280. ring.srs = str(SpatialReference(4326)) # back to WGS84
  281. self.assertEqual(4326, ring.srs.srid)
  282. # Using the `srid` property.
  283. ring.srid = 4322
  284. self.assertEqual('WGS 72', ring.srs.name)
  285. self.assertEqual(4322, ring.srid)
  286. # srs/srid may be assigned their own values, even when srs is None.
  287. mpoly = OGRGeometry(mp.wkt, srs=None)
  288. mpoly.srs = mpoly.srs
  289. mpoly.srid = mpoly.srid
  290. def test_srs_transform(self):
  291. "Testing transform()."
  292. orig = OGRGeometry('POINT (-104.609 38.255)', 4326)
  293. trans = OGRGeometry('POINT (992385.4472045 481455.4944650)', 2774)
  294. # Using an srid, a SpatialReference object, and a CoordTransform object
  295. # or transformations.
  296. t1, t2, t3 = orig.clone(), orig.clone(), orig.clone()
  297. t1.transform(trans.srid)
  298. t2.transform(SpatialReference('EPSG:2774'))
  299. ct = CoordTransform(SpatialReference('WGS84'), SpatialReference(2774))
  300. t3.transform(ct)
  301. # Testing use of the `clone` keyword.
  302. k1 = orig.clone()
  303. k2 = k1.transform(trans.srid, clone=True)
  304. self.assertEqual(k1, orig)
  305. self.assertNotEqual(k1, k2)
  306. # Different PROJ versions use different transformations, all are
  307. # correct as having a 1 meter accuracy.
  308. prec = -1
  309. for p in (t1, t2, t3, k2):
  310. self.assertAlmostEqual(trans.x, p.x, prec)
  311. self.assertAlmostEqual(trans.y, p.y, prec)
  312. def test_transform_dim(self):
  313. "Testing coordinate dimension is the same on transformed geometries."
  314. ls_orig = OGRGeometry('LINESTRING(-104.609 38.255)', 4326)
  315. ls_trans = OGRGeometry('LINESTRING(992385.4472045 481455.4944650)', 2774)
  316. # Different PROJ versions use different transformations, all are
  317. # correct as having a 1 meter accuracy.
  318. prec = -1
  319. ls_orig.transform(ls_trans.srs)
  320. # Making sure the coordinate dimension is still 2D.
  321. self.assertEqual(2, ls_orig.coord_dim)
  322. self.assertAlmostEqual(ls_trans.x[0], ls_orig.x[0], prec)
  323. self.assertAlmostEqual(ls_trans.y[0], ls_orig.y[0], prec)
  324. def test_difference(self):
  325. "Testing difference()."
  326. for i in range(len(self.geometries.topology_geoms)):
  327. a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a)
  328. b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b)
  329. d1 = OGRGeometry(self.geometries.diff_geoms[i].wkt)
  330. d2 = a.difference(b)
  331. self.assertTrue(d1.geos.equals(d2.geos))
  332. self.assertTrue(d1.geos.equals((a - b).geos)) # __sub__ is difference operator
  333. a -= b # testing __isub__
  334. self.assertTrue(d1.geos.equals(a.geos))
  335. def test_intersection(self):
  336. "Testing intersects() and intersection()."
  337. for i in range(len(self.geometries.topology_geoms)):
  338. a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a)
  339. b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b)
  340. i1 = OGRGeometry(self.geometries.intersect_geoms[i].wkt)
  341. self.assertTrue(a.intersects(b))
  342. i2 = a.intersection(b)
  343. self.assertTrue(i1.geos.equals(i2.geos))
  344. self.assertTrue(i1.geos.equals((a & b).geos)) # __and__ is intersection operator
  345. a &= b # testing __iand__
  346. self.assertTrue(i1.geos.equals(a.geos))
  347. def test_symdifference(self):
  348. "Testing sym_difference()."
  349. for i in range(len(self.geometries.topology_geoms)):
  350. a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a)
  351. b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b)
  352. d1 = OGRGeometry(self.geometries.sdiff_geoms[i].wkt)
  353. d2 = a.sym_difference(b)
  354. self.assertTrue(d1.geos.equals(d2.geos))
  355. self.assertTrue(d1.geos.equals((a ^ b).geos)) # __xor__ is symmetric difference operator
  356. a ^= b # testing __ixor__
  357. self.assertTrue(d1.geos.equals(a.geos))
  358. def test_union(self):
  359. "Testing union()."
  360. for i in range(len(self.geometries.topology_geoms)):
  361. a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a)
  362. b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b)
  363. u1 = OGRGeometry(self.geometries.union_geoms[i].wkt)
  364. u2 = a.union(b)
  365. self.assertTrue(u1.geos.equals(u2.geos))
  366. self.assertTrue(u1.geos.equals((a | b).geos)) # __or__ is union operator
  367. a |= b # testing __ior__
  368. self.assertTrue(u1.geos.equals(a.geos))
  369. def test_add(self):
  370. "Testing GeometryCollection.add()."
  371. # Can't insert a Point into a MultiPolygon.
  372. mp = OGRGeometry('MultiPolygon')
  373. pnt = OGRGeometry('POINT(5 23)')
  374. with self.assertRaises(GDALException):
  375. mp.add(pnt)
  376. # GeometryCollection.add may take an OGRGeometry (if another collection
  377. # of the same type all child geoms will be added individually) or WKT.
  378. for mp in self.geometries.multipolygons:
  379. mpoly = OGRGeometry(mp.wkt)
  380. mp1 = OGRGeometry('MultiPolygon')
  381. mp2 = OGRGeometry('MultiPolygon')
  382. mp3 = OGRGeometry('MultiPolygon')
  383. for poly in mpoly:
  384. mp1.add(poly) # Adding a geometry at a time
  385. mp2.add(poly.wkt) # Adding WKT
  386. mp3.add(mpoly) # Adding a MultiPolygon's entire contents at once.
  387. for tmp in (mp1, mp2, mp3):
  388. self.assertEqual(mpoly, tmp)
  389. def test_extent(self):
  390. "Testing `extent` property."
  391. # The xmin, ymin, xmax, ymax of the MultiPoint should be returned.
  392. mp = OGRGeometry('MULTIPOINT(5 23, 0 0, 10 50)')
  393. self.assertEqual((0.0, 0.0, 10.0, 50.0), mp.extent)
  394. # Testing on the 'real world' Polygon.
  395. poly = OGRGeometry(self.geometries.polygons[3].wkt)
  396. ring = poly.shell
  397. x, y = ring.x, ring.y
  398. xmin, ymin = min(x), min(y)
  399. xmax, ymax = max(x), max(y)
  400. self.assertEqual((xmin, ymin, xmax, ymax), poly.extent)
  401. def test_25D(self):
  402. "Testing 2.5D geometries."
  403. pnt_25d = OGRGeometry('POINT(1 2 3)')
  404. self.assertEqual('Point25D', pnt_25d.geom_type.name)
  405. self.assertEqual(3.0, pnt_25d.z)
  406. self.assertEqual(3, pnt_25d.coord_dim)
  407. ls_25d = OGRGeometry('LINESTRING(1 1 1,2 2 2,3 3 3)')
  408. self.assertEqual('LineString25D', ls_25d.geom_type.name)
  409. self.assertEqual([1.0, 2.0, 3.0], ls_25d.z)
  410. self.assertEqual(3, ls_25d.coord_dim)
  411. def test_pickle(self):
  412. "Testing pickle support."
  413. g1 = OGRGeometry('LINESTRING(1 1 1,2 2 2,3 3 3)', 'WGS84')
  414. g2 = pickle.loads(pickle.dumps(g1))
  415. self.assertEqual(g1, g2)
  416. self.assertEqual(4326, g2.srs.srid)
  417. self.assertEqual(g1.srs.wkt, g2.srs.wkt)
  418. def test_ogrgeometry_transform_workaround(self):
  419. "Testing coordinate dimensions on geometries after transformation."
  420. # A bug in GDAL versions prior to 1.7 changes the coordinate
  421. # dimension of a geometry after it has been transformed.
  422. # This test ensures that the bug workarounds employed within
  423. # `OGRGeometry.transform` indeed work.
  424. wkt_2d = "MULTILINESTRING ((0 0,1 1,2 2))"
  425. wkt_3d = "MULTILINESTRING ((0 0 0,1 1 1,2 2 2))"
  426. srid = 4326
  427. # For both the 2D and 3D MultiLineString, ensure _both_ the dimension
  428. # of the collection and the component LineString have the expected
  429. # coordinate dimension after transform.
  430. geom = OGRGeometry(wkt_2d, srid)
  431. geom.transform(srid)
  432. self.assertEqual(2, geom.coord_dim)
  433. self.assertEqual(2, geom[0].coord_dim)
  434. self.assertEqual(wkt_2d, geom.wkt)
  435. geom = OGRGeometry(wkt_3d, srid)
  436. geom.transform(srid)
  437. self.assertEqual(3, geom.coord_dim)
  438. self.assertEqual(3, geom[0].coord_dim)
  439. self.assertEqual(wkt_3d, geom.wkt)
  440. # Testing binary predicates, `assertIs` is used to check that bool is returned.
  441. def test_equivalence_regression(self):
  442. "Testing equivalence methods with non-OGRGeometry instances."
  443. self.assertIsNotNone(OGRGeometry('POINT(0 0)'))
  444. self.assertNotEqual(OGRGeometry('LINESTRING(0 0, 1 1)'), 3)
  445. def test_contains(self):
  446. self.assertIs(OGRGeometry('POINT(0 0)').contains(OGRGeometry('POINT(0 0)')), True)
  447. self.assertIs(OGRGeometry('POINT(0 0)').contains(OGRGeometry('POINT(0 1)')), False)
  448. def test_crosses(self):
  449. self.assertIs(OGRGeometry('LINESTRING(0 0, 1 1)').crosses(OGRGeometry('LINESTRING(0 1, 1 0)')), True)
  450. self.assertIs(OGRGeometry('LINESTRING(0 0, 0 1)').crosses(OGRGeometry('LINESTRING(1 0, 1 1)')), False)
  451. def test_disjoint(self):
  452. self.assertIs(OGRGeometry('LINESTRING(0 0, 1 1)').disjoint(OGRGeometry('LINESTRING(0 1, 1 0)')), False)
  453. self.assertIs(OGRGeometry('LINESTRING(0 0, 0 1)').disjoint(OGRGeometry('LINESTRING(1 0, 1 1)')), True)
  454. def test_equals(self):
  455. self.assertIs(OGRGeometry('POINT(0 0)').contains(OGRGeometry('POINT(0 0)')), True)
  456. self.assertIs(OGRGeometry('POINT(0 0)').contains(OGRGeometry('POINT(0 1)')), False)
  457. def test_intersects(self):
  458. self.assertIs(OGRGeometry('LINESTRING(0 0, 1 1)').intersects(OGRGeometry('LINESTRING(0 1, 1 0)')), True)
  459. self.assertIs(OGRGeometry('LINESTRING(0 0, 0 1)').intersects(OGRGeometry('LINESTRING(1 0, 1 1)')), False)
  460. def test_overlaps(self):
  461. self.assertIs(
  462. OGRGeometry('POLYGON ((0 0, 0 2, 2 2, 2 0, 0 0))').overlaps(
  463. OGRGeometry('POLYGON ((1 1, 1 5, 5 5, 5 1, 1 1))')
  464. ), True
  465. )
  466. self.assertIs(OGRGeometry('POINT(0 0)').overlaps(OGRGeometry('POINT(0 1)')), False)
  467. def test_touches(self):
  468. self.assertIs(
  469. OGRGeometry('POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))').touches(OGRGeometry('LINESTRING(0 2, 2 0)')), True
  470. )
  471. self.assertIs(OGRGeometry('POINT(0 0)').touches(OGRGeometry('POINT(0 1)')), False)
  472. def test_within(self):
  473. self.assertIs(
  474. OGRGeometry('POINT(0.5 0.5)').within(OGRGeometry('POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))')), True
  475. )
  476. self.assertIs(OGRGeometry('POINT(0 0)').within(OGRGeometry('POINT(0 1)')), False)
  477. def test_from_gml(self):
  478. self.assertEqual(
  479. OGRGeometry('POINT(0 0)'),
  480. OGRGeometry.from_gml(
  481. '<gml:Point gml:id="p21" srsName="http://www.opengis.net/def/crs/EPSG/0/4326">'
  482. ' <gml:pos srsDimension="2">0 0</gml:pos>'
  483. '</gml:Point>'
  484. ),
  485. )
  486. def test_empty(self):
  487. self.assertIs(OGRGeometry('POINT (0 0)').empty, False)
  488. self.assertIs(OGRGeometry('POINT EMPTY').empty, True)
  489. def test_empty_point_to_geos(self):
  490. p = OGRGeometry('POINT EMPTY', srs=4326)
  491. self.assertEqual(p.geos.ewkt, p.ewkt)