1
0
mirror of https://github.com/django/django.git synced 2025-10-31 09:41:08 +00:00

Refs #33476 -- Reformatted code with Black.

This commit is contained in:
django-bot
2022-02-03 20:24:19 +01:00
committed by Mariusz Felisiak
parent f68fa8b45d
commit 9c19aff7c7
1992 changed files with 139577 additions and 96284 deletions

View File

@@ -16,7 +16,7 @@ class City3D(NamedModel):
pointg = models.PointField(dim=3, geography=True)
class Meta:
required_db_features = {'supports_3d_storage'}
required_db_features = {"supports_3d_storage"}
class Interstate2D(NamedModel):
@@ -27,7 +27,7 @@ class Interstate3D(NamedModel):
line = models.LineStringField(dim=3, srid=4269)
class Meta:
required_db_features = {'supports_3d_storage'}
required_db_features = {"supports_3d_storage"}
class InterstateProj2D(NamedModel):
@@ -38,7 +38,7 @@ class InterstateProj3D(NamedModel):
line = models.LineStringField(dim=3, srid=32140)
class Meta:
required_db_features = {'supports_3d_storage'}
required_db_features = {"supports_3d_storage"}
class Polygon2D(NamedModel):
@@ -49,11 +49,10 @@ class Polygon3D(NamedModel):
poly = models.PolygonField(dim=3, srid=32140)
class Meta:
required_db_features = {'supports_3d_storage'}
required_db_features = {"supports_3d_storage"}
class SimpleModel(models.Model):
class Meta:
abstract = True
@@ -66,11 +65,11 @@ class Point3D(SimpleModel):
point = models.PointField(dim=3)
class Meta:
required_db_features = {'supports_3d_storage'}
required_db_features = {"supports_3d_storage"}
class MultiPoint3D(SimpleModel):
mpoint = models.MultiPointField(dim=3)
class Meta:
required_db_features = {'supports_3d_storage'}
required_db_features = {"supports_3d_storage"}

View File

