| 1 |
import os |
| 2 |
import re |
| 3 |
|
| 4 |
from django.contrib.gis.db.models import Extent3D, Q, Union |
| 5 |
from django.contrib.gis.db.models.functions import ( |
| 6 |
AsGeoJSON, |
| 7 |
AsKML, |
| 8 |
Length, |
| 9 |
Perimeter, |
| 10 |
Scale, |
| 11 |
Translate, |
| 12 |
) |
| 13 |
from django.contrib.gis.geos import GEOSGeometry, LineString, Point, Polygon |
| 14 |
from django.test import TestCase, skipUnlessDBFeature |
| 15 |
|
| 16 |
from ..utils import FuncTestMixin |
| 17 |
from .models import ( |
| 18 |
City3D, |
| 19 |
Interstate2D, |
| 20 |
Interstate3D, |
| 21 |
InterstateProj2D, |
| 22 |
InterstateProj3D, |
| 23 |
MultiPoint3D, |
| 24 |
Point2D, |
| 25 |
Point3D, |
| 26 |
Polygon2D, |
| 27 |
Polygon3D, |
| 28 |
) |
| 29 |
|
| 30 |
data_path = os.path.realpath(os.path.join(os.path.dirname(__file__), "..", "data")) |
| 31 |
city_file = os.path.join(data_path, "cities", "cities.shp") |
| 32 |
vrt_file = os.path.join(data_path, "test_vrt", "test_vrt.vrt") |
| 33 |
|
| 34 |
# The coordinates of each city, with Z values corresponding to their |
| 35 |
# altitude in meters. |
| 36 |
city_data = ( |
| 37 |
("Houston", (-95.363151, 29.763374, 18)), |
| 38 |
("Dallas", (-96.801611, 32.782057, 147)), |
| 39 |
("Oklahoma City", (-97.521157, 34.464642, 380)), |
| 40 |
("Wellington", (174.783117, -41.315268, 14)), |
| 41 |
("Pueblo", (-104.609252, 38.255001, 1433)), |
| 42 |
("Lawrence", (-95.235060, 38.971823, 251)), |
| 43 |
("Chicago", (-87.650175, 41.850385, 181)), |
| 44 |
("Victoria", (-123.305196, 48.462611, 15)), |
| 45 |
) |
| 46 |
|
| 47 |
# Reference mapping of city name to its altitude (Z value). |
| 48 |
city_dict = {name: coords for name, coords in city_data} |
| 49 |
|
| 50 |
# 3D freeway data derived from the National Elevation Dataset: |
| 51 |
# http://seamless.usgs.gov/products/9arc.php |
| 52 |
interstate_data = ( |
| 53 |
( |
| 54 |
"I-45", |
| 55 |
"LINESTRING(-95.3708481 29.7765870 11.339,-95.3694580 29.7787980 4.536," |
| 56 |
"-95.3690305 29.7797359 9.762,-95.3691886 29.7812450 12.448," |
| 57 |
"-95.3696447 29.7850144 10.457,-95.3702511 29.7868518 9.418," |
| 58 |
"-95.3706724 29.7881286 14.858,-95.3711632 29.7896157 15.386," |
| 59 |
"-95.3714525 29.7936267 13.168,-95.3717848 29.7955007 15.104," |
| 60 |
"-95.3717719 29.7969804 16.516,-95.3717305 29.7982117 13.923," |
| 61 |
"-95.3717254 29.8000778 14.385,-95.3719875 29.8013539 15.160," |
| 62 |
"-95.3720575 29.8026785 15.544,-95.3721321 29.8040912 14.975," |
| 63 |
"-95.3722074 29.8050998 15.688,-95.3722779 29.8060430 16.099," |
| 64 |
"-95.3733818 29.8076750 15.197,-95.3741563 29.8103686 17.268," |
| 65 |
"-95.3749458 29.8129927 19.857,-95.3763564 29.8144557 15.435)", |
| 66 |
( |
| 67 |
11.339, |
| 68 |
4.536, |
| 69 |
9.762, |
| 70 |
12.448, |
| 71 |
10.457, |
| 72 |
9.418, |
| 73 |
14.858, |
| 74 |
15.386, |
| 75 |
13.168, |
| 76 |
15.104, |
| 77 |
16.516, |
| 78 |
13.923, |
| 79 |
14.385, |
| 80 |
15.16, |
| 81 |
15.544, |
| 82 |
14.975, |
| 83 |
15.688, |
| 84 |
16.099, |
| 85 |
15.197, |
| 86 |
17.268, |
| 87 |
19.857, |
| 88 |
15.435, |
| 89 |
), |
| 90 |
), |
| 91 |
) |
| 92 |
|
| 93 |
# Bounding box polygon for inner-loop of Houston (in projected coordinate |
| 94 |
# system 32140), with elevation values from the National Elevation Dataset |
| 95 |
# (see above). |
| 96 |
bbox_data = ( |
| 97 |
"POLYGON((941527.97 4225693.20,962596.48 4226349.75,963152.57 4209023.95," |
| 98 |
"942051.75 4208366.38,941527.97 4225693.20))", |
| 99 |
(21.71, 13.21, 9.12, 16.40, 21.71), |
| 100 |
) |
| 101 |
|
| 102 |
|
| 103 |
class Geo3DLoadingHelper: |
| 104 |
def _load_interstate_data(self): |
| 105 |
# Interstate (2D / 3D and Geographic/Projected variants) |
| 106 |
for name, line, exp_z in interstate_data: |
| 107 |
line_3d = GEOSGeometry(line, srid=4269) |
| 108 |
line_2d = LineString([coord[:2] for coord in line_3d.coords], srid=4269) |
| 109 |
|
| 110 |
# Creating a geographic and projected version of the |
| 111 |
# interstate in both 2D and 3D. |
| 112 |
Interstate3D.objects.create(name=name, line=line_3d) |
| 113 |
InterstateProj3D.objects.create(name=name, line=line_3d) |
| 114 |
Interstate2D.objects.create(name=name, line=line_2d) |
| 115 |
InterstateProj2D.objects.create(name=name, line=line_2d) |
| 116 |
|
| 117 |
def _load_city_data(self): |
| 118 |
for name, pnt_data in city_data: |
| 119 |
City3D.objects.create( |
| 120 |
name=name, |
| 121 |
point=Point(*pnt_data, srid=4326), |
| 122 |
pointg=Point(*pnt_data, srid=4326), |
| 123 |
) |
| 124 |
|
| 125 |
def _load_polygon_data(self): |
| 126 |
bbox_wkt, bbox_z = bbox_data |
| 127 |
bbox_2d = GEOSGeometry(bbox_wkt, srid=32140) |
| 128 |
bbox_3d = Polygon( |
| 129 |
tuple((x, y, z) for (x, y), z in zip(bbox_2d[0].coords, bbox_z)), srid=32140 |
| 130 |
) |
| 131 |
Polygon2D.objects.create(name="2D BBox", poly=bbox_2d) |
| 132 |
Polygon3D.objects.create(name="3D BBox", poly=bbox_3d) |
| 133 |
|
| 134 |
|
| 135 |
@skipUnlessDBFeature("supports_3d_storage") |
| 136 |
class Geo3DTest(Geo3DLoadingHelper, TestCase): |
| 137 |
""" |
| 138 |
Only a subset of the PostGIS routines are 3D-enabled, and this TestCase |
| 139 |
tries to test the features that can handle 3D and that are also |
| 140 |
available within GeoDjango. For more information, see the PostGIS docs |
| 141 |
on the routines that support 3D: |
| 142 |
|
| 143 |
https://postgis.net/docs/PostGIS_Special_Functions_Index.html#PostGIS_3D_Functions |
| 144 |
""" |
| 145 |
|
| 146 |
def test_3d_hasz(self): |
| 147 |
""" |
| 148 |
Make sure data is 3D and has expected Z values -- shouldn't change |
| 149 |
because of coordinate system. |
| 150 |
""" |
| 151 |
self._load_interstate_data() |
| 152 |
for name, line, exp_z in interstate_data: |
| 153 |
interstate = Interstate3D.objects.get(name=name) |
| 154 |
interstate_proj = InterstateProj3D.objects.get(name=name) |
| 155 |
for i in [interstate, interstate_proj]: |
| 156 |
self.assertTrue(i.line.hasz) |
| 157 |
self.assertEqual(exp_z, tuple(i.line.z)) |
| 158 |
|
| 159 |
self._load_city_data() |
| 160 |
for name, pnt_data in city_data: |
| 161 |
city = City3D.objects.get(name=name) |
| 162 |
# Testing both geometry and geography fields |
| 163 |
self.assertTrue(city.point.hasz) |
| 164 |
self.assertTrue(city.pointg.hasz) |
| 165 |
self.assertEqual(city.point.z, pnt_data[2]) |
| 166 |
self.assertEqual(city.pointg.z, pnt_data[2]) |
| 167 |
|
| 168 |
def test_3d_polygons(self): |
| 169 |
""" |
| 170 |
Test the creation of polygon 3D models. |
| 171 |
""" |
| 172 |
self._load_polygon_data() |
| 173 |
p3d = Polygon3D.objects.get(name="3D BBox") |
| 174 |
self.assertTrue(p3d.poly.hasz) |
| 175 |
self.assertIsInstance(p3d.poly, Polygon) |
| 176 |
self.assertEqual(p3d.poly.srid, 32140) |
| 177 |
|
| 178 |
def test_3d_layermapping(self): |
| 179 |
""" |
| 180 |
Testing LayerMapping on 3D models. |
| 181 |
""" |
| 182 |
# Import here as GDAL is required for those imports |
| 183 |
from django.contrib.gis.utils import LayerMapError, LayerMapping |
| 184 |
|
| 185 |
point_mapping = {"point": "POINT"} |
| 186 |
mpoint_mapping = {"mpoint": "MULTIPOINT"} |
| 187 |
|
| 188 |
# The VRT is 3D, but should still be able to map sans the Z. |
| 189 |
lm = LayerMapping(Point2D, vrt_file, point_mapping, transform=False) |
| 190 |
lm.save() |
| 191 |
self.assertEqual(3, Point2D.objects.count()) |
| 192 |
|
| 193 |
# The city shapefile is 2D, and won't be able to fill the coordinates |
| 194 |
# in the 3D model -- thus, a LayerMapError is raised. |
| 195 |
with self.assertRaises(LayerMapError): |
| 196 |
LayerMapping(Point3D, city_file, point_mapping, transform=False) |
| 197 |
|
| 198 |
# 3D model should take 3D data just fine. |
| 199 |
lm = LayerMapping(Point3D, vrt_file, point_mapping, transform=False) |
| 200 |
lm.save() |
| 201 |
self.assertEqual(3, Point3D.objects.count()) |
| 202 |
|
| 203 |
# Making sure LayerMapping.make_multi works right, by converting |
| 204 |
# a Point25D into a MultiPoint25D. |
| 205 |
lm = LayerMapping(MultiPoint3D, vrt_file, mpoint_mapping, transform=False) |
| 206 |
lm.save() |
| 207 |
self.assertEqual(3, MultiPoint3D.objects.count()) |
| 208 |
|
| 209 |
def test_bulk_create_point_field(self): |
| 210 |
objs = Point2D.objects.bulk_create([Point2D(), Point2D()]) |
| 211 |
self.assertEqual(len(objs), 2) |
| 212 |
|
| 213 |
@skipUnlessDBFeature("supports_3d_functions") |
| 214 |
def test_union(self): |
| 215 |
""" |
| 216 |
Testing the Union aggregate of 3D models. |
| 217 |
""" |
| 218 |
# PostGIS query that returned the reference EWKT for this test: |
| 219 |
# `SELECT ST_AsText(ST_Union(point)) FROM geo3d_city3d;` |
| 220 |
self._load_city_data() |
| 221 |
ref_ewkt = ( |
| 222 |
"SRID=4326;MULTIPOINT(-123.305196 48.462611 15,-104.609252 38.255001 1433," |
| 223 |
"-97.521157 34.464642 380,-96.801611 32.782057 147,-95.363151 29.763374 18," |
| 224 |
"-95.23506 38.971823 251,-87.650175 41.850385 181,174.783117 -41.315268 14)" |
| 225 |
) |
| 226 |
ref_union = GEOSGeometry(ref_ewkt) |
| 227 |
union = City3D.objects.aggregate(Union("point"))["point__union"] |
| 228 |
self.assertTrue(union.hasz) |
| 229 |
# Ordering of points in the resulting geometry may vary between implementations |
| 230 |
self.assertEqual({p.ewkt for p in ref_union}, {p.ewkt for p in union}) |
| 231 |
|
| 232 |
@skipUnlessDBFeature("supports_3d_functions") |
| 233 |
def test_extent(self): |
| 234 |
""" |
| 235 |
Testing the Extent3D aggregate for 3D models. |
| 236 |
""" |
| 237 |
self._load_city_data() |
| 238 |
# `SELECT ST_Extent3D(point) FROM geo3d_city3d;` |
| 239 |
ref_extent3d = (-123.305196, -41.315268, 14, 174.783117, 48.462611, 1433) |
| 240 |
extent = City3D.objects.aggregate(Extent3D("point"))["point__extent3d"] |
| 241 |
|
| 242 |
def check_extent3d(extent3d, tol=6): |
| 243 |
for ref_val, ext_val in zip(ref_extent3d, extent3d): |
| 244 |
self.assertAlmostEqual(ref_val, ext_val, tol) |
| 245 |
|
| 246 |
check_extent3d(extent) |
| 247 |
self.assertIsNone( |
| 248 |
City3D.objects.none().aggregate(Extent3D("point"))["point__extent3d"] |
| 249 |
) |
| 250 |
|
| 251 |
@skipUnlessDBFeature("supports_3d_functions") |
| 252 |
def test_extent3d_filter(self): |
| 253 |
self._load_city_data() |
| 254 |
extent3d = City3D.objects.aggregate( |
| 255 |
ll_cities=Extent3D("point", filter=Q(name__contains="ll")) |
| 256 |
)["ll_cities"] |
| 257 |
ref_extent3d = (-96.801611, -41.315268, 14.0, 174.783117, 32.782057, 147.0) |
| 258 |
for ref_val, ext_val in zip(ref_extent3d, extent3d): |
| 259 |
self.assertAlmostEqual(ref_val, ext_val, 6) |
| 260 |
|
| 261 |
|
| 262 |
@skipUnlessDBFeature("supports_3d_functions") |
| 263 |
class Geo3DFunctionsTests(FuncTestMixin, Geo3DLoadingHelper, TestCase): |
| 264 |
def test_kml(self): |
| 265 |
""" |
| 266 |
Test KML() function with Z values. |
| 267 |
""" |
| 268 |
self._load_city_data() |
| 269 |
h = City3D.objects.annotate(kml=AsKML("point", precision=6)).get(name="Houston") |
| 270 |
# KML should be 3D. |
| 271 |
# `SELECT ST_AsKML(point, 6) FROM geo3d_city3d WHERE name = 'Houston';` |
| 272 |
ref_kml_regex = re.compile( |
| 273 |
r"^<Point><coordinates>-95.363\d+,29.763\d+,18</coordinates></Point>$" |
| 274 |
) |
| 275 |
self.assertTrue(ref_kml_regex.match(h.kml)) |
| 276 |
|
| 277 |
def test_geojson(self): |
| 278 |
""" |
| 279 |
Test GeoJSON() function with Z values. |
| 280 |
""" |
| 281 |
self._load_city_data() |
| 282 |
h = City3D.objects.annotate(geojson=AsGeoJSON("point", precision=6)).get( |
| 283 |
name="Houston" |
| 284 |
) |
| 285 |
# GeoJSON should be 3D |
| 286 |
# `SELECT ST_AsGeoJSON(point, 6) FROM geo3d_city3d WHERE name='Houston';` |
| 287 |
ref_json_regex = re.compile( |
| 288 |
r'^{"type":"Point","coordinates":\[-95.363151,29.763374,18(\.0+)?\]}$' |
| 289 |
) |
| 290 |
self.assertTrue(ref_json_regex.match(h.geojson)) |
| 291 |
|
| 292 |
def test_perimeter(self): |
| 293 |
""" |
| 294 |
Testing Perimeter() function on 3D fields. |
| 295 |
""" |
| 296 |
self._load_polygon_data() |
| 297 |
# Reference query for values below: |
| 298 |
# `SELECT ST_Perimeter3D(poly), ST_Perimeter2D(poly) FROM geo3d_polygon3d;` |
| 299 |
ref_perim_3d = 76859.2620451 |
| 300 |
ref_perim_2d = 76859.2577803 |
| 301 |
tol = 6 |
| 302 |
poly2d = Polygon2D.objects.annotate(perimeter=Perimeter("poly")).get( |
| 303 |
name="2D BBox" |
| 304 |
) |
| 305 |
self.assertAlmostEqual(ref_perim_2d, poly2d.perimeter.m, tol) |
| 306 |
poly3d = Polygon3D.objects.annotate(perimeter=Perimeter("poly")).get( |
| 307 |
name="3D BBox" |
| 308 |
) |
| 309 |
self.assertAlmostEqual(ref_perim_3d, poly3d.perimeter.m, tol) |
| 310 |
|
| 311 |
def test_length(self): |
| 312 |
""" |
| 313 |
Testing Length() function on 3D fields. |
| 314 |
""" |
| 315 |
# ST_Length_Spheroid Z-aware, and thus does not need to use |
| 316 |
# a separate function internally. |
| 317 |
# `SELECT ST_Length_Spheroid(line, 'SPHEROID["GRS 1980",6378137,298.257222101]') |
| 318 |
# FROM geo3d_interstate[2d|3d];` |
| 319 |
self._load_interstate_data() |
| 320 |
tol = 3 |
| 321 |
ref_length_2d = 4368.1721949481 |
| 322 |
ref_length_3d = 4368.62547052088 |
| 323 |
inter2d = Interstate2D.objects.annotate(length=Length("line")).get(name="I-45") |
| 324 |
self.assertAlmostEqual(ref_length_2d, inter2d.length.m, tol) |
| 325 |
inter3d = Interstate3D.objects.annotate(length=Length("line")).get(name="I-45") |
| 326 |
self.assertAlmostEqual(ref_length_3d, inter3d.length.m, tol) |
| 327 |
|
| 328 |
# Making sure `ST_Length3D` is used on for a projected |
| 329 |
# and 3D model rather than `ST_Length`. |
| 330 |
# `SELECT ST_Length(line) FROM geo3d_interstateproj2d;` |
| 331 |
ref_length_2d = 4367.71564892392 |
| 332 |
# `SELECT ST_Length3D(line) FROM geo3d_interstateproj3d;` |
| 333 |
ref_length_3d = 4368.16897234101 |
| 334 |
inter2d = InterstateProj2D.objects.annotate(length=Length("line")).get( |
| 335 |
name="I-45" |
| 336 |
) |
| 337 |
self.assertAlmostEqual(ref_length_2d, inter2d.length.m, tol) |
| 338 |
inter3d = InterstateProj3D.objects.annotate(length=Length("line")).get( |
| 339 |
name="I-45" |
| 340 |
) |
| 341 |
self.assertAlmostEqual(ref_length_3d, inter3d.length.m, tol) |
| 342 |
|
| 343 |
def test_scale(self): |
| 344 |
""" |
| 345 |
Testing Scale() function on Z values. |
| 346 |
""" |
| 347 |
self._load_city_data() |
| 348 |
# Mapping of City name to reference Z values. |
| 349 |
zscales = (-3, 4, 23) |
| 350 |
for zscale in zscales: |
| 351 |
for city in City3D.objects.annotate(scale=Scale("point", 1.0, 1.0, zscale)): |
| 352 |
self.assertEqual(city_dict[city.name][2] * zscale, city.scale.z) |
| 353 |
|
| 354 |
def test_translate(self): |
| 355 |
""" |
| 356 |
Testing Translate() function on Z values. |
| 357 |
""" |
| 358 |
self._load_city_data() |
| 359 |
ztranslations = (5.23, 23, -17) |
| 360 |
for ztrans in ztranslations: |
| 361 |
for city in City3D.objects.annotate( |
| 362 |
translate=Translate("point", 0, 0, ztrans) |
| 363 |
): |
| 364 |
self.assertEqual(city_dict[city.name][2] + ztrans, city.translate.z) |