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

/django/contrib/gis/tests/geogapp/tests.py

https://code.google.com/p/mango-py/
Python | 87 lines | 60 code | 12 blank | 15 comment | 3 complexity | da79a6b3219bd4f7fac8be57a5d9b93f MD5 | raw file
Possible License(s): BSD-3-Clause
  1. """
  2. Tests for geography support in PostGIS 1.5+
  3. """
  4. import os
  5. from django.contrib.gis import gdal
  6. from django.contrib.gis.measure import D
  7. from django.test import TestCase
  8. from models import City, County, Zipcode
  9. class GeographyTest(TestCase):
  10. def test01_fixture_load(self):
  11. "Ensure geography features loaded properly."
  12. self.assertEqual(8, City.objects.count())
  13. def test02_distance_lookup(self):
  14. "Testing GeoQuerySet distance lookup support on non-point geography fields."
  15. z = Zipcode.objects.get(code='77002')
  16. cities1 = list(City.objects
  17. .filter(point__distance_lte=(z.poly, D(mi=500)))
  18. .order_by('name')
  19. .values_list('name', flat=True))
  20. cities2 = list(City.objects
  21. .filter(point__dwithin=(z.poly, D(mi=500)))
  22. .order_by('name')
  23. .values_list('name', flat=True))
  24. for cities in [cities1, cities2]:
  25. self.assertEqual(['Dallas', 'Houston', 'Oklahoma City'], cities)
  26. def test03_distance_method(self):
  27. "Testing GeoQuerySet.distance() support on non-point geography fields."
  28. # `GeoQuerySet.distance` is not allowed geometry fields.
  29. htown = City.objects.get(name='Houston')
  30. qs = Zipcode.objects.distance(htown.point)
  31. def test04_invalid_operators_functions(self):
  32. "Ensuring exceptions are raised for operators & functions invalid on geography fields."
  33. # Only a subset of the geometry functions & operator are available
  34. # to PostGIS geography types. For more information, visit:
  35. # http://postgis.refractions.net/documentation/manual-1.5/ch08.html#PostGIS_GeographyFunctions
  36. z = Zipcode.objects.get(code='77002')
  37. # ST_Within not available.
  38. self.assertRaises(ValueError, City.objects.filter(point__within=z.poly).count)
  39. # `@` operator not available.
  40. self.assertRaises(ValueError, City.objects.filter(point__contained=z.poly).count)
  41. # Regression test for #14060, `~=` was never really implemented for PostGIS.
  42. htown = City.objects.get(name='Houston')
  43. self.assertRaises(ValueError, City.objects.get, point__exact=htown.point)
  44. def test05_geography_layermapping(self):
  45. "Testing LayerMapping support on models with geography fields."
  46. # There is a similar test in `layermap` that uses the same data set,
  47. # but the County model here is a bit different.
  48. if not gdal.HAS_GDAL: return
  49. from django.contrib.gis.utils import LayerMapping
  50. # Getting the shapefile and mapping dictionary.
  51. shp_path = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', 'data'))
  52. co_shp = os.path.join(shp_path, 'counties', 'counties.shp')
  53. co_mapping = {'name' : 'Name',
  54. 'state' : 'State',
  55. 'mpoly' : 'MULTIPOLYGON',
  56. }
  57. # Reference county names, number of polygons, and state names.
  58. names = ['Bexar', 'Galveston', 'Harris', 'Honolulu', 'Pueblo']
  59. num_polys = [1, 2, 1, 19, 1] # Number of polygons for each.
  60. st_names = ['Texas', 'Texas', 'Texas', 'Hawaii', 'Colorado']
  61. lm = LayerMapping(County, co_shp, co_mapping, source_srs=4269, unique='name')
  62. lm.save(silent=True, strict=True)
  63. for c, name, num_poly, state in zip(County.objects.order_by('name'), names, num_polys, st_names):
  64. self.assertEqual(4326, c.mpoly.srid)
  65. self.assertEqual(num_poly, len(c.mpoly))
  66. self.assertEqual(name, c.name)
  67. self.assertEqual(state, c.state)
  68. def test06_geography_area(self):
  69. "Testing that Area calculations work on geography columns."
  70. from django.contrib.gis.measure import A
  71. # SELECT ST_Area(poly) FROM geogapp_zipcode WHERE code='77002';
  72. ref_area = 5439084.70637573
  73. tol = 5
  74. z = Zipcode.objects.area().get(code='77002')
  75. self.assertAlmostEqual(z.area.sq_m, ref_area, tol)