2009-12-23 21:45:08 +01:00
|
|
|
"""
|
2014-07-26 22:55:31 +02:00
|
|
|
Tests for geography support in PostGIS
|
2009-12-23 21:45:08 +01:00
|
|
|
"""
|
2024-01-26 12:45:07 +01:00
|
|
|
|
2009-12-25 15:37:57 +01:00
|
|
|
import os
|
2011-10-17 20:45:22 +02:00
|
|
|
|
2016-04-10 19:55:29 +02:00
|
|
|
from django.contrib.gis.db import models
|
2015-01-19 16:09:41 +01:00
|
|
|
from django.contrib.gis.db.models.functions import Area, Distance
|
2009-12-23 21:45:08 +01:00
|
|
|
from django.contrib.gis.measure import D
|
2023-02-02 16:23:16 +01:00
|
|
|
from django.core.exceptions import ValidationError
|
2017-10-06 18:47:08 +02:00
|
|
|
from django.db import NotSupportedError, connection
|
2016-04-10 19:55:29 +02:00
|
|
|
from django.db.models.functions import Cast
|
2016-11-07 13:00:40 +01:00
|
|
|
from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
|
2023-02-02 16:23:16 +01:00
|
|
|
from django.test.utils import CaptureQueriesContext
|
2011-10-17 20:45:22 +02:00
|
|
|
|
2020-11-14 15:08:30 +01:00
|
|
|
from ..utils import FuncTestMixin
|
2023-02-02 16:23:16 +01:00
|
|
|
from .models import City, CityUnique, County, Zipcode
|
2011-10-17 20:45:22 +02:00
|
|
|
|
2009-12-23 21:45:08 +01:00
|
|
|
|
|
|
|
class GeographyTest(TestCase):
|
2014-07-26 19:15:54 +02:00
|
|
|
fixtures = ["initial"]
|
2009-12-23 21:45:08 +01:00
|
|
|
|
|
|
|
def test01_fixture_load(self):
|
|
|
|
"Ensure geography features loaded properly."
|
|
|
|
self.assertEqual(8, City.objects.count())
|
|
|
|
|
2014-08-21 18:47:57 +02:00
|
|
|
@skipUnlessDBFeature("supports_distances_lookups", "supports_distance_geodetic")
|
2009-12-23 21:45:08 +01:00
|
|
|
def test02_distance_lookup(self):
|
2016-11-07 13:00:40 +01:00
|
|
|
"Testing distance lookup support on non-point geography fields."
|
2009-12-23 21:45:08 +01:00
|
|
|
z = Zipcode.objects.get(code="77002")
|
2009-12-25 15:37:57 +01:00
|
|
|
cities1 = list(
|
|
|
|
City.objects.filter(point__distance_lte=(z.poly, D(mi=500)))
|
|
|
|
.order_by("name")
|
|
|
|
.values_list("name", flat=True)
|
2022-02-03 20:24:19 +01:00
|
|
|
)
|
2009-12-25 15:37:57 +01:00
|
|
|
cities2 = list(
|
|
|
|
City.objects.filter(point__dwithin=(z.poly, D(mi=500)))
|
|
|
|
.order_by("name")
|
|
|
|
.values_list("name", flat=True)
|
2022-02-03 20:24:19 +01:00
|
|
|
)
|
2009-12-25 15:37:57 +01:00
|
|
|
for cities in [cities1, cities2]:
|
|
|
|
self.assertEqual(["Dallas", "Houston", "Oklahoma City"], cities)
|
2009-12-23 21:45:08 +01:00
|
|
|
|
2023-02-02 16:23:16 +01:00
|
|
|
@skipUnlessDBFeature("supports_geography", "supports_geometry_field_unique_index")
|
|
|
|
def test_geography_unique(self):
|
2009-12-23 21:45:08 +01:00
|
|
|
"""
|
2023-02-02 16:23:16 +01:00
|
|
|
Cast geography fields to geometry type when validating uniqueness to
|
|
|
|
remove the reliance on unavailable ~= operator.
|
2022-02-04 08:08:27 +01:00
|
|
|
"""
|
2023-02-02 16:23:16 +01:00
|
|
|
htown = City.objects.get(name="Houston")
|
|
|
|
CityUnique.objects.create(point=htown.point)
|
|
|
|
duplicate = CityUnique(point=htown.point)
|
|
|
|
msg = "City unique with this Point already exists."
|
|
|
|
with self.assertRaisesMessage(ValidationError, msg):
|
|
|
|
duplicate.validate_unique()
|
2009-12-25 15:37:57 +01:00
|
|
|
|
2023-02-02 16:23:16 +01:00
|
|
|
@skipUnlessDBFeature("supports_geography")
|
|
|
|
def test_operators_functions_unavailable_for_geography(self):
|
|
|
|
"""
|
|
|
|
Geography fields are cast to geometry if the relevant operators or
|
|
|
|
functions are not available.
|
|
|
|
"""
|
|
|
|
z = Zipcode.objects.get(code="77002")
|
|
|
|
point_field = "%s.%s::geometry" % (
|
|
|
|
connection.ops.quote_name(City._meta.db_table),
|
|
|
|
connection.ops.quote_name("point"),
|
|
|
|
)
|
|
|
|
# ST_Within.
|
|
|
|
qs = City.objects.filter(point__within=z.poly)
|
|
|
|
with CaptureQueriesContext(connection) as ctx:
|
|
|
|
self.assertEqual(qs.count(), 1)
|
|
|
|
self.assertIn(f"ST_Within({point_field}", ctx.captured_queries[0]["sql"])
|
|
|
|
# @ operator.
|
|
|
|
qs = City.objects.filter(point__contained=z.poly)
|
|
|
|
with CaptureQueriesContext(connection) as ctx:
|
|
|
|
self.assertEqual(qs.count(), 1)
|
|
|
|
self.assertIn(f"{point_field} @", ctx.captured_queries[0]["sql"])
|
|
|
|
# ~= operator.
|
2010-09-12 04:07:04 +02:00
|
|
|
htown = City.objects.get(name="Houston")
|
2023-02-02 16:23:16 +01:00
|
|
|
qs = City.objects.filter(point__exact=htown.point)
|
|
|
|
with CaptureQueriesContext(connection) as ctx:
|
|
|
|
self.assertEqual(qs.count(), 1)
|
|
|
|
self.assertIn(f"{point_field} ~=", ctx.captured_queries[0]["sql"])
|
2010-09-12 04:07:04 +02:00
|
|
|
|
2009-12-25 15:37:57 +01:00
|
|
|
def test05_geography_layermapping(self):
|
|
|
|
"Testing LayerMapping support on models with geography fields."
|
|
|
|
# There is a similar test in `layermap` that uses the same data set,
|
|
|
|
# but the County model here is a bit different.
|
|
|
|
from django.contrib.gis.utils import LayerMapping
|
|
|
|
|
|
|
|
# Getting the shapefile and mapping dictionary.
|
2017-01-20 14:01:02 +01:00
|
|
|
shp_path = os.path.realpath(
|
|
|
|
os.path.join(os.path.dirname(__file__), "..", "data")
|
2022-02-03 20:24:19 +01:00
|
|
|
)
|
2009-12-25 15:37:57 +01:00
|
|
|
co_shp = os.path.join(shp_path, "counties", "counties.shp")
|
2019-01-03 00:18:19 +01:00
|
|
|
co_mapping = {
|
|
|
|
"name": "Name",
|
|
|
|
"state": "State",
|
|
|
|
"mpoly": "MULTIPOLYGON",
|
|
|
|
}
|
2009-12-25 15:37:57 +01:00
|
|
|
# Reference county names, number of polygons, and state names.
|
|
|
|
names = ["Bexar", "Galveston", "Harris", "Honolulu", "Pueblo"]
|
2013-11-02 22:02:56 +01:00
|
|
|
num_polys = [1, 2, 1, 19, 1] # Number of polygons for each.
|
2009-12-25 15:37:57 +01:00
|
|
|
st_names = ["Texas", "Texas", "Texas", "Hawaii", "Colorado"]
|
|
|
|
|
|
|
|
lm = LayerMapping(County, co_shp, co_mapping, source_srs=4269, unique="name")
|
|
|
|
lm.save(silent=True, strict=True)
|
|
|
|
|
|
|
|
for c, name, num_poly, state in zip(
|
|
|
|
County.objects.order_by("name"), names, num_polys, st_names
|
|
|
|
):
|
|
|
|
self.assertEqual(4326, c.mpoly.srid)
|
|
|
|
self.assertEqual(num_poly, len(c.mpoly))
|
|
|
|
self.assertEqual(name, c.name)
|
|
|
|
self.assertEqual(state, c.state)
|
2010-10-12 19:13:27 +02:00
|
|
|
|
2015-01-19 16:09:41 +01:00
|
|
|
|
2017-09-11 17:56:39 +02:00
|
|
|
class GeographyFunctionTests(FuncTestMixin, TestCase):
|
2015-01-19 16:09:41 +01:00
|
|
|
fixtures = ["initial"]
|
|
|
|
|
2016-04-10 19:55:29 +02:00
|
|
|
@skipUnlessDBFeature("supports_extent_aggr")
|
|
|
|
def test_cast_aggregate(self):
|
|
|
|
"""
|
|
|
|
Cast a geography to a geometry field for an aggregate function that
|
|
|
|
expects a geometry input.
|
|
|
|
"""
|
2020-11-09 01:55:50 +01:00
|
|
|
if not connection.features.supports_geography:
|
2016-04-10 19:55:29 +02:00
|
|
|
self.skipTest("This test needs geography support")
|
|
|
|
expected = (
|
|
|
|
-96.8016128540039,
|
|
|
|
29.7633724212646,
|
|
|
|
-95.3631439208984,
|
|
|
|
32.782058715820,
|
|
|
|
)
|
|
|
|
res = City.objects.filter(name__in=("Houston", "Dallas")).aggregate(
|
|
|
|
extent=models.Extent(Cast("point", models.PointField()))
|
|
|
|
)
|
|
|
|
for val, exp in zip(res["extent"], expected):
|
|
|
|
self.assertAlmostEqual(exp, val, 4)
|
|
|
|
|
2015-01-19 16:09:41 +01:00
|
|
|
@skipUnlessDBFeature("has_Distance_function", "supports_distance_geodetic")
|
|
|
|
def test_distance_function(self):
|
|
|
|
"""
|
|
|
|
Testing Distance() support on non-point geography fields.
|
|
|
|
"""
|
2020-11-14 15:08:30 +01:00
|
|
|
if connection.ops.oracle:
|
2015-11-19 11:57:49 +01:00
|
|
|
ref_dists = [0, 4899.68, 8081.30, 9115.15]
|
2020-11-14 15:08:30 +01:00
|
|
|
elif connection.ops.spatialite:
|
2021-04-03 16:23:19 +02:00
|
|
|
if connection.ops.spatial_version < (5,):
|
|
|
|
# SpatiaLite < 5 returns non-zero distance for polygons and points
|
|
|
|
# covered by that polygon.
|
|
|
|
ref_dists = [326.61, 4899.68, 8081.30, 9115.15]
|
|
|
|
else:
|
|
|
|
ref_dists = [0, 4899.68, 8081.30, 9115.15]
|
2015-11-19 11:57:49 +01:00
|
|
|
else:
|
|
|
|
ref_dists = [0, 4891.20, 8071.64, 9123.95]
|
2015-01-19 16:09:41 +01:00
|
|
|
htown = City.objects.get(name="Houston")
|
2017-04-01 15:47:49 +02:00
|
|
|
qs = Zipcode.objects.annotate(
|
|
|
|
distance=Distance("poly", htown.point),
|
|
|
|
distance2=Distance(htown.point, "poly"),
|
|
|
|
)
|
2015-01-19 16:09:41 +01:00
|
|
|
for z, ref in zip(qs, ref_dists):
|
|
|
|
self.assertAlmostEqual(z.distance.m, ref, 2)
|
2017-04-01 15:47:49 +02:00
|
|
|
|
2020-11-14 15:08:30 +01:00
|
|
|
if connection.ops.postgis:
|
2017-04-01 15:47:49 +02:00
|
|
|
# PostGIS casts geography to geometry when distance2 is calculated.
|
|
|
|
ref_dists = [0, 4899.68, 8081.30, 9115.15]
|
|
|
|
for z, ref in zip(qs, ref_dists):
|
|
|
|
self.assertAlmostEqual(z.distance2.m, ref, 2)
|
|
|
|
|
2020-11-14 15:08:30 +01:00
|
|
|
if not connection.ops.spatialite:
|
2016-11-16 13:07:36 +01:00
|
|
|
# Distance function combined with a lookup.
|
|
|
|
hzip = Zipcode.objects.get(code="77002")
|
|
|
|
self.assertEqual(qs.get(distance__lte=0), hzip)
|
2015-01-19 16:09:41 +01:00
|
|
|
|
2016-11-16 13:07:36 +01:00
|
|
|
@skipUnlessDBFeature("has_Area_function", "supports_area_geodetic")
|
2015-01-19 16:09:41 +01:00
|
|
|
def test_geography_area(self):
|
|
|
|
"""
|
|
|
|
Testing that Area calculations work on geography columns.
|
|
|
|
"""
|
|
|
|
# SELECT ST_Area(poly) FROM geogapp_zipcode WHERE code='77002';
|
|
|
|
z = Zipcode.objects.annotate(area=Area("poly")).get(code="77002")
|
2016-01-28 13:44:55 +01:00
|
|
|
# Round to the nearest thousand as possible values (depending on
|
|
|
|
# the database and geolib) include 5439084, 5439100, 5439101.
|
|
|
|
rounded_value = z.area.sq_m
|
|
|
|
rounded_value -= z.area.sq_m % 1000
|
|
|
|
self.assertEqual(rounded_value, 5439000)
|
2016-11-16 13:07:36 +01:00
|
|
|
|
|
|
|
@skipUnlessDBFeature("has_Area_function")
|
|
|
|
@skipIfDBFeature("supports_area_geodetic")
|
|
|
|
def test_geodetic_area_raises_if_not_supported(self):
|
2017-10-06 18:47:08 +02:00
|
|
|
with self.assertRaisesMessage(
|
|
|
|
NotSupportedError, "Area on geodetic coordinate systems not supported."
|
|
|
|
):
|
2016-11-16 13:07:36 +01:00
|
|
|
Zipcode.objects.annotate(area=Area("poly")).get(code="77002")
|