1. # Copyright (c) 2008-2009 Aryeh Leib Taurog, all rights reserved.
    
  2. # Modified from original contribution by Aryeh Leib Taurog, which was
    
  3. # released under the New BSD license.
    
  4. 
    
  5. import unittest
    
  6. 
    
  7. from django.contrib.gis.geos import (
    
  8.     LinearRing,
    
  9.     LineString,
    
  10.     MultiPoint,
    
  11.     Point,
    
  12.     Polygon,
    
  13.     fromstr,
    
  14. )
    
  15. 
    
  16. 
    
  17. def api_get_distance(x):
    
  18.     return x.distance(Point(-200, -200))
    
  19. 
    
  20. 
    
  21. def api_get_buffer(x):
    
  22.     return x.buffer(10)
    
  23. 
    
  24. 
    
  25. def api_get_geom_typeid(x):
    
  26.     return x.geom_typeid
    
  27. 
    
  28. 
    
  29. def api_get_num_coords(x):
    
  30.     return x.num_coords
    
  31. 
    
  32. 
    
  33. def api_get_centroid(x):
    
  34.     return x.centroid
    
  35. 
    
  36. 
    
  37. def api_get_empty(x):
    
  38.     return x.empty
    
  39. 
    
  40. 
    
  41. def api_get_valid(x):
    
  42.     return x.valid
    
  43. 
    
  44. 
    
  45. def api_get_simple(x):
    
  46.     return x.simple
    
  47. 
    
  48. 
    
  49. def api_get_ring(x):
    
  50.     return x.ring
    
  51. 
    
  52. 
    
  53. def api_get_boundary(x):
    
  54.     return x.boundary
    
  55. 
    
  56. 
    
  57. def api_get_convex_hull(x):
    
  58.     return x.convex_hull
    
  59. 
    
  60. 
    
  61. def api_get_extent(x):
    
  62.     return x.extent
    
  63. 
    
  64. 
    
  65. def api_get_area(x):
    
  66.     return x.area
    
  67. 
    
  68. 
    
  69. def api_get_length(x):
    
  70.     return x.length
    
  71. 
    
  72. 
    
  73. geos_function_tests = [
    
  74.     val
    
  75.     for name, val in vars().items()
    
  76.     if hasattr(val, "__call__") and name.startswith("api_get_")
    
  77. ]
    
  78. 
    
  79. 
    
  80. class GEOSMutationTest(unittest.TestCase):
    
  81.     """
    
  82.     Tests Pythonic Mutability of Python GEOS geometry wrappers
    
  83.     get/set/delitem on a slice, normal list methods
    
  84.     """
    
  85. 
    
  86.     def test00_GEOSIndexException(self):
    
  87.         "Testing Geometry IndexError"
    
  88.         p = Point(1, 2)
    
  89.         for i in range(-2, 2):
    
  90.             p._checkindex(i)
    
  91.         with self.assertRaises(IndexError):
    
  92.             p._checkindex(2)
    
  93.         with self.assertRaises(IndexError):
    
  94.             p._checkindex(-3)
    
  95. 
    
  96.     def test01_PointMutations(self):
    
  97.         "Testing Point mutations"
    
  98.         for p in (Point(1, 2, 3), fromstr("POINT (1 2 3)")):
    
  99.             self.assertEqual(
    
  100.                 p._get_single_external(1), 2.0, "Point _get_single_external"
    
  101.             )
    
  102. 
    
  103.             # _set_single
    
  104.             p._set_single(0, 100)
    
  105.             self.assertEqual(p.coords, (100.0, 2.0, 3.0), "Point _set_single")
    
  106. 
    
  107.             # _set_list
    
  108.             p._set_list(2, (50, 3141))
    
  109.             self.assertEqual(p.coords, (50.0, 3141.0), "Point _set_list")
    
  110. 
    
  111.     def test02_PointExceptions(self):
    
  112.         "Testing Point exceptions"
    
  113.         with self.assertRaises(TypeError):
    
  114.             Point(range(1))
    
  115.         with self.assertRaises(TypeError):
    
  116.             Point(range(4))
    
  117. 
    
  118.     def test03_PointApi(self):
    
  119.         "Testing Point API"
    
  120.         q = Point(4, 5, 3)
    
  121.         for p in (Point(1, 2, 3), fromstr("POINT (1 2 3)")):
    
  122.             p[0:2] = [4, 5]
    
  123.             for f in geos_function_tests:
    
  124.                 self.assertEqual(f(q), f(p), "Point " + f.__name__)
    
  125. 
    
  126.     def test04_LineStringMutations(self):
    
  127.         "Testing LineString mutations"
    
  128.         for ls in (
    
  129.             LineString((1, 0), (4, 1), (6, -1)),
    
  130.             fromstr("LINESTRING (1 0,4 1,6 -1)"),
    
  131.         ):
    
  132.             self.assertEqual(
    
  133.                 ls._get_single_external(1),
    
  134.                 (4.0, 1.0),
    
  135.                 "LineString _get_single_external",
    
  136.             )
    
  137. 
    
  138.             # _set_single
    
  139.             ls._set_single(0, (-50, 25))
    
  140.             self.assertEqual(
    
  141.                 ls.coords,
    
  142.                 ((-50.0, 25.0), (4.0, 1.0), (6.0, -1.0)),
    
  143.                 "LineString _set_single",
    
  144.             )
    
  145. 
    
  146.             # _set_list
    
  147.             ls._set_list(2, ((-50.0, 25.0), (6.0, -1.0)))
    
  148.             self.assertEqual(
    
  149.                 ls.coords, ((-50.0, 25.0), (6.0, -1.0)), "LineString _set_list"
    
  150.             )
    
  151. 
    
  152.             lsa = LineString(ls.coords)
    
  153.             for f in geos_function_tests:
    
  154.                 self.assertEqual(f(lsa), f(ls), "LineString " + f.__name__)
    
  155. 
    
  156.     def test05_Polygon(self):
    
  157.         "Testing Polygon mutations"
    
  158.         for pg in (
    
  159.             Polygon(
    
  160.                 ((1, 0), (4, 1), (6, -1), (8, 10), (1, 0)),
    
  161.                 ((5, 4), (6, 4), (6, 3), (5, 4)),
    
  162.             ),
    
  163.             fromstr("POLYGON ((1 0,4 1,6 -1,8 10,1 0),(5 4,6 4,6 3,5 4))"),
    
  164.         ):
    
  165.             self.assertEqual(
    
  166.                 pg._get_single_external(0),
    
  167.                 LinearRing((1, 0), (4, 1), (6, -1), (8, 10), (1, 0)),
    
  168.                 "Polygon _get_single_external(0)",
    
  169.             )
    
  170.             self.assertEqual(
    
  171.                 pg._get_single_external(1),
    
  172.                 LinearRing((5, 4), (6, 4), (6, 3), (5, 4)),
    
  173.                 "Polygon _get_single_external(1)",
    
  174.             )
    
  175. 
    
  176.             # _set_list
    
  177.             pg._set_list(
    
  178.                 2,
    
  179.                 (
    
  180.                     ((1, 2), (10, 0), (12, 9), (-1, 15), (1, 2)),
    
  181.                     ((4, 2), (5, 2), (5, 3), (4, 2)),
    
  182.                 ),
    
  183.             )
    
  184.             self.assertEqual(
    
  185.                 pg.coords,
    
  186.                 (
    
  187.                     ((1.0, 2.0), (10.0, 0.0), (12.0, 9.0), (-1.0, 15.0), (1.0, 2.0)),
    
  188.                     ((4.0, 2.0), (5.0, 2.0), (5.0, 3.0), (4.0, 2.0)),
    
  189.                 ),
    
  190.                 "Polygon _set_list",
    
  191.             )
    
  192. 
    
  193.             lsa = Polygon(*pg.coords)
    
  194.             for f in geos_function_tests:
    
  195.                 self.assertEqual(f(lsa), f(pg), "Polygon " + f.__name__)
    
  196. 
    
  197.     def test06_Collection(self):
    
  198.         "Testing Collection mutations"
    
  199.         points = (
    
  200.             MultiPoint(*map(Point, ((3, 4), (-1, 2), (5, -4), (2, 8)))),
    
  201.             fromstr("MULTIPOINT (3 4,-1 2,5 -4,2 8)"),
    
  202.         )
    
  203.         for mp in points:
    
  204.             self.assertEqual(
    
  205.                 mp._get_single_external(2),
    
  206.                 Point(5, -4),
    
  207.                 "Collection _get_single_external",
    
  208.             )
    
  209. 
    
  210.             mp._set_list(3, map(Point, ((5, 5), (3, -2), (8, 1))))
    
  211.             self.assertEqual(
    
  212.                 mp.coords, ((5.0, 5.0), (3.0, -2.0), (8.0, 1.0)), "Collection _set_list"
    
  213.             )
    
  214. 
    
  215.             lsa = MultiPoint(*map(Point, ((5, 5), (3, -2), (8, 1))))
    
  216.             for f in geos_function_tests:
    
  217.                 self.assertEqual(f(lsa), f(mp), "MultiPoint " + f.__name__)