Reformatted some GIS distance tests

This commit is contained in:
Claude Paroz 2013-09-24 11:31:33 +02:00
parent 6ede1db331
commit 45403d330a
1 changed files with 54 additions and 35 deletions

View File

@ -36,8 +36,10 @@ class DistanceTest(TestCase):
cities.sort() cities.sort()
return cities return cities
def test01_init(self): def test_init(self):
"Test initialization of distance models." """
Test initialization of distance models.
"""
self.assertEqual(9, SouthTexasCity.objects.count()) self.assertEqual(9, SouthTexasCity.objects.count())
self.assertEqual(9, SouthTexasCityFt.objects.count()) self.assertEqual(9, SouthTexasCityFt.objects.count())
self.assertEqual(11, AustraliaCity.objects.count()) self.assertEqual(11, AustraliaCity.objects.count())
@ -47,8 +49,10 @@ class DistanceTest(TestCase):
self.assertEqual(1, SouthTexasInterstate.objects.count()) self.assertEqual(1, SouthTexasInterstate.objects.count())
@no_spatialite @no_spatialite
def test02_dwithin(self): def test_dwithin(self):
"Testing the `dwithin` lookup type." """
Test the `dwithin` lookup type.
"""
# Distances -- all should be equal (except for the # Distances -- all should be equal (except for the
# degree/meter pair in au_cities, that's somewhat # degree/meter pair in au_cities, that's somewhat
# approximate). # approximate).
@ -85,10 +89,12 @@ class DistanceTest(TestCase):
# Distance objects into a DWithin query using a geodetic field. # Distance objects into a DWithin query using a geodetic field.
self.assertRaises(ValueError, AustraliaCity.objects.filter(point__dwithin=(self.au_pnt, dist)).count) self.assertRaises(ValueError, AustraliaCity.objects.filter(point__dwithin=(self.au_pnt, dist)).count)
else: else:
self.assertEqual(au_cities, self.get_names(qs.filter(point__dwithin=(self.au_pnt, dist)))) self.assertListEqual(au_cities, self.get_names(qs.filter(point__dwithin=(self.au_pnt, dist))))
def test03a_distance_method(self): def test_distance_projected(self):
"Testing the `distance` GeoQuerySet method on projected coordinate systems." """
Test the `distance` GeoQuerySet method on projected coordinate systems.
"""
# The point for La Grange, TX # The point for La Grange, TX
lagrange = GEOSGeometry('POINT(-96.876369 29.905320)', 4326) lagrange = GEOSGeometry('POINT(-96.876369 29.905320)', 4326)
# Reference distances in feet and in meters. Got these values from # Reference distances in feet and in meters. Got these values from
@ -117,8 +123,7 @@ class DistanceTest(TestCase):
# Original query done on PostGIS, have to adjust AlmostEqual tolerance # Original query done on PostGIS, have to adjust AlmostEqual tolerance
# for Oracle. # for Oracle.
if oracle: tol = 2 tol = 2 if oracle else 5
else: tol = 5
# Ensuring expected distances are returned for each distance queryset. # Ensuring expected distances are returned for each distance queryset.
for qs in dist_qs: for qs in dist_qs:
@ -127,10 +132,11 @@ class DistanceTest(TestCase):
self.assertAlmostEqual(ft_distances[i], c.distance.survey_ft, tol) self.assertAlmostEqual(ft_distances[i], c.distance.survey_ft, tol)
@no_spatialite @no_spatialite
def test03b_distance_method(self): def test_distance_geodetic(self):
"Testing the `distance` GeoQuerySet method on geodetic coordnate systems." """
if oracle: tol = 2 Test the `distance` GeoQuerySet method on geodetic coordinate systems.
else: tol = 5 """
tol = 2 if oracle else 5
# Testing geodetic distance calculation with a non-point geometry # Testing geodetic distance calculation with a non-point geometry
# (a LineString of Wollongong and Shellharbour coords). # (a LineString of Wollongong and Shellharbour coords).
@ -188,8 +194,10 @@ class DistanceTest(TestCase):
self.assertAlmostEqual(sphere_distances[i], c.distance.m, tol) self.assertAlmostEqual(sphere_distances[i], c.distance.m, tol)
@no_oracle # Oracle already handles geographic distance calculation. @no_oracle # Oracle already handles geographic distance calculation.
def test03c_distance_method(self): def test_distance_transform(self):
"Testing the `distance` GeoQuerySet method used with `transform` on a geographic field." """
Test the `distance` GeoQuerySet method used with `transform` on a geographic field.
"""
# Normally you can't compute distances from a geometry field # Normally you can't compute distances from a geometry field
# that is not a PointField (on PostGIS 1.4 and below). # that is not a PointField (on PostGIS 1.4 and below).
if not connection.ops.geography: if not connection.ops.geography:
@ -216,12 +224,14 @@ class DistanceTest(TestCase):
for buf in [buf1, buf2]: for buf in [buf1, buf2]:
qs = CensusZipcode.objects.exclude(name='77005').transform(32140).distance(buf) qs = CensusZipcode.objects.exclude(name='77005').transform(32140).distance(buf)
self.assertEqual(ref_zips, self.get_names(qs)) self.assertListEqual(ref_zips, self.get_names(qs))
for i, z in enumerate(qs): for i, z in enumerate(qs):
self.assertAlmostEqual(z.distance.m, dists_m[i], 5) self.assertAlmostEqual(z.distance.m, dists_m[i], 5)
def test04_distance_lookups(self): def test_distance_lookups(self):
"Testing the `distance_lt`, `distance_gt`, `distance_lte`, and `distance_gte` lookup types." """
Test the `distance_lt`, `distance_gt`, `distance_lte`, and `distance_gte` lookup types.
"""
# Retrieving the cities within a 20km 'donut' w/a 7km radius 'hole' # Retrieving the cities within a 20km 'donut' w/a 7km radius 'hole'
# (thus, Houston and Southside place will be excluded as tested in # (thus, Houston and Southside place will be excluded as tested in
# the `test02_dwithin` above). # the `test02_dwithin` above).
@ -247,8 +257,10 @@ class DistanceTest(TestCase):
qs = SouthTexasZipcode.objects.exclude(name='77005').filter(poly__distance_lte=(z.poly, D(m=300))) qs = SouthTexasZipcode.objects.exclude(name='77005').filter(poly__distance_lte=(z.poly, D(m=300)))
self.assertEqual(['77002', '77025', '77401'], self.get_names(qs)) self.assertEqual(['77002', '77025', '77401'], self.get_names(qs))
def test05_geodetic_distance_lookups(self): def test_geodetic_distance_lookups(self):
"Testing distance lookups on geodetic coordinate systems." """
Test distance lookups on geodetic coordinate systems.
"""
# Line is from Canberra to Sydney. Query is for all other cities within # Line is from Canberra to Sydney. Query is for all other cities within
# a 100km of that line (which should exclude only Hobart & Adelaide). # a 100km of that line (which should exclude only Hobart & Adelaide).
line = GEOSGeometry('LINESTRING(144.9630 -37.8143,151.2607 -33.8870)', 4326) line = GEOSGeometry('LINESTRING(144.9630 -37.8143,151.2607 -33.8870)', 4326)
@ -269,7 +281,8 @@ class DistanceTest(TestCase):
# Ensured that a ValueError was raised, none of the rest of the test is # Ensured that a ValueError was raised, none of the rest of the test is
# support on this backend, so bail now. # support on this backend, so bail now.
if spatialite: return if spatialite:
return
# Too many params (4 in this case) should raise a ValueError. # Too many params (4 in this case) should raise a ValueError.
self.assertRaises(ValueError, len, self.assertRaises(ValueError, len,
@ -310,8 +323,10 @@ class DistanceTest(TestCase):
cities = self.get_names(qs) cities = self.get_names(qs)
self.assertEqual(cities, ['Adelaide', 'Hobart', 'Shellharbour', 'Thirroul']) self.assertEqual(cities, ['Adelaide', 'Hobart', 'Shellharbour', 'Thirroul'])
def test06_area(self): def test_area(self):
"Testing the `area` GeoQuerySet method." """
Test the `area` GeoQuerySet method.
"""
# Reference queries: # Reference queries:
# SELECT ST_Area(poly) FROM distapp_southtexaszipcode; # SELECT ST_Area(poly) FROM distapp_southtexaszipcode;
area_sq_m = [5437908.90234375, 10183031.4389648, 11254471.0073242, 9881708.91772461] area_sq_m = [5437908.90234375, 10183031.4389648, 11254471.0073242, 9881708.91772461]
@ -321,8 +336,10 @@ class DistanceTest(TestCase):
for i, z in enumerate(SouthTexasZipcode.objects.area()): for i, z in enumerate(SouthTexasZipcode.objects.area()):
self.assertAlmostEqual(area_sq_m[i], z.area.sq_m, tol) self.assertAlmostEqual(area_sq_m[i], z.area.sq_m, tol)
def test07_length(self): def test_length(self):
"Testing the `length` GeoQuerySet method." """
Test the `length` GeoQuerySet method.
"""
# Reference query (should use `length_spheroid`). # Reference query (should use `length_spheroid`).
# SELECT ST_length_spheroid(ST_GeomFromText('<wkt>', 4326) 'SPHEROID["WGS 84",6378137,298.257223563, AUTHORITY["EPSG","7030"]]'); # SELECT ST_length_spheroid(ST_GeomFromText('<wkt>', 4326) 'SPHEROID["WGS 84",6378137,298.257223563, AUTHORITY["EPSG","7030"]]');
len_m1 = 473504.769553813 len_m1 = 473504.769553813
@ -333,8 +350,7 @@ class DistanceTest(TestCase):
self.assertRaises(ValueError, Interstate.objects.length) self.assertRaises(ValueError, Interstate.objects.length)
else: else:
qs = Interstate.objects.length() qs = Interstate.objects.length()
if oracle: tol = 2 tol = 2 if oracle else 3
else: tol = 3
self.assertAlmostEqual(len_m1, qs[0].length.m, tol) self.assertAlmostEqual(len_m1, qs[0].length.m, tol)
# Now doing length on a projected coordinate system. # Now doing length on a projected coordinate system.
@ -342,13 +358,14 @@ class DistanceTest(TestCase):
self.assertAlmostEqual(len_m2, i10.length.m, 2) self.assertAlmostEqual(len_m2, i10.length.m, 2)
@no_spatialite @no_spatialite
def test08_perimeter(self): def test_perimeter(self):
"Testing the `perimeter` GeoQuerySet method." """
Test the `perimeter` GeoQuerySet method.
"""
# Reference query: # Reference query:
# SELECT ST_Perimeter(distapp_southtexaszipcode.poly) FROM distapp_southtexaszipcode; # SELECT ST_Perimeter(distapp_southtexaszipcode.poly) FROM distapp_southtexaszipcode;
perim_m = [18404.3550889361, 15627.2108551001, 20632.5588368978, 17094.5996143697] perim_m = [18404.3550889361, 15627.2108551001, 20632.5588368978, 17094.5996143697]
if oracle: tol = 2 tol = 2 if oracle else 7
else: tol = 7
for i, z in enumerate(SouthTexasZipcode.objects.perimeter()): for i, z in enumerate(SouthTexasZipcode.objects.perimeter()):
self.assertAlmostEqual(perim_m[i], z.perimeter.m, tol) self.assertAlmostEqual(perim_m[i], z.perimeter.m, tol)
@ -356,13 +373,15 @@ class DistanceTest(TestCase):
for i, c in enumerate(SouthTexasCity.objects.perimeter(model_att='perim')): for i, c in enumerate(SouthTexasCity.objects.perimeter(model_att='perim')):
self.assertEqual(0, c.perim.m) self.assertEqual(0, c.perim.m)
def test09_measurement_null_fields(self): def test_measurement_null_fields(self):
"Testing the measurement GeoQuerySet methods on fields with NULL values." """
Test the measurement GeoQuerySet methods on fields with NULL values.
"""
# Creating SouthTexasZipcode w/NULL value. # Creating SouthTexasZipcode w/NULL value.
SouthTexasZipcode.objects.create(name='78212') SouthTexasZipcode.objects.create(name='78212')
# Performing distance/area queries against the NULL PolygonField, # Performing distance/area queries against the NULL PolygonField,
# and ensuring the result of the operations is None. # and ensuring the result of the operations is None.
htown = SouthTexasCity.objects.get(name='Downtown Houston') htown = SouthTexasCity.objects.get(name='Downtown Houston')
z = SouthTexasZipcode.objects.distance(htown.point).area().get(name='78212') z = SouthTexasZipcode.objects.distance(htown.point).area().get(name='78212')
self.assertEqual(None, z.distance) self.assertIsNone(z.distance)
self.assertEqual(None, z.area) self.assertIsNone(z.area)