@@ -3,32 +3,45 @@ import re
from django.contrib.gis.db.models import Extent3D, Union
from django.contrib.gis.db.models.functions import (
AsGeoJSON, AsKML, Length, Perimeter, Scale, Translate,
AsGeoJSON,
AsKML,
Length,
Perimeter,
Scale,
Translate,
)
from django.contrib.gis.geos import GEOSGeometry, LineString, Point, Polygon
from django.test import TestCase, skipUnlessDBFeature
from ..utils import FuncTestMixin
from .models import (
City3D, Interstate2D, Interstate3D, InterstateProj2D, InterstateProj3D,
MultiPoint3D, Point2D, Point3D, Polygon2D, Polygon3D,
City3D,
Interstate2D,
Interstate3D,
InterstateProj2D,
InterstateProj3D,
MultiPoint3D,
Point2D,
Point3D,
Polygon2D,
Polygon3D,
)
data_path = os.path.realpath(os.path.join(os.path.dirname(__file__), '..', 'data'))
city_file = os.path.join(data_path, 'cities', 'cities.shp')
vrt_file = os.path.join(data_path, 'test_vrt', 'test_vrt.vrt')
data_path = os.path.realpath(os.path.join(os.path.dirname(__file__), "..", "data"))
city_file = os.path.join(data_path, "cities", "cities.shp")
vrt_file = os.path.join(data_path, "test_vrt", "test_vrt.vrt")
# The coordinates of each city, with Z values corresponding to their
# altitude in meters.
city_data = (
('Houston', (-95.363151, 29.763374, 18)),
('Dallas', (-96.801611, 32.782057, 147)),
('Oklahoma City', (-97.521157, 34.464642, 380)),
('Wellington', (174.783117, -41.315268, 14)),
('Pueblo', (-104.609252, 38.255001, 1433)),
('Lawrence', (-95.235060, 38.971823, 251)),
('Chicago', (-87.650175, 41.850385, 181)),
('Victoria', (-123.305196, 48.462611, 15)),
("Houston", (-95.363151, 29.763374, 18)),
("Dallas", (-96.801611, 32.782057, 147)),
("Oklahoma City", (-97.521157, 34.464642, 380)),
("Wellington", (174.783117, -41.315268, 14)),
("Pueblo", (-104.609252, 38.255001, 1433)),
("Lawrence", (-95.235060, 38.971823, 251)),
("Chicago", (-87.650175, 41.850385, 181)),
("Victoria", (-123.305196, 48.462611, 15)),
)
# Reference mapping of city name to its altitude (Z value).
@@ -37,32 +50,53 @@ city_dict = {name: coords for name, coords in city_data}
# 3D freeway data derived from the National Elevation Dataset:
# http://seamless.usgs.gov/products/9arc.php
interstate_data = (
('I-45',
'LINESTRING(-95.3708481 29.7765870 11.339,-95.3694580 29.7787980 4.536,'
'-95.3690305 29.7797359 9.762,-95.3691886 29.7812450 12.448,'
'-95.3696447 29.7850144 10.457,-95.3702511 29.7868518 9.418,'
'-95.3706724 29.7881286 14.858,-95.3711632 29.7896157 15.386,'
'-95.3714525 29.7936267 13.168,-95.3717848 29.7955007 15.104,'
'-95.3717719 29.7969804 16.516,-95.3717305 29.7982117 13.923,'
'-95.3717254 29.8000778 14.385,-95.3719875 29.8013539 15.160,'
'-95.3720575 29.8026785 15.544,-95.3721321 29.8040912 14.975,'
'-95.3722074 29.8050998 15.688,-95.3722779 29.8060430 16.099,'
'-95.3733818 29.8076750 15.197,-95.3741563 29.8103686 17.268,'
'-95.3749458 29.8129927 19.857,-95.3763564 29.8144557 15.435)',
(11.339, 4.536, 9.762, 12.448, 10.457, 9.418, 14.858,
15.386, 13.168, 15.104, 16.516, 13.923, 14.385, 15.16,
15.544, 14.975, 15.688, 16.099, 15.197, 17.268, 19.857,
15.435),
),
(
"I-45",
"LINESTRING(-95.3708481 29.7765870 11.339,-95.3694580 29.7787980 4.536,"
"-95.3690305 29.7797359 9.762,-95.3691886 29.7812450 12.448,"
"-95.3696447 29.7850144 10.457,-95.3702511 29.7868518 9.418,"
"-95.3706724 29.7881286 14.858,-95.3711632 29.7896157 15.386,"
"-95.3714525 29.7936267 13.168,-95.3717848 29.7955007 15.104,"
"-95.3717719 29.7969804 16.516,-95.3717305 29.7982117 13.923,"
"-95.3717254 29.8000778 14.385,-95.3719875 29.8013539 15.160,"
"-95.3720575 29.8026785 15.544,-95.3721321 29.8040912 14.975,"
"-95.3722074 29.8050998 15.688,-95.3722779 29.8060430 16.099,"
"-95.3733818 29.8076750 15.197,-95.3741563 29.8103686 17.268,"
"-95.3749458 29.8129927 19.857,-95.3763564 29.8144557 15.435)",
(
11.339,
4.536,
9.762,
12.448,
10.457,
9.418,
14.858,
15.386,
13.168,
15.104,
16.516,
13.923,
14.385,
15.16,
15.544,
14.975,
15.688,
16.099,
15.197,
17.268,
19.857,
15.435,
),
),
)
# Bounding box polygon for inner-loop of Houston (in projected coordinate
# system 32140), with elevation values from the National Elevation Dataset
# (see above).
bbox_data = (
'POLYGON((941527.97 4225693.20,962596.48 4226349.75,963152.57 4209023.95,'
'942051.75 4208366.38,941527.97 4225693.20))',
(21.71, 13.21, 9.12, 16.40, 21.71)
"POLYGON((941527.97 4225693.20,962596.48 4226349.75,963152.57 4209023.95,"
"942051.75 4208366.38,941527.97 4225693.20))",
(21.71, 13.21, 9.12, 16.40, 21.71),
)
@@ -83,15 +117,19 @@ class Geo3DLoadingHelper:
def _load_city_data(self):
for name, pnt_data in city_data:
City3D.objects.create(
name=name, point=Point(*pnt_data, srid=4326), pointg=Point(*pnt_data, srid=4326),
name=name,
point=Point(*pnt_data, srid=4326),
pointg=Point(*pnt_data, srid=4326),
)
def _load_polygon_data(self):
bbox_wkt, bbox_z = bbox_data
bbox_2d = GEOSGeometry(bbox_wkt, srid=32140)
bbox_3d = Polygon(tuple((x, y, z) for (x, y), z in zip(bbox_2d[0].coords, bbox_z)), srid=32140)
Polygon2D.objects.create(name='2D BBox', poly=bbox_2d)
Polygon3D.objects.create(name='3D BBox', poly=bbox_3d)
bbox_3d = Polygon(
tuple((x, y, z) for (x, y), z in zip(bbox_2d[0].coords, bbox_z)), srid=32140
)
Polygon2D.objects.create(name="2D BBox", poly=bbox_2d)
Polygon3D.objects.create(name="3D BBox", poly=bbox_3d)
@skipUnlessDBFeature("supports_3d_storage")
@@ -132,7 +170,7 @@ class Geo3DTest(Geo3DLoadingHelper, TestCase):
Test the creation of polygon 3D models.
"""
self._load_polygon_data()
p3d = Polygon3D.objects.get(name='3D BBox')
p3d = Polygon3D.objects.get(name="3D BBox")
self.assertTrue(p3d.poly.hasz)
self.assertIsInstance(p3d.poly, Polygon)
self.assertEqual(p3d.poly.srid, 32140)
@@ -144,8 +182,8 @@ class Geo3DTest(Geo3DLoadingHelper, TestCase):
# Import here as GDAL is required for those imports
from django.contrib.gis.utils import LayerMapError, LayerMapping
point_mapping = {'point': 'POINT'}
mpoint_mapping = {'mpoint': 'MULTIPOINT'}
point_mapping = {"point": "POINT"}
mpoint_mapping = {"mpoint": "MULTIPOINT"}
# The VRT is 3D, but should still be able to map sans the Z.
lm = LayerMapping(Point2D, vrt_file, point_mapping, transform=False)
@@ -177,12 +215,12 @@ class Geo3DTest(Geo3DLoadingHelper, TestCase):
# `SELECT ST_AsText(ST_Union(point)) FROM geo3d_city3d;`
self._load_city_data()
ref_ewkt = (
'SRID=4326;MULTIPOINT(-123.305196 48.462611 15,-104.609252 38.255001 1433,'
'-97.521157 34.464642 380,-96.801611 32.782057 147,-95.363151 29.763374 18,'
'-95.23506 38.971823 251,-87.650175 41.850385 181,174.783117 -41.315268 14)'
"SRID=4326;MULTIPOINT(-123.305196 48.462611 15,-104.609252 38.255001 1433,"
"-97.521157 34.464642 380,-96.801611 32.782057 147,-95.363151 29.763374 18,"
"-95.23506 38.971823 251,-87.650175 41.850385 181,174.783117 -41.315268 14)"
)
ref_union = GEOSGeometry(ref_ewkt)
union = City3D.objects.aggregate(Union('point'))['point__union']
union = City3D.objects.aggregate(Union("point"))["point__union"]
self.assertTrue(union.hasz)
# Ordering of points in the resulting geometry may vary between implementations
self.assertEqual({p.ewkt for p in ref_union}, {p.ewkt for p in union})
@@ -195,14 +233,16 @@ class Geo3DTest(Geo3DLoadingHelper, TestCase):
self._load_city_data()
# `SELECT ST_Extent3D(point) FROM geo3d_city3d;`
ref_extent3d = (-123.305196, -41.315268, 14, 174.783117, 48.462611, 1433)
extent = City3D.objects.aggregate(Extent3D('point'))['point__extent3d']
extent = City3D.objects.aggregate(Extent3D("point"))["point__extent3d"]
def check_extent3d(extent3d, tol=6):
for ref_val, ext_val in zip(ref_extent3d, extent3d):
self.assertAlmostEqual(ref_val, ext_val, tol)
check_extent3d(extent)
self.assertIsNone(City3D.objects.none().aggregate(Extent3D('point'))['point__extent3d'])
self.assertIsNone(
City3D.objects.none().aggregate(Extent3D("point"))["point__extent3d"]
)
@skipUnlessDBFeature("supports_3d_functions")
@@ -212,10 +252,12 @@ class Geo3DFunctionsTests(FuncTestMixin, Geo3DLoadingHelper, TestCase):
Test KML() function with Z values.
"""
self._load_city_data()
h = City3D.objects.annotate(kml=AsKML('point', precision=6)).get(name='Houston')
h = City3D.objects.annotate(kml=AsKML("point", precision=6)).get(name="Houston")
# KML should be 3D.
# `SELECT ST_AsKML(point, 6) FROM geo3d_city3d WHERE name = 'Houston';`
ref_kml_regex = re.compile(r'^<Point><coordinates>-95.363\d+,29.763\d+,18</coordinates></Point>$')
ref_kml_regex = re.compile(
r"^<Point><coordinates>-95.363\d+,29.763\d+,18</coordinates></Point>$"
)
self.assertTrue(ref_kml_regex.match(h.kml))
def test_geojson(self):
@@ -223,10 +265,14 @@ class Geo3DFunctionsTests(FuncTestMixin, Geo3DLoadingHelper, TestCase):
Test GeoJSON() function with Z values.
"""
self._load_city_data()
h = City3D.objects.annotate(geojson=AsGeoJSON('point', precision=6)).get(name='Houston')
h = City3D.objects.annotate(geojson=AsGeoJSON("point", precision=6)).get(
name="Houston"
)
# GeoJSON should be 3D
# `SELECT ST_AsGeoJSON(point, 6) FROM geo3d_city3d WHERE name='Houston';`
ref_json_regex = re.compile(r'^{"type":"Point","coordinates":\[-95.363151,29.763374,18(\.0+)?\]}$')
ref_json_regex = re.compile(
r'^{"type":"Point","coordinates":\[-95.363151,29.763374,18(\.0+)?\]}$'
)
self.assertTrue(ref_json_regex.match(h.geojson))
def test_perimeter(self):
@@ -239,9 +285,13 @@ class Geo3DFunctionsTests(FuncTestMixin, Geo3DLoadingHelper, TestCase):
ref_perim_3d = 76859.2620451
ref_perim_2d = 76859.2577803
tol = 6
poly2d = Polygon2D.objects.annotate(perimeter=Perimeter('poly')).get(name='2D BBox')
poly2d = Polygon2D.objects.annotate(perimeter=Perimeter("poly")).get(
name="2D BBox"
)
self.assertAlmostEqual(ref_perim_2d, poly2d.perimeter.m, tol)
poly3d = Polygon3D.objects.annotate(perimeter=Perimeter('poly')).get(name='3D BBox')
poly3d = Polygon3D.objects.annotate(perimeter=Perimeter("poly")).get(
name="3D BBox"
)
self.assertAlmostEqual(ref_perim_3d, poly3d.perimeter.m, tol)
def test_length(self):
@@ -256,9 +306,9 @@ class Geo3DFunctionsTests(FuncTestMixin, Geo3DLoadingHelper, TestCase):
tol = 3
ref_length_2d = 4368.1721949481
ref_length_3d = 4368.62547052088
inter2d = Interstate2D.objects.annotate(length=Length('line')).get(name='I-45')
inter2d = Interstate2D.objects.annotate(length=Length("line")).get(name="I-45")
self.assertAlmostEqual(ref_length_2d, inter2d.length.m, tol)
inter3d = Interstate3D.objects.annotate(length=Length('line')).get(name='I-45')
inter3d = Interstate3D.objects.annotate(length=Length("line")).get(name="I-45")
self.assertAlmostEqual(ref_length_3d, inter3d.length.m, tol)
# Making sure `ST_Length3D` is used on for a projected
@@ -267,9 +317,13 @@ class Geo3DFunctionsTests(FuncTestMixin, Geo3DLoadingHelper, TestCase):
ref_length_2d = 4367.71564892392
# `SELECT ST_Length3D(line) FROM geo3d_interstateproj3d;`
ref_length_3d = 4368.16897234101
inter2d = InterstateProj2D.objects.annotate(length=Length('line')).get(name='I-45')
inter2d = InterstateProj2D.objects.annotate(length=Length("line")).get(
name="I-45"
)
self.assertAlmostEqual(ref_length_2d, inter2d.length.m, tol)
inter3d = InterstateProj3D.objects.annotate(length=Length('line')).get(name='I-45')
inter3d = InterstateProj3D.objects.annotate(length=Length("line")).get(
name="I-45"
)
self.assertAlmostEqual(ref_length_3d, inter3d.length.m, tol)
def test_scale(self):
@@ -280,7 +334,7 @@ class Geo3DFunctionsTests(FuncTestMixin, Geo3DLoadingHelper, TestCase):
# Mapping of City name to reference Z values.
zscales = (-3, 4, 23)
for zscale in zscales:
for city in City3D.objects.annotate(scale=Scale('point', 1.0, 1.0, zscale)):
for city in City3D.objects.annotate(scale=Scale("point", 1.0, 1.0, zscale)):
self.assertEqual(city_dict[city.name][2] * zscale, city.scale.z)
def test_translate(self):
@@ -290,5 +344,7 @@ class Geo3DFunctionsTests(FuncTestMixin, Geo3DLoadingHelper, TestCase):
self._load_city_data()
ztranslations = (5.23, 23, -17)
for ztrans in ztranslations:
for city in City3D.objects.annotate(translate=Translate('point', 0, 0, ztrans)):
for city in City3D.objects.annotate(
translate=Translate("point", 0, 0, ztrans)
):
self.assertEqual(city_dict[city.name][2] + ztrans, city.translate.z)