Debian Codegraph @

tests/django_restframework_gis_tests/test_fields.py

raw
1 import json
2
3 from django.contrib.gis.geos import GEOSGeometry
4 from django.test import TestCase
5 from rest_framework import serializers
6
7 from rest_framework_gis import serializers as gis_serializers
8
9 Point = {"type": "Point", "coordinates": [-105.0162, 39.5742]}
10 Point31287 = {"type": "Point", "coordinates": [625826.2376404074, 483198.2074507246]}
11
12 MultiPoint = {
13 "type": "MultiPoint",
14 "coordinates": [
15 [-105.0162, 39.5742],
16 [-80.6665, 35.0539],
17 [-80.6665, 35.0539], # Dupe
18 [-80.672, 35.049],
19 ],
20 }
21
22 LineString = {
23 "type": "LineString",
24 "coordinates": [
25 [-101.7443, 39.3215],
26 [-101.4021, 39.3300],
27 [-101.4038, 39.3300],
28 [-101.4038, 39.3300], # Dupe
29 [-97.6354, 38.8739],
30 ],
31 }
32
33 MultiLineString = {
34 "type": "MultiLineString",
35 "coordinates": [
36 [
37 [-105.0214, 39.5780],
38 [-105.0215, 39.5778],
39 [-105.0215, 39.5774],
40 [-105.0215, 39.5771],
41 [-105.0215, 39.5771], # Dupe
42 [-105.0215, 39.5770],
43 [-105.0215, 39.5767],
44 ],
45 [
46 [-105.0171, 39.5744],
47 [-105.0169, 39.5743],
48 [-105.0166, 39.5743],
49 [-105.0166, 39.5743], # Dupe
50 [-105.0165, 39.5744],
51 [-105.0159, 39.5742],
52 ],
53 ],
54 }
55
56 Polygon = {
57 "type": "Polygon",
58 "coordinates": [
59 [
60 [-84.3228, 34.9895],
61 [-82.6062, 36.0335],
62 [-82.6062, 35.9913],
63 [-82.6062, 35.9791],
64 [-82.5787, 35.9613],
65 [-82.5787, 35.9613], # Dupe
66 [-82.5677, 35.9513],
67 [-84.2211, 34.9850],
68 [-84.3228, 34.9895],
69 ],
70 [
71 [-75.6903, 35.7420],
72 [-75.5914, 35.7420],
73 [-75.5914, 35.7420], # Dupe
74 [-75.7067, 35.7420],
75 [-75.6903, 35.7420],
76 ],
77 ],
78 }
79
80 MultiPolygon = {
81 "type": "MultiPolygon",
82 "coordinates": [
83 [
84 [
85 [-84.3228, 34.9895],
86 [-84.3227, 34.9895],
87 [-84.3227, 34.9895], # Dupe
88 [-84.2211, 34.9850],
89 [-84.3228, 34.9895],
90 ],
91 [
92 [-75.6903, 35.7420],
93 [-75.5913, 35.7420],
94 [-75.5913, 35.7420], # Dupe
95 [-75.5914, 35.7420],
96 [-75.6903, 35.7420],
97 ],
98 ],
99 [
100 [
101 [-109.0283, 36.9850],
102 [-102.0629, 40.9798],
103 [-102.0629, 40.9798], # Dupe
104 [-109.0283, 36.9851],
105 [-109.0283, 36.9850],
106 ],
107 ],
108 ],
109 }
110
111 GeometryCollection = {
112 "type": "GeometryCollection",
113 "geometries": [Point, Polygon, LineString],
114 }
115
116
117 class BaseTestCase(TestCase):
118 @staticmethod
119 def get_instance(data_dict):
120 class Model:
121 def __init__(self, geojson_dict):
122 self.geometry = GEOSGeometry(json.dumps(geojson_dict))
123
124 return Model(data_dict)
125
126 @staticmethod
127 def create_serializer(**kwargs):
128 class LocationGeoSerializer(serializers.Serializer):
129 geometry = gis_serializers.GeometryField(**kwargs)
130
131 return LocationGeoSerializer
132
133 def normalize(self, data):
134 """
135 To help with equality operators, cast nested inputted data from
136 OrderedDict or GeoJsonDict to dict and from tuple to array.
137 """
138 if isinstance(data, (tuple, list)):
139 return [self.normalize(d) for d in data]
140 if isinstance(data, dict):
141 return {k: self.normalize(v) for k, v in data.items()}
142 return data
143
144
145 class TestTransform(BaseTestCase):
146 def test_no_transform_4326_Point_no_srid(self):
147 model = self.get_instance(Point)
148 Serializer = self.create_serializer()
149 data = Serializer(model).data
150
151 expected_coords = (-105.0162, 39.5742)
152 for lat, lon in zip(
153 data["geometry"]["coordinates"],
154 expected_coords,
155 ):
156 self.assertAlmostEqual(lat, lon, places=5)
157
158 def test_no_transform_4326_Point_set_srid(self):
159 model = self.get_instance(Point)
160 model.geometry.srid = 4326
161 Serializer = self.create_serializer()
162 data = Serializer(model).data
163
164 expected_coords = (-105.0162, 39.5742)
165 for lat, lon in zip(
166 data["geometry"]["coordinates"],
167 expected_coords,
168 ):
169 self.assertAlmostEqual(lat, lon, places=5)
170
171 def test_transform_Point_no_transform(self):
172 model = self.get_instance(Point31287)
173 model.geometry.srid = 31287
174 Serializer = self.create_serializer(transform=None)
175 data = Serializer(model).data
176
177 expected_coords = (625826.2376404074, 483198.2074507246)
178 for lat, lon in zip(
179 data["geometry"]["coordinates"],
180 expected_coords,
181 ):
182 self.assertAlmostEqual(lat, lon, places=5)
183
184 def test_transform_Point_no_srid(self):
185 model = self.get_instance(Point31287)
186 Serializer = self.create_serializer()
187 data = Serializer(model).data
188
189 expected_coords = (625826.2376404074, 483198.2074507246)
190 for lat, lon in zip(
191 data["geometry"]["coordinates"],
192 expected_coords,
193 ):
194 self.assertAlmostEqual(lat, lon, places=5)
195
196 def test_transform_Point_to_4326(self):
197 model = self.get_instance(Point31287)
198 model.geometry.srid = 31287
199 Serializer = self.create_serializer(transform=4326)
200 data = Serializer(model).data
201
202 expected_coords = (16.372500007573713, 48.20833306345481)
203 for lat, lon in zip(
204 data["geometry"]["coordinates"],
205 expected_coords,
206 ):
207 self.assertAlmostEqual(lat, lon, places=5)
208
209 def test_transform_Point_to_3857(self):
210 model = self.get_instance(Point31287)
211 model.geometry.srid = 31287
212 Serializer = self.create_serializer(transform=3857)
213 data = Serializer(model).data
214
215 expected_coords = (1822578.363856016, 6141584.271938089)
216 for lat, lon in zip(
217 data["geometry"]["coordinates"],
218 expected_coords,
219 ):
220 self.assertAlmostEqual(lat, lon, places=1)
221
222 def test_transform_Point_bbox_to_4326(self):
223 model = self.get_instance(Point31287)
224 model.geometry.srid = 31287
225 Serializer = self.create_serializer(auto_bbox=True, transform=4326)
226 data = Serializer(model).data
227
228 expected_coords = (
229 16.372500007573713,
230 48.20833306345481,
231 16.372500007573713,
232 48.20833306345481,
233 )
234 for received, expected in zip(
235 data["geometry"]["bbox"],
236 expected_coords,
237 ):
238 self.assertAlmostEqual(received, expected, places=5)
239
240
241 class TestPrecision(BaseTestCase):
242 def test_precision_Point(self):
243 model = self.get_instance(Point)
244 Serializer = self.create_serializer(precision=2)
245 data = Serializer(model).data
246 self.assertEqual(
247 self.normalize(data),
248 {"geometry": {"type": "Point", "coordinates": [-105.02, 39.57]}},
249 )
250
251 def test_precision_MultiPoint(self):
252 model = self.get_instance(MultiPoint)
253 Serializer = self.create_serializer(precision=2)
254 data = Serializer(model).data
255 self.assertEqual(
256 self.normalize(data),
257 {
258 "geometry": {
259 "type": "MultiPoint",
260 "coordinates": [
261 [-105.02, 39.57],
262 [-80.67, 35.05],
263 [-80.67, 35.05],
264 [-80.67, 35.05],
265 ],
266 }
267 },
268 )
269
270 def test_precision_LineString(self):
271 model = self.get_instance(LineString)
272 Serializer = self.create_serializer(precision=2)
273 data = Serializer(model).data
274 self.assertEqual(
275 self.normalize(data),
276 {
277 "geometry": {
278 "type": "LineString",
279 "coordinates": [
280 [-101.74, 39.32],
281 [-101.40, 39.33],
282 [-101.40, 39.33],
283 [-101.40, 39.33],
284 [-97.64, 38.87],
285 ],
286 }
287 },
288 )
289
290 def test_precision_MultiLineString(self):
291 model = self.get_instance(MultiLineString)
292 Serializer = self.create_serializer(precision=2)
293 data = Serializer(model).data
294 self.assertEqual(
295 self.normalize(data),
296 {
297 "geometry": {
298 "type": "MultiLineString",
299 "coordinates": [
300 [
301 [-105.02, 39.58],
302 [-105.02, 39.58],
303 [-105.02, 39.58],
304 [-105.02, 39.58],
305 [-105.02, 39.58],
306 [-105.02, 39.58],
307 [-105.02, 39.58],
308 ],
309 [
310 [-105.02, 39.57],
311 [-105.02, 39.57],
312 [-105.02, 39.57],
313 [-105.02, 39.57],
314 [-105.02, 39.57],
315 [-105.02, 39.57],
316 ],
317 ],
318 }
319 },
320 )
321
322 def test_precision_Polygon(self):
323 model = self.get_instance(Polygon)
324 Serializer = self.create_serializer(precision=2)
325 data = Serializer(model).data
326 self.assertEqual(
327 self.normalize(data),
328 {
329 "geometry": {
330 "type": "Polygon",
331 "coordinates": [
332 [
333 [-84.32, 34.99],
334 [-82.61, 36.03],
335 [-82.61, 35.99],
336 [-82.61, 35.98],
337 [-82.58, 35.96],
338 [-82.58, 35.96],
339 [-82.57, 35.95],
340 [-84.22, 34.98],
341 [-84.32, 34.99],
342 ],
343 [
344 [-75.69, 35.74],
345 [-75.59, 35.74],
346 [-75.59, 35.74],
347 [-75.71, 35.74],
348 [-75.69, 35.74],
349 ],
350 ],
351 }
352 },
353 )
354
355 def test_precision_MultiPolygon(self):
356 model = self.get_instance(MultiPolygon)
357 Serializer = self.create_serializer(precision=2)
358 data = Serializer(model).data
359
360 self.assertEqual(
361 self.normalize(data),
362 {
363 "geometry": {
364 "type": "MultiPolygon",
365 "coordinates": [
366 [
367 [
368 [-84.32, 34.99],
369 [-84.32, 34.99],
370 [-84.32, 34.99],
371 [-84.22, 34.98],
372 [-84.32, 34.99],
373 ],
374 [
375 [-75.69, 35.74],
376 [-75.59, 35.74],
377 [-75.59, 35.74],
378 [-75.59, 35.74],
379 [-75.69, 35.74],
380 ],
381 ],
382 [
383 [
384 [-109.03, 36.98],
385 [-102.06, 40.98],
386 [-102.06, 40.98],
387 [-109.03, 36.99],
388 [-109.03, 36.98],
389 ],
390 ],
391 ],
392 }
393 },
394 )
395
396 def test_precision_GeometryCollection(self):
397 model = self.get_instance(GeometryCollection)
398 Serializer = self.create_serializer(precision=2)
399 data = Serializer(model).data
400 self.assertEqual(
401 self.normalize(data),
402 {
403 "geometry": {
404 "type": "GeometryCollection",
405 "geometries": [
406 {"type": "Point", "coordinates": [-105.02, 39.57]},
407 {
408 "type": "Polygon",
409 "coordinates": [
410 [
411 [-84.32, 34.99],
412 [-82.61, 36.03],
413 [-82.61, 35.99],
414 [-82.61, 35.98],
415 [-82.58, 35.96],
416 [-82.58, 35.96],
417 [-82.57, 35.95],
418 [-84.22, 34.98],
419 [-84.32, 34.99],
420 ],
421 [
422 [-75.69, 35.74],
423 [-75.59, 35.74],
424 [-75.59, 35.74],
425 [-75.71, 35.74],
426 [-75.69, 35.74],
427 ],
428 ],
429 },
430 {
431 "type": "LineString",
432 "coordinates": [
433 [-101.74, 39.32],
434 [-101.4, 39.33],
435 [-101.4, 39.33],
436 [-101.4, 39.33],
437 [-97.64, 38.87],
438 ],
439 },
440 ],
441 }
442 },
443 )
444
445
446 class TestRmRedundant(BaseTestCase):
447 def test_rm_redundant_Point(self):
448 model = self.get_instance({"type": "Point", "coordinates": [-1.1, -1.1]})
449 Serializer = self.create_serializer(remove_duplicates=True)
450 data = Serializer(model).data
451 self.assertEqual(
452 self.normalize(data),
453 {"geometry": {"type": "Point", "coordinates": [-1.1, -1.1]}},
454 )
455
456 def test_rm_redundant_MultiPoint(self):
457 model = self.get_instance(MultiPoint)
458 Serializer = self.create_serializer(remove_duplicates=True)
459 data = Serializer(model).data
460 self.assertEqual(
461 self.normalize(data),
462 {
463 "geometry": {
464 "type": "MultiPoint",
465 "coordinates": [
466 [-105.0162, 39.5742],
467 [-80.6665, 35.0539],
468 # [-80.6665, 35.0539], # Dupe
469 [-80.672, 35.049],
470 ],
471 }
472 },
473 )
474
475 def test_rm_redundant_LineString(self):
476 model = self.get_instance(LineString)
477 Serializer = self.create_serializer(remove_duplicates=True)
478 data = Serializer(model).data
479 self.assertEqual(
480 self.normalize(data),
481 {
482 "geometry": {
483 "type": "LineString",
484 "coordinates": [
485 [-101.7443, 39.3215],
486 [-101.4021, 39.3300],
487 [-101.4038, 39.3300],
488 # [-101.4038, 39.3300], # Dupe
489 [-97.6354, 38.8739],
490 ],
491 }
492 },
493 )
494
495 def test_rm_redundant_MultiLineString(self):
496 model = self.get_instance(MultiLineString)
497 Serializer = self.create_serializer(remove_duplicates=True)
498 data = Serializer(model).data
499 self.assertEqual(
500 self.normalize(data),
501 {
502 "geometry": {
503 "type": "MultiLineString",
504 "coordinates": [
505 [
506 [-105.0214, 39.5780],
507 [-105.0215, 39.5778],
508 [-105.0215, 39.5774],
509 [-105.0215, 39.5771],
510 # [-105.0215, 39.5771], # Dupe
511 [-105.0215, 39.5770],
512 [-105.0215, 39.5767],
513 ],
514 [
515 [-105.0171, 39.5744],
516 [-105.0169, 39.5743],
517 [-105.0166, 39.5743],
518 # [-105.0166, 39.5743], # Dupe
519 [-105.0165, 39.5744],
520 [-105.0159, 39.5742],
521 ],
522 ],
523 }
524 },
525 )
526
527 def test_rm_redundant_Polygon(self):
528 model = self.get_instance(Polygon)
529 Serializer = self.create_serializer(remove_duplicates=True)
530 data = Serializer(model).data
531 self.assertEqual(
532 self.normalize(data),
533 {
534 "geometry": {
535 "type": "Polygon",
536 "coordinates": [
537 [
538 [-84.3228, 34.9895],
539 [-82.6062, 36.0335],
540 [-82.6062, 35.9913],
541 [-82.6062, 35.9791],
542 [-82.5787, 35.9613],
543 # [-82.5787, 35.9613], # Dupe
544 [-82.5677, 35.9513],
545 [-84.2211, 34.985],
546 [-84.3228, 34.9895],
547 ],
548 [
549 [-75.6903, 35.742],
550 [-75.5914, 35.742],
551 # [-75.5914, 35.7420], # Dupe
552 [-75.7067, 35.742],
553 [-75.6903, 35.742],
554 ],
555 ],
556 }
557 },
558 )
559
560 def test_rm_redundant_MultiPolygon(self):
561 model = self.get_instance(MultiPolygon)
562 Serializer = self.create_serializer(remove_duplicates=True)
563 data = Serializer(model).data
564 self.assertEqual(
565 self.normalize(data),
566 {
567 "geometry": {
568 "type": "MultiPolygon",
569 "coordinates": [
570 [
571 [
572 [-84.3228, 34.9895],
573 [-84.3227, 34.9895],
574 # [-84.3227, 34.9895], # Dupe
575 [-84.2211, 34.9850],
576 [-84.3228, 34.9895],
577 ],
578 [
579 [-75.6903, 35.7420],
580 [-75.5913, 35.7420],
581 # [-75.5913, 35.7420], # Dupe
582 [-75.5914, 35.7420],
583 [-75.6903, 35.7420],
584 ],
585 ],
586 [
587 [
588 [-109.0283, 36.9850],
589 [-102.0629, 40.9798],
590 # [-102.0629, 40.9798], # Dupe
591 [-109.0283, 36.9851],
592 [-109.0283, 36.9850],
593 ],
594 ],
595 ],
596 }
597 },
598 )
599
600 def test_rm_redundant_MultiPolygon_single_polygon(self):
601 MultiPolygon = {
602 "type": "MultiPolygon",
603 "coordinates": [
604 [
605 [
606 [-109.17227935791, 45.0041122436525],
607 [-109.218215942383, 45.0039901733398],
608 [-109.218215942383, 45.0039901733398],
609 [-109.175567626953, 45.0041999816896],
610 [-109.17227935791, 45.0041122436525],
611 ]
612 ]
613 ],
614 }
615 model = self.get_instance(MultiPolygon)
616 Serializer = self.create_serializer(remove_duplicates=True)
617 data = Serializer(model).data
618 self.assertEqual(
619 self.normalize(data),
620 {
621 "geometry": {
622 "type": "MultiPolygon",
623 "coordinates": [
624 [
625 [
626 [-109.17227935791, 45.0041122436525],
627 [-109.218215942383, 45.0039901733398],
628 # [-109.218215942383, 45.0039901733398], # Dupe
629 [-109.175567626953, 45.0041999816896],
630 [-109.17227935791, 45.0041122436525],
631 ]
632 ]
633 ],
634 }
635 },
636 )
637
638 def test_rm_redundant_GeometryCollection(self):
639 model = self.get_instance(GeometryCollection)
640 Serializer = self.create_serializer(remove_duplicates=True)
641 data = Serializer(model).data
642 self.assertEqual(
643 self.normalize(data),
644 {
645 "geometry": {
646 "type": "GeometryCollection",
647 "geometries": [
648 {"type": "Point", "coordinates": [-105.0162, 39.5742]},
649 {
650 "type": "Polygon",
651 "coordinates": [
652 [
653 [-84.3228, 34.9895],
654 [-82.6062, 36.0335],
655 [-82.6062, 35.9913],
656 [-82.6062, 35.9791],
657 [-82.5787, 35.9613],
658 # [-82.5787, 35.9613], # Dupe
659 [-82.5677, 35.9513],
660 [-84.2211, 34.985],
661 [-84.3228, 34.9895],
662 ],
663 [
664 [-75.6903, 35.742],
665 [-75.5914, 35.742],
666 # [-75.5914, 35.7420], # Dupe
667 [-75.7067, 35.742],
668 [-75.6903, 35.742],
669 ],
670 ],
671 },
672 {
673 "type": "LineString",
674 "coordinates": [
675 [-101.7443, 39.3215],
676 [-101.4021, 39.3300],
677 [-101.4038, 39.3300],
678 # [-101.4038, 39.3300], # Dupe
679 [-97.6354, 38.8739],
680 ],
681 },
682 ],
683 }
684 },
685 )
686
687
688 class TestBoundingBox(BaseTestCase):
689 def test_boundingbox_Point(self):
690 model = self.get_instance(Point)
691 Serializer = self.create_serializer(auto_bbox=True)
692 data = Serializer(model).data
693 self.assertEqual(
694 self.normalize(data),
695 {
696 "geometry": {
697 "type": "Point",
698 "bbox": [-105.0162, 39.5742, -105.0162, 39.5742],
699 "coordinates": [-105.0162, 39.5742],
700 }
701 },
702 )
703
704 def test_boundingbox_MultiPoint(self):
705 model = self.get_instance(MultiPoint)
706 Serializer = self.create_serializer(auto_bbox=True)
707 data = Serializer(model).data
708 self.assertEqual(
709 self.normalize(data),
710 {
711 "geometry": {
712 "type": "MultiPoint",
713 "bbox": [-105.0162, 35.049, -80.6665, 39.5742],
714 "coordinates": [
715 [-105.0162, 39.5742],
716 [-80.6665, 35.0539],
717 [-80.6665, 35.0539],
718 [-80.672, 35.049],
719 ],
720 }
721 },
722 )
723
724 def test_boundingbox_LineString(self):
725 model = self.get_instance(LineString)
726 Serializer = self.create_serializer(auto_bbox=True)
727 data = Serializer(model).data
728 self.assertEqual(
729 self.normalize(data),
730 {
731 "geometry": {
732 "type": "LineString",
733 "bbox": [-101.7443, 38.8739, -97.6354, 39.33],
734 "coordinates": [
735 [-101.7443, 39.3215],
736 [-101.4021, 39.3300],
737 [-101.4038, 39.3300],
738 [-101.4038, 39.3300],
739 [-97.6354, 38.8739],
740 ],
741 }
742 },
743 )
744
745 def test_boundingbox_Polygon(self):
746 model = self.get_instance(Polygon)
747 Serializer = self.create_serializer(auto_bbox=True)
748 data = Serializer(model).data
749 self.assertEqual(
750 self.normalize(data),
751 {
752 "geometry": {
753 "type": "Polygon",
754 "bbox": [-84.3228, 34.985, -82.5677, 36.0335],
755 "coordinates": [
756 [
757 [-84.3228, 34.9895],
758 [-82.6062, 36.0335],
759 [-82.6062, 35.9913],
760 [-82.6062, 35.9791],
761 [-82.5787, 35.9613],
762 [-82.5787, 35.9613], # Dupe
763 [-82.5677, 35.9513],
764 [-84.2211, 34.9850],
765 [-84.3228, 34.9895],
766 ],
767 [
768 [-75.6903, 35.7420],
769 [-75.5914, 35.7420],
770 [-75.5914, 35.7420], # Dupe
771 [-75.7067, 35.7420],
772 [-75.6903, 35.7420],
773 ],
774 ],
775 }
776 },
777 )