PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/django/contrib/gis/gdal/tests/test_geom.py

https://code.google.com/p/mango-py/
Python | 492 lines | 386 code | 60 blank | 46 comment | 43 complexity | 52cd43e37256a39c9d80832711402187 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from django.contrib.gis.gdal import OGRGeometry, OGRGeomType, \
  2. OGRException, OGRIndexError, SpatialReference, CoordTransform, \
  3. GDAL_VERSION
  4. from django.utils import unittest
  5. from django.contrib.gis.geometry.test_data import TestDataMixin
  6. class OGRGeomTest(unittest.TestCase, TestDataMixin):
  7. "This tests the OGR Geometry."
  8. def test00a_geomtype(self):
  9. "Testing OGRGeomType object."
  10. # OGRGeomType should initialize on all these inputs.
  11. try:
  12. g = OGRGeomType(1)
  13. g = OGRGeomType(7)
  14. g = OGRGeomType('point')
  15. g = OGRGeomType('GeometrycollectioN')
  16. g = OGRGeomType('LINearrING')
  17. g = OGRGeomType('Unknown')
  18. except:
  19. self.fail('Could not create an OGRGeomType object!')
  20. # Should throw TypeError on this input
  21. self.assertRaises(OGRException, OGRGeomType, 23)
  22. self.assertRaises(OGRException, OGRGeomType, 'fooD')
  23. self.assertRaises(OGRException, OGRGeomType, 9)
  24. # Equivalence can take strings, ints, and other OGRGeomTypes
  25. self.assertEqual(True, OGRGeomType(1) == OGRGeomType(1))
  26. self.assertEqual(True, OGRGeomType(7) == 'GeometryCollection')
  27. self.assertEqual(True, OGRGeomType('point') == 'POINT')
  28. self.assertEqual(False, OGRGeomType('point') == 2)
  29. self.assertEqual(True, OGRGeomType('unknown') == 0)
  30. self.assertEqual(True, OGRGeomType(6) == 'MULtiPolyGON')
  31. self.assertEqual(False, OGRGeomType(1) != OGRGeomType('point'))
  32. self.assertEqual(True, OGRGeomType('POINT') != OGRGeomType(6))
  33. # Testing the Django field name equivalent property.
  34. self.assertEqual('PointField', OGRGeomType('Point').django)
  35. self.assertEqual('GeometryField', OGRGeomType('Unknown').django)
  36. self.assertEqual(None, OGRGeomType('none').django)
  37. # 'Geometry' initialization implies an unknown geometry type.
  38. gt = OGRGeomType('Geometry')
  39. self.assertEqual(0, gt.num)
  40. self.assertEqual('Unknown', gt.name)
  41. def test00b_geomtype_25d(self):
  42. "Testing OGRGeomType object with 25D types."
  43. wkb25bit = OGRGeomType.wkb25bit
  44. self.assertTrue(OGRGeomType(wkb25bit + 1) == 'Point25D')
  45. self.assertTrue(OGRGeomType('MultiLineString25D') == (5 + wkb25bit))
  46. self.assertEqual('GeometryCollectionField', OGRGeomType('GeometryCollection25D').django)
  47. def test01a_wkt(self):
  48. "Testing WKT output."
  49. for g in self.geometries.wkt_out:
  50. geom = OGRGeometry(g.wkt)
  51. self.assertEqual(g.wkt, geom.wkt)
  52. def test01a_ewkt(self):
  53. "Testing EWKT input/output."
  54. for ewkt_val in ('POINT (1 2 3)', 'LINEARRING (0 0,1 1,2 1,0 0)'):
  55. # First with ewkt output when no SRID in EWKT
  56. self.assertEqual(ewkt_val, OGRGeometry(ewkt_val).ewkt)
  57. # No test consumption with an SRID specified.
  58. ewkt_val = 'SRID=4326;%s' % ewkt_val
  59. geom = OGRGeometry(ewkt_val)
  60. self.assertEqual(ewkt_val, geom.ewkt)
  61. self.assertEqual(4326, geom.srs.srid)
  62. def test01b_gml(self):
  63. "Testing GML output."
  64. for g in self.geometries.wkt_out:
  65. geom = OGRGeometry(g.wkt)
  66. exp_gml = g.gml
  67. if GDAL_VERSION >= (1, 8):
  68. # In GDAL 1.8, the non-conformant GML tag <gml:GeometryCollection> was
  69. # replaced with <gml:MultiGeometry>.
  70. exp_gml = exp_gml.replace('GeometryCollection', 'MultiGeometry')
  71. self.assertEqual(exp_gml, geom.gml)
  72. def test01c_hex(self):
  73. "Testing HEX input/output."
  74. for g in self.geometries.hex_wkt:
  75. geom1 = OGRGeometry(g.wkt)
  76. self.assertEqual(g.hex, geom1.hex)
  77. # Constructing w/HEX
  78. geom2 = OGRGeometry(g.hex)
  79. self.assertEqual(geom1, geom2)
  80. def test01d_wkb(self):
  81. "Testing WKB input/output."
  82. from binascii import b2a_hex
  83. for g in self.geometries.hex_wkt:
  84. geom1 = OGRGeometry(g.wkt)
  85. wkb = geom1.wkb
  86. self.assertEqual(b2a_hex(wkb).upper(), g.hex)
  87. # Constructing w/WKB.
  88. geom2 = OGRGeometry(wkb)
  89. self.assertEqual(geom1, geom2)
  90. def test01e_json(self):
  91. "Testing GeoJSON input/output."
  92. from django.contrib.gis.gdal.prototypes.geom import GEOJSON
  93. if not GEOJSON: return
  94. for g in self.geometries.json_geoms:
  95. geom = OGRGeometry(g.wkt)
  96. if not hasattr(g, 'not_equal'):
  97. self.assertEqual(g.json, geom.json)
  98. self.assertEqual(g.json, geom.geojson)
  99. self.assertEqual(OGRGeometry(g.wkt), OGRGeometry(geom.json))
  100. def test02_points(self):
  101. "Testing Point objects."
  102. prev = OGRGeometry('POINT(0 0)')
  103. for p in self.geometries.points:
  104. if not hasattr(p, 'z'): # No 3D
  105. pnt = OGRGeometry(p.wkt)
  106. self.assertEqual(1, pnt.geom_type)
  107. self.assertEqual('POINT', pnt.geom_name)
  108. self.assertEqual(p.x, pnt.x)
  109. self.assertEqual(p.y, pnt.y)
  110. self.assertEqual((p.x, p.y), pnt.tuple)
  111. def test03_multipoints(self):
  112. "Testing MultiPoint objects."
  113. for mp in self.geometries.multipoints:
  114. mgeom1 = OGRGeometry(mp.wkt) # First one from WKT
  115. self.assertEqual(4, mgeom1.geom_type)
  116. self.assertEqual('MULTIPOINT', mgeom1.geom_name)
  117. mgeom2 = OGRGeometry('MULTIPOINT') # Creating empty multipoint
  118. mgeom3 = OGRGeometry('MULTIPOINT')
  119. for g in mgeom1:
  120. mgeom2.add(g) # adding each point from the multipoints
  121. mgeom3.add(g.wkt) # should take WKT as well
  122. self.assertEqual(mgeom1, mgeom2) # they should equal
  123. self.assertEqual(mgeom1, mgeom3)
  124. self.assertEqual(mp.coords, mgeom2.coords)
  125. self.assertEqual(mp.n_p, mgeom2.point_count)
  126. def test04_linestring(self):
  127. "Testing LineString objects."
  128. prev = OGRGeometry('POINT(0 0)')
  129. for ls in self.geometries.linestrings:
  130. linestr = OGRGeometry(ls.wkt)
  131. self.assertEqual(2, linestr.geom_type)
  132. self.assertEqual('LINESTRING', linestr.geom_name)
  133. self.assertEqual(ls.n_p, linestr.point_count)
  134. self.assertEqual(ls.coords, linestr.tuple)
  135. self.assertEqual(True, linestr == OGRGeometry(ls.wkt))
  136. self.assertEqual(True, linestr != prev)
  137. self.assertRaises(OGRIndexError, linestr.__getitem__, len(linestr))
  138. prev = linestr
  139. # Testing the x, y properties.
  140. x = [tmpx for tmpx, tmpy in ls.coords]
  141. y = [tmpy for tmpx, tmpy in ls.coords]
  142. self.assertEqual(x, linestr.x)
  143. self.assertEqual(y, linestr.y)
  144. def test05_multilinestring(self):
  145. "Testing MultiLineString objects."
  146. prev = OGRGeometry('POINT(0 0)')
  147. for mls in self.geometries.multilinestrings:
  148. mlinestr = OGRGeometry(mls.wkt)
  149. self.assertEqual(5, mlinestr.geom_type)
  150. self.assertEqual('MULTILINESTRING', mlinestr.geom_name)
  151. self.assertEqual(mls.n_p, mlinestr.point_count)
  152. self.assertEqual(mls.coords, mlinestr.tuple)
  153. self.assertEqual(True, mlinestr == OGRGeometry(mls.wkt))
  154. self.assertEqual(True, mlinestr != prev)
  155. prev = mlinestr
  156. for ls in mlinestr:
  157. self.assertEqual(2, ls.geom_type)
  158. self.assertEqual('LINESTRING', ls.geom_name)
  159. self.assertRaises(OGRIndexError, mlinestr.__getitem__, len(mlinestr))
  160. def test06_linearring(self):
  161. "Testing LinearRing objects."
  162. prev = OGRGeometry('POINT(0 0)')
  163. for rr in self.geometries.linearrings:
  164. lr = OGRGeometry(rr.wkt)
  165. #self.assertEqual(101, lr.geom_type.num)
  166. self.assertEqual('LINEARRING', lr.geom_name)
  167. self.assertEqual(rr.n_p, len(lr))
  168. self.assertEqual(True, lr == OGRGeometry(rr.wkt))
  169. self.assertEqual(True, lr != prev)
  170. prev = lr
  171. def test07a_polygons(self):
  172. "Testing Polygon objects."
  173. # Testing `from_bbox` class method
  174. bbox = (-180,-90,180,90)
  175. p = OGRGeometry.from_bbox( bbox )
  176. self.assertEqual(bbox, p.extent)
  177. prev = OGRGeometry('POINT(0 0)')
  178. for p in self.geometries.polygons:
  179. poly = OGRGeometry(p.wkt)
  180. self.assertEqual(3, poly.geom_type)
  181. self.assertEqual('POLYGON', poly.geom_name)
  182. self.assertEqual(p.n_p, poly.point_count)
  183. self.assertEqual(p.n_i + 1, len(poly))
  184. # Testing area & centroid.
  185. self.assertAlmostEqual(p.area, poly.area, 9)
  186. x, y = poly.centroid.tuple
  187. self.assertAlmostEqual(p.centroid[0], x, 9)
  188. self.assertAlmostEqual(p.centroid[1], y, 9)
  189. # Testing equivalence
  190. self.assertEqual(True, poly == OGRGeometry(p.wkt))
  191. self.assertEqual(True, poly != prev)
  192. if p.ext_ring_cs:
  193. ring = poly[0]
  194. self.assertEqual(p.ext_ring_cs, ring.tuple)
  195. self.assertEqual(p.ext_ring_cs, poly[0].tuple)
  196. self.assertEqual(len(p.ext_ring_cs), ring.point_count)
  197. for r in poly:
  198. self.assertEqual('LINEARRING', r.geom_name)
  199. def test07b_closepolygons(self):
  200. "Testing closing Polygon objects."
  201. # Both rings in this geometry are not closed.
  202. poly = OGRGeometry('POLYGON((0 0, 5 0, 5 5, 0 5), (1 1, 2 1, 2 2, 2 1))')
  203. self.assertEqual(8, poly.point_count)
  204. print "\nBEGIN - expecting IllegalArgumentException; safe to ignore.\n"
  205. try:
  206. c = poly.centroid
  207. except OGRException:
  208. # Should raise an OGR exception, rings are not closed
  209. pass
  210. else:
  211. self.fail('Should have raised an OGRException!')
  212. print "\nEND - expecting IllegalArgumentException; safe to ignore.\n"
  213. # Closing the rings -- doesn't work on GDAL versions 1.4.1 and below:
  214. # http://trac.osgeo.org/gdal/ticket/1673
  215. if GDAL_VERSION <= (1, 4, 1): return
  216. poly.close_rings()
  217. self.assertEqual(10, poly.point_count) # Two closing points should've been added
  218. self.assertEqual(OGRGeometry('POINT(2.5 2.5)'), poly.centroid)
  219. def test08_multipolygons(self):
  220. "Testing MultiPolygon objects."
  221. prev = OGRGeometry('POINT(0 0)')
  222. for mp in self.geometries.multipolygons:
  223. mpoly = OGRGeometry(mp.wkt)
  224. self.assertEqual(6, mpoly.geom_type)
  225. self.assertEqual('MULTIPOLYGON', mpoly.geom_name)
  226. if mp.valid:
  227. self.assertEqual(mp.n_p, mpoly.point_count)
  228. self.assertEqual(mp.num_geom, len(mpoly))
  229. self.assertRaises(OGRIndexError, mpoly.__getitem__, len(mpoly))
  230. for p in mpoly:
  231. self.assertEqual('POLYGON', p.geom_name)
  232. self.assertEqual(3, p.geom_type)
  233. self.assertEqual(mpoly.wkt, OGRGeometry(mp.wkt).wkt)
  234. def test09a_srs(self):
  235. "Testing OGR Geometries with Spatial Reference objects."
  236. for mp in self.geometries.multipolygons:
  237. # Creating a geometry w/spatial reference
  238. sr = SpatialReference('WGS84')
  239. mpoly = OGRGeometry(mp.wkt, sr)
  240. self.assertEqual(sr.wkt, mpoly.srs.wkt)
  241. # Ensuring that SRS is propagated to clones.
  242. klone = mpoly.clone()
  243. self.assertEqual(sr.wkt, klone.srs.wkt)
  244. # Ensuring all children geometries (polygons and their rings) all
  245. # return the assigned spatial reference as well.
  246. for poly in mpoly:
  247. self.assertEqual(sr.wkt, poly.srs.wkt)
  248. for ring in poly:
  249. self.assertEqual(sr.wkt, ring.srs.wkt)
  250. # Ensuring SRS propagate in topological ops.
  251. a = OGRGeometry(self.geometries.topology_geoms[0].wkt_a, sr)
  252. b = OGRGeometry(self.geometries.topology_geoms[0].wkt_b, sr)
  253. diff = a.difference(b)
  254. union = a.union(b)
  255. self.assertEqual(sr.wkt, diff.srs.wkt)
  256. self.assertEqual(sr.srid, union.srs.srid)
  257. # Instantiating w/an integer SRID
  258. mpoly = OGRGeometry(mp.wkt, 4326)
  259. self.assertEqual(4326, mpoly.srid)
  260. mpoly.srs = SpatialReference(4269)
  261. self.assertEqual(4269, mpoly.srid)
  262. self.assertEqual('NAD83', mpoly.srs.name)
  263. # Incrementing through the multipolyogn after the spatial reference
  264. # has been re-assigned.
  265. for poly in mpoly:
  266. self.assertEqual(mpoly.srs.wkt, poly.srs.wkt)
  267. poly.srs = 32140
  268. for ring in poly:
  269. # Changing each ring in the polygon
  270. self.assertEqual(32140, ring.srs.srid)
  271. self.assertEqual('NAD83 / Texas South Central', ring.srs.name)
  272. ring.srs = str(SpatialReference(4326)) # back to WGS84
  273. self.assertEqual(4326, ring.srs.srid)
  274. # Using the `srid` property.
  275. ring.srid = 4322
  276. self.assertEqual('WGS 72', ring.srs.name)
  277. self.assertEqual(4322, ring.srid)
  278. def test09b_srs_transform(self):
  279. "Testing transform()."
  280. orig = OGRGeometry('POINT (-104.609 38.255)', 4326)
  281. trans = OGRGeometry('POINT (992385.4472045 481455.4944650)', 2774)
  282. # Using an srid, a SpatialReference object, and a CoordTransform object
  283. # or transformations.
  284. t1, t2, t3 = orig.clone(), orig.clone(), orig.clone()
  285. t1.transform(trans.srid)
  286. t2.transform(SpatialReference('EPSG:2774'))
  287. ct = CoordTransform(SpatialReference('WGS84'), SpatialReference(2774))
  288. t3.transform(ct)
  289. # Testing use of the `clone` keyword.
  290. k1 = orig.clone()
  291. k2 = k1.transform(trans.srid, clone=True)
  292. self.assertEqual(k1, orig)
  293. self.assertNotEqual(k1, k2)
  294. prec = 3
  295. for p in (t1, t2, t3, k2):
  296. self.assertAlmostEqual(trans.x, p.x, prec)
  297. self.assertAlmostEqual(trans.y, p.y, prec)
  298. def test09c_transform_dim(self):
  299. "Testing coordinate dimension is the same on transformed geometries."
  300. ls_orig = OGRGeometry('LINESTRING(-104.609 38.255)', 4326)
  301. ls_trans = OGRGeometry('LINESTRING(992385.4472045 481455.4944650)', 2774)
  302. prec = 3
  303. ls_orig.transform(ls_trans.srs)
  304. # Making sure the coordinate dimension is still 2D.
  305. self.assertEqual(2, ls_orig.coord_dim)
  306. self.assertAlmostEqual(ls_trans.x[0], ls_orig.x[0], prec)
  307. self.assertAlmostEqual(ls_trans.y[0], ls_orig.y[0], prec)
  308. def test10_difference(self):
  309. "Testing difference()."
  310. for i in xrange(len(self.geometries.topology_geoms)):
  311. a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a)
  312. b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b)
  313. d1 = OGRGeometry(self.geometries.diff_geoms[i].wkt)
  314. d2 = a.difference(b)
  315. self.assertEqual(d1, d2)
  316. self.assertEqual(d1, a - b) # __sub__ is difference operator
  317. a -= b # testing __isub__
  318. self.assertEqual(d1, a)
  319. def test11_intersection(self):
  320. "Testing intersects() and intersection()."
  321. for i in xrange(len(self.geometries.topology_geoms)):
  322. a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a)
  323. b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b)
  324. i1 = OGRGeometry(self.geometries.intersect_geoms[i].wkt)
  325. self.assertEqual(True, a.intersects(b))
  326. i2 = a.intersection(b)
  327. self.assertEqual(i1, i2)
  328. self.assertEqual(i1, a & b) # __and__ is intersection operator
  329. a &= b # testing __iand__
  330. self.assertEqual(i1, a)
  331. def test12_symdifference(self):
  332. "Testing sym_difference()."
  333. for i in xrange(len(self.geometries.topology_geoms)):
  334. a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a)
  335. b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b)
  336. d1 = OGRGeometry(self.geometries.sdiff_geoms[i].wkt)
  337. d2 = a.sym_difference(b)
  338. self.assertEqual(d1, d2)
  339. self.assertEqual(d1, a ^ b) # __xor__ is symmetric difference operator
  340. a ^= b # testing __ixor__
  341. self.assertEqual(d1, a)
  342. def test13_union(self):
  343. "Testing union()."
  344. for i in xrange(len(self.geometries.topology_geoms)):
  345. a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a)
  346. b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b)
  347. u1 = OGRGeometry(self.geometries.union_geoms[i].wkt)
  348. u2 = a.union(b)
  349. self.assertEqual(u1, u2)
  350. self.assertEqual(u1, a | b) # __or__ is union operator
  351. a |= b # testing __ior__
  352. self.assertEqual(u1, a)
  353. def test14_add(self):
  354. "Testing GeometryCollection.add()."
  355. # Can't insert a Point into a MultiPolygon.
  356. mp = OGRGeometry('MultiPolygon')
  357. pnt = OGRGeometry('POINT(5 23)')
  358. self.assertRaises(OGRException, mp.add, pnt)
  359. # GeometryCollection.add may take an OGRGeometry (if another collection
  360. # of the same type all child geoms will be added individually) or WKT.
  361. for mp in self.geometries.multipolygons:
  362. mpoly = OGRGeometry(mp.wkt)
  363. mp1 = OGRGeometry('MultiPolygon')
  364. mp2 = OGRGeometry('MultiPolygon')
  365. mp3 = OGRGeometry('MultiPolygon')
  366. for poly in mpoly:
  367. mp1.add(poly) # Adding a geometry at a time
  368. mp2.add(poly.wkt) # Adding WKT
  369. mp3.add(mpoly) # Adding a MultiPolygon's entire contents at once.
  370. for tmp in (mp1, mp2, mp3): self.assertEqual(mpoly, tmp)
  371. def test15_extent(self):
  372. "Testing `extent` property."
  373. # The xmin, ymin, xmax, ymax of the MultiPoint should be returned.
  374. mp = OGRGeometry('MULTIPOINT(5 23, 0 0, 10 50)')
  375. self.assertEqual((0.0, 0.0, 10.0, 50.0), mp.extent)
  376. # Testing on the 'real world' Polygon.
  377. poly = OGRGeometry(self.geometries.polygons[3].wkt)
  378. ring = poly.shell
  379. x, y = ring.x, ring.y
  380. xmin, ymin = min(x), min(y)
  381. xmax, ymax = max(x), max(y)
  382. self.assertEqual((xmin, ymin, xmax, ymax), poly.extent)
  383. def test16_25D(self):
  384. "Testing 2.5D geometries."
  385. pnt_25d = OGRGeometry('POINT(1 2 3)')
  386. self.assertEqual('Point25D', pnt_25d.geom_type.name)
  387. self.assertEqual(3.0, pnt_25d.z)
  388. self.assertEqual(3, pnt_25d.coord_dim)
  389. ls_25d = OGRGeometry('LINESTRING(1 1 1,2 2 2,3 3 3)')
  390. self.assertEqual('LineString25D', ls_25d.geom_type.name)
  391. self.assertEqual([1.0, 2.0, 3.0], ls_25d.z)
  392. self.assertEqual(3, ls_25d.coord_dim)
  393. def test17_pickle(self):
  394. "Testing pickle support."
  395. import cPickle
  396. g1 = OGRGeometry('LINESTRING(1 1 1,2 2 2,3 3 3)', 'WGS84')
  397. g2 = cPickle.loads(cPickle.dumps(g1))
  398. self.assertEqual(g1, g2)
  399. self.assertEqual(4326, g2.srs.srid)
  400. self.assertEqual(g1.srs.wkt, g2.srs.wkt)
  401. def test18_ogrgeometry_transform_workaround(self):
  402. "Testing coordinate dimensions on geometries after transformation."
  403. # A bug in GDAL versions prior to 1.7 changes the coordinate
  404. # dimension of a geometry after it has been transformed.
  405. # This test ensures that the bug workarounds employed within
  406. # `OGRGeometry.transform` indeed work.
  407. wkt_2d = "MULTILINESTRING ((0 0,1 1,2 2))"
  408. wkt_3d = "MULTILINESTRING ((0 0 0,1 1 1,2 2 2))"
  409. srid = 4326
  410. # For both the 2D and 3D MultiLineString, ensure _both_ the dimension
  411. # of the collection and the component LineString have the expected
  412. # coordinate dimension after transform.
  413. geom = OGRGeometry(wkt_2d, srid)
  414. geom.transform(srid)
  415. self.assertEqual(2, geom.coord_dim)
  416. self.assertEqual(2, geom[0].coord_dim)
  417. self.assertEqual(wkt_2d, geom.wkt)
  418. geom = OGRGeometry(wkt_3d, srid)
  419. geom.transform(srid)
  420. self.assertEqual(3, geom.coord_dim)
  421. self.assertEqual(3, geom[0].coord_dim)
  422. self.assertEqual(wkt_3d, geom.wkt)
  423. def test19_equivalence_regression(self):
  424. "Testing equivalence methods with non-OGRGeometry instances."
  425. self.assertNotEqual(None, OGRGeometry('POINT(0 0)'))
  426. self.assertEqual(False, OGRGeometry('LINESTRING(0 0, 1 1)') == 3)
  427. def suite():
  428. s = unittest.TestSuite()
  429. s.addTest(unittest.makeSuite(OGRGeomTest))
  430. return s
  431. def run(verbosity=2):
  432. unittest.TextTestRunner(verbosity=verbosity).run(suite())