hc99 commited on
Commit
9dcd016
·
verified ·
1 Parent(s): b65ca24

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. testbed/django__django/tests/get_object_or_404/__init__.py +0 -0
  2. testbed/django__django/tests/get_object_or_404/models.py +35 -0
  3. testbed/django__django/tests/gis_tests/data/__init__.py +0 -0
  4. testbed/django__django/tests/gis_tests/data/ch-city/ch-city.dbf +0 -0
  5. testbed/django__django/tests/gis_tests/data/cities/cities.prj +1 -0
  6. testbed/django__django/tests/gis_tests/data/cities/cities.shp +0 -0
  7. testbed/django__django/tests/gis_tests/data/counties/counties.dbf +0 -0
  8. testbed/django__django/tests/gis_tests/data/counties/counties.shp +0 -0
  9. testbed/django__django/tests/gis_tests/data/counties/counties.shx +0 -0
  10. testbed/django__django/tests/gis_tests/data/gas_lines/gas_leitung.dbf +0 -0
  11. testbed/django__django/tests/gis_tests/data/gas_lines/gas_leitung.prj +1 -0
  12. testbed/django__django/tests/gis_tests/data/gas_lines/gas_leitung.shp +0 -0
  13. testbed/django__django/tests/gis_tests/data/gas_lines/gas_leitung.shx +0 -0
  14. testbed/django__django/tests/gis_tests/data/geometries.json +141 -0
  15. testbed/django__django/tests/gis_tests/data/has_nulls/has_nulls.geojson +139 -0
  16. testbed/django__django/tests/gis_tests/data/interstates/interstates.dbf +0 -0
  17. testbed/django__django/tests/gis_tests/data/interstates/interstates.prj +1 -0
  18. testbed/django__django/tests/gis_tests/data/interstates/interstates.shp +0 -0
  19. testbed/django__django/tests/gis_tests/data/interstates/interstates.shx +0 -0
  20. testbed/django__django/tests/gis_tests/data/invalid/emptypoints.dbf +0 -0
  21. testbed/django__django/tests/gis_tests/data/invalid/emptypoints.shp +0 -0
  22. testbed/django__django/tests/gis_tests/data/invalid/emptypoints.shx +0 -0
  23. testbed/django__django/tests/gis_tests/data/rasters/__init__.py +0 -0
  24. testbed/django__django/tests/gis_tests/data/rasters/raster.numpy.txt +0 -0
  25. testbed/django__django/tests/gis_tests/data/rasters/raster.tif +0 -0
  26. testbed/django__django/tests/gis_tests/data/rasters/textrasters.py +16 -0
  27. testbed/django__django/tests/gis_tests/data/test_point/test_point.dbf +0 -0
  28. testbed/django__django/tests/gis_tests/data/test_point/test_point.prj +1 -0
  29. testbed/django__django/tests/gis_tests/data/test_point/test_point.shp +0 -0
  30. testbed/django__django/tests/gis_tests/data/test_point/test_point.shx +0 -0
  31. testbed/django__django/tests/gis_tests/data/test_poly/test_poly.dbf +0 -0
  32. testbed/django__django/tests/gis_tests/data/test_poly/test_poly.prj +1 -0
  33. testbed/django__django/tests/gis_tests/data/test_poly/test_poly.shp +0 -0
  34. testbed/django__django/tests/gis_tests/data/test_poly/test_poly.shx +0 -0
  35. testbed/django__django/tests/gis_tests/data/test_vrt/test_vrt.csv +4 -0
  36. testbed/django__django/tests/gis_tests/data/test_vrt/test_vrt.vrt +7 -0
  37. testbed/django__django/tests/gis_tests/data/texas.dbf +0 -0
  38. testbed/django__django/tests/gis_tests/distapp/__init__.py +0 -0
  39. testbed/django__django/tests/gis_tests/distapp/fixtures/initial.json +314 -0
  40. testbed/django__django/tests/gis_tests/distapp/models.py +52 -0
  41. testbed/django__django/tests/gis_tests/distapp/tests.py +796 -0
  42. testbed/django__django/tests/gis_tests/gdal_tests/__init__.py +0 -0
  43. testbed/django__django/tests/gis_tests/gdal_tests/test_driver.py +85 -0
  44. testbed/django__django/tests/gis_tests/gdal_tests/test_ds.py +355 -0
  45. testbed/django__django/tests/gis_tests/gdal_tests/test_envelope.py +93 -0
  46. testbed/django__django/tests/gis_tests/gdal_tests/test_geom.py +634 -0
  47. testbed/django__django/tests/gis_tests/gdal_tests/test_raster.py +991 -0
  48. testbed/django__django/tests/gis_tests/gdal_tests/test_srs.py +403 -0
  49. testbed/django__django/tests/gis_tests/gdal_tests/tests.py +16 -0
  50. testbed/django__django/tests/gis_tests/geo3d/__init__.py +0 -0
testbed/django__django/tests/get_object_or_404/__init__.py ADDED
File without changes
testbed/django__django/tests/get_object_or_404/models.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ DB-API Shortcuts
3
+
4
+ ``get_object_or_404()`` is a shortcut function to be used in view functions for
5
+ performing a ``get()`` lookup and raising a ``Http404`` exception if a
6
+ ``DoesNotExist`` exception was raised during the ``get()`` call.
7
+
8
+ ``get_list_or_404()`` is a shortcut function to be used in view functions for
9
+ performing a ``filter()`` lookup and raising a ``Http404`` exception if a
10
+ ``DoesNotExist`` exception was raised during the ``filter()`` call.
11
+ """
12
+
13
+ from django.db import models
14
+
15
+
16
+ class Author(models.Model):
17
+ name = models.CharField(max_length=50)
18
+
19
+
20
+ class ArticleManager(models.Manager):
21
+ def get_queryset(self):
22
+ return super().get_queryset().filter(authors__name__icontains="sir")
23
+
24
+
25
+ class AttributeErrorManager(models.Manager):
26
+ def get_queryset(self):
27
+ raise AttributeError("AttributeErrorManager")
28
+
29
+
30
+ class Article(models.Model):
31
+ authors = models.ManyToManyField(Author)
32
+ title = models.CharField(max_length=50)
33
+ objects = models.Manager()
34
+ by_a_sir = ArticleManager()
35
+ attribute_error_objects = AttributeErrorManager()
testbed/django__django/tests/gis_tests/data/__init__.py ADDED
File without changes
testbed/django__django/tests/gis_tests/data/ch-city/ch-city.dbf ADDED
Binary file (285 Bytes). View file
 
testbed/django__django/tests/gis_tests/data/cities/cities.prj ADDED
@@ -0,0 +1 @@
 
 
1
+ GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
testbed/django__django/tests/gis_tests/data/cities/cities.shp ADDED
Binary file (184 Bytes). View file
 
testbed/django__django/tests/gis_tests/data/counties/counties.dbf ADDED
Binary file (3.96 kB). View file
 
testbed/django__django/tests/gis_tests/data/counties/counties.shp ADDED
Binary file (37.4 kB). View file
 
testbed/django__django/tests/gis_tests/data/counties/counties.shx ADDED
Binary file (292 Bytes). View file
 
testbed/django__django/tests/gis_tests/data/gas_lines/gas_leitung.dbf ADDED
Binary file (5.45 kB). View file
 
testbed/django__django/tests/gis_tests/data/gas_lines/gas_leitung.prj ADDED
@@ -0,0 +1 @@
 
 
1
+ PROJCS["MGI_Ferro_Austria_GK_East",GEOGCS["GCS_MGI_Ferro",DATUM["D_MGI",SPHEROID["Bessel_1841",6377397.155,299.1528128]],PRIMEM["Ferro",-17.66666666666667],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",0.0],PARAMETER["False_Northing",-5000000.0],PARAMETER["Central_Meridian",34.0],PARAMETER["Scale_Factor",1.0],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]
testbed/django__django/tests/gis_tests/data/gas_lines/gas_leitung.shp ADDED
Binary file (38.1 kB). View file
 
testbed/django__django/tests/gis_tests/data/gas_lines/gas_leitung.shx ADDED
Binary file (1.8 kB). View file
 
testbed/django__django/tests/gis_tests/data/geometries.json ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "polygons": [
3
+ {"wkt": "POLYGON ((0 0, 0 100, 100 100, 100 0, 0 0), (10 10, 10 90, 90 90, 90 10, 10 10))", "n_i": 1, "ext_ring_cs": [[0, 0], [0, 100], [100, 100], [100, 0], [0, 0]], "n_p": 10, "area": 3600.0, "centroid": [50.0, 50.0]},
4
+ {"wkt": "POLYGON ((0 0, 0 100, 100 100, 100 0, 0 0), (10 10, 10 20, 20 20, 20 10, 10 10), (80 80, 80 90, 90 90, 90 80, 80 80))", "n_i": 2, "ext_ring_cs": [[0, 0], [0, 100], [100, 100], [100, 0], [0, 0]], "n_p": 15, "area": 9800.0, "centroid": [50.0, 50.0]},
5
+ {"wkt": "POLYGON ((0 0, 0 100, 100 100, 100 0, 0 0))", "n_i": 0, "ext_ring_cs": [[0, 0], [0, 100], [100, 100], [100, 0], [0, 0]], "n_p": 5, "area": 10000.0, "centroid": [50.0, 50.0]},
6
+ {"wkt": "POLYGON ((-95.3848703124799471 29.7056021479768511, -95.3851905195191847 29.7046588196500281, -95.3859356966379011 29.7025053545605502, -95.3860723000647539 29.7020963367038391, -95.3871517697222089 29.6989779021280995, -95.3865578518265522 29.6990856888057202, -95.3862634205175226 29.6999471753441782, -95.3861991779541967 29.6999591988978615, -95.3856773799358137 29.6998323107113578, -95.3856209915427229 29.6998005235473741, -95.3855833545501639 29.6996619391729801, -95.3855776331865002 29.6996232659570047, -95.3850162731712885 29.6997236706530536, -95.3831047357410284 29.7000847603095082, -95.3829800724914776 29.7000676365023502, -95.3828084594470909 29.6999969684031200, -95.3828131504821499 29.6999090511531065, -95.3828022942979601 29.6998152117366025, -95.3827893930918833 29.6997790953076759, -95.3825174668099862 29.6998267772748825, -95.3823521544804862 29.7000451723151606, -95.3820491918785223 29.6999682034582335, -95.3817932841505893 29.6999640407204772, -95.3815438924600443 29.7005983712500630, -95.3807812390843424 29.7007538492921590, -95.3778578936435935 29.7012966201172048, -95.3770817300034679 29.7010555145969093, -95.3772763716395957 29.7004995005932031, -95.3769891024414420 29.7005797730360186, -95.3759855007185990 29.7007754783987821, -95.3759516423090474 29.7007305400669388, -95.3765252155960042 29.6989549173240874, -95.3766842746727832 29.6985134987163164, -95.3768510987262914 29.6980530300744938, -95.3769198676258014 29.6977137204527573, -95.3769616670751930 29.6973351617272172, -95.3770309229297766 29.6969821084304186, -95.3772352596880637 29.6959751305871613, -95.3776232419333354 29.6945439060847463, -95.3776849628727064 29.6943364710766069, -95.3779699491714723 29.6926548349458947, -95.3781945479573494 29.6920088336742545, -95.3785807118394189 29.6908279316076005, -95.3787441368896651 29.6908846275832197, -95.3787903214163890 29.6907152912461640, -95.3791765069353659 29.6893335376821526, -95.3794935959513026 29.6884781789101595, -95.3796592071232112 29.6880066681407619, -95.3799788182090111 29.6873687353035081, -95.3801545516183893 29.6868782380716993, -95.3801258908302145 29.6867756621337762, -95.3801104284899566 29.6867229678809572, -95.3803803523746154 29.6863753372986459, -95.3821028558287622 29.6837392961470421, -95.3827289584682205 29.6828097375216160, -95.3827494698109035 29.6790739156259278, -95.3826022014838486 29.6776502228345507, -95.3825047356438063 29.6765773006280753, -95.3823473035336917 29.6750405250369127, -95.3824540163482055 29.6750076408228587, -95.3838984230304305 29.6745679207378679, -95.3916547074937426 29.6722459226508377, -95.3926154662749468 29.6719609085105489, -95.3967246645118081 29.6707316485589736, -95.3974588054406780 29.6705065336410989, -95.3978523748756828 29.6703795547846845, -95.3988598162279970 29.6700874981900853, -95.3995628600665952 29.6698505300412414, -95.4134721665944170 29.6656841279906232, -95.4143262068232616 29.6654291174019278, -95.4159685142480214 29.6649750989232288, -95.4180067396277565 29.6643253024318021, -95.4185886692196590 29.6641482768691063, -95.4234155309609662 29.6626925393704788, -95.4287785503196346 29.6611023620959706, -95.4310287312749352 29.6604222580752648, -95.4320295629628959 29.6603361318136720, -95.4332899683975739 29.6600560661713608, -95.4342675748811047 29.6598454934599900, -95.4343110414310871 29.6598411486215490, -95.4345576779282538 29.6598147020668499, -95.4348823041721630 29.6597875803673112, -95.4352827715209457 29.6597762346946681, -95.4355290431309982 29.6597827926562374, -95.4359197997999331 29.6598014511782715, -95.4361907884752156 29.6598444333523368, -95.4364608955807228 29.6598901433108217, -95.4367250147512323 29.6599494499910712, -95.4364898759758091 29.6601880616540186, -95.4354501111810691 29.6616378572201107, -95.4381459623171224 29.6631265631655126, -95.4367852490863129 29.6642266600024023, -95.4370040894557263 29.6643425389568769, -95.4367078350812648 29.6645492592343238, -95.4366081749871285 29.6646291473027297, -95.4358539359938192 29.6652308742342932, -95.4350327668927889 29.6658995989314462, -95.4350580905272921 29.6678812477895271, -95.4349710541447536 29.6680054925936965, -95.4349500440473548 29.6671410080890006, -95.4341492724148850 29.6678790545191688, -95.4340248868274728 29.6680353198492135, -95.4333227845797438 29.6689245624945990, -95.4331325652123326 29.6691616138940901, -95.4321314741096955 29.6704473333237253, -95.4320435792664341 29.6702578985411982, -95.4320147929883547 29.6701800936425109, -95.4319764538662980 29.6683246590817085, -95.4317490976340679 29.6684974372577166, -95.4305958185342718 29.6694049049170374, -95.4296600735653016 29.6701723430938493, -95.4284928989940937 29.6710931793380972, -95.4274630532378580 29.6719378813640091, -95.4273056811974811 29.6720684984625791, -95.4260554084574864 29.6730668861566969, -95.4253558063699643 29.6736342467365724, -95.4249278826026028 29.6739557343648919, -95.4248648873821423 29.6745400910786152, -95.4260016131471929 29.6750987014005858, -95.4258567183010911 29.6753452063069929, -95.4260238081486847 29.6754322077221353, -95.4258707374502393 29.6756647377294307, -95.4257951755816691 29.6756407098663360, -95.4257701599566985 29.6761077719536068, -95.4257726684792260 29.6761711204603955, -95.4257980187195614 29.6770219651929423, -95.4252712669032519 29.6770161558853758, -95.4249234392992065 29.6770068683962300, -95.4249574272905789 29.6779707498635759, -95.4244725881033702 29.6779825646764159, -95.4222269476429545 29.6780711474441716, -95.4223032371999267 29.6796029391538809, -95.4239133706588945 29.6795331493690355, -95.4224579084327331 29.6813706893847780, -95.4224290108823965 29.6821953228763924, -95.4230916478977349 29.6822130268724109, -95.4222928279595521 29.6832041816675343, -95.4228763710016352 29.6832087677714505, -95.4223401691637179 29.6838987872753748, -95.4211655906087088 29.6838784024852984, -95.4201984153205558 29.6851319258758082, -95.4206156387716362 29.6851623398125319, -95.4213438084897660 29.6851763011334739, -95.4212071118618752 29.6853679931624974, -95.4202651399651245 29.6865313962980508, -95.4172061157659783 29.6865816431043932, -95.4182217951255183 29.6872251197301544, -95.4178664826439160 29.6876750901471631, -95.4180678442928780 29.6877960336377207, -95.4188763472917572 29.6882826379510938, -95.4185374500596311 29.6887137897831934, -95.4182121713132290 29.6885097429738813, -95.4179857231741551 29.6888118367840086, -95.4183106010563620 29.6890048676118212, -95.4179489865331334 29.6894546700979056, -95.4175581746284820 29.6892323606815438, -95.4173439957341571 29.6894990139807007, -95.4177411199311081 29.6897435034738422, -95.4175789200209721 29.6899207529979208, -95.4170598559864800 29.6896042165807508, -95.4166733682539814 29.6900891174451367, -95.4165941362704331 29.6900347214235047, -95.4163537218065301 29.6903529467753238, -95.4126843270708775 29.6881086357212780, -95.4126604121378392 29.6880942378803496, -95.4126672298953338 29.6885951670109982, -95.4126680884821923 29.6887052446594275, -95.4158080137241882 29.6906382377959339, -95.4152061403821961 29.6910871045531586, -95.4155842583188161 29.6917382915894308, -95.4157426793520358 29.6920726941677096, -95.4154520563662203 29.6922052332446427, -95.4151389936167078 29.6923261661269571, -95.4148649784384872 29.6924343866430256, -95.4144051352401590 29.6925623927348106, -95.4146792019416665 29.6926770338507744, -95.4148824479948985 29.6928117893696388, -95.4149851734360226 29.6929823719519774, -95.4140436551925291 29.6929626643100946, -95.4140465993023241 29.6926545917254892, -95.4137269186733334 29.6927395764256090, -95.4137372859685513 29.6935432485666624, -95.4135702836218655 29.6933186678088283, -95.4133925235973237 29.6930415229852152, -95.4133017035615580 29.6928685062036166, -95.4129588921634593 29.6929391128977862, -95.4125107395559695 29.6930481664661485, -95.4102647423187307 29.6935850183258019, -95.4081931340840157 29.6940907430947760, -95.4078783596459772 29.6941703429951609, -95.4049213975000043 29.6948723732981961, -95.4045944244127071 29.6949626434239207, -95.4045865139788134 29.6954109019001358, -95.4045953345484037 29.6956972800496963, -95.4038879332535146 29.6958296089365490, -95.4040366394459340 29.6964389004769842, -95.4032774779020798 29.6965643341263892, -95.4026066501239853 29.6966646227683881, -95.4024991226393837 29.6961389766619703, -95.4011781398631911 29.6963566063186377, -95.4011524097636112 29.6962596176762190, -95.4018184046368276 29.6961399466727336, -95.4016995838361908 29.6956442609415099, -95.4007100753964608 29.6958900524002978, -95.4008032469935188 29.6962639900781404, -95.3995660267125487 29.6965636449370329, -95.3996140564775601 29.6967877962763644, -95.3996364430014410 29.6968901984825280, -95.3984003269631842 29.6968679634805746, -95.3981442026887265 29.6983660679730335, -95.3980178461957706 29.6990890276252415, -95.3977097967130163 29.7008526152273049, -95.3962347157626027 29.7009697553607630, -95.3951949050136250 29.7004740386619019, -95.3957564950617183 29.6990281830553187, -95.3965927101519924 29.6968771129030706, -95.3957496517238184 29.6970800358387095, -95.3957720559467361 29.6972264611230727, -95.3957391586571788 29.6973548894558732, -95.3956286413405365 29.6974949857280883, -95.3955111053256957 29.6975661086270186, -95.3953215342724121 29.6976022763384790, -95.3951795558443365 29.6975846977491038, -95.3950369632041060 29.6975175779330200, -95.3949401089966500 29.6974269267953304, -95.3948740281415581 29.6972903308506346, -95.3946650813866910 29.6973397326847923, -95.3947654059391112 29.6974882560192022, -95.3949627316619768 29.6980355864961858, -95.3933200807862249 29.6984590863712796, -95.3932606497523494 29.6984464798710839, -95.3932983699113350 29.6983154306484352, -95.3933058014696655 29.6982165816983610, -95.3932946347785133 29.6981089778195759, -95.3931780601756287 29.6977068906794841, -95.3929928222970602 29.6977541771878180, -95.3930873169846478 29.6980676264932946, -95.3932743746374570 29.6981249406449663, -95.3929512584706316 29.6989526513922222, -95.3919850280655197 29.7014358632108646, -95.3918950918929056 29.7014169320765724, -95.3916928317890296 29.7019232352846423, -95.3915424614970959 29.7022988712928289, -95.3901530441668939 29.7058519502930061, -95.3899656322116698 29.7059156823562418, -95.3897628748670883 29.7059900058266777, -95.3896062677805787 29.7060738276384946, -95.3893941800512266 29.7061891695242046, -95.3892150365492455 29.7062641292949436, -95.3890502563035199 29.7063339729630940, -95.3888717930715586 29.7063896908080736, -95.3886925428988945 29.7064453871994978, -95.3885376849411983 29.7064797304524149, -95.3883284158984139 29.7065153575050189, -95.3881046767627794 29.7065368368267357, -95.3878809284696132 29.7065363048447537, -95.3876046356120924 29.7065288525102424, -95.3873060894974714 29.7064822806001452, -95.3869851943158409 29.7063993367575350, -95.3865967896568065 29.7062870572919202, -95.3861785624983156 29.7061492099008184, -95.3857375009733488 29.7059887337478798, -95.3854573290902152 29.7058683664514618, -95.3848703124799471 29.7056021479768511))", "n_i": 0, "ext_ring_cs": false, "n_p": 264, "area": 0.00129917360654, "centroid": [-95.403569179437341, 29.681772571690402]}
7
+ ],
8
+ "multipolygons": [
9
+ {"wkt": "MULTIPOLYGON (((100 20, 180 20, 180 100, 100 100, 100 20)), ((20 100, 100 100, 100 180, 20 180, 20 100)), ((100 180, 180 180, 180 260, 100 260, 100 180)), ((180 100, 260 100, 260 180, 180 180, 180 100)))","valid": true, "num_geom":4, "n_p":20},
10
+ {"wkt": "MULTIPOLYGON (((60 300, 320 220, 260 60, 60 100, 60 300)), ((60 300, 320 220, 260 60, 60 100, 60 300)))", "valid": false},
11
+ {"wkt": "MULTIPOLYGON (((180 60, 240 160, 300 60, 180 60)), ((80 80, 180 60, 160 140, 240 160, 360 140, 300 60, 420 100, 320 280, 120 260, 80 80)))", "valid": true, "num_geom": 2, "n_p": 14}
12
+ ],
13
+ "errors": [
14
+ {"wkt": "GEOMETR##!@#%#............a32515", "bad": true, "hex": false},
15
+ {"wkt": "Foo.Bar", "bad": true, "hex": false},
16
+ {"wkt": "POINT (5, 23)", "bad": true, "hex": false},
17
+ {"wkt": "AAABBBDDDAAD##@#1113511111-098111111111111111533333333333333", "bad": true, "hex": true},
18
+ {"wkt": "FFFFFFFFFFFFFFFFF1355555555555555555565111", "bad": true, "hex": true},
19
+ {"wkt": "", "bad": true, "hex": false}
20
+ ],
21
+ "wkt_out": [
22
+ {"wkt": "POINT (110 130)", "ewkt": "POINT (110 130)", "kml": "<Point><coordinates>110.0,130.0,0</coordinates></Point>", "gml": "<gml:Point><gml:coordinates>110,130</gml:coordinates></gml:Point>"},
23
+ {"wkt": "POINT (110 130 85)", "ewkt": "POINT Z (110 130 85)", "kml": "<Point><coordinates>110.0,130.0,85.0</coordinates></Point>", "gml": "<gml:Point><gml:coordinates>110,130,85</gml:coordinates></gml:Point>"},
24
+ {"wkt": "LINESTRING (40 40,50 130,130 130)", "ewkt": "LINESTRING (40 40, 50 130, 130 130)", "kml": "<LineString><coordinates>40.0,40.0,0 50.0,130.0,0 130.0,130.0,0</coordinates></LineString>", "gml": "<gml:LineString><gml:coordinates>40,40 50,130 130,130</gml:coordinates></gml:LineString>"},
25
+ {"wkt": "POLYGON ((150 150,410 150,280 20,20 20,150 150),(170 120,330 120,260 50,100 50,170 120))", "ewkt": "POLYGON ((150 150, 410 150, 280 20, 20 20, 150 150), (170 120, 330 120, 260 50, 100 50, 170 120))", "kml": "<Polygon><outerBoundaryIs><LinearRing><coordinates>150.0,150.0,0 410.0,150.0,0 280.0,20.0,0 20.0,20.0,0 150.0,150.0,0</coordinates></LinearRing></outerBoundaryIs><innerBoundaryIs><LinearRing><coordinates>170.0,120.0,0 330.0,120.0,0 260.0,50.0,0 100.0,50.0,0 170.0,120.0,0</coordinates></LinearRing></innerBoundaryIs></Polygon>", "gml": "<gml:Polygon><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>150,150 410,150 280,20 20,20 150,150</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs><gml:innerBoundaryIs><gml:LinearRing><gml:coordinates>170,120 330,120 260,50 100,50 170,120</gml:coordinates></gml:LinearRing></gml:innerBoundaryIs></gml:Polygon>"},
26
+ {"wkt": "MULTIPOINT (10 80,110 170,110 120)", "ewkt": "MULTIPOINT (10 80, 110 170, 110 120)", "kml": "<MultiGeometry><Point><coordinates>10.0,80.0,0</coordinates></Point><Point><coordinates>110.0,170.0,0</coordinates></Point><Point><coordinates>110.0,120.0,0</coordinates></Point></MultiGeometry>", "gml": "<gml:MultiPoint><gml:pointMember><gml:Point><gml:coordinates>10,80</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>110,170</gml:coordinates></gml:Point></gml:pointMember><gml:pointMember><gml:Point><gml:coordinates>110,120</gml:coordinates></gml:Point></gml:pointMember></gml:MultiPoint>"},
27
+ {"wkt": "MULTILINESTRING ((110 100,40 30,180 30),(170 30,110 90,50 30))", "ewkt": "MULTILINESTRING ((110 100, 40 30, 180 30), (170 30, 110 90, 50 30))", "kml": "<MultiGeometry><LineString><coordinates>110.0,100.0,0 40.0,30.0,0 180.0,30.0,0</coordinates></LineString><LineString><coordinates>170.0,30.0,0 110.0,90.0,0 50.0,30.0,0</coordinates></LineString></MultiGeometry>", "gml": "<gml:MultiLineString><gml:lineStringMember><gml:LineString><gml:coordinates>110,100 40,30 180,30</gml:coordinates></gml:LineString></gml:lineStringMember><gml:lineStringMember><gml:LineString><gml:coordinates>170,30 110,90 50,30</gml:coordinates></gml:LineString></gml:lineStringMember></gml:MultiLineString>"},
28
+ {"wkt": "MULTIPOLYGON (((110 110,70 200,150 200,110 110),(110 110,100 180,120 180,110 110)),((110 110,150 20,70 20,110 110),(110 110,120 40,100 40,110 110)))", "ewkt": "MULTIPOLYGON (((110 110, 70 200, 150 200, 110 110), (110 110, 100 180, 120 180, 110 110)), ((110 110, 150 20, 70 20, 110 110), (110 110, 120 40, 100 40, 110 110)))", "kml": "<MultiGeometry><Polygon><outerBoundaryIs><LinearRing><coordinates>110.0,110.0,0 70.0,200.0,0 150.0,200.0,0 110.0,110.0,0</coordinates></LinearRing></outerBoundaryIs><innerBoundaryIs><LinearRing><coordinates>110.0,110.0,0 100.0,180.0,0 120.0,180.0,0 110.0,110.0,0</coordinates></LinearRing></innerBoundaryIs></Polygon><Polygon><outerBoundaryIs><LinearRing><coordinates>110.0,110.0,0 150.0,20.0,0 70.0,20.0,0 110.0,110.0,0</coordinates></LinearRing></outerBoundaryIs><innerBoundaryIs><LinearRing><coordinates>110.0,110.0,0 120.0,40.0,0 100.0,40.0,0 110.0,110.0,0</coordinates></LinearRing></innerBoundaryIs></Polygon></MultiGeometry>", "gml": "<gml:MultiPolygon><gml:polygonMember><gml:Polygon><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>110,110 70,200 150,200 110,110</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs><gml:innerBoundaryIs><gml:LinearRing><gml:coordinates>110,110 100,180 120,180 110,110</gml:coordinates></gml:LinearRing></gml:innerBoundaryIs></gml:Polygon></gml:polygonMember><gml:polygonMember><gml:Polygon><gml:outerBoundaryIs><gml:LinearRing><gml:coordinates>110,110 150,20 70,20 110,110</gml:coordinates></gml:LinearRing></gml:outerBoundaryIs><gml:innerBoundaryIs><gml:LinearRing><gml:coordinates>110,110 120,40 100,40 110,110</gml:coordinates></gml:LinearRing></gml:innerBoundaryIs></gml:Polygon></gml:polygonMember></gml:MultiPolygon>"},
29
+ {"wkt": "GEOMETRYCOLLECTION (POINT (110 260),LINESTRING (110 0,110 60))", "ewkt": "GEOMETRYCOLLECTION (POINT (110 260), LINESTRING (110 0, 110 60))", "kml": "<MultiGeometry><Point><coordinates>110.0,260.0,0</coordinates></Point><LineString><coordinates>110.0,0.0,0 110.0,60.0,0</coordinates></LineString></MultiGeometry>", "gml": "<gml:MultiGeometry><gml:geometryMember><gml:Point><gml:coordinates>110,260</gml:coordinates></gml:Point></gml:geometryMember><gml:geometryMember><gml:LineString><gml:coordinates>110,0 110,60</gml:coordinates></gml:LineString></gml:geometryMember></gml:MultiGeometry>"}
30
+ ],
31
+ "hex_wkt": [
32
+ {"wkt": "POINT(0 1)", "hex": "01010000000000000000000000000000000000F03F"},
33
+ {"wkt": "POINT(0 1 5)", "hex": "01010000800000000000000000000000000000F03F0000000000001440"},
34
+ {"wkt": "LINESTRING(0 1, 2 3, 4 5)", "hex": "0102000000030000000000000000000000000000000000F03F0000000000000040000000000000084000000000000010400000000000001440"},
35
+ {"wkt": "POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))", "hex": "010300000001000000050000000000000000000000000000000000000000000000000024400000000000000000000000000000244000000000000024400000000000000000000000000000244000000000000000000000000000000000"},
36
+ {"wkt": "MULTIPOINT(0 0, 10 0, 10 10, 0 10, 0 0)", "hex": "010400000005000000010100000000000000000000000000000000000000010100000000000000000024400000000000000000010100000000000000000024400000000000002440010100000000000000000000000000000000002440010100000000000000000000000000000000000000"},
37
+ {"wkt": "MULTILINESTRING((0 0, 10 0, 10 10, 0 10),(20 20, 30 20))", "hex": "01050000000200000001020000000400000000000000000000000000000000000000000000000000244000000000000000000000000000002440000000000000244000000000000000000000000000002440010200000002000000000000000000344000000000000034400000000000003E400000000000003440"},
38
+ {"wkt": "MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0)),((20 20, 20 30, 30 30, 30 20, 20 20),(25 25, 25 26, 26 26, 26 25, 25 25)))", "hex": "010600000002000000010300000001000000050000000000000000000000000000000000000000000000000024400000000000000000000000000000244000000000000024400000000000000000000000000000244000000000000000000000000000000000010300000002000000050000000000000000003440000000000000344000000000000034400000000000003E400000000000003E400000000000003E400000000000003E40000000000000344000000000000034400000000000003440050000000000000000003940000000000000394000000000000039400000000000003A400000000000003A400000000000003A400000000000003A40000000000000394000000000000039400000000000003940"},
39
+ {"wkt": "GEOMETRYCOLLECTION(MULTIPOLYGON(((0 0, 10 0, 10 10, 0 10, 0 0)),((20 20, 20 30, 30 30, 30 20, 20 20),(25 25, 25 26, 26 26, 26 25, 25 25))),MULTILINESTRING((0 0, 10 0, 10 10, 0 10),(20 20, 30 20)),MULTIPOINT(0 0, 10 0, 10 10, 0 10, 0 0))", "hex": "010700000003000000010600000002000000010300000001000000050000000000000000000000000000000000000000000000000024400000000000000000000000000000244000000000000024400000000000000000000000000000244000000000000000000000000000000000010300000002000000050000000000000000003440000000000000344000000000000034400000000000003E400000000000003E400000000000003E400000000000003E40000000000000344000000000000034400000000000003440050000000000000000003940000000000000394000000000000039400000000000003A400000000000003A400000000000003A400000000000003A4000000000000039400000000000003940000000000000394001050000000200000001020000000400000000000000000000000000000000000000000000000000244000000000000000000000000000002440000000000000244000000000000000000000000000002440010200000002000000000000000000344000000000000034400000000000003E400000000000003440010400000005000000010100000000000000000000000000000000000000010100000000000000000024400000000000000000010100000000000000000024400000000000002440010100000000000000000000000000000000002440010100000000000000000000000000000000000000"}
40
+ ],
41
+ "json_geoms": [
42
+ {"wkt": "POINT(100 0)", "json": "{ \"type\": \"Point\", \"coordinates\": [ 100.000000, 0.000000 ] }"},
43
+ {"wkt": "POLYGON((0 0, -10 0, -10 -10, 0 -10, 0 0))", "json": "{ \"type\": \"Polygon\", \"coordinates\": [ [ [ 0.000000, 0.000000 ], [ -10.000000, 0.000000 ], [ -10.000000, -10.000000 ], [ 0.000000, -10.000000 ], [ 0.000000, 0.000000 ] ] ] }"},
44
+ {"wkt": "MULTIPOLYGON(((102 2, 103 2, 103 3, 102 3, 102 2)), ((100.0 0.0, 101.0 0.0, 101.0 1.0, 100.0 1.0, 100.0 0.0), (100.2 0.2, 100.8 0.2, 100.8 0.8, 100.2 0.8, 100.2 0.2)))", "json": "{ \"type\": \"MultiPolygon\", \"coordinates\": [ [ [ [ 102.000000, 2.000000 ], [ 103.000000, 2.000000 ], [ 103.000000, 3.000000 ], [ 102.000000, 3.000000 ], [ 102.000000, 2.000000 ] ] ], [ [ [ 100.000000, 0.000000 ], [ 101.000000, 0.000000 ], [ 101.000000, 1.000000 ], [ 100.000000, 1.000000 ], [ 100.000000, 0.000000 ] ], [ [ 100.200000, 0.200000 ], [ 100.800000, 0.200000 ], [ 100.800000, 0.800000 ], [ 100.200000, 0.800000 ], [ 100.200000, 0.200000 ] ] ] ] }"},
45
+ {"wkt": "GEOMETRYCOLLECTION(POINT(100 0),LINESTRING(101.0 0.0, 102.0 1.0))", "json": "{ \"type\": \"GeometryCollection\", \"geometries\": [ { \"type\": \"Point\", \"coordinates\": [ 100.000000, 0.000000 ] }, { \"type\": \"LineString\", \"coordinates\": [ [ 101.000000, 0.000000 ], [ 102.000000, 1.000000 ] ] } ] }"},
46
+ {"wkt": "MULTILINESTRING((100.0 0.0, 101.0 1.0),(102.0 2.0, 103.0 3.0))", "json": "\\n\\n{ \"type\": \"MultiLineString\",\\n \"coordinates\": [\\n [ [100.0, 0.0], [101.0, 1.0] ],\\n [ [102.0, 2.0], [103.0, 3.0] ]\\n ]\\n }\\n\\n", "not_equal": true}
47
+ ],
48
+ "points": [
49
+ {"wkt": "POINT (5 23)", "x": 5.0, "y": 23.0, "centroid": [5.0, 23.0]},
50
+ {"wkt": "POINT (-95.338492 29.723893)", "x": -95.338492, "y": 29.723893, "centroid": [-95.338492, 29.723893]},
51
+ {"wkt": "POINT(1.234 5.678)", "x": 1.234, "y": 5.678, "centroid": [1.234, 5.678]},
52
+ {"wkt": "POINT(4.321 8.765)", "x": 4.321, "y": 8.765, "centroid": [4.321, 8.765]},
53
+ {"wkt": "POINT(10 10)", "x": 10, "y": 10, "centroid": [10.0, 10.0]},
54
+ {"wkt": "POINT (5 23 8)", "x": 5.0, "y": 23.0, "z": 8.0, "centroid": [5.0, 23.0]}
55
+ ],
56
+ "multipoints":[
57
+ {"wkt": "MULTIPOINT(10 10, 20 20 )", "n_p": 2, "coords": [[10.0, 10.0], [20.0, 20.0]], "centroid": [15.0, 15.0]},
58
+ {"wkt": "MULTIPOINT(10 10, 20 20, 10 20, 20 10)", "n_p": 4, "coords": [[10.0, 10.0], [20.0, 20.0], [10.0, 20.0], [20.0, 10.0]], "centroid": [15.0, 15.0]}
59
+ ],
60
+ "linestrings": [
61
+ {"wkt": "LINESTRING (60 180, 120 100, 180 180)", "n_p": 3, "centroid": [120.0, 140.0], "coords": [[60.0, 180.0], [120.0, 100.0], [180.0, 180.0]]},
62
+ {"wkt": "LINESTRING (0 0, 5 5, 10 5, 10 10)", "n_p": 4, "centroid": [6.1611652351681556, 4.6966991411008934], "coords": [[0.0, 0.0], [5.0, 5.0], [10.0, 5.0], [10.0, 10.0]]}
63
+ ],
64
+ "linearrings": [
65
+ {"wkt": "LINEARRING (649899.3065171393100172 4176512.3807915160432458, 649902.7294133581453934 4176512.7834989596158266, 649906.5550170192727819 4176514.3942507002502680, 649910.5820134161040187 4176516.0050024418160319, 649914.4076170771149918 4176518.0184616246260703, 649917.2264131171396002 4176519.4278986593708396, 649920.0452871860470623 4176521.6427505780011415, 649922.0587463703704998 4176522.8507948759943247, 649924.2735982896992937 4176524.4616246484220028, 649926.2870574744883925 4176525.4683542405255139, 649927.8978092158213258 4176526.8777912775985897, 649929.3072462501004338 4176528.0858355751261115, 649930.1126611357321963 4176529.4952726080082357, 649927.4951798024121672 4176506.9444361114874482, 649899.3065171393100172 4176512.3807915160432458)", "n_p": 15}
66
+ ],
67
+ "multilinestrings": [
68
+ {"wkt": "MULTILINESTRING ((0 0, 0 100), (100 0, 100 100))", "n_p": 4, "centroid": [50.0, 50.0], "coords": [[[0, 0], [0, 100]], [[100, 0], [100, 100]]]},
69
+ {"wkt": "MULTILINESTRING ((20 20, 60 60), (20 -20, 60 -60), (-20 -20, -60 -60), (-20 20, -60 60), (-80 0, 0 80, 80 0, 0 -80, -80 0), (-40 20, -40 -20), (-20 40, 20 40), (40 20, 40 -20), (20 -40, -20 -40))", "n_p": 21, "centroid": [0.0, 0.0], "coords": [[[20.0, 20.0], [60.0, 60.0]], [[20.0, -20.0], [60.0, -60.0]], [[-20.0, -20.0], [-60.0, -60.0]], [[-20.0, 20.0], [-60.0, 60.0]], [[-80.0, 0.0], [0.0, 80.0], [80.0, 0.0], [0.0, -80.0], [-80.0, 0.0]], [[-40.0, 20.0], [-40.0, -20.0]], [[-20.0, 40.0], [20.0, 40.0]], [[40.0, 20.0], [40.0, -20.0]], [[20.0, -40.0], [-20.0, -40.0]]]}
70
+ ],
71
+ "buffer_geoms": [
72
+ {"wkt": "POINT(0 0)",
73
+ "buffer_wkt": "POLYGON ((5 0,4.903926402016153 -0.97545161008064,4.619397662556435 -1.913417161825447,4.157348061512728 -2.777851165098009,3.53553390593274 -3.535533905932735,2.777851165098015 -4.157348061512724,1.913417161825454 -4.619397662556431,0.975451610080648 -4.903926402016151,0.000000000000008 -5.0,-0.975451610080632 -4.903926402016154,-1.913417161825439 -4.619397662556437,-2.777851165098002 -4.157348061512732,-3.53553390593273 -3.535533905932746,-4.157348061512719 -2.777851165098022,-4.619397662556429 -1.913417161825462,-4.903926402016149 -0.975451610080656,-5.0 -0.000000000000016,-4.903926402016156 0.975451610080624,-4.619397662556441 1.913417161825432,-4.157348061512737 2.777851165097995,-3.535533905932752 3.535533905932723,-2.777851165098029 4.157348061512714,-1.913417161825468 4.619397662556426,-0.975451610080661 4.903926402016149,-0.000000000000019 5.0,0.975451610080624 4.903926402016156,1.913417161825434 4.61939766255644,2.777851165097998 4.157348061512735,3.535533905932727 3.535533905932748,4.157348061512719 2.777851165098022,4.619397662556429 1.91341716182546,4.90392640201615 0.975451610080652,5 0))",
74
+ "width": 5.0, "quadsegs": 8
75
+ },
76
+ {"wkt": "POLYGON((0 0, 10 0, 10 10, 0 10, 0 0))",
77
+ "buffer_wkt": "POLYGON ((-2 0,-2 10,-1.961570560806461 10.390180644032258,-1.847759065022573 10.765366864730179,-1.662939224605091 11.111140466039204,-1.414213562373095 11.414213562373096,-1.111140466039204 11.662939224605092,-0.765366864730179 11.847759065022574,-0.390180644032256 11.961570560806461,0 12,10 12,10.390180644032256 11.961570560806461,10.765366864730179 11.847759065022574,11.111140466039204 11.66293922460509,11.414213562373096 11.414213562373096,11.66293922460509 11.111140466039204,11.847759065022574 10.765366864730179,11.961570560806461 10.390180644032256,12 10,12 0,11.961570560806461 -0.390180644032256,11.847759065022574 -0.76536686473018,11.66293922460509 -1.111140466039204,11.414213562373096 -1.414213562373095,11.111140466039204 -1.66293922460509,10.765366864730179 -1.847759065022573,10.390180644032256 -1.961570560806461,10 -2,0.0 -2.0,-0.390180644032255 -1.961570560806461,-0.765366864730177 -1.847759065022575,-1.1111404660392 -1.662939224605093,-1.41421356237309 -1.4142135623731,-1.662939224605086 -1.111140466039211,-1.84775906502257 -0.765366864730189,-1.961570560806459 -0.390180644032268,-2 0))",
78
+ "width": 2.0, "quadsegs": 8
79
+ }
80
+ ],
81
+ "buffer_with_style_geoms": [
82
+ {"wkt": "POINT (0 0)",
83
+ "buffer_wkt": "POLYGON EMPTY",
84
+ "width": 5.0, "end_cap_style": 2, "join_style": 2
85
+ },
86
+ {"wkt": "POLYGON ((0 0, 10 0, 10 10, 0 10, 0 0))",
87
+ "buffer_wkt": "POLYGON ((-2 -2, -2 12, 12 12, 12 -2, -2 -2))",
88
+ "width": 2.0, "end_cap_style": 2, "join_style": 2
89
+ },
90
+ {"wkt": "LINESTRING (0 0, 10 0)",
91
+ "buffer_wkt": "POLYGON ((10 2, 10 -2, 0 -2, 0 2, 10 2))",
92
+ "width": 2.0, "end_cap_style": 2, "join_style": 2
93
+ },
94
+ {"wkt": "LINESTRING (0 0, 10 0, 10 10, 0 10)",
95
+ "buffer_wkt": "POLYGON ((8 2, 8 8, 0 8, 0 12, 12 12, 12 -2, 0 -2, 0 2, 8 2))",
96
+ "width": 2.0, "end_cap_style": 2, "join_style": 2
97
+ }
98
+ ],
99
+ "relate_geoms": [
100
+ {"wkt_a": "MULTIPOINT(80 70, 20 20, 200 170, 140 120)",
101
+ "wkt_b": "MULTIPOINT(80 170, 140 120, 200 80, 80 70)",
102
+ "pattern": "0F0FFF0F2", "result": true
103
+ },
104
+ {"wkt_a": "POINT(20 20)",
105
+ "wkt_b": "POINT(40 60)",
106
+ "pattern": "FF0FFF0F2", "result": true
107
+ },
108
+ {"wkt_a": "POINT(110 110)",
109
+ "wkt_b": "LINESTRING(200 200, 110 110, 200 20, 20 20, 110 110, 20 200, 200 200)",
110
+ "pattern": "0FFFFF1F2", "result": true
111
+ },
112
+ {"wkt_a": "MULTILINESTRING((20 20, 90 20, 170 20), (90 20, 90 80, 90 140))",
113
+ "wkt_b": "MULTILINESTRING((90 20, 170 100, 170 140), (130 140, 130 60, 90 20, 20 90, 90 20))",
114
+ "pattern": "FF10F0102", "result": true
115
+ }
116
+ ],
117
+ "topology_geoms": [
118
+ {"wkt_a": "POLYGON ((-5.0 0.0, -5.0 10.0, 5.0 10.0, 5.0 0.0, -5.0 0.0))",
119
+ "wkt_b": "POLYGON ((0.0 -5.0, 0.0 5.0, 10.0 5.0, 10.0 -5.0, 0.0 -5.0))"
120
+ },
121
+ {"wkt_a": "POLYGON ((2 0, 18 0, 18 15, 2 15, 2 0))",
122
+ "wkt_b": "POLYGON ((10 1, 11 3, 13 4, 15 6, 16 8, 16 10, 15 12, 13 13, 11 12, 10 10, 9 12, 7 13, 5 12, 4 10, 4 8, 5 6, 7 4, 9 3, 10 1))"
123
+ }
124
+ ],
125
+ "diff_geoms": [
126
+ {"wkt": "POLYGON ((-5 0,-5 10,5 10,5 5,0 5,0 0,-5 0))"},
127
+ {"wkt": "POLYGON ((2 0, 2 15, 18 15, 18 0, 2 0), (10 1, 11 3, 13 4, 15 6, 16 8, 16 10, 15 12, 13 13, 11 12, 10 10, 9 12, 7 13, 5 12, 4 10, 4 8, 5 6, 7 4, 9 3, 10 1))"}
128
+ ],
129
+ "sdiff_geoms": [
130
+ {"wkt": "MULTIPOLYGON (((-5 0,-5 10,5 10,5 5,0 5,0 0,-5 0)),((0 0,5 0,5 5,10 5,10 -5,0 -5,0 0)))"},
131
+ {"wkt": "POLYGON ((2 0, 2 15, 18 15, 18 0, 2 0), (10 1, 11 3, 13 4, 15 6, 16 8, 16 10, 15 12, 13 13, 11 12, 10 10, 9 12, 7 13, 5 12, 4 10, 4 8, 5 6, 7 4, 9 3, 10 1))"}
132
+ ],
133
+ "intersect_geoms": [
134
+ {"wkt": "POLYGON ((5 5,5 0,0 0,0 5,5 5))"},
135
+ {"wkt": "POLYGON ((10 1, 9 3, 7 4, 5 6, 4 8, 4 10, 5 12, 7 13, 9 12, 10 10, 11 12, 13 13, 15 12, 16 10, 16 8, 15 6, 13 4, 11 3, 10 1))"}
136
+ ],
137
+ "union_geoms": [
138
+ {"wkt": "POLYGON ((-5 0,-5 10,5 10,5 5,10 5,10 -5,0 -5,0 0,-5 0))"},
139
+ {"wkt": "POLYGON ((2 0, 2 15, 18 15, 18 0, 2 0))"}
140
+ ]
141
+ }
testbed/django__django/tests/gis_tests/data/has_nulls/has_nulls.geojson ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "type": "FeatureCollection",
3
+ "name": "has_nulls",
4
+ "crs": {
5
+ "type": "name",
6
+ "properties": {
7
+ "name": "urn:ogc:def:crs:OGC:1.3:CRS84"
8
+ }
9
+ },
10
+ "features": [
11
+ {
12
+ "type": "Feature",
13
+ "properties": {
14
+ "uuid": "1378c26f-cbe6-44b0-929f-eb330d4991f5",
15
+ "boolean": true,
16
+ "datetime": "1994-08-14T11:32:14+0000",
17
+ "name": "Philadelphia",
18
+ "integer": 5,
19
+ "num": 1.001
20
+ },
21
+ "geometry": {
22
+ "type": "Polygon",
23
+ "coordinates": [
24
+ [
25
+ [
26
+ -75.14923095703125,
27
+ 39.8928799002948
28
+ ],
29
+ [
30
+ -75.25772094726562,
31
+ 39.889718875996685
32
+ ],
33
+ [
34
+ -75.201416015625,
35
+ 40.03076909369051
36
+ ],
37
+ [
38
+ -75.07644653320312,
39
+ 39.99500778093748
40
+ ],
41
+ [
42
+ -75.14785766601562,
43
+ 39.95291166179976
44
+ ],
45
+ [
46
+ -75.135498046875,
47
+ 39.91605629078665
48
+ ],
49
+ [
50
+ -75.14923095703125,
51
+ 39.8928799002948
52
+ ]
53
+ ]
54
+ ]
55
+ }
56
+ },
57
+ {
58
+ "type": "Feature",
59
+ "properties": {
60
+ "uuid": "fa2ba67c-a135-4338-b924-a9622b5d869f",
61
+ "integer": null,
62
+ "num": null
63
+ },
64
+ "geometry": {
65
+ "type": "Polygon",
66
+ "coordinates": [
67
+ [
68
+ [
69
+ -75.11215209960938,
70
+ 39.94870062390347
71
+ ],
72
+ [
73
+ -75.02838134765625,
74
+ 39.990799335838034
75
+ ],
76
+ [
77
+ -74.959716796875,
78
+ 39.920269337633975
79
+ ],
80
+ [
81
+ -75.025634765625,
82
+ 39.8465036024177
83
+ ],
84
+ [
85
+ -75.12725830078125,
86
+ 39.871803651624425
87
+ ],
88
+ [
89
+ -75.11215209960938,
90
+ 39.94870062390347
91
+ ]
92
+ ]
93
+ ]
94
+ }
95
+ },
96
+ {
97
+ "type": "Feature",
98
+ "properties": {
99
+ "uuid": "4494c1f3-55ab-4256-b365-12115cb388d5",
100
+ "datetime": "2018-11-29T03:02:52+0000",
101
+ "name": "north",
102
+ "integer": 8,
103
+ "num": 0.0,
104
+ "boolean": false
105
+ },
106
+ "geometry": {
107
+ "type": "Polygon",
108
+ "coordinates": [
109
+ [
110
+ [
111
+ -75.24948120117188,
112
+ 39.99395569397331
113
+ ],
114
+ [
115
+ -75.27420043945312,
116
+ 40.0507451947963
117
+ ],
118
+ [
119
+ -75.20553588867188,
120
+ 40.119040222688795
121
+ ],
122
+ [
123
+ -75.08193969726562,
124
+ 40.03287211677227
125
+ ],
126
+ [
127
+ -75.16433715820312,
128
+ 39.97396296240704
129
+ ],
130
+ [
131
+ -75.24948120117188,
132
+ 39.99395569397331
133
+ ]
134
+ ]
135
+ ]
136
+ }
137
+ }
138
+ ]
139
+ }
testbed/django__django/tests/gis_tests/data/interstates/interstates.dbf ADDED
Binary file (412 Bytes). View file
 
testbed/django__django/tests/gis_tests/data/interstates/interstates.prj ADDED
@@ -0,0 +1 @@
 
 
1
+ GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
testbed/django__django/tests/gis_tests/data/interstates/interstates.shp ADDED
Binary file (892 Bytes). View file
 
testbed/django__django/tests/gis_tests/data/interstates/interstates.shx ADDED
Binary file (124 Bytes). View file
 
testbed/django__django/tests/gis_tests/data/invalid/emptypoints.dbf ADDED
Binary file (77 Bytes). View file
 
testbed/django__django/tests/gis_tests/data/invalid/emptypoints.shp ADDED
Binary file (112 Bytes). View file
 
testbed/django__django/tests/gis_tests/data/invalid/emptypoints.shx ADDED
Binary file (108 Bytes). View file
 
testbed/django__django/tests/gis_tests/data/rasters/__init__.py ADDED
File without changes
testbed/django__django/tests/gis_tests/data/rasters/raster.numpy.txt ADDED
The diff for this file is too large to render. See raw diff
 
testbed/django__django/tests/gis_tests/data/rasters/raster.tif ADDED
testbed/django__django/tests/gis_tests/data/rasters/textrasters.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Text-based test rasters
3
+ """
4
+
5
+ JSON_RASTER = """{
6
+ "srid": 4326,
7
+ "origin": [0, 0],
8
+ "scale": [-1, 1],
9
+ "skew": [0, 0],
10
+ "width": 5,
11
+ "height": 5,
12
+ "nr_of_bands": 1,
13
+ "bands": [{"data": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
14
+ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]}]
15
+ }
16
+ """
testbed/django__django/tests/gis_tests/data/test_point/test_point.dbf ADDED
Binary file (749 Bytes). View file
 
testbed/django__django/tests/gis_tests/data/test_point/test_point.prj ADDED
@@ -0,0 +1 @@
 
 
1
+ GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
testbed/django__django/tests/gis_tests/data/test_point/test_point.shp ADDED
Binary file (240 Bytes). View file
 
testbed/django__django/tests/gis_tests/data/test_point/test_point.shx ADDED
Binary file (140 Bytes). View file
 
testbed/django__django/tests/gis_tests/data/test_poly/test_poly.dbf ADDED
Binary file (501 Bytes). View file
 
testbed/django__django/tests/gis_tests/data/test_poly/test_poly.prj ADDED
@@ -0,0 +1 @@
 
 
1
+ GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",0.017453292519943295]]
testbed/django__django/tests/gis_tests/data/test_poly/test_poly.shp ADDED
Binary file (620 Bytes). View file
 
testbed/django__django/tests/gis_tests/data/test_poly/test_poly.shx ADDED
Binary file (124 Bytes). View file
 
testbed/django__django/tests/gis_tests/data/test_vrt/test_vrt.csv ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ POINT_X,POINT_Y,NUM
2
+ 1.0,2.0,5
3
+ 5.0,23.0,17
4
+ 100.0,523.5,23
testbed/django__django/tests/gis_tests/data/test_vrt/test_vrt.vrt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ <OGRVRTDataSource>
2
+ <OGRVRTLayer name="test_vrt">
3
+ <SrcDataSource relativeToVRT="1">test_vrt.csv</SrcDataSource>
4
+ <GeometryType>wkbPoint25D</GeometryType>
5
+ <GeometryField encoding="PointFromColumns" x="POINT_X" y="POINT_Y" z="NUM"/>
6
+ </OGRVRTLayer>
7
+ </OGRVRTDataSource>
testbed/django__django/tests/gis_tests/data/texas.dbf ADDED
Binary file (660 Bytes). View file
 
testbed/django__django/tests/gis_tests/distapp/__init__.py ADDED
File without changes
testbed/django__django/tests/gis_tests/distapp/fixtures/initial.json ADDED
@@ -0,0 +1,314 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "pk": 1,
4
+ "model": "distapp.southtexascity",
5
+ "fields": {
6
+ "name": "Downtown Houston",
7
+ "point": "SRID=32140;POINT (951640.5473284651525319 4219369.2617166414856911)"
8
+ }
9
+ },
10
+ {
11
+ "pk": 2,
12
+ "model": "distapp.southtexascity",
13
+ "fields": {
14
+ "name": "West University Place",
15
+ "point": "SRID=32140;POINT (943547.9223275661934167 4213623.6534501984715462)"
16
+ }
17
+ },
18
+ {
19
+ "pk": 3,
20
+ "model": "distapp.southtexascity",
21
+ "fields": {
22
+ "name": "Southside Place",
23
+ "point": "SRID=32140;POINT (944704.6433068963233382 4212768.8761719493195415)"
24
+ }
25
+ },
26
+ {
27
+ "pk": 4,
28
+ "model": "distapp.southtexascity",
29
+ "fields": {
30
+ "name": "Bellaire",
31
+ "point": "SRID=32140;POINT (942595.6691285020206124 4212686.7258348800241947)"
32
+ }
33
+ },
34
+ {
35
+ "pk": 5,
36
+ "model": "distapp.southtexascity",
37
+ "fields": {
38
+ "name": "Pearland",
39
+ "point": "SRID=32140;POINT (959674.6165056140162051 4197465.6525986101478338)"
40
+ }
41
+ },
42
+ {
43
+ "pk": 6,
44
+ "model": "distapp.southtexascity",
45
+ "fields": {
46
+ "name": "Galveston",
47
+ "point": "SRID=32140;POINT (1008151.1600717274704948 4170027.4765489147976041)"
48
+ }
49
+ },
50
+ {
51
+ "pk": 7,
52
+ "model": "distapp.southtexascity",
53
+ "fields": {
54
+ "name": "Sealy",
55
+ "point": "SRID=32140;POINT (874859.2868079885374755 4219186.8640952249988914)"
56
+ }
57
+ },
58
+ {
59
+ "pk": 8,
60
+ "model": "distapp.southtexascity",
61
+ "fields": {
62
+ "name": "San Antonio",
63
+ "point": "SRID=32140;POINT (649173.9104827501578256 4176413.2778564509935677)"
64
+ }
65
+ },
66
+ {
67
+ "pk": 9,
68
+ "model": "distapp.southtexascity",
69
+ "fields": {
70
+ "name": "Saint Hedwig",
71
+ "point": "SRID=32140;POINT (677644.6499522707890719 4175467.0674387565813959)"
72
+ }
73
+ },
74
+ {
75
+ "pk": 1,
76
+ "model": "distapp.southtexascityft",
77
+ "fields": {
78
+ "name": "Downtown Houston",
79
+ "point": "SRID=2278;POINT (3122174.0290268082171679 13843047.3191486913710833)"
80
+ }
81
+ },
82
+ {
83
+ "pk": 2,
84
+ "model": "distapp.southtexascityft",
85
+ "fields": {
86
+ "name": "West University Place",
87
+ "point": "SRID=2278;POINT (3095623.4751696921885014 13824196.9360278695821762)"
88
+ }
89
+ },
90
+ {
91
+ "pk": 3,
92
+ "model": "distapp.southtexascityft",
93
+ "fields": {
94
+ "name": "Southside Place",
95
+ "point": "SRID=2278;POINT (3099418.4839160442352295 13821392.5542408134788275)"
96
+ }
97
+ },
98
+ {
99
+ "pk": 4,
100
+ "model": "distapp.southtexascityft",
101
+ "fields": {
102
+ "name": "Bellaire",
103
+ "point": "SRID=2278;POINT (3092499.2911324291490018 13821123.0326766129583120)"
104
+ }
105
+ },
106
+ {
107
+ "pk": 5,
108
+ "model": "distapp.southtexascityft",
109
+ "fields": {
110
+ "name": "Pearland",
111
+ "point": "SRID=2278;POINT (3148532.4709855038672686 13771185.2282339502125978)"
112
+ }
113
+ },
114
+ {
115
+ "pk": 6,
116
+ "model": "distapp.southtexascityft",
117
+ "fields": {
118
+ "name": "Galveston",
119
+ "point": "SRID=2278;POINT (3307575.9310019947588444 13681165.1456442419439554)"
120
+ }
121
+ },
122
+ {
123
+ "pk": 7,
124
+ "model": "distapp.southtexascityft",
125
+ "fields": {
126
+ "name": "Sealy",
127
+ "point": "SRID=2278;POINT (2870267.5101358774118125 13842448.9029524270445108)"
128
+ }
129
+ },
130
+ {
131
+ "pk": 8,
132
+ "model": "distapp.southtexascityft",
133
+ "fields": {
134
+ "name": "San Antonio",
135
+ "point": "SRID=2278;POINT (2129831.4046421577222645 13702115.8954340498894453)"
136
+ }
137
+ },
138
+ {
139
+ "pk": 9,
140
+ "model": "distapp.southtexascityft",
141
+ "fields": {
142
+ "name": "Saint Hedwig",
143
+ "point": "SRID=2278;POINT (2223239.1557184099219739 13699011.5367553308606148)"
144
+ }
145
+ },
146
+ {
147
+ "pk": 1,
148
+ "model": "distapp.australiacity",
149
+ "fields": {
150
+ "name": "Wollongong",
151
+ "point": "SRID=4326;POINT (150.9019999999999868 -34.4245000000000019)"
152
+ }
153
+ },
154
+ {
155
+ "pk": 2,
156
+ "model": "distapp.australiacity",
157
+ "fields": {
158
+ "name": "Shellharbour",
159
+ "point": "SRID=4326;POINT (150.8700000000000045 -34.5788999999999973)"
160
+ }
161
+ },
162
+ {
163
+ "pk": 3,
164
+ "model": "distapp.australiacity",
165
+ "fields": {
166
+ "name": "Thirroul",
167
+ "point": "SRID=4326;POINT (150.9240000000000066 -34.3147000000000020)"
168
+ }
169
+ },
170
+ {
171
+ "pk": 4,
172
+ "model": "distapp.australiacity",
173
+ "fields": {
174
+ "name": "Mittagong",
175
+ "point": "SRID=4326;POINT (150.4490000000000123 -34.4508999999999972)"
176
+ }
177
+ },
178
+ {
179
+ "pk": 5,
180
+ "model": "distapp.australiacity",
181
+ "fields": {
182
+ "name": "Batemans Bay",
183
+ "point": "SRID=4326;POINT (150.1750000000000114 -35.7081999999999979)"
184
+ }
185
+ },
186
+ {
187
+ "pk": 6,
188
+ "model": "distapp.australiacity",
189
+ "fields": {
190
+ "name": "Canberra",
191
+ "point": "SRID=4326;POINT (144.9629999999999939 -37.8143000000000029)"
192
+ }
193
+ },
194
+ {
195
+ "pk": 7,
196
+ "model": "distapp.australiacity",
197
+ "fields": {
198
+ "name": "Melbourne",
199
+ "point": "SRID=4326;POINT (145.9629999999999939 -37.8143000000000029)"
200
+ }
201
+ },
202
+ {
203
+ "pk": 8,
204
+ "model": "distapp.australiacity",
205
+ "fields": {
206
+ "name": "Sydney",
207
+ "point": "SRID=4326;POINT (151.2607099999999889 -33.8870339999999999)"
208
+ }
209
+ },
210
+ {
211
+ "pk": 9,
212
+ "model": "distapp.australiacity",
213
+ "fields": {
214
+ "name": "Hobart",
215
+ "point": "SRID=4326;POINT (147.3300000000000125 -42.8826999999999998)"
216
+ }
217
+ },
218
+ {
219
+ "pk": 10,
220
+ "model": "distapp.australiacity",
221
+ "fields": {
222
+ "name": "Adelaide",
223
+ "point": "SRID=4326;POINT (138.5999999999999943 -34.9258000000000024)"
224
+ }
225
+ },
226
+ {
227
+ "pk": 11,
228
+ "model": "distapp.australiacity",
229
+ "fields": {
230
+ "name": "Hillsdale",
231
+ "point": "SRID=4326;POINT (151.2313409999999863 -33.9526850000000024)"
232
+ }
233
+ },
234
+ {
235
+ "pk": 1,
236
+ "model": "distapp.censuszipcode",
237
+ "fields": {
238
+ "name": "77002",
239
+ "poly": "SRID=4269;POLYGON ((-95.3650149999999996 29.7723270000000007, -95.3624149999999986 29.7723270000000007, -95.3609150000000056 29.7718269999999983, -95.3546149999999955 29.7718269999999983, -95.3515150000000062 29.7725270000000002, -95.3509150000000005 29.7653269999999992, -95.3510150000000039 29.7624360000000010, -95.3501150000000024 29.7603280000000012, -95.3475150000000014 29.7585279999999983, -95.3523150000000044 29.7539279999999984, -95.3564149999999984 29.7563279999999999, -95.3582150000000013 29.7540280000000017, -95.3602149999999966 29.7563279999999999, -95.3634150000000034 29.7571280000000016, -95.3640139999999974 29.7563800000000001, -95.3634150000000034 29.7539279999999984, -95.3600150000000042 29.7518279999999997, -95.3618150000000071 29.7495280000000015, -95.3627149999999943 29.7500280000000004, -95.3675159999999948 29.7441279999999999, -95.3693159999999978 29.7451280000000011, -95.3739159999999941 29.7441279999999999, -95.3801160000000010 29.7380279999999999, -95.3879160000000041 29.7279289999999996, -95.3885159999999956 29.7296289999999992, -95.3879160000000041 29.7321290000000005, -95.3829159999999945 29.7374280000000013, -95.3766159999999985 29.7422280000000008, -95.3726159999999936 29.7472279999999998, -95.3786010000000033 29.7508459999999992, -95.3786159999999938 29.7520279999999993, -95.3786159999999938 29.7544280000000008, -95.3760160000000070 29.7545280000000005, -95.3746160000000032 29.7598279999999988, -95.3736159999999984 29.7611279999999994, -95.3719159999999988 29.7639279999999999, -95.3723159999999979 29.7687269999999984, -95.3658839999999941 29.7679100000000005, -95.3660150000000044 29.7671269999999986, -95.3587150000000037 29.7653269999999992, -95.3586150000000004 29.7663270000000004, -95.3591150000000027 29.7672269999999983, -95.3602149999999966 29.7670269999999988, -95.3627829999999932 29.7682670000000016, -95.3653149999999954 29.7705270000000013, -95.3650149999999996 29.7723270000000007))"
240
+ }
241
+ },
242
+ {
243
+ "pk": 2,
244
+ "model": "distapp.censuszipcode",
245
+ "fields": {
246
+ "name": "77005",
247
+ "poly": "SRID=4269;POLYGON ((-95.4479180000000014 29.7272749999999988, -95.4280169999999970 29.7287290000000013, -95.4211169999999953 29.7290290000000006, -95.4186169999999976 29.7276290000000003, -95.4185169999999943 29.7264289999999995, -95.4021170000000041 29.7266289999999991, -95.4021170000000041 29.7257290000000012, -95.3953159999999940 29.7257290000000012, -95.3919159999999948 29.7262290000000000, -95.3897160000000071 29.7258290000000009, -95.3965170000000029 29.7154290000000003, -95.3975169999999935 29.7159289999999991, -95.4009170000000069 29.7114289999999990, -95.4114170000000001 29.7150290000000012, -95.4184170000000051 29.7147289999999984, -95.4183170000000018 29.7062300000000015, -95.4408179999999930 29.7059299999999986, -95.4450180000000046 29.7068300000000001, -95.4466180000000008 29.7076300000000018, -95.4474179999999990 29.7100299999999997, -95.4479180000000014 29.7272749999999988))"
248
+ }
249
+ },
250
+ {
251
+ "pk": 3,
252
+ "model": "distapp.censuszipcode",
253
+ "fields": {
254
+ "name": "77025",
255
+ "poly": "SRID=4269;POLYGON ((-95.4183170000000018 29.7062300000000015, -95.4147169999999960 29.7061290000000007, -95.4146170000000069 29.7053300000000000, -95.4182169999999985 29.7053300000000000, -95.4198169999999948 29.6953299999999984, -95.4194839999999971 29.6941960000000016, -95.4171659999999946 29.6909010000000002, -95.4145170000000036 29.6943300000000008, -95.4133170000000064 29.6926300000000012, -95.4126169999999973 29.6897300000000008, -95.4128170000000040 29.6875299999999989, -95.4140869999999950 29.6850549999999984, -95.4191650000000067 29.6854280000000017, -95.4216169999999977 29.6851300000000009, -95.4257170000000059 29.6798299999999990, -95.4250169999999969 29.6792300000000004, -95.4245169999999945 29.6776300000000006, -95.4274180000000030 29.6776300000000006, -95.4380179999999996 29.6646310000000000, -95.4367129999999975 29.6644110000000012, -95.4401179999999982 29.6622309999999985, -95.4392179999999968 29.6610310000000013, -95.4377180000000038 29.6601309999999998, -95.4357179999999943 29.6597310000000007, -95.4318180000000069 29.6603309999999993, -95.4414179999999988 29.6566310000000009, -95.4413179999999954 29.6563310000000016, -95.4418179999999978 29.6561309999999985, -95.4417179999999945 29.6590309999999988, -95.4411180000000030 29.6610310000000013, -95.4467180000000042 29.6564310000000013, -95.4465179999999975 29.6734310000000008, -95.4469179999999966 29.6901299999999999, -95.4474179999999990 29.7100299999999997, -95.4466180000000008 29.7076300000000018, -95.4450180000000046 29.7068300000000001, -95.4408179999999930 29.7059299999999986, -95.4183170000000018 29.7062300000000015))"
256
+ }
257
+ },
258
+ {
259
+ "pk": 4,
260
+ "model": "distapp.censuszipcode",
261
+ "fields": {
262
+ "name": "77401",
263
+ "poly": "SRID=4269;POLYGON ((-95.4479180000000014 29.7272749999999988, -95.4474179999999990 29.7100299999999997, -95.4469179999999966 29.6901299999999999, -95.4543180000000007 29.6889299999999992, -95.4758190000000013 29.6890299999999989, -95.4758190000000013 29.6911300000000011, -95.4844190000000026 29.6910300000000014, -95.4845190000000059 29.6990300000000005, -95.4804189999999977 29.7013299999999987, -95.4804189999999977 29.6983299999999986, -95.4741190000000017 29.6983299999999986, -95.4741190000000017 29.7045299999999983, -95.4727189999999979 29.7128300000000003, -95.4680189999999982 29.7129300000000001, -95.4682190000000048 29.7202289999999998, -95.4640179999999958 29.7202289999999998, -95.4641179999999991 29.7245290000000004, -95.4630180000000053 29.7259290000000007, -95.4598179999999985 29.7261290000000002, -95.4599180000000018 29.7203289999999996, -95.4514180000000039 29.7204289999999993, -95.4517749999999978 29.7263030000000015, -95.4513180000000006 29.7270290000000017, -95.4479180000000014 29.7272749999999988))"
264
+ }
265
+ },
266
+ {
267
+ "pk": 1,
268
+ "model": "distapp.southtexaszipcode",
269
+ "fields": {
270
+ "name": "77002",
271
+ "poly": "SRID=32140;POLYGON ((951429.5409192872466519 4220355.4988148519769311, 951680.8273045794339851 4220363.3144243787974119, 951827.5242763287387788 4220312.4344643373042345, 952436.4125371500849724 4220331.4033580468967557, 952733.6038219904294237 4220418.2972162021324039, 952816.4884229446761310 4219622.4721539746969938, 952816.8189755391795188 4219301.8983748322352767, 952911.1013538520783186 4219071.0846072295680642, 953168.6460092773195356 4218879.5251544509083033, 952720.5711025173077360 4218355.4412181628867984, 952315.9552874444052577 4218608.9603398498147726, 952149.8976783070247620 4218348.7396276174113154, 951948.6335086676990613 4218597.5187743240967393, 951636.5529057731619105 4218676.5191153362393379, 951581.2288653708528727 4218591.8528460673987865, 951647.5797480825567618 4218322.0148884318768978, 951983.4872268696781248 4218099.5997478468343616, 951817.4139249715954065 4217839.3842275496572256, 951728.6875000044237822 4217892.0683275610208511, 951284.8837139783427119 4217224.0225605471059680, 951107.4276299364864826 4217329.3996630022302270, 950666.1605060384608805 4217204.8158583818003535, 950087.6947283914778382 4216510.4676794996485114, 949368.1435158180538565 4215368.3503736928105354, 949304.3111934799235314 4215554.8883790764957666, 949353.7683630869723856 4215833.6369291869923472, 949819.0428102404111996 4216435.6196845080703497, 950411.6597438841126859 4216986.2362841432914138, 950781.1835370573680848 4217552.1403498277068138, 950190.1823301381664351 4217935.0160658890381455, 950184.6762747208122164 4218065.9165289513766766, 950176.4407065160339698 4218331.7956013763323426, 950427.4286870528012514 4218350.6616439744830132, 950544.5537801468744874 4218942.0070019531995058, 950636.7473976470064372 4219089.0227092057466507, 950791.4425049067940563 4219404.3132666023448110, 950736.2833876821678132 4219934.7619700236245990, 951360.7634590273955837 4219863.5588980689644814, 951350.7981466529890895 4219776.4222313342615962, 952062.5793334231711924 4219598.9663440436124802, 952068.7943994677625597 4219710.0501319468021393, 952017.3623401129152626 4219808.2495522042736411, 951911.7334559946320951 4219782.7825336037203670, 951659.2534216577187181 4219912.4288446288555861, 951406.7457121167099103 4220155.1877524554729462, 951429.5409192872466519 4220355.4988148519769311))"
272
+ }
273
+ },
274
+ {
275
+ "pk": 2,
276
+ "model": "distapp.southtexaszipcode",
277
+ "fields": {
278
+ "name": "77005",
279
+ "poly": "SRID=32140;POLYGON ((943568.6284788220655173 4215118.1416417406871915, 945487.9980521594407037 4215337.8481928687542677, 946154.1517634749179706 4215391.4863748690113425, 946400.6304061285918579 4215243.7918130923062563, 946414.3720398070290685 4215111.1480321725830436, 947999.4599494948051870 4215181.9933486394584179, 948002.5282293657073751 4215082.2888173414394259, 948660.1417131680063903 4215102.5451732110232115, 948987.1902349996380508 4215168.0774542130529881, 949201.2831489123636857 4215130.3313774159178138, 948579.1820738224778324 4213957.9057207889854908, 948480.7719409475103021 4214010.3167602103203535, 948167.3284030471695587 4213501.6696778312325478, 947139.6595406322740018 4213869.2741038342937827, 946463.7483147600432858 4213815.2817387804389000, 946502.2630172523204237 4212874.0348161971196532, 944327.1378451543860137 4212774.3432321334257722, 943917.9093748427694663 4212861.6900106100365520, 943760.4753683793824166 4212945.6127811912447214, 943675.0267053353600204 4213209.1422286527231336, 943568.6284788220655173 4215118.1416417406871915))"
280
+ }
281
+ },
282
+ {
283
+ "pk": 3,
284
+ "model": "distapp.southtexaszipcode",
285
+ "fields": {
286
+ "name": "77025",
287
+ "poly": "SRID=32140;POLYGON ((946502.2630172523204237 4212874.0348161971196532, 946850.7713855113834143 4212873.5172070376574993, 946863.1571223458740860 4212785.2985141929239035, 946514.9888159723486751 4212774.6266902117058635, 946394.1713412774261087 4211662.0614421153441072, 946430.2274363302858546 4211537.4205564847216010, 946665.6199423221405596 4211179.2615583632141352, 946910.2001898437738419 4211566.9887375179678202, 947032.0462735339533538 4211382.2191729480400681, 947109.6117705166107044 4211063.0284381639212370, 947097.7440171607304364 4210818.7144018746912479, 946983.3062754627317190 4210540.7604114022105932, 946490.8323894963832572 4210567.0268272040411830, 946254.6550409919582307 4210526.7516811080276966, 945876.0008902740664780 4209927.4740557167679071, 945945.7498299231519923 4209863.0751969292759895, 945999.5397084732539952 4209687.3030018107965589, 945718.8972609748598188 4209678.7238750727847219, 944737.3260139031335711 4208207.3771017007529736, 944864.3308290732093155 4208186.8525157077237964, 944542.2412279490381479 4207935.3116904916241765, 944633.3693750606616959 4207805.0250262534245849, 944781.5411592419259250 4207709.7424235185608268, 944976.4055775101296604 4207671.3274960424751043, 945351.7294723913073540 4207749.3068800456821918, 944435.3426235395018011 4207311.1031644716858864, 944446.0302430286537856 4207278.1630970388650894, 944398.3243151376955211 4207254.5342014739289880, 944398.2226623257156461 4207576.0964536992833018, 944449.5330111491493881 4207799.4275652254000306, 943923.1866919576423243 4207273.3492023386061192, 943885.2983187574427575 4209157.2369952416047454, 943790.3796654250472784 4211006.0231411391869187, 943675.0267053353600204 4213209.1422286527231336, 943760.4753683793824166 4212945.6127811912447214, 943917.9093748427694663 4212861.6900106100365520, 944327.1378451543860137 4212774.3432321334257722, 946502.2630172523204237 4212874.0348161971196532))"
288
+ }
289
+ },
290
+ {
291
+ "pk": 4,
292
+ "model": "distapp.southtexaszipcode",
293
+ "fields": {
294
+ "name": "77401",
295
+ "poly": "SRID=32140;POLYGON ((943568.6284788220655173 4215118.1416417406871915, 943675.0267053353600204 4213209.1422286527231336, 943790.3796654250472784 4211006.0231411391869187, 943078.6190994082717225 4210851.3527729213237762, 940998.4896008004434407 4210799.5486384732648730, 940991.4769605984911323 4211032.1947893090546131, 940159.9452393862884492 4210996.0721307108178735, 940123.6234478773549199 4211882.0554536599665880, 940512.5092238665092736 4212148.7901582065969706, 940522.5142344047781080 4211816.4372764797881246, 941131.8601800086908042 4211834.7971962466835976, 941111.1461491615045816 4212521.6589920166879892, 941218.8068687882041559 4213445.2531420132145286, 941672.9982470837421715 4213470.0535286460071802, 941629.2300919098779559 4214278.0848444234579802, 942035.4691281586419791 4214290.3638116680085659, 942011.3920782012864947 4214766.4446664610877633, 942113.0660258040297776 4214924.7603673832491040, 942421.8192426580935717 4214956.2823535148054361, 942431.6055385319050401 4214313.4400705620646477, 943253.2209875346161425 4214349.4362764349207282, 943198.9497700630454347 4214999.1347201215103269, 943240.6978847808204591 4215080.9052637927234173, 943568.6284788220655173 4215118.1416417406871915))"
296
+ }
297
+ },
298
+ {
299
+ "pk": 1,
300
+ "model": "distapp.interstate",
301
+ "fields": {
302
+ "path": "SRID=4326;LINESTRING (-104.4780170766107972 36.6669879187069370, -104.4468522338494978 36.7992540939338610, -104.4621269262599981 36.9372149776075034, -104.5126119783767962 37.0816326882088703, -104.5247764602161027 37.2930049989204804, -104.7084397427667994 37.4915025992539768, -104.8126599016281943 37.6951428562186308, -104.8452887035466006 37.8761339565947921, -104.7160169341002955 38.0595176333779932, -104.6165437927668052 38.3043204585510608, -104.6437227858174026 38.5397998656473675, -104.7596170387259065 38.7322907594295032, -104.8380078676821938 38.8999846060434109, -104.8501253693505930 39.0998018921335770, -104.8791648316464062 39.2436877645750286, -104.8635041274215070 39.3785278162751027, -104.8894471170052043 39.5929228239604996, -104.9721242843343987 39.6952848241968468, -105.0112104500356054 39.7273080432393968, -105.0010368577104032 39.7667760781157114, -104.9818356189999946 39.8146650412196692, -104.9858891550477011 39.8880691125083189, -104.9873548059578070 39.9811723457101635, -104.9766220487419019 40.0979642345069180, -104.9818565932953049 40.3605653066288426, -104.9912746373996981 40.7490448444765576)",
303
+ "name": "I-25"
304
+ }
305
+ },
306
+ {
307
+ "pk": 1,
308
+ "model": "distapp.southtexasinterstate",
309
+ "fields": {
310
+ "path": "SRID=32140;LINESTRING (924952.5000000000000000 4220931.5999999996274710, 925065.3000000000465661 4220931.5999999996274710, 929568.4000000000232831 4221057.7999999998137355)",
311
+ "name": "I-10"
312
+ }
313
+ }
314
+ ]
testbed/django__django/tests/gis_tests/distapp/models.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from django.contrib.gis.db import models
2
+
3
+ from ..utils import gisfield_may_be_null
4
+
5
+
6
+ class NamedModel(models.Model):
7
+ name = models.CharField(max_length=30)
8
+
9
+ class Meta:
10
+ abstract = True
11
+
12
+ def __str__(self):
13
+ return self.name
14
+
15
+
16
+ class SouthTexasCity(NamedModel):
17
+ "City model on projected coordinate system for South Texas."
18
+ point = models.PointField(srid=32140)
19
+ radius = models.IntegerField(default=10000)
20
+
21
+
22
+ class SouthTexasCityFt(NamedModel):
23
+ "Same City model as above, but U.S. survey feet are the units."
24
+ point = models.PointField(srid=2278)
25
+
26
+
27
+ class AustraliaCity(NamedModel):
28
+ "City model for Australia, using WGS84."
29
+ point = models.PointField()
30
+ radius = models.IntegerField(default=10000)
31
+ allowed_distance = models.FloatField(default=0.5)
32
+ ref_point = models.PointField(null=True)
33
+
34
+
35
+ class CensusZipcode(NamedModel):
36
+ "Model for a few South Texas ZIP codes (in original Census NAD83)."
37
+ poly = models.PolygonField(srid=4269)
38
+
39
+
40
+ class SouthTexasZipcode(NamedModel):
41
+ "Model for a few South Texas ZIP codes."
42
+ poly = models.PolygonField(srid=32140, null=gisfield_may_be_null)
43
+
44
+
45
+ class Interstate(NamedModel):
46
+ "Geodetic model for U.S. Interstates."
47
+ path = models.LineStringField()
48
+
49
+
50
+ class SouthTexasInterstate(NamedModel):
51
+ "Projected model for South Texas Interstates."
52
+ path = models.LineStringField(srid=32140)
testbed/django__django/tests/gis_tests/distapp/tests.py ADDED
@@ -0,0 +1,796 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from django.contrib.gis.db.models.functions import (
2
+ Area,
3
+ Distance,
4
+ Length,
5
+ Perimeter,
6
+ Transform,
7
+ Union,
8
+ )
9
+ from django.contrib.gis.geos import GEOSGeometry, LineString, Point
10
+ from django.contrib.gis.measure import D # alias for Distance
11
+ from django.db import NotSupportedError, connection
12
+ from django.db.models import (
13
+ Case,
14
+ Count,
15
+ Exists,
16
+ F,
17
+ IntegerField,
18
+ OuterRef,
19
+ Q,
20
+ Value,
21
+ When,
22
+ )
23
+ from django.test import TestCase, skipIfDBFeature, skipUnlessDBFeature
24
+
25
+ from ..utils import FuncTestMixin
26
+ from .models import (
27
+ AustraliaCity,
28
+ CensusZipcode,
29
+ Interstate,
30
+ SouthTexasCity,
31
+ SouthTexasCityFt,
32
+ SouthTexasInterstate,
33
+ SouthTexasZipcode,
34
+ )
35
+
36
+
37
+ class DistanceTest(TestCase):
38
+ fixtures = ["initial"]
39
+
40
+ def setUp(self):
41
+ # A point we are testing distances with -- using a WGS84
42
+ # coordinate that'll be implicitly transformed to that to
43
+ # the coordinate system of the field, EPSG:32140 (Texas South Central
44
+ # w/units in meters)
45
+ self.stx_pnt = GEOSGeometry(
46
+ "POINT (-95.370401017314293 29.704867409475465)", 4326
47
+ )
48
+ # Another one for Australia
49
+ self.au_pnt = GEOSGeometry("POINT (150.791 -34.4919)", 4326)
50
+
51
+ def get_names(self, qs):
52
+ cities = [c.name for c in qs]
53
+ cities.sort()
54
+ return cities
55
+
56
+ def test_init(self):
57
+ """
58
+ Test initialization of distance models.
59
+ """
60
+ self.assertEqual(9, SouthTexasCity.objects.count())
61
+ self.assertEqual(9, SouthTexasCityFt.objects.count())
62
+ self.assertEqual(11, AustraliaCity.objects.count())
63
+ self.assertEqual(4, SouthTexasZipcode.objects.count())
64
+ self.assertEqual(4, CensusZipcode.objects.count())
65
+ self.assertEqual(1, Interstate.objects.count())
66
+ self.assertEqual(1, SouthTexasInterstate.objects.count())
67
+
68
+ @skipUnlessDBFeature("supports_dwithin_lookup")
69
+ def test_dwithin(self):
70
+ """
71
+ Test the `dwithin` lookup type.
72
+ """
73
+ # Distances -- all should be equal (except for the
74
+ # degree/meter pair in au_cities, that's somewhat
75
+ # approximate).
76
+ tx_dists = [(7000, 22965.83), D(km=7), D(mi=4.349)]
77
+ au_dists = [(0.5, 32000), D(km=32), D(mi=19.884)]
78
+
79
+ # Expected cities for Australia and Texas.
80
+ tx_cities = ["Downtown Houston", "Southside Place"]
81
+ au_cities = ["Mittagong", "Shellharbour", "Thirroul", "Wollongong"]
82
+
83
+ # Performing distance queries on two projected coordinate systems one
84
+ # with units in meters and the other in units of U.S. survey feet.
85
+ for dist in tx_dists:
86
+ if isinstance(dist, tuple):
87
+ dist1, dist2 = dist
88
+ else:
89
+ dist1 = dist2 = dist
90
+ qs1 = SouthTexasCity.objects.filter(point__dwithin=(self.stx_pnt, dist1))
91
+ qs2 = SouthTexasCityFt.objects.filter(point__dwithin=(self.stx_pnt, dist2))
92
+ for qs in qs1, qs2:
93
+ with self.subTest(dist=dist, qs=qs):
94
+ self.assertEqual(tx_cities, self.get_names(qs))
95
+
96
+ # With a complex geometry expression
97
+ self.assertFalse(
98
+ SouthTexasCity.objects.exclude(point__dwithin=(Union("point", "point"), 0))
99
+ )
100
+
101
+ # Now performing the `dwithin` queries on a geodetic coordinate system.
102
+ for dist in au_dists:
103
+ with self.subTest(dist=dist):
104
+ type_error = isinstance(dist, D) and not connection.ops.oracle
105
+ if isinstance(dist, tuple):
106
+ if connection.ops.oracle or connection.ops.spatialite:
107
+ # Result in meters
108
+ dist = dist[1]
109
+ else:
110
+ # Result in units of the field
111
+ dist = dist[0]
112
+
113
+ # Creating the query set.
114
+ qs = AustraliaCity.objects.order_by("name")
115
+ if type_error:
116
+ # A ValueError should be raised on PostGIS when trying to
117
+ # pass Distance objects into a DWithin query using a
118
+ # geodetic field.
119
+ with self.assertRaises(ValueError):
120
+ AustraliaCity.objects.filter(
121
+ point__dwithin=(self.au_pnt, dist)
122
+ ).count()
123
+ else:
124
+ self.assertEqual(
125
+ au_cities,
126
+ self.get_names(qs.filter(point__dwithin=(self.au_pnt, dist))),
127
+ )
128
+
129
+ @skipUnlessDBFeature("supports_distances_lookups")
130
+ def test_distance_lookups(self):
131
+ # Retrieving the cities within a 20km 'donut' w/a 7km radius 'hole'
132
+ # (thus, Houston and Southside place will be excluded as tested in
133
+ # the `test02_dwithin` above).
134
+ for model in [SouthTexasCity, SouthTexasCityFt]:
135
+ stx_pnt = self.stx_pnt.transform(
136
+ model._meta.get_field("point").srid, clone=True
137
+ )
138
+ qs = model.objects.filter(point__distance_gte=(stx_pnt, D(km=7))).filter(
139
+ point__distance_lte=(stx_pnt, D(km=20)),
140
+ )
141
+ cities = self.get_names(qs)
142
+ self.assertEqual(cities, ["Bellaire", "Pearland", "West University Place"])
143
+
144
+ # Doing a distance query using Polygons instead of a Point.
145
+ z = SouthTexasZipcode.objects.get(name="77005")
146
+ qs = SouthTexasZipcode.objects.exclude(name="77005").filter(
147
+ poly__distance_lte=(z.poly, D(m=275))
148
+ )
149
+ self.assertEqual(["77025", "77401"], self.get_names(qs))
150
+ # If we add a little more distance 77002 should be included.
151
+ qs = SouthTexasZipcode.objects.exclude(name="77005").filter(
152
+ poly__distance_lte=(z.poly, D(m=300))
153
+ )
154
+ self.assertEqual(["77002", "77025", "77401"], self.get_names(qs))
155
+
156
+ @skipUnlessDBFeature("supports_distances_lookups", "supports_distance_geodetic")
157
+ def test_geodetic_distance_lookups(self):
158
+ """
159
+ Test distance lookups on geodetic coordinate systems.
160
+ """
161
+ # Line is from Canberra to Sydney. Query is for all other cities within
162
+ # a 100km of that line (which should exclude only Hobart & Adelaide).
163
+ line = GEOSGeometry("LINESTRING(144.9630 -37.8143,151.2607 -33.8870)", 4326)
164
+ dist_qs = AustraliaCity.objects.filter(point__distance_lte=(line, D(km=100)))
165
+ expected_cities = [
166
+ "Batemans Bay",
167
+ "Canberra",
168
+ "Hillsdale",
169
+ "Melbourne",
170
+ "Mittagong",
171
+ "Shellharbour",
172
+ "Sydney",
173
+ "Thirroul",
174
+ "Wollongong",
175
+ ]
176
+ if connection.ops.spatialite:
177
+ # SpatiaLite is less accurate and returns 102.8km for Batemans Bay.
178
+ expected_cities.pop(0)
179
+ self.assertEqual(expected_cities, self.get_names(dist_qs))
180
+
181
+ msg = "2, 3, or 4-element tuple required for 'distance_lte' lookup."
182
+ with self.assertRaisesMessage(ValueError, msg): # Too many params.
183
+ len(
184
+ AustraliaCity.objects.filter(
185
+ point__distance_lte=(
186
+ "POINT(5 23)",
187
+ D(km=100),
188
+ "spheroid",
189
+ "4",
190
+ None,
191
+ )
192
+ )
193
+ )
194
+
195
+ with self.assertRaisesMessage(ValueError, msg): # Too few params.
196
+ len(AustraliaCity.objects.filter(point__distance_lte=("POINT(5 23)",)))
197
+
198
+ msg = "For 4-element tuples the last argument must be the 'spheroid' directive."
199
+ with self.assertRaisesMessage(ValueError, msg):
200
+ len(
201
+ AustraliaCity.objects.filter(
202
+ point__distance_lte=("POINT(5 23)", D(km=100), "spheroid", "4")
203
+ )
204
+ )
205
+
206
+ # Getting all cities w/in 550 miles of Hobart.
207
+ hobart = AustraliaCity.objects.get(name="Hobart")
208
+ qs = AustraliaCity.objects.exclude(name="Hobart").filter(
209
+ point__distance_lte=(hobart.point, D(mi=550))
210
+ )
211
+ cities = self.get_names(qs)
212
+ self.assertEqual(cities, ["Batemans Bay", "Canberra", "Melbourne"])
213
+
214
+ # Cities that are either really close or really far from Wollongong --
215
+ # and using different units of distance.
216
+ wollongong = AustraliaCity.objects.get(name="Wollongong")
217
+ d1, d2 = D(yd=19500), D(nm=400) # Yards (~17km) & Nautical miles.
218
+
219
+ # Normal geodetic distance lookup (uses `distance_sphere` on PostGIS.
220
+ gq1 = Q(point__distance_lte=(wollongong.point, d1))
221
+ gq2 = Q(point__distance_gte=(wollongong.point, d2))
222
+ qs1 = AustraliaCity.objects.exclude(name="Wollongong").filter(gq1 | gq2)
223
+
224
+ # Geodetic distance lookup but telling GeoDjango to use `distance_spheroid`
225
+ # instead (we should get the same results b/c accuracy variance won't matter
226
+ # in this test case).
227
+ querysets = [qs1]
228
+ if connection.features.has_DistanceSpheroid_function:
229
+ gq3 = Q(point__distance_lte=(wollongong.point, d1, "spheroid"))
230
+ gq4 = Q(point__distance_gte=(wollongong.point, d2, "spheroid"))
231
+ qs2 = AustraliaCity.objects.exclude(name="Wollongong").filter(gq3 | gq4)
232
+ querysets.append(qs2)
233
+
234
+ for qs in querysets:
235
+ cities = self.get_names(qs)
236
+ self.assertEqual(cities, ["Adelaide", "Hobart", "Shellharbour", "Thirroul"])
237
+
238
+ @skipUnlessDBFeature("supports_distances_lookups")
239
+ def test_distance_lookups_with_expression_rhs(self):
240
+ stx_pnt = self.stx_pnt.transform(
241
+ SouthTexasCity._meta.get_field("point").srid, clone=True
242
+ )
243
+ qs = SouthTexasCity.objects.filter(
244
+ point__distance_lte=(stx_pnt, F("radius")),
245
+ ).order_by("name")
246
+ self.assertEqual(
247
+ self.get_names(qs),
248
+ [
249
+ "Bellaire",
250
+ "Downtown Houston",
251
+ "Southside Place",
252
+ "West University Place",
253
+ ],
254
+ )
255
+
256
+ # With a combined expression
257
+ qs = SouthTexasCity.objects.filter(
258
+ point__distance_lte=(stx_pnt, F("radius") * 2),
259
+ ).order_by("name")
260
+ self.assertEqual(len(qs), 5)
261
+ self.assertIn("Pearland", self.get_names(qs))
262
+
263
+ # With spheroid param
264
+ if connection.features.supports_distance_geodetic:
265
+ hobart = AustraliaCity.objects.get(name="Hobart")
266
+ AustraliaCity.objects.update(ref_point=hobart.point)
267
+ for ref_point in [hobart.point, F("ref_point")]:
268
+ qs = AustraliaCity.objects.filter(
269
+ point__distance_lte=(ref_point, F("radius") * 70, "spheroid"),
270
+ ).order_by("name")
271
+ self.assertEqual(
272
+ self.get_names(qs), ["Canberra", "Hobart", "Melbourne"]
273
+ )
274
+
275
+ # With a complex geometry expression
276
+ self.assertFalse(
277
+ SouthTexasCity.objects.filter(
278
+ point__distance_gt=(Union("point", "point"), 0)
279
+ )
280
+ )
281
+ self.assertEqual(
282
+ SouthTexasCity.objects.filter(
283
+ point__distance_lte=(Union("point", "point"), 0)
284
+ ).count(),
285
+ SouthTexasCity.objects.count(),
286
+ )
287
+
288
+ @skipUnlessDBFeature("supports_distances_lookups")
289
+ def test_distance_annotation_group_by(self):
290
+ stx_pnt = self.stx_pnt.transform(
291
+ SouthTexasCity._meta.get_field("point").srid,
292
+ clone=True,
293
+ )
294
+ qs = (
295
+ SouthTexasCity.objects.annotate(
296
+ relative_distance=Case(
297
+ When(point__distance_lte=(stx_pnt, D(km=20)), then=Value(20)),
298
+ default=Value(100),
299
+ output_field=IntegerField(),
300
+ ),
301
+ )
302
+ .values("relative_distance")
303
+ .annotate(count=Count("pk"))
304
+ )
305
+ self.assertCountEqual(
306
+ qs,
307
+ [
308
+ {"relative_distance": 20, "count": 5},
309
+ {"relative_distance": 100, "count": 4},
310
+ ],
311
+ )
312
+
313
+ def test_mysql_geodetic_distance_error(self):
314
+ if not connection.ops.mysql:
315
+ self.skipTest("This is a MySQL-specific test.")
316
+ msg = (
317
+ "Only numeric values of degree units are allowed on geodetic distance "
318
+ "queries."
319
+ )
320
+ with self.assertRaisesMessage(ValueError, msg):
321
+ AustraliaCity.objects.filter(
322
+ point__distance_lte=(Point(0, 0), D(m=100))
323
+ ).exists()
324
+
325
+ @skipUnlessDBFeature("supports_dwithin_lookup")
326
+ def test_dwithin_subquery(self):
327
+ """dwithin lookup in a subquery using OuterRef as a parameter."""
328
+ qs = CensusZipcode.objects.annotate(
329
+ annotated_value=Exists(
330
+ SouthTexasCity.objects.filter(
331
+ point__dwithin=(OuterRef("poly"), D(m=10)),
332
+ )
333
+ )
334
+ ).filter(annotated_value=True)
335
+ self.assertEqual(self.get_names(qs), ["77002", "77025", "77401"])
336
+
337
+ @skipUnlessDBFeature("supports_dwithin_lookup", "supports_dwithin_distance_expr")
338
+ def test_dwithin_with_expression_rhs(self):
339
+ # LineString of Wollongong and Adelaide coords.
340
+ ls = LineString(((150.902, -34.4245), (138.6, -34.9258)), srid=4326)
341
+ qs = AustraliaCity.objects.filter(
342
+ point__dwithin=(ls, F("allowed_distance")),
343
+ ).order_by("name")
344
+ self.assertEqual(
345
+ self.get_names(qs),
346
+ ["Adelaide", "Mittagong", "Shellharbour", "Thirroul", "Wollongong"],
347
+ )
348
+
349
+ @skipIfDBFeature("supports_dwithin_distance_expr")
350
+ def test_dwithin_with_expression_rhs_not_supported(self):
351
+ ls = LineString(((150.902, -34.4245), (138.6, -34.9258)), srid=4326)
352
+ msg = (
353
+ "This backend does not support expressions for specifying "
354
+ "distance in the dwithin lookup."
355
+ )
356
+ with self.assertRaisesMessage(NotSupportedError, msg):
357
+ list(
358
+ AustraliaCity.objects.filter(
359
+ point__dwithin=(ls, F("allowed_distance")),
360
+ )
361
+ )
362
+
363
+
364
+ """
365
+ =============================
366
+ Distance functions on PostGIS
367
+ =============================
368
+
369
+ | Projected Geometry | Lon/lat Geometry | Geography (4326)
370
+
371
+ ST_Distance(geom1, geom2) | OK (meters) | :-( (degrees) | OK (meters)
372
+
373
+ ST_Distance(geom1, geom2, use_spheroid=False) | N/A | N/A | OK (meters), less accurate, quick
374
+
375
+ Distance_Sphere(geom1, geom2) | N/A | OK (meters) | N/A
376
+
377
+ Distance_Spheroid(geom1, geom2, spheroid) | N/A | OK (meters) | N/A
378
+
379
+ ST_Perimeter(geom1) | OK | :-( (degrees) | OK
380
+
381
+
382
+ ================================
383
+ Distance functions on SpatiaLite
384
+ ================================
385
+
386
+ | Projected Geometry | Lon/lat Geometry
387
+
388
+ ST_Distance(geom1, geom2) | OK (meters) | N/A
389
+
390
+ ST_Distance(geom1, geom2, use_ellipsoid=True) | N/A | OK (meters)
391
+
392
+ ST_Distance(geom1, geom2, use_ellipsoid=False) | N/A | OK (meters), less accurate, quick
393
+
394
+ Perimeter(geom1) | OK | :-( (degrees)
395
+
396
+ """ # NOQA
397
+
398
+
399
+ class DistanceFunctionsTests(FuncTestMixin, TestCase):
400
+ fixtures = ["initial"]
401
+
402
+ @skipUnlessDBFeature("has_Area_function")
403
+ def test_area(self):
404
+ # Reference queries:
405
+ # SELECT ST_Area(poly) FROM distapp_southtexaszipcode;
406
+ area_sq_m = [
407
+ 5437908.90234375,
408
+ 10183031.4389648,
409
+ 11254471.0073242,
410
+ 9881708.91772461,
411
+ ]
412
+ # Tolerance has to be lower for Oracle
413
+ tol = 2
414
+ for i, z in enumerate(
415
+ SouthTexasZipcode.objects.annotate(area=Area("poly")).order_by("name")
416
+ ):
417
+ self.assertAlmostEqual(area_sq_m[i], z.area.sq_m, tol)
418
+
419
+ @skipUnlessDBFeature("has_Distance_function")
420
+ def test_distance_simple(self):
421
+ """
422
+ Test a simple distance query, with projected coordinates and without
423
+ transformation.
424
+ """
425
+ lagrange = GEOSGeometry("POINT(805066.295722839 4231496.29461335)", 32140)
426
+ houston = (
427
+ SouthTexasCity.objects.annotate(dist=Distance("point", lagrange))
428
+ .order_by("id")
429
+ .first()
430
+ )
431
+ tol = 2 if connection.ops.oracle else 5
432
+ self.assertAlmostEqual(houston.dist.m, 147075.069813, tol)
433
+
434
+ @skipUnlessDBFeature("has_Distance_function", "has_Transform_function")
435
+ def test_distance_projected(self):
436
+ """
437
+ Test the `Distance` function on projected coordinate systems.
438
+ """
439
+ # The point for La Grange, TX
440
+ lagrange = GEOSGeometry("POINT(-96.876369 29.905320)", 4326)
441
+ # Reference distances in feet and in meters. Got these values from
442
+ # using the provided raw SQL statements.
443
+ # SELECT ST_Distance(
444
+ # point,
445
+ # ST_Transform(ST_GeomFromText('POINT(-96.876369 29.905320)', 4326), 32140)
446
+ # )
447
+ # FROM distapp_southtexascity;
448
+ m_distances = [
449
+ 147075.069813,
450
+ 139630.198056,
451
+ 140888.552826,
452
+ 138809.684197,
453
+ 158309.246259,
454
+ 212183.594374,
455
+ 70870.188967,
456
+ 165337.758878,
457
+ 139196.085105,
458
+ ]
459
+ # SELECT ST_Distance(
460
+ # point,
461
+ # ST_Transform(ST_GeomFromText('POINT(-96.876369 29.905320)', 4326), 2278)
462
+ # )
463
+ # FROM distapp_southtexascityft;
464
+ ft_distances = [
465
+ 482528.79154625,
466
+ 458103.408123001,
467
+ 462231.860397575,
468
+ 455411.438904354,
469
+ 519386.252102563,
470
+ 696139.009211594,
471
+ 232513.278304279,
472
+ 542445.630586414,
473
+ 456679.155883207,
474
+ ]
475
+
476
+ # Testing using different variations of parameters and using models
477
+ # with different projected coordinate systems.
478
+ dist1 = SouthTexasCity.objects.annotate(
479
+ distance=Distance("point", lagrange)
480
+ ).order_by("id")
481
+ dist2 = SouthTexasCityFt.objects.annotate(
482
+ distance=Distance("point", lagrange)
483
+ ).order_by("id")
484
+ dist_qs = [dist1, dist2]
485
+
486
+ # Ensuring expected distances are returned for each distance queryset.
487
+ for qs in dist_qs:
488
+ for i, c in enumerate(qs):
489
+ with self.subTest(c=c):
490
+ self.assertAlmostEqual(m_distances[i], c.distance.m, -1)
491
+ self.assertAlmostEqual(ft_distances[i], c.distance.survey_ft, -1)
492
+
493
+ @skipUnlessDBFeature("has_Distance_function", "supports_distance_geodetic")
494
+ def test_distance_geodetic(self):
495
+ """
496
+ Test the `Distance` function on geodetic coordinate systems.
497
+ """
498
+ # Testing geodetic distance calculation with a non-point geometry
499
+ # (a LineString of Wollongong and Shellharbour coords).
500
+ ls = LineString(((150.902, -34.4245), (150.87, -34.5789)), srid=4326)
501
+
502
+ # Reference query:
503
+ # SELECT ST_distance_sphere(
504
+ # point,
505
+ # ST_GeomFromText('LINESTRING(150.9020 -34.4245,150.8700 -34.5789)', 4326)
506
+ # )
507
+ # FROM distapp_australiacity ORDER BY name;
508
+ distances = [
509
+ 1120954.92533513,
510
+ 140575.720018241,
511
+ 640396.662906304,
512
+ 60580.9693849269,
513
+ 972807.955955075,
514
+ 568451.8357838,
515
+ 40435.4335201384,
516
+ 0,
517
+ 68272.3896586844,
518
+ 12375.0643697706,
519
+ 0,
520
+ ]
521
+ qs = AustraliaCity.objects.annotate(distance=Distance("point", ls)).order_by(
522
+ "name"
523
+ )
524
+ for city, distance in zip(qs, distances):
525
+ with self.subTest(city=city, distance=distance):
526
+ # Testing equivalence to within a meter (kilometer on SpatiaLite).
527
+ tol = -3 if connection.ops.spatialite else 0
528
+ self.assertAlmostEqual(distance, city.distance.m, tol)
529
+
530
+ @skipUnlessDBFeature("has_Distance_function", "supports_distance_geodetic")
531
+ def test_distance_geodetic_spheroid(self):
532
+ tol = 2 if connection.ops.oracle else 4
533
+
534
+ # Got the reference distances using the raw SQL statements:
535
+ # SELECT ST_distance_spheroid(
536
+ # point,
537
+ # ST_GeomFromText('POINT(151.231341 -33.952685)', 4326),
538
+ # 'SPHEROID["WGS 84",6378137.0,298.257223563]'
539
+ # )
540
+ # FROM distapp_australiacity WHERE (NOT (id = 11));
541
+ # SELECT ST_distance_sphere(
542
+ # point,
543
+ # ST_GeomFromText('POINT(151.231341 -33.952685)', 4326)
544
+ # )
545
+ # FROM distapp_australiacity WHERE (NOT (id = 11)); st_distance_sphere
546
+ spheroid_distances = [
547
+ 60504.0628957201,
548
+ 77023.9489850262,
549
+ 49154.8867574404,
550
+ 90847.4358768573,
551
+ 217402.811919332,
552
+ 709599.234564757,
553
+ 640011.483550888,
554
+ 7772.00667991925,
555
+ 1047861.78619339,
556
+ 1165126.55236034,
557
+ ]
558
+ sphere_distances = [
559
+ 60580.9693849267,
560
+ 77144.0435286473,
561
+ 49199.4415344719,
562
+ 90804.7533823494,
563
+ 217713.384600405,
564
+ 709134.127242793,
565
+ 639828.157159169,
566
+ 7786.82949717788,
567
+ 1049204.06569028,
568
+ 1162623.7238134,
569
+ ]
570
+ # Testing with spheroid distances first.
571
+ hillsdale = AustraliaCity.objects.get(name="Hillsdale")
572
+ qs = (
573
+ AustraliaCity.objects.exclude(id=hillsdale.id)
574
+ .annotate(distance=Distance("point", hillsdale.point, spheroid=True))
575
+ .order_by("id")
576
+ )
577
+ for i, c in enumerate(qs):
578
+ with self.subTest(c=c):
579
+ self.assertAlmostEqual(spheroid_distances[i], c.distance.m, tol)
580
+ if connection.ops.postgis or connection.ops.spatialite:
581
+ # PostGIS uses sphere-only distances by default, testing these as well.
582
+ qs = (
583
+ AustraliaCity.objects.exclude(id=hillsdale.id)
584
+ .annotate(distance=Distance("point", hillsdale.point))
585
+ .order_by("id")
586
+ )
587
+ for i, c in enumerate(qs):
588
+ with self.subTest(c=c):
589
+ self.assertAlmostEqual(sphere_distances[i], c.distance.m, tol)
590
+
591
+ @skipIfDBFeature("supports_distance_geodetic")
592
+ @skipUnlessDBFeature("has_Distance_function")
593
+ def test_distance_function_raw_result(self):
594
+ distance = (
595
+ Interstate.objects.annotate(
596
+ d=Distance(Point(0, 0, srid=4326), Point(0, 1, srid=4326)),
597
+ )
598
+ .first()
599
+ .d
600
+ )
601
+ self.assertEqual(distance, 1)
602
+
603
+ @skipUnlessDBFeature("has_Distance_function")
604
+ def test_distance_function_d_lookup(self):
605
+ qs = Interstate.objects.annotate(
606
+ d=Distance(Point(0, 0, srid=3857), Point(0, 1, srid=3857)),
607
+ ).filter(d=D(m=1))
608
+ self.assertTrue(qs.exists())
609
+
610
+ @skipUnlessDBFeature("supports_tolerance_parameter")
611
+ def test_distance_function_tolerance_escaping(self):
612
+ qs = (
613
+ Interstate.objects.annotate(
614
+ d=Distance(
615
+ Point(500, 500, srid=3857),
616
+ Point(0, 0, srid=3857),
617
+ tolerance="0.05) = 1 OR 1=1 OR (1+1",
618
+ ),
619
+ )
620
+ .filter(d=D(m=1))
621
+ .values("pk")
622
+ )
623
+ msg = "The tolerance parameter has the wrong type"
624
+ with self.assertRaisesMessage(TypeError, msg):
625
+ qs.exists()
626
+
627
+ @skipUnlessDBFeature("supports_tolerance_parameter")
628
+ def test_distance_function_tolerance(self):
629
+ # Tolerance is greater than distance.
630
+ qs = (
631
+ Interstate.objects.annotate(
632
+ d=Distance(
633
+ Point(0, 0, srid=3857),
634
+ Point(1, 1, srid=3857),
635
+ tolerance=1.5,
636
+ ),
637
+ )
638
+ .filter(d=0)
639
+ .values("pk")
640
+ )
641
+ self.assertIs(qs.exists(), True)
642
+
643
+ @skipIfDBFeature("supports_distance_geodetic")
644
+ @skipUnlessDBFeature("has_Distance_function")
645
+ def test_distance_function_raw_result_d_lookup(self):
646
+ qs = Interstate.objects.annotate(
647
+ d=Distance(Point(0, 0, srid=4326), Point(0, 1, srid=4326)),
648
+ ).filter(d=D(m=1))
649
+ msg = "Distance measure is supplied, but units are unknown for result."
650
+ with self.assertRaisesMessage(ValueError, msg):
651
+ list(qs)
652
+
653
+ @skipUnlessDBFeature("has_Distance_function", "has_Transform_function")
654
+ def test_distance_transform(self):
655
+ """
656
+ Test the `Distance` function used with `Transform` on a geographic field.
657
+ """
658
+ # We'll be using a Polygon (created by buffering the centroid
659
+ # of 77005 to 100m) -- which aren't allowed in geographic distance
660
+ # queries normally, however our field has been transformed to
661
+ # a non-geographic system.
662
+ z = SouthTexasZipcode.objects.get(name="77005")
663
+
664
+ # Reference query:
665
+ # SELECT ST_Distance(ST_Transform("distapp_censuszipcode"."poly", 32140),
666
+ # ST_GeomFromText('<buffer_wkt>', 32140))
667
+ # FROM "distapp_censuszipcode";
668
+ dists_m = [3553.30384972258, 1243.18391525602, 2186.15439472242]
669
+
670
+ # Having our buffer in the SRID of the transformation and of the field
671
+ # -- should get the same results. The first buffer has no need for
672
+ # transformation SQL because it is the same SRID as what was given
673
+ # to `transform()`. The second buffer will need to be transformed,
674
+ # however.
675
+ buf1 = z.poly.centroid.buffer(100)
676
+ buf2 = buf1.transform(4269, clone=True)
677
+ ref_zips = ["77002", "77025", "77401"]
678
+
679
+ for buf in [buf1, buf2]:
680
+ qs = (
681
+ CensusZipcode.objects.exclude(name="77005")
682
+ .annotate(distance=Distance(Transform("poly", 32140), buf))
683
+ .order_by("name")
684
+ )
685
+ self.assertEqual(ref_zips, sorted(c.name for c in qs))
686
+ for i, z in enumerate(qs):
687
+ self.assertAlmostEqual(z.distance.m, dists_m[i], 5)
688
+
689
+ @skipUnlessDBFeature("has_Distance_function")
690
+ def test_distance_order_by(self):
691
+ qs = (
692
+ SouthTexasCity.objects.annotate(
693
+ distance=Distance("point", Point(3, 3, srid=32140))
694
+ )
695
+ .order_by("distance")
696
+ .values_list("name", flat=True)
697
+ .filter(name__in=("San Antonio", "Pearland"))
698
+ )
699
+ self.assertSequenceEqual(qs, ["San Antonio", "Pearland"])
700
+
701
+ @skipUnlessDBFeature("has_Length_function")
702
+ def test_length(self):
703
+ """
704
+ Test the `Length` function.
705
+ """
706
+ # Reference query (should use `length_spheroid`).
707
+ # SELECT ST_length_spheroid(
708
+ # ST_GeomFromText('<wkt>', 4326)
709
+ # 'SPHEROID["WGS 84",6378137,298.257223563, AUTHORITY["EPSG","7030"]]'
710
+ # );
711
+ len_m1 = 473504.769553813
712
+ len_m2 = 4617.668
713
+
714
+ if connection.features.supports_length_geodetic:
715
+ qs = Interstate.objects.annotate(length=Length("path"))
716
+ tol = 2 if connection.ops.oracle else 3
717
+ self.assertAlmostEqual(len_m1, qs[0].length.m, tol)
718
+ # TODO: test with spheroid argument (True and False)
719
+ else:
720
+ # Does not support geodetic coordinate systems.
721
+ with self.assertRaises(NotSupportedError):
722
+ list(Interstate.objects.annotate(length=Length("path")))
723
+
724
+ # Now doing length on a projected coordinate system.
725
+ i10 = SouthTexasInterstate.objects.annotate(length=Length("path")).get(
726
+ name="I-10"
727
+ )
728
+ self.assertAlmostEqual(len_m2, i10.length.m, 2)
729
+ self.assertTrue(
730
+ SouthTexasInterstate.objects.annotate(length=Length("path"))
731
+ .filter(length__gt=4000)
732
+ .exists()
733
+ )
734
+ # Length with an explicit geometry value.
735
+ qs = Interstate.objects.annotate(length=Length(i10.path))
736
+ self.assertAlmostEqual(qs.first().length.m, len_m2, 2)
737
+
738
+ @skipUnlessDBFeature("has_Perimeter_function")
739
+ def test_perimeter(self):
740
+ """
741
+ Test the `Perimeter` function.
742
+ """
743
+ # Reference query:
744
+ # SELECT ST_Perimeter(distapp_southtexaszipcode.poly)
745
+ # FROM distapp_southtexaszipcode;
746
+ perim_m = [
747
+ 18404.3550889361,
748
+ 15627.2108551001,
749
+ 20632.5588368978,
750
+ 17094.5996143697,
751
+ ]
752
+ tol = 2 if connection.ops.oracle else 7
753
+ qs = SouthTexasZipcode.objects.annotate(perimeter=Perimeter("poly")).order_by(
754
+ "name"
755
+ )
756
+ for i, z in enumerate(qs):
757
+ self.assertAlmostEqual(perim_m[i], z.perimeter.m, tol)
758
+
759
+ # Running on points; should return 0.
760
+ qs = SouthTexasCity.objects.annotate(perim=Perimeter("point"))
761
+ for city in qs:
762
+ self.assertEqual(0, city.perim.m)
763
+
764
+ @skipUnlessDBFeature("has_Perimeter_function")
765
+ def test_perimeter_geodetic(self):
766
+ # Currently only Oracle supports calculating the perimeter on geodetic
767
+ # geometries (without being transformed).
768
+ qs1 = CensusZipcode.objects.annotate(perim=Perimeter("poly"))
769
+ if connection.features.supports_perimeter_geodetic:
770
+ self.assertAlmostEqual(qs1[0].perim.m, 18406.3818954314, 3)
771
+ else:
772
+ with self.assertRaises(NotSupportedError):
773
+ list(qs1)
774
+ # But should work fine when transformed to projected coordinates
775
+ qs2 = CensusZipcode.objects.annotate(
776
+ perim=Perimeter(Transform("poly", 32140))
777
+ ).filter(name="77002")
778
+ self.assertAlmostEqual(qs2[0].perim.m, 18404.355, 3)
779
+
780
+ @skipUnlessDBFeature(
781
+ "supports_null_geometries", "has_Area_function", "has_Distance_function"
782
+ )
783
+ def test_measurement_null_fields(self):
784
+ """
785
+ Test the measurement functions on fields with NULL values.
786
+ """
787
+ # Creating SouthTexasZipcode w/NULL value.
788
+ SouthTexasZipcode.objects.create(name="78212")
789
+ # Performing distance/area queries against the NULL PolygonField,
790
+ # and ensuring the result of the operations is None.
791
+ htown = SouthTexasCity.objects.get(name="Downtown Houston")
792
+ z = SouthTexasZipcode.objects.annotate(
793
+ distance=Distance("poly", htown.point), area=Area("poly")
794
+ ).get(name="78212")
795
+ self.assertIsNone(z.distance)
796
+ self.assertIsNone(z.area)
testbed/django__django/tests/gis_tests/gdal_tests/__init__.py ADDED
File without changes
testbed/django__django/tests/gis_tests/gdal_tests/test_driver.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+ from unittest import mock
3
+
4
+ from django.contrib.gis.gdal import Driver, GDALException
5
+
6
+ valid_drivers = (
7
+ # vector
8
+ "ESRI Shapefile",
9
+ "MapInfo File",
10
+ "TIGER",
11
+ "S57",
12
+ "DGN",
13
+ "Memory",
14
+ "CSV",
15
+ "GML",
16
+ "KML",
17
+ # raster
18
+ "GTiff",
19
+ "JPEG",
20
+ "MEM",
21
+ "PNG",
22
+ )
23
+
24
+ invalid_drivers = ("Foo baz", "clucka", "ESRI Shp", "ESRI rast")
25
+
26
+ aliases = {
27
+ "eSrI": "ESRI Shapefile",
28
+ "TigER/linE": "TIGER",
29
+ "SHAPE": "ESRI Shapefile",
30
+ "sHp": "ESRI Shapefile",
31
+ "tiFf": "GTiff",
32
+ "tIf": "GTiff",
33
+ "jPEg": "JPEG",
34
+ "jpG": "JPEG",
35
+ }
36
+
37
+
38
+ class DriverTest(unittest.TestCase):
39
+ def test01_valid_driver(self):
40
+ "Testing valid GDAL/OGR Data Source Drivers."
41
+ for d in valid_drivers:
42
+ dr = Driver(d)
43
+ self.assertEqual(d, str(dr))
44
+
45
+ def test02_invalid_driver(self):
46
+ "Testing invalid GDAL/OGR Data Source Drivers."
47
+ for i in invalid_drivers:
48
+ with self.assertRaises(GDALException):
49
+ Driver(i)
50
+
51
+ def test03_aliases(self):
52
+ "Testing driver aliases."
53
+ for alias, full_name in aliases.items():
54
+ dr = Driver(alias)
55
+ self.assertEqual(full_name, str(dr))
56
+
57
+ @mock.patch("django.contrib.gis.gdal.driver.vcapi.get_driver_count")
58
+ @mock.patch("django.contrib.gis.gdal.driver.rcapi.get_driver_count")
59
+ @mock.patch("django.contrib.gis.gdal.driver.vcapi.register_all")
60
+ @mock.patch("django.contrib.gis.gdal.driver.rcapi.register_all")
61
+ def test_registered(self, rreg, vreg, rcount, vcount):
62
+ """
63
+ Prototypes are registered only if their respective driver counts are
64
+ zero.
65
+ """
66
+
67
+ def check(rcount_val, vcount_val):
68
+ vreg.reset_mock()
69
+ rreg.reset_mock()
70
+ rcount.return_value = rcount_val
71
+ vcount.return_value = vcount_val
72
+ Driver.ensure_registered()
73
+ if rcount_val:
74
+ self.assertFalse(rreg.called)
75
+ else:
76
+ rreg.assert_called_once_with()
77
+ if vcount_val:
78
+ self.assertFalse(vreg.called)
79
+ else:
80
+ vreg.assert_called_once_with()
81
+
82
+ check(0, 0)
83
+ check(120, 0)
84
+ check(0, 120)
85
+ check(120, 120)
testbed/django__django/tests/gis_tests/gdal_tests/test_ds.py ADDED
@@ -0,0 +1,355 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import re
3
+ from datetime import datetime
4
+ from pathlib import Path
5
+
6
+ from django.contrib.gis.gdal import DataSource, Envelope, GDALException, OGRGeometry
7
+ from django.contrib.gis.gdal.field import OFTDateTime, OFTInteger, OFTReal, OFTString
8
+ from django.contrib.gis.geos import GEOSGeometry
9
+ from django.test import SimpleTestCase
10
+
11
+ from ..test_data import TEST_DATA, TestDS, get_ds_file
12
+
13
+ wgs_84_wkt = (
14
+ 'GEOGCS["GCS_WGS_1984",DATUM["WGS_1984",SPHEROID["WGS_1984",'
15
+ '6378137,298.257223563]],PRIMEM["Greenwich",0],UNIT["Degree",'
16
+ "0.017453292519943295]]"
17
+ )
18
+ # Using a regex because of small differences depending on GDAL versions.
19
+ wgs_84_wkt_regex = r'^GEOGCS\["(GCS_)?WGS[ _](19)?84".*$'
20
+
21
+ datetime_format = "%Y-%m-%dT%H:%M:%S"
22
+
23
+ # List of acceptable data sources.
24
+ ds_list = (
25
+ TestDS(
26
+ "test_point",
27
+ nfeat=5,
28
+ nfld=3,
29
+ geom="POINT",
30
+ gtype=1,
31
+ driver="ESRI Shapefile",
32
+ fields={"dbl": OFTReal, "int": OFTInteger, "str": OFTString},
33
+ extent=(-1.35011, 0.166623, -0.524093, 0.824508), # Got extent from QGIS
34
+ srs_wkt=wgs_84_wkt,
35
+ field_values={
36
+ "dbl": [float(i) for i in range(1, 6)],
37
+ "int": list(range(1, 6)),
38
+ "str": [str(i) for i in range(1, 6)],
39
+ },
40
+ fids=range(5),
41
+ ),
42
+ TestDS(
43
+ "test_vrt",
44
+ ext="vrt",
45
+ nfeat=3,
46
+ nfld=3,
47
+ geom="POINT",
48
+ gtype="Point25D",
49
+ driver="OGR_VRT",
50
+ fields={
51
+ "POINT_X": OFTString,
52
+ "POINT_Y": OFTString,
53
+ "NUM": OFTString,
54
+ }, # VRT uses CSV, which all types are OFTString.
55
+ extent=(1.0, 2.0, 100.0, 523.5), # Min/Max from CSV
56
+ field_values={
57
+ "POINT_X": ["1.0", "5.0", "100.0"],
58
+ "POINT_Y": ["2.0", "23.0", "523.5"],
59
+ "NUM": ["5", "17", "23"],
60
+ },
61
+ fids=range(1, 4),
62
+ ),
63
+ TestDS(
64
+ "test_poly",
65
+ nfeat=3,
66
+ nfld=3,
67
+ geom="POLYGON",
68
+ gtype=3,
69
+ driver="ESRI Shapefile",
70
+ fields={"float": OFTReal, "int": OFTInteger, "str": OFTString},
71
+ extent=(-1.01513, -0.558245, 0.161876, 0.839637), # Got extent from QGIS
72
+ srs_wkt=wgs_84_wkt,
73
+ ),
74
+ TestDS(
75
+ "has_nulls",
76
+ nfeat=3,
77
+ nfld=6,
78
+ geom="POLYGON",
79
+ gtype=3,
80
+ driver="GeoJSON",
81
+ ext="geojson",
82
+ fields={
83
+ "uuid": OFTString,
84
+ "name": OFTString,
85
+ "num": OFTReal,
86
+ "integer": OFTInteger,
87
+ "datetime": OFTDateTime,
88
+ "boolean": OFTInteger,
89
+ },
90
+ extent=(-75.274200, 39.846504, -74.959717, 40.119040), # Got extent from QGIS
91
+ field_values={
92
+ "uuid": [
93
+ "1378c26f-cbe6-44b0-929f-eb330d4991f5",
94
+ "fa2ba67c-a135-4338-b924-a9622b5d869f",
95
+ "4494c1f3-55ab-4256-b365-12115cb388d5",
96
+ ],
97
+ "name": ["Philadelphia", None, "north"],
98
+ "num": [1.001, None, 0.0],
99
+ "integer": [5, None, 8],
100
+ "boolean": [True, None, False],
101
+ "datetime": [
102
+ datetime.strptime("1994-08-14T11:32:14", datetime_format),
103
+ None,
104
+ datetime.strptime("2018-11-29T03:02:52", datetime_format),
105
+ ],
106
+ },
107
+ fids=range(3),
108
+ ),
109
+ )
110
+
111
+ bad_ds = (TestDS("foo"),)
112
+
113
+
114
+ class DataSourceTest(SimpleTestCase):
115
+ def test01_valid_shp(self):
116
+ "Testing valid SHP Data Source files."
117
+
118
+ for source in ds_list:
119
+ # Loading up the data source
120
+ ds = DataSource(source.ds)
121
+
122
+ # The layer count is what's expected (only 1 layer in a SHP file).
123
+ self.assertEqual(1, len(ds))
124
+
125
+ # Making sure GetName works
126
+ self.assertEqual(source.ds, ds.name)
127
+
128
+ # Making sure the driver name matches up
129
+ self.assertEqual(source.driver, str(ds.driver))
130
+
131
+ # Making sure indexing works
132
+ msg = "Index out of range when accessing layers in a datasource: %s."
133
+ with self.assertRaisesMessage(IndexError, msg % len(ds)):
134
+ ds.__getitem__(len(ds))
135
+
136
+ with self.assertRaisesMessage(
137
+ IndexError, "Invalid OGR layer name given: invalid."
138
+ ):
139
+ ds.__getitem__("invalid")
140
+
141
+ def test_ds_input_pathlib(self):
142
+ test_shp = Path(get_ds_file("test_point", "shp"))
143
+ ds = DataSource(test_shp)
144
+ self.assertEqual(len(ds), 1)
145
+
146
+ def test02_invalid_shp(self):
147
+ "Testing invalid SHP files for the Data Source."
148
+ for source in bad_ds:
149
+ with self.assertRaises(GDALException):
150
+ DataSource(source.ds)
151
+
152
+ def test03a_layers(self):
153
+ "Testing Data Source Layers."
154
+ for source in ds_list:
155
+ ds = DataSource(source.ds)
156
+
157
+ # Incrementing through each layer, this tests DataSource.__iter__
158
+ for layer in ds:
159
+ self.assertEqual(layer.name, source.name)
160
+ self.assertEqual(str(layer), source.name)
161
+ # Making sure we get the number of features we expect
162
+ self.assertEqual(len(layer), source.nfeat)
163
+
164
+ # Making sure we get the number of fields we expect
165
+ self.assertEqual(source.nfld, layer.num_fields)
166
+ self.assertEqual(source.nfld, len(layer.fields))
167
+
168
+ # Testing the layer's extent (an Envelope), and its properties
169
+ self.assertIsInstance(layer.extent, Envelope)
170
+ self.assertAlmostEqual(source.extent[0], layer.extent.min_x, 5)
171
+ self.assertAlmostEqual(source.extent[1], layer.extent.min_y, 5)
172
+ self.assertAlmostEqual(source.extent[2], layer.extent.max_x, 5)
173
+ self.assertAlmostEqual(source.extent[3], layer.extent.max_y, 5)
174
+
175
+ # Now checking the field names.
176
+ flds = layer.fields
177
+ for f in flds:
178
+ self.assertIn(f, source.fields)
179
+
180
+ # Negative FIDs are not allowed.
181
+ with self.assertRaisesMessage(
182
+ IndexError, "Negative indices are not allowed on OGR Layers."
183
+ ):
184
+ layer.__getitem__(-1)
185
+ with self.assertRaisesMessage(IndexError, "Invalid feature id: 50000."):
186
+ layer.__getitem__(50000)
187
+
188
+ if hasattr(source, "field_values"):
189
+ # Testing `Layer.get_fields` (which uses Layer.__iter__)
190
+ for fld_name, fld_value in source.field_values.items():
191
+ self.assertEqual(fld_value, layer.get_fields(fld_name))
192
+
193
+ # Testing `Layer.__getitem__`.
194
+ for i, fid in enumerate(source.fids):
195
+ feat = layer[fid]
196
+ self.assertEqual(fid, feat.fid)
197
+ # Maybe this should be in the test below, but we might
198
+ # as well test the feature values here while in this
199
+ # loop.
200
+ for fld_name, fld_value in source.field_values.items():
201
+ self.assertEqual(fld_value[i], feat.get(fld_name))
202
+
203
+ msg = (
204
+ "Index out of range when accessing field in a feature: %s."
205
+ )
206
+ with self.assertRaisesMessage(IndexError, msg % len(feat)):
207
+ feat.__getitem__(len(feat))
208
+
209
+ with self.assertRaisesMessage(
210
+ IndexError, "Invalid OFT field name given: invalid."
211
+ ):
212
+ feat.__getitem__("invalid")
213
+
214
+ def test03b_layer_slice(self):
215
+ "Test indexing and slicing on Layers."
216
+ # Using the first data-source because the same slice
217
+ # can be used for both the layer and the control values.
218
+ source = ds_list[0]
219
+ ds = DataSource(source.ds)
220
+
221
+ sl = slice(1, 3)
222
+ feats = ds[0][sl]
223
+
224
+ for fld_name in ds[0].fields:
225
+ test_vals = [feat.get(fld_name) for feat in feats]
226
+ control_vals = source.field_values[fld_name][sl]
227
+ self.assertEqual(control_vals, test_vals)
228
+
229
+ def test03c_layer_references(self):
230
+ """
231
+ Ensure OGR objects keep references to the objects they belong to.
232
+ """
233
+ source = ds_list[0]
234
+
235
+ # See ticket #9448.
236
+ def get_layer():
237
+ # This DataSource object is not accessible outside this
238
+ # scope. However, a reference should still be kept alive
239
+ # on the `Layer` returned.
240
+ ds = DataSource(source.ds)
241
+ return ds[0]
242
+
243
+ # Making sure we can call OGR routines on the Layer returned.
244
+ lyr = get_layer()
245
+ self.assertEqual(source.nfeat, len(lyr))
246
+ self.assertEqual(source.gtype, lyr.geom_type.num)
247
+
248
+ # Same issue for Feature/Field objects, see #18640
249
+ self.assertEqual(str(lyr[0]["str"]), "1")
250
+
251
+ def test04_features(self):
252
+ "Testing Data Source Features."
253
+ for source in ds_list:
254
+ ds = DataSource(source.ds)
255
+
256
+ # Incrementing through each layer
257
+ for layer in ds:
258
+ # Incrementing through each feature in the layer
259
+ for feat in layer:
260
+ # Making sure the number of fields, and the geometry type
261
+ # are what's expected.
262
+ self.assertEqual(source.nfld, len(list(feat)))
263
+ self.assertEqual(source.gtype, feat.geom_type)
264
+
265
+ # Making sure the fields match to an appropriate OFT type.
266
+ for k, v in source.fields.items():
267
+ # Making sure we get the proper OGR Field instance, using
268
+ # a string value index for the feature.
269
+ self.assertIsInstance(feat[k], v)
270
+ self.assertIsInstance(feat.fields[0], str)
271
+
272
+ # Testing Feature.__iter__
273
+ for fld in feat:
274
+ self.assertIn(fld.name, source.fields)
275
+
276
+ def test05_geometries(self):
277
+ "Testing Geometries from Data Source Features."
278
+ for source in ds_list:
279
+ ds = DataSource(source.ds)
280
+
281
+ # Incrementing through each layer and feature.
282
+ for layer in ds:
283
+ geoms = layer.get_geoms()
284
+ geos_geoms = layer.get_geoms(geos=True)
285
+ self.assertEqual(len(geoms), len(geos_geoms))
286
+ self.assertEqual(len(geoms), len(layer))
287
+ for feat, geom, geos_geom in zip(layer, geoms, geos_geoms):
288
+ g = feat.geom
289
+ self.assertEqual(geom, g)
290
+ self.assertIsInstance(geos_geom, GEOSGeometry)
291
+ self.assertEqual(g, geos_geom.ogr)
292
+ # Making sure we get the right Geometry name & type
293
+ self.assertEqual(source.geom, g.geom_name)
294
+ self.assertEqual(source.gtype, g.geom_type)
295
+
296
+ # Making sure the SpatialReference is as expected.
297
+ if hasattr(source, "srs_wkt"):
298
+ self.assertIsNotNone(re.match(wgs_84_wkt_regex, g.srs.wkt))
299
+
300
+ def test06_spatial_filter(self):
301
+ "Testing the Layer.spatial_filter property."
302
+ ds = DataSource(get_ds_file("cities", "shp"))
303
+ lyr = ds[0]
304
+
305
+ # When not set, it should be None.
306
+ self.assertIsNone(lyr.spatial_filter)
307
+
308
+ # Must be set a/an OGRGeometry or 4-tuple.
309
+ with self.assertRaises(TypeError):
310
+ lyr._set_spatial_filter("foo")
311
+
312
+ # Setting the spatial filter with a tuple/list with the extent of
313
+ # a buffer centering around Pueblo.
314
+ with self.assertRaises(ValueError):
315
+ lyr._set_spatial_filter(list(range(5)))
316
+ filter_extent = (-105.609252, 37.255001, -103.609252, 39.255001)
317
+ lyr.spatial_filter = (-105.609252, 37.255001, -103.609252, 39.255001)
318
+ self.assertEqual(OGRGeometry.from_bbox(filter_extent), lyr.spatial_filter)
319
+ feats = [feat for feat in lyr]
320
+ self.assertEqual(1, len(feats))
321
+ self.assertEqual("Pueblo", feats[0].get("Name"))
322
+
323
+ # Setting the spatial filter with an OGRGeometry for buffer centering
324
+ # around Houston.
325
+ filter_geom = OGRGeometry(
326
+ "POLYGON((-96.363151 28.763374,-94.363151 28.763374,"
327
+ "-94.363151 30.763374,-96.363151 30.763374,-96.363151 28.763374))"
328
+ )
329
+ lyr.spatial_filter = filter_geom
330
+ self.assertEqual(filter_geom, lyr.spatial_filter)
331
+ feats = [feat for feat in lyr]
332
+ self.assertEqual(1, len(feats))
333
+ self.assertEqual("Houston", feats[0].get("Name"))
334
+
335
+ # Clearing the spatial filter by setting it to None. Now
336
+ # should indicate that there are 3 features in the Layer.
337
+ lyr.spatial_filter = None
338
+ self.assertEqual(3, len(lyr))
339
+
340
+ def test07_integer_overflow(self):
341
+ "Testing that OFTReal fields, treated as OFTInteger, do not overflow."
342
+ # Using *.dbf from Census 2010 TIGER Shapefile for Texas,
343
+ # which has land area ('ALAND10') stored in a Real field
344
+ # with no precision.
345
+ ds = DataSource(os.path.join(TEST_DATA, "texas.dbf"))
346
+ feat = ds[0][0]
347
+ # Reference value obtained using `ogrinfo`.
348
+ self.assertEqual(676586997978, feat.get("ALAND10"))
349
+
350
+ def test_nonexistent_field(self):
351
+ source = ds_list[0]
352
+ ds = DataSource(source.ds)
353
+ msg = "invalid field name: nonexistent"
354
+ with self.assertRaisesMessage(GDALException, msg):
355
+ ds[0].get_fields("nonexistent")
testbed/django__django/tests/gis_tests/gdal_tests/test_envelope.py ADDED
@@ -0,0 +1,93 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from django.contrib.gis.gdal import Envelope, GDALException
4
+
5
+
6
+ class TestPoint:
7
+ def __init__(self, x, y):
8
+ self.x = x
9
+ self.y = y
10
+
11
+
12
+ class EnvelopeTest(unittest.TestCase):
13
+ def setUp(self):
14
+ self.e = Envelope(0, 0, 5, 5)
15
+
16
+ def test01_init(self):
17
+ "Testing Envelope initialization."
18
+ e1 = Envelope((0, 0, 5, 5))
19
+ Envelope(0, 0, 5, 5)
20
+ Envelope(0, "0", "5", 5) # Thanks to ww for this
21
+ Envelope(e1._envelope)
22
+ with self.assertRaises(GDALException):
23
+ Envelope((5, 5, 0, 0))
24
+ with self.assertRaises(GDALException):
25
+ Envelope(5, 5, 0, 0)
26
+ with self.assertRaises(GDALException):
27
+ Envelope((0, 0, 5, 5, 3))
28
+ with self.assertRaises(GDALException):
29
+ Envelope(())
30
+ with self.assertRaises(ValueError):
31
+ Envelope(0, "a", 5, 5)
32
+ with self.assertRaises(TypeError):
33
+ Envelope("foo")
34
+ with self.assertRaises(GDALException):
35
+ Envelope((1, 1, 0, 0))
36
+ # Shouldn't raise an exception for min_x == max_x or min_y == max_y
37
+ Envelope(0, 0, 0, 0)
38
+
39
+ def test02_properties(self):
40
+ "Testing Envelope properties."
41
+ e = Envelope(0, 0, 2, 3)
42
+ self.assertEqual(0, e.min_x)
43
+ self.assertEqual(0, e.min_y)
44
+ self.assertEqual(2, e.max_x)
45
+ self.assertEqual(3, e.max_y)
46
+ self.assertEqual((0, 0), e.ll)
47
+ self.assertEqual((2, 3), e.ur)
48
+ self.assertEqual((0, 0, 2, 3), e.tuple)
49
+ self.assertEqual("POLYGON((0.0 0.0,0.0 3.0,2.0 3.0,2.0 0.0,0.0 0.0))", e.wkt)
50
+ self.assertEqual("(0.0, 0.0, 2.0, 3.0)", str(e))
51
+
52
+ def test03_equivalence(self):
53
+ "Testing Envelope equivalence."
54
+ e1 = Envelope(0.523, 0.217, 253.23, 523.69)
55
+ e2 = Envelope((0.523, 0.217, 253.23, 523.69))
56
+ self.assertEqual(e1, e2)
57
+ self.assertEqual((0.523, 0.217, 253.23, 523.69), e1)
58
+
59
+ def test04_expand_to_include_pt_2_params(self):
60
+ "Testing Envelope expand_to_include -- point as two parameters."
61
+ self.e.expand_to_include(2, 6)
62
+ self.assertEqual((0, 0, 5, 6), self.e)
63
+ self.e.expand_to_include(-1, -1)
64
+ self.assertEqual((-1, -1, 5, 6), self.e)
65
+
66
+ def test05_expand_to_include_pt_2_tuple(self):
67
+ "Testing Envelope expand_to_include -- point as a single 2-tuple parameter."
68
+ self.e.expand_to_include((10, 10))
69
+ self.assertEqual((0, 0, 10, 10), self.e)
70
+ self.e.expand_to_include((-10, -10))
71
+ self.assertEqual((-10, -10, 10, 10), self.e)
72
+
73
+ def test06_expand_to_include_extent_4_params(self):
74
+ "Testing Envelope expand_to_include -- extent as 4 parameters."
75
+ self.e.expand_to_include(-1, 1, 3, 7)
76
+ self.assertEqual((-1, 0, 5, 7), self.e)
77
+
78
+ def test06_expand_to_include_extent_4_tuple(self):
79
+ "Testing Envelope expand_to_include -- extent as a single 4-tuple parameter."
80
+ self.e.expand_to_include((-1, 1, 3, 7))
81
+ self.assertEqual((-1, 0, 5, 7), self.e)
82
+
83
+ def test07_expand_to_include_envelope(self):
84
+ "Testing Envelope expand_to_include with Envelope as parameter."
85
+ self.e.expand_to_include(Envelope(-1, 1, 3, 7))
86
+ self.assertEqual((-1, 0, 5, 7), self.e)
87
+
88
+ def test08_expand_to_include_point(self):
89
+ "Testing Envelope expand_to_include with Point as parameter."
90
+ self.e.expand_to_include(TestPoint(-1, 1))
91
+ self.assertEqual((-1, 0, 5, 5), self.e)
92
+ self.e.expand_to_include(TestPoint(10, 10))
93
+ self.assertEqual((-1, 0, 10, 10), self.e)
testbed/django__django/tests/gis_tests/gdal_tests/test_geom.py ADDED
@@ -0,0 +1,634 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import pickle
3
+
4
+ from django.contrib.gis.gdal import (
5
+ CoordTransform,
6
+ GDALException,
7
+ OGRGeometry,
8
+ OGRGeomType,
9
+ SpatialReference,
10
+ )
11
+ from django.template import Context
12
+ from django.template.engine import Engine
13
+ from django.test import SimpleTestCase
14
+
15
+ from ..test_data import TestDataMixin
16
+
17
+
18
+ class OGRGeomTest(SimpleTestCase, TestDataMixin):
19
+ "This tests the OGR Geometry."
20
+
21
+ def test_geomtype(self):
22
+ "Testing OGRGeomType object."
23
+
24
+ # OGRGeomType should initialize on all these inputs.
25
+ OGRGeomType(1)
26
+ OGRGeomType(7)
27
+ OGRGeomType("point")
28
+ OGRGeomType("GeometrycollectioN")
29
+ OGRGeomType("LINearrING")
30
+ OGRGeomType("Unknown")
31
+
32
+ # Should throw TypeError on this input
33
+ with self.assertRaises(GDALException):
34
+ OGRGeomType(23)
35
+ with self.assertRaises(GDALException):
36
+ OGRGeomType("fooD")
37
+ with self.assertRaises(GDALException):
38
+ OGRGeomType(9)
39
+
40
+ # Equivalence can take strings, ints, and other OGRGeomTypes
41
+ self.assertEqual(OGRGeomType(1), OGRGeomType(1))
42
+ self.assertEqual(OGRGeomType(7), "GeometryCollection")
43
+ self.assertEqual(OGRGeomType("point"), "POINT")
44
+ self.assertNotEqual(OGRGeomType("point"), 2)
45
+ self.assertEqual(OGRGeomType("unknown"), 0)
46
+ self.assertEqual(OGRGeomType(6), "MULtiPolyGON")
47
+ self.assertEqual(OGRGeomType(1), OGRGeomType("point"))
48
+ self.assertNotEqual(OGRGeomType("POINT"), OGRGeomType(6))
49
+
50
+ # Testing the Django field name equivalent property.
51
+ self.assertEqual("PointField", OGRGeomType("Point").django)
52
+ self.assertEqual("GeometryField", OGRGeomType("Geometry").django)
53
+ self.assertEqual("GeometryField", OGRGeomType("Unknown").django)
54
+ self.assertIsNone(OGRGeomType("none").django)
55
+
56
+ # 'Geometry' initialization implies an unknown geometry type.
57
+ gt = OGRGeomType("Geometry")
58
+ self.assertEqual(0, gt.num)
59
+ self.assertEqual("Unknown", gt.name)
60
+
61
+ def test_geomtype_25d(self):
62
+ "Testing OGRGeomType object with 25D types."
63
+ wkb25bit = OGRGeomType.wkb25bit
64
+ self.assertEqual(OGRGeomType(wkb25bit + 1), "Point25D")
65
+ self.assertEqual(OGRGeomType("MultiLineString25D"), (5 + wkb25bit))
66
+ self.assertEqual(
67
+ "GeometryCollectionField", OGRGeomType("GeometryCollection25D").django
68
+ )
69
+
70
+ def test_wkt(self):
71
+ "Testing WKT output."
72
+ for g in self.geometries.wkt_out:
73
+ geom = OGRGeometry(g.wkt)
74
+ self.assertEqual(g.wkt, geom.wkt)
75
+
76
+ def test_ewkt(self):
77
+ "Testing EWKT input/output."
78
+ for ewkt_val in ("POINT (1 2 3)", "LINEARRING (0 0,1 1,2 1,0 0)"):
79
+ # First with ewkt output when no SRID in EWKT
80
+ self.assertEqual(ewkt_val, OGRGeometry(ewkt_val).ewkt)
81
+ # No test consumption with an SRID specified.
82
+ ewkt_val = "SRID=4326;%s" % ewkt_val
83
+ geom = OGRGeometry(ewkt_val)
84
+ self.assertEqual(ewkt_val, geom.ewkt)
85
+ self.assertEqual(4326, geom.srs.srid)
86
+
87
+ def test_gml(self):
88
+ "Testing GML output."
89
+ for g in self.geometries.wkt_out:
90
+ geom = OGRGeometry(g.wkt)
91
+ exp_gml = g.gml
92
+ self.assertEqual(exp_gml, geom.gml)
93
+
94
+ def test_hex(self):
95
+ "Testing HEX input/output."
96
+ for g in self.geometries.hex_wkt:
97
+ geom1 = OGRGeometry(g.wkt)
98
+ self.assertEqual(g.hex.encode(), geom1.hex)
99
+ # Constructing w/HEX
100
+ geom2 = OGRGeometry(g.hex)
101
+ self.assertEqual(geom1, geom2)
102
+
103
+ def test_wkb(self):
104
+ "Testing WKB input/output."
105
+ for g in self.geometries.hex_wkt:
106
+ geom1 = OGRGeometry(g.wkt)
107
+ wkb = geom1.wkb
108
+ self.assertEqual(wkb.hex().upper(), g.hex)
109
+ # Constructing w/WKB.
110
+ geom2 = OGRGeometry(wkb)
111
+ self.assertEqual(geom1, geom2)
112
+
113
+ def test_json(self):
114
+ "Testing GeoJSON input/output."
115
+ for g in self.geometries.json_geoms:
116
+ geom = OGRGeometry(g.wkt)
117
+ if not hasattr(g, "not_equal"):
118
+ # Loading jsons to prevent decimal differences
119
+ self.assertEqual(json.loads(g.json), json.loads(geom.json))
120
+ self.assertEqual(json.loads(g.json), json.loads(geom.geojson))
121
+ self.assertEqual(OGRGeometry(g.wkt), OGRGeometry(geom.json))
122
+ # Test input with some garbage content (but valid json) (#15529)
123
+ geom = OGRGeometry(
124
+ '{"type": "Point", "coordinates": [ 100.0, 0.0 ], "other": "<test>"}'
125
+ )
126
+ self.assertIsInstance(geom, OGRGeometry)
127
+
128
+ def test_points(self):
129
+ "Testing Point objects."
130
+
131
+ OGRGeometry("POINT(0 0)")
132
+ for p in self.geometries.points:
133
+ if not hasattr(p, "z"): # No 3D
134
+ pnt = OGRGeometry(p.wkt)
135
+ self.assertEqual(1, pnt.geom_type)
136
+ self.assertEqual("POINT", pnt.geom_name)
137
+ self.assertEqual(p.x, pnt.x)
138
+ self.assertEqual(p.y, pnt.y)
139
+ self.assertEqual((p.x, p.y), pnt.tuple)
140
+
141
+ def test_multipoints(self):
142
+ "Testing MultiPoint objects."
143
+ for mp in self.geometries.multipoints:
144
+ mgeom1 = OGRGeometry(mp.wkt) # First one from WKT
145
+ self.assertEqual(4, mgeom1.geom_type)
146
+ self.assertEqual("MULTIPOINT", mgeom1.geom_name)
147
+ mgeom2 = OGRGeometry("MULTIPOINT") # Creating empty multipoint
148
+ mgeom3 = OGRGeometry("MULTIPOINT")
149
+ for g in mgeom1:
150
+ mgeom2.add(g) # adding each point from the multipoints
151
+ mgeom3.add(g.wkt) # should take WKT as well
152
+ self.assertEqual(mgeom1, mgeom2) # they should equal
153
+ self.assertEqual(mgeom1, mgeom3)
154
+ self.assertEqual(mp.coords, mgeom2.coords)
155
+ self.assertEqual(mp.n_p, mgeom2.point_count)
156
+
157
+ def test_linestring(self):
158
+ "Testing LineString objects."
159
+ prev = OGRGeometry("POINT(0 0)")
160
+ for ls in self.geometries.linestrings:
161
+ linestr = OGRGeometry(ls.wkt)
162
+ self.assertEqual(2, linestr.geom_type)
163
+ self.assertEqual("LINESTRING", linestr.geom_name)
164
+ self.assertEqual(ls.n_p, linestr.point_count)
165
+ self.assertEqual(ls.coords, linestr.tuple)
166
+ self.assertEqual(linestr, OGRGeometry(ls.wkt))
167
+ self.assertNotEqual(linestr, prev)
168
+ msg = "Index out of range when accessing points of a line string: %s."
169
+ with self.assertRaisesMessage(IndexError, msg % len(linestr)):
170
+ linestr.__getitem__(len(linestr))
171
+ prev = linestr
172
+
173
+ # Testing the x, y properties.
174
+ x = [tmpx for tmpx, tmpy in ls.coords]
175
+ y = [tmpy for tmpx, tmpy in ls.coords]
176
+ self.assertEqual(x, linestr.x)
177
+ self.assertEqual(y, linestr.y)
178
+
179
+ def test_multilinestring(self):
180
+ "Testing MultiLineString objects."
181
+ prev = OGRGeometry("POINT(0 0)")
182
+ for mls in self.geometries.multilinestrings:
183
+ mlinestr = OGRGeometry(mls.wkt)
184
+ self.assertEqual(5, mlinestr.geom_type)
185
+ self.assertEqual("MULTILINESTRING", mlinestr.geom_name)
186
+ self.assertEqual(mls.n_p, mlinestr.point_count)
187
+ self.assertEqual(mls.coords, mlinestr.tuple)
188
+ self.assertEqual(mlinestr, OGRGeometry(mls.wkt))
189
+ self.assertNotEqual(mlinestr, prev)
190
+ prev = mlinestr
191
+ for ls in mlinestr:
192
+ self.assertEqual(2, ls.geom_type)
193
+ self.assertEqual("LINESTRING", ls.geom_name)
194
+ msg = "Index out of range when accessing geometry in a collection: %s."
195
+ with self.assertRaisesMessage(IndexError, msg % len(mlinestr)):
196
+ mlinestr.__getitem__(len(mlinestr))
197
+
198
+ def test_linearring(self):
199
+ "Testing LinearRing objects."
200
+ prev = OGRGeometry("POINT(0 0)")
201
+ for rr in self.geometries.linearrings:
202
+ lr = OGRGeometry(rr.wkt)
203
+ # self.assertEqual(101, lr.geom_type.num)
204
+ self.assertEqual("LINEARRING", lr.geom_name)
205
+ self.assertEqual(rr.n_p, len(lr))
206
+ self.assertEqual(lr, OGRGeometry(rr.wkt))
207
+ self.assertNotEqual(lr, prev)
208
+ prev = lr
209
+
210
+ def test_polygons(self):
211
+ "Testing Polygon objects."
212
+
213
+ # Testing `from_bbox` class method
214
+ bbox = (-180, -90, 180, 90)
215
+ p = OGRGeometry.from_bbox(bbox)
216
+ self.assertEqual(bbox, p.extent)
217
+
218
+ prev = OGRGeometry("POINT(0 0)")
219
+ for p in self.geometries.polygons:
220
+ poly = OGRGeometry(p.wkt)
221
+ self.assertEqual(3, poly.geom_type)
222
+ self.assertEqual("POLYGON", poly.geom_name)
223
+ self.assertEqual(p.n_p, poly.point_count)
224
+ self.assertEqual(p.n_i + 1, len(poly))
225
+ msg = "Index out of range when accessing rings of a polygon: %s."
226
+ with self.assertRaisesMessage(IndexError, msg % len(poly)):
227
+ poly.__getitem__(len(poly))
228
+
229
+ # Testing area & centroid.
230
+ self.assertAlmostEqual(p.area, poly.area, 9)
231
+ x, y = poly.centroid.tuple
232
+ self.assertAlmostEqual(p.centroid[0], x, 9)
233
+ self.assertAlmostEqual(p.centroid[1], y, 9)
234
+
235
+ # Testing equivalence
236
+ self.assertEqual(poly, OGRGeometry(p.wkt))
237
+ self.assertNotEqual(poly, prev)
238
+
239
+ if p.ext_ring_cs:
240
+ ring = poly[0]
241
+ self.assertEqual(p.ext_ring_cs, ring.tuple)
242
+ self.assertEqual(p.ext_ring_cs, poly[0].tuple)
243
+ self.assertEqual(len(p.ext_ring_cs), ring.point_count)
244
+
245
+ for r in poly:
246
+ self.assertEqual("LINEARRING", r.geom_name)
247
+
248
+ def test_polygons_templates(self):
249
+ # Accessing Polygon attributes in templates should work.
250
+ engine = Engine()
251
+ template = engine.from_string("{{ polygons.0.wkt }}")
252
+ polygons = [OGRGeometry(p.wkt) for p in self.geometries.multipolygons[:2]]
253
+ content = template.render(Context({"polygons": polygons}))
254
+ self.assertIn("MULTIPOLYGON (((100", content)
255
+
256
+ def test_closepolygons(self):
257
+ "Testing closing Polygon objects."
258
+ # Both rings in this geometry are not closed.
259
+ poly = OGRGeometry("POLYGON((0 0, 5 0, 5 5, 0 5), (1 1, 2 1, 2 2, 2 1))")
260
+ self.assertEqual(8, poly.point_count)
261
+ with self.assertRaises(GDALException):
262
+ poly.centroid
263
+
264
+ poly.close_rings()
265
+ self.assertEqual(
266
+ 10, poly.point_count
267
+ ) # Two closing points should've been added
268
+ self.assertEqual(OGRGeometry("POINT(2.5 2.5)"), poly.centroid)
269
+
270
+ def test_multipolygons(self):
271
+ "Testing MultiPolygon objects."
272
+ OGRGeometry("POINT(0 0)")
273
+ for mp in self.geometries.multipolygons:
274
+ mpoly = OGRGeometry(mp.wkt)
275
+ self.assertEqual(6, mpoly.geom_type)
276
+ self.assertEqual("MULTIPOLYGON", mpoly.geom_name)
277
+ if mp.valid:
278
+ self.assertEqual(mp.n_p, mpoly.point_count)
279
+ self.assertEqual(mp.num_geom, len(mpoly))
280
+ msg = "Index out of range when accessing geometry in a collection: %s."
281
+ with self.assertRaisesMessage(IndexError, msg % len(mpoly)):
282
+ mpoly.__getitem__(len(mpoly))
283
+ for p in mpoly:
284
+ self.assertEqual("POLYGON", p.geom_name)
285
+ self.assertEqual(3, p.geom_type)
286
+ self.assertEqual(mpoly.wkt, OGRGeometry(mp.wkt).wkt)
287
+
288
+ def test_srs(self):
289
+ "Testing OGR Geometries with Spatial Reference objects."
290
+ for mp in self.geometries.multipolygons:
291
+ # Creating a geometry w/spatial reference
292
+ sr = SpatialReference("WGS84")
293
+ mpoly = OGRGeometry(mp.wkt, sr)
294
+ self.assertEqual(sr.wkt, mpoly.srs.wkt)
295
+
296
+ # Ensuring that SRS is propagated to clones.
297
+ klone = mpoly.clone()
298
+ self.assertEqual(sr.wkt, klone.srs.wkt)
299
+
300
+ # Ensuring all children geometries (polygons and their rings) all
301
+ # return the assigned spatial reference as well.
302
+ for poly in mpoly:
303
+ self.assertEqual(sr.wkt, poly.srs.wkt)
304
+ for ring in poly:
305
+ self.assertEqual(sr.wkt, ring.srs.wkt)
306
+
307
+ # Ensuring SRS propagate in topological ops.
308
+ a = OGRGeometry(self.geometries.topology_geoms[0].wkt_a, sr)
309
+ b = OGRGeometry(self.geometries.topology_geoms[0].wkt_b, sr)
310
+ diff = a.difference(b)
311
+ union = a.union(b)
312
+ self.assertEqual(sr.wkt, diff.srs.wkt)
313
+ self.assertEqual(sr.srid, union.srs.srid)
314
+
315
+ # Instantiating w/an integer SRID
316
+ mpoly = OGRGeometry(mp.wkt, 4326)
317
+ self.assertEqual(4326, mpoly.srid)
318
+ mpoly.srs = SpatialReference(4269)
319
+ self.assertEqual(4269, mpoly.srid)
320
+ self.assertEqual("NAD83", mpoly.srs.name)
321
+
322
+ # Incrementing through the multipolygon after the spatial reference
323
+ # has been re-assigned.
324
+ for poly in mpoly:
325
+ self.assertEqual(mpoly.srs.wkt, poly.srs.wkt)
326
+ poly.srs = 32140
327
+ for ring in poly:
328
+ # Changing each ring in the polygon
329
+ self.assertEqual(32140, ring.srs.srid)
330
+ self.assertEqual("NAD83 / Texas South Central", ring.srs.name)
331
+ ring.srs = str(SpatialReference(4326)) # back to WGS84
332
+ self.assertEqual(4326, ring.srs.srid)
333
+
334
+ # Using the `srid` property.
335
+ ring.srid = 4322
336
+ self.assertEqual("WGS 72", ring.srs.name)
337
+ self.assertEqual(4322, ring.srid)
338
+
339
+ # srs/srid may be assigned their own values, even when srs is None.
340
+ mpoly = OGRGeometry(mp.wkt, srs=None)
341
+ mpoly.srs = mpoly.srs
342
+ mpoly.srid = mpoly.srid
343
+
344
+ def test_srs_transform(self):
345
+ "Testing transform()."
346
+ orig = OGRGeometry("POINT (-104.609 38.255)", 4326)
347
+ trans = OGRGeometry("POINT (992385.4472045 481455.4944650)", 2774)
348
+
349
+ # Using an srid, a SpatialReference object, and a CoordTransform object
350
+ # or transformations.
351
+ t1, t2, t3 = orig.clone(), orig.clone(), orig.clone()
352
+ t1.transform(trans.srid)
353
+ t2.transform(SpatialReference("EPSG:2774"))
354
+ ct = CoordTransform(SpatialReference("WGS84"), SpatialReference(2774))
355
+ t3.transform(ct)
356
+
357
+ # Testing use of the `clone` keyword.
358
+ k1 = orig.clone()
359
+ k2 = k1.transform(trans.srid, clone=True)
360
+ self.assertEqual(k1, orig)
361
+ self.assertNotEqual(k1, k2)
362
+
363
+ # Different PROJ versions use different transformations, all are
364
+ # correct as having a 1 meter accuracy.
365
+ prec = -1
366
+ for p in (t1, t2, t3, k2):
367
+ self.assertAlmostEqual(trans.x, p.x, prec)
368
+ self.assertAlmostEqual(trans.y, p.y, prec)
369
+
370
+ def test_transform_dim(self):
371
+ "Testing coordinate dimension is the same on transformed geometries."
372
+ ls_orig = OGRGeometry("LINESTRING(-104.609 38.255)", 4326)
373
+ ls_trans = OGRGeometry("LINESTRING(992385.4472045 481455.4944650)", 2774)
374
+
375
+ # Different PROJ versions use different transformations, all are
376
+ # correct as having a 1 meter accuracy.
377
+ prec = -1
378
+ ls_orig.transform(ls_trans.srs)
379
+ # Making sure the coordinate dimension is still 2D.
380
+ self.assertEqual(2, ls_orig.coord_dim)
381
+ self.assertAlmostEqual(ls_trans.x[0], ls_orig.x[0], prec)
382
+ self.assertAlmostEqual(ls_trans.y[0], ls_orig.y[0], prec)
383
+
384
+ def test_difference(self):
385
+ "Testing difference()."
386
+ for i in range(len(self.geometries.topology_geoms)):
387
+ a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a)
388
+ b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b)
389
+ d1 = OGRGeometry(self.geometries.diff_geoms[i].wkt)
390
+ d2 = a.difference(b)
391
+ self.assertTrue(d1.geos.equals(d2.geos))
392
+ self.assertTrue(
393
+ d1.geos.equals((a - b).geos)
394
+ ) # __sub__ is difference operator
395
+ a -= b # testing __isub__
396
+ self.assertTrue(d1.geos.equals(a.geos))
397
+
398
+ def test_intersection(self):
399
+ "Testing intersects() and intersection()."
400
+ for i in range(len(self.geometries.topology_geoms)):
401
+ a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a)
402
+ b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b)
403
+ i1 = OGRGeometry(self.geometries.intersect_geoms[i].wkt)
404
+ self.assertTrue(a.intersects(b))
405
+ i2 = a.intersection(b)
406
+ self.assertTrue(i1.geos.equals(i2.geos))
407
+ self.assertTrue(
408
+ i1.geos.equals((a & b).geos)
409
+ ) # __and__ is intersection operator
410
+ a &= b # testing __iand__
411
+ self.assertTrue(i1.geos.equals(a.geos))
412
+
413
+ def test_symdifference(self):
414
+ "Testing sym_difference()."
415
+ for i in range(len(self.geometries.topology_geoms)):
416
+ a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a)
417
+ b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b)
418
+ d1 = OGRGeometry(self.geometries.sdiff_geoms[i].wkt)
419
+ d2 = a.sym_difference(b)
420
+ self.assertTrue(d1.geos.equals(d2.geos))
421
+ self.assertTrue(
422
+ d1.geos.equals((a ^ b).geos)
423
+ ) # __xor__ is symmetric difference operator
424
+ a ^= b # testing __ixor__
425
+ self.assertTrue(d1.geos.equals(a.geos))
426
+
427
+ def test_union(self):
428
+ "Testing union()."
429
+ for i in range(len(self.geometries.topology_geoms)):
430
+ a = OGRGeometry(self.geometries.topology_geoms[i].wkt_a)
431
+ b = OGRGeometry(self.geometries.topology_geoms[i].wkt_b)
432
+ u1 = OGRGeometry(self.geometries.union_geoms[i].wkt)
433
+ u2 = a.union(b)
434
+ self.assertTrue(u1.geos.equals(u2.geos))
435
+ self.assertTrue(u1.geos.equals((a | b).geos)) # __or__ is union operator
436
+ a |= b # testing __ior__
437
+ self.assertTrue(u1.geos.equals(a.geos))
438
+
439
+ def test_add(self):
440
+ "Testing GeometryCollection.add()."
441
+ # Can't insert a Point into a MultiPolygon.
442
+ mp = OGRGeometry("MultiPolygon")
443
+ pnt = OGRGeometry("POINT(5 23)")
444
+ with self.assertRaises(GDALException):
445
+ mp.add(pnt)
446
+
447
+ # GeometryCollection.add may take an OGRGeometry (if another collection
448
+ # of the same type all child geoms will be added individually) or WKT.
449
+ for mp in self.geometries.multipolygons:
450
+ mpoly = OGRGeometry(mp.wkt)
451
+ mp1 = OGRGeometry("MultiPolygon")
452
+ mp2 = OGRGeometry("MultiPolygon")
453
+ mp3 = OGRGeometry("MultiPolygon")
454
+
455
+ for poly in mpoly:
456
+ mp1.add(poly) # Adding a geometry at a time
457
+ mp2.add(poly.wkt) # Adding WKT
458
+ mp3.add(mpoly) # Adding a MultiPolygon's entire contents at once.
459
+ for tmp in (mp1, mp2, mp3):
460
+ self.assertEqual(mpoly, tmp)
461
+
462
+ def test_extent(self):
463
+ "Testing `extent` property."
464
+ # The xmin, ymin, xmax, ymax of the MultiPoint should be returned.
465
+ mp = OGRGeometry("MULTIPOINT(5 23, 0 0, 10 50)")
466
+ self.assertEqual((0.0, 0.0, 10.0, 50.0), mp.extent)
467
+ # Testing on the 'real world' Polygon.
468
+ poly = OGRGeometry(self.geometries.polygons[3].wkt)
469
+ ring = poly.shell
470
+ x, y = ring.x, ring.y
471
+ xmin, ymin = min(x), min(y)
472
+ xmax, ymax = max(x), max(y)
473
+ self.assertEqual((xmin, ymin, xmax, ymax), poly.extent)
474
+
475
+ def test_25D(self):
476
+ "Testing 2.5D geometries."
477
+ pnt_25d = OGRGeometry("POINT(1 2 3)")
478
+ self.assertEqual("Point25D", pnt_25d.geom_type.name)
479
+ self.assertEqual(3.0, pnt_25d.z)
480
+ self.assertEqual(3, pnt_25d.coord_dim)
481
+ ls_25d = OGRGeometry("LINESTRING(1 1 1,2 2 2,3 3 3)")
482
+ self.assertEqual("LineString25D", ls_25d.geom_type.name)
483
+ self.assertEqual([1.0, 2.0, 3.0], ls_25d.z)
484
+ self.assertEqual(3, ls_25d.coord_dim)
485
+
486
+ def test_pickle(self):
487
+ "Testing pickle support."
488
+ g1 = OGRGeometry("LINESTRING(1 1 1,2 2 2,3 3 3)", "WGS84")
489
+ g2 = pickle.loads(pickle.dumps(g1))
490
+ self.assertEqual(g1, g2)
491
+ self.assertEqual(4326, g2.srs.srid)
492
+ self.assertEqual(g1.srs.wkt, g2.srs.wkt)
493
+
494
+ def test_ogrgeometry_transform_workaround(self):
495
+ "Testing coordinate dimensions on geometries after transformation."
496
+ # A bug in GDAL versions prior to 1.7 changes the coordinate
497
+ # dimension of a geometry after it has been transformed.
498
+ # This test ensures that the bug workarounds employed within
499
+ # `OGRGeometry.transform` indeed work.
500
+ wkt_2d = "MULTILINESTRING ((0 0,1 1,2 2))"
501
+ wkt_3d = "MULTILINESTRING ((0 0 0,1 1 1,2 2 2))"
502
+ srid = 4326
503
+
504
+ # For both the 2D and 3D MultiLineString, ensure _both_ the dimension
505
+ # of the collection and the component LineString have the expected
506
+ # coordinate dimension after transform.
507
+ geom = OGRGeometry(wkt_2d, srid)
508
+ geom.transform(srid)
509
+ self.assertEqual(2, geom.coord_dim)
510
+ self.assertEqual(2, geom[0].coord_dim)
511
+ self.assertEqual(wkt_2d, geom.wkt)
512
+
513
+ geom = OGRGeometry(wkt_3d, srid)
514
+ geom.transform(srid)
515
+ self.assertEqual(3, geom.coord_dim)
516
+ self.assertEqual(3, geom[0].coord_dim)
517
+ self.assertEqual(wkt_3d, geom.wkt)
518
+
519
+ # Testing binary predicates, `assertIs` is used to check that bool is returned.
520
+
521
+ def test_equivalence_regression(self):
522
+ "Testing equivalence methods with non-OGRGeometry instances."
523
+ self.assertIsNotNone(OGRGeometry("POINT(0 0)"))
524
+ self.assertNotEqual(OGRGeometry("LINESTRING(0 0, 1 1)"), 3)
525
+
526
+ def test_contains(self):
527
+ self.assertIs(
528
+ OGRGeometry("POINT(0 0)").contains(OGRGeometry("POINT(0 0)")), True
529
+ )
530
+ self.assertIs(
531
+ OGRGeometry("POINT(0 0)").contains(OGRGeometry("POINT(0 1)")), False
532
+ )
533
+
534
+ def test_crosses(self):
535
+ self.assertIs(
536
+ OGRGeometry("LINESTRING(0 0, 1 1)").crosses(
537
+ OGRGeometry("LINESTRING(0 1, 1 0)")
538
+ ),
539
+ True,
540
+ )
541
+ self.assertIs(
542
+ OGRGeometry("LINESTRING(0 0, 0 1)").crosses(
543
+ OGRGeometry("LINESTRING(1 0, 1 1)")
544
+ ),
545
+ False,
546
+ )
547
+
548
+ def test_disjoint(self):
549
+ self.assertIs(
550
+ OGRGeometry("LINESTRING(0 0, 1 1)").disjoint(
551
+ OGRGeometry("LINESTRING(0 1, 1 0)")
552
+ ),
553
+ False,
554
+ )
555
+ self.assertIs(
556
+ OGRGeometry("LINESTRING(0 0, 0 1)").disjoint(
557
+ OGRGeometry("LINESTRING(1 0, 1 1)")
558
+ ),
559
+ True,
560
+ )
561
+
562
+ def test_equals(self):
563
+ self.assertIs(
564
+ OGRGeometry("POINT(0 0)").contains(OGRGeometry("POINT(0 0)")), True
565
+ )
566
+ self.assertIs(
567
+ OGRGeometry("POINT(0 0)").contains(OGRGeometry("POINT(0 1)")), False
568
+ )
569
+
570
+ def test_intersects(self):
571
+ self.assertIs(
572
+ OGRGeometry("LINESTRING(0 0, 1 1)").intersects(
573
+ OGRGeometry("LINESTRING(0 1, 1 0)")
574
+ ),
575
+ True,
576
+ )
577
+ self.assertIs(
578
+ OGRGeometry("LINESTRING(0 0, 0 1)").intersects(
579
+ OGRGeometry("LINESTRING(1 0, 1 1)")
580
+ ),
581
+ False,
582
+ )
583
+
584
+ def test_overlaps(self):
585
+ self.assertIs(
586
+ OGRGeometry("POLYGON ((0 0, 0 2, 2 2, 2 0, 0 0))").overlaps(
587
+ OGRGeometry("POLYGON ((1 1, 1 5, 5 5, 5 1, 1 1))")
588
+ ),
589
+ True,
590
+ )
591
+ self.assertIs(
592
+ OGRGeometry("POINT(0 0)").overlaps(OGRGeometry("POINT(0 1)")), False
593
+ )
594
+
595
+ def test_touches(self):
596
+ self.assertIs(
597
+ OGRGeometry("POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))").touches(
598
+ OGRGeometry("LINESTRING(0 2, 2 0)")
599
+ ),
600
+ True,
601
+ )
602
+ self.assertIs(
603
+ OGRGeometry("POINT(0 0)").touches(OGRGeometry("POINT(0 1)")), False
604
+ )
605
+
606
+ def test_within(self):
607
+ self.assertIs(
608
+ OGRGeometry("POINT(0.5 0.5)").within(
609
+ OGRGeometry("POLYGON ((0 0, 0 1, 1 1, 1 0, 0 0))")
610
+ ),
611
+ True,
612
+ )
613
+ self.assertIs(
614
+ OGRGeometry("POINT(0 0)").within(OGRGeometry("POINT(0 1)")), False
615
+ )
616
+
617
+ def test_from_gml(self):
618
+ self.assertEqual(
619
+ OGRGeometry("POINT(0 0)"),
620
+ OGRGeometry.from_gml(
621
+ '<gml:Point gml:id="p21" '
622
+ 'srsName="http://www.opengis.net/def/crs/EPSG/0/4326">'
623
+ ' <gml:pos srsDimension="2">0 0</gml:pos>'
624
+ "</gml:Point>"
625
+ ),
626
+ )
627
+
628
+ def test_empty(self):
629
+ self.assertIs(OGRGeometry("POINT (0 0)").empty, False)
630
+ self.assertIs(OGRGeometry("POINT EMPTY").empty, True)
631
+
632
+ def test_empty_point_to_geos(self):
633
+ p = OGRGeometry("POINT EMPTY", srs=4326)
634
+ self.assertEqual(p.geos.ewkt, p.ewkt)
testbed/django__django/tests/gis_tests/gdal_tests/test_raster.py ADDED
@@ -0,0 +1,991 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import shutil
3
+ import struct
4
+ import tempfile
5
+ import zipfile
6
+ from pathlib import Path
7
+ from unittest import mock
8
+
9
+ from django.contrib.gis.gdal import GDAL_VERSION, GDALRaster, SpatialReference
10
+ from django.contrib.gis.gdal.error import GDALException
11
+ from django.contrib.gis.gdal.raster.band import GDALBand
12
+ from django.contrib.gis.shortcuts import numpy
13
+ from django.core.files.temp import NamedTemporaryFile
14
+ from django.test import SimpleTestCase
15
+
16
+ from ..data.rasters.textrasters import JSON_RASTER
17
+
18
+
19
+ class GDALRasterTests(SimpleTestCase):
20
+ """
21
+ Test a GDALRaster instance created from a file (GeoTiff).
22
+ """
23
+
24
+ def setUp(self):
25
+ self.rs_path = os.path.join(
26
+ os.path.dirname(__file__), "../data/rasters/raster.tif"
27
+ )
28
+ self.rs = GDALRaster(self.rs_path)
29
+
30
+ def test_gdalraster_input_as_path(self):
31
+ rs_path = Path(__file__).parent.parent / "data" / "rasters" / "raster.tif"
32
+ rs = GDALRaster(rs_path)
33
+ self.assertEqual(str(rs_path), rs.name)
34
+
35
+ def test_rs_name_repr(self):
36
+ self.assertEqual(self.rs_path, self.rs.name)
37
+ self.assertRegex(repr(self.rs), r"<Raster object at 0x\w+>")
38
+
39
+ def test_rs_driver(self):
40
+ self.assertEqual(self.rs.driver.name, "GTiff")
41
+
42
+ def test_rs_size(self):
43
+ self.assertEqual(self.rs.width, 163)
44
+ self.assertEqual(self.rs.height, 174)
45
+
46
+ def test_rs_srs(self):
47
+ self.assertEqual(self.rs.srs.srid, 3086)
48
+ self.assertEqual(self.rs.srs.units, (1.0, "metre"))
49
+
50
+ def test_rs_srid(self):
51
+ rast = GDALRaster(
52
+ {
53
+ "width": 16,
54
+ "height": 16,
55
+ "srid": 4326,
56
+ }
57
+ )
58
+ self.assertEqual(rast.srid, 4326)
59
+ rast.srid = 3086
60
+ self.assertEqual(rast.srid, 3086)
61
+
62
+ def test_geotransform_and_friends(self):
63
+ # Assert correct values for file based raster
64
+ self.assertEqual(
65
+ self.rs.geotransform,
66
+ [511700.4680706557, 100.0, 0.0, 435103.3771231986, 0.0, -100.0],
67
+ )
68
+ self.assertEqual(self.rs.origin, [511700.4680706557, 435103.3771231986])
69
+ self.assertEqual(self.rs.origin.x, 511700.4680706557)
70
+ self.assertEqual(self.rs.origin.y, 435103.3771231986)
71
+ self.assertEqual(self.rs.scale, [100.0, -100.0])
72
+ self.assertEqual(self.rs.scale.x, 100.0)
73
+ self.assertEqual(self.rs.scale.y, -100.0)
74
+ self.assertEqual(self.rs.skew, [0, 0])
75
+ self.assertEqual(self.rs.skew.x, 0)
76
+ self.assertEqual(self.rs.skew.y, 0)
77
+ # Create in-memory rasters and change gtvalues
78
+ rsmem = GDALRaster(JSON_RASTER)
79
+ # geotransform accepts both floats and ints
80
+ rsmem.geotransform = [0.0, 1.0, 2.0, 3.0, 4.0, 5.0]
81
+ self.assertEqual(rsmem.geotransform, [0.0, 1.0, 2.0, 3.0, 4.0, 5.0])
82
+ rsmem.geotransform = range(6)
83
+ self.assertEqual(rsmem.geotransform, [float(x) for x in range(6)])
84
+ self.assertEqual(rsmem.origin, [0, 3])
85
+ self.assertEqual(rsmem.origin.x, 0)
86
+ self.assertEqual(rsmem.origin.y, 3)
87
+ self.assertEqual(rsmem.scale, [1, 5])
88
+ self.assertEqual(rsmem.scale.x, 1)
89
+ self.assertEqual(rsmem.scale.y, 5)
90
+ self.assertEqual(rsmem.skew, [2, 4])
91
+ self.assertEqual(rsmem.skew.x, 2)
92
+ self.assertEqual(rsmem.skew.y, 4)
93
+ self.assertEqual(rsmem.width, 5)
94
+ self.assertEqual(rsmem.height, 5)
95
+
96
+ def test_geotransform_bad_inputs(self):
97
+ rsmem = GDALRaster(JSON_RASTER)
98
+ error_geotransforms = [
99
+ [1, 2],
100
+ [1, 2, 3, 4, 5, "foo"],
101
+ [1, 2, 3, 4, 5, 6, "foo"],
102
+ ]
103
+ msg = "Geotransform must consist of 6 numeric values."
104
+ for geotransform in error_geotransforms:
105
+ with self.subTest(i=geotransform), self.assertRaisesMessage(
106
+ ValueError, msg
107
+ ):
108
+ rsmem.geotransform = geotransform
109
+
110
+ def test_rs_extent(self):
111
+ self.assertEqual(
112
+ self.rs.extent,
113
+ (
114
+ 511700.4680706557,
115
+ 417703.3771231986,
116
+ 528000.4680706557,
117
+ 435103.3771231986,
118
+ ),
119
+ )
120
+
121
+ def test_rs_bands(self):
122
+ self.assertEqual(len(self.rs.bands), 1)
123
+ self.assertIsInstance(self.rs.bands[0], GDALBand)
124
+
125
+ def test_memory_based_raster_creation(self):
126
+ # Create uint8 raster with full pixel data range (0-255)
127
+ rast = GDALRaster(
128
+ {
129
+ "datatype": 1,
130
+ "width": 16,
131
+ "height": 16,
132
+ "srid": 4326,
133
+ "bands": [
134
+ {
135
+ "data": range(256),
136
+ "nodata_value": 255,
137
+ }
138
+ ],
139
+ }
140
+ )
141
+
142
+ # Get array from raster
143
+ result = rast.bands[0].data()
144
+ if numpy:
145
+ result = result.flatten().tolist()
146
+
147
+ # Assert data is same as original input
148
+ self.assertEqual(result, list(range(256)))
149
+
150
+ def test_file_based_raster_creation(self):
151
+ # Prepare tempfile
152
+ rstfile = NamedTemporaryFile(suffix=".tif")
153
+
154
+ # Create file-based raster from scratch
155
+ GDALRaster(
156
+ {
157
+ "datatype": self.rs.bands[0].datatype(),
158
+ "driver": "tif",
159
+ "name": rstfile.name,
160
+ "width": 163,
161
+ "height": 174,
162
+ "nr_of_bands": 1,
163
+ "srid": self.rs.srs.wkt,
164
+ "origin": (self.rs.origin.x, self.rs.origin.y),
165
+ "scale": (self.rs.scale.x, self.rs.scale.y),
166
+ "skew": (self.rs.skew.x, self.rs.skew.y),
167
+ "bands": [
168
+ {
169
+ "data": self.rs.bands[0].data(),
170
+ "nodata_value": self.rs.bands[0].nodata_value,
171
+ }
172
+ ],
173
+ }
174
+ )
175
+
176
+ # Reload newly created raster from file
177
+ restored_raster = GDALRaster(rstfile.name)
178
+ # Presence of TOWGS84 depend on GDAL/Proj versions.
179
+ self.assertEqual(
180
+ restored_raster.srs.wkt.replace("TOWGS84[0,0,0,0,0,0,0],", ""),
181
+ self.rs.srs.wkt.replace("TOWGS84[0,0,0,0,0,0,0],", ""),
182
+ )
183
+ self.assertEqual(restored_raster.geotransform, self.rs.geotransform)
184
+ if numpy:
185
+ numpy.testing.assert_equal(
186
+ restored_raster.bands[0].data(), self.rs.bands[0].data()
187
+ )
188
+ else:
189
+ self.assertEqual(restored_raster.bands[0].data(), self.rs.bands[0].data())
190
+
191
+ def test_nonexistent_file(self):
192
+ msg = 'Unable to read raster source input "nonexistent.tif".'
193
+ with self.assertRaisesMessage(GDALException, msg):
194
+ GDALRaster("nonexistent.tif")
195
+
196
+ def test_vsi_raster_creation(self):
197
+ # Open a raster as a file object.
198
+ with open(self.rs_path, "rb") as dat:
199
+ # Instantiate a raster from the file binary buffer.
200
+ vsimem = GDALRaster(dat.read())
201
+ # The data of the in-memory file is equal to the source file.
202
+ result = vsimem.bands[0].data()
203
+ target = self.rs.bands[0].data()
204
+ if numpy:
205
+ result = result.flatten().tolist()
206
+ target = target.flatten().tolist()
207
+ self.assertEqual(result, target)
208
+
209
+ def test_vsi_raster_deletion(self):
210
+ path = "/vsimem/raster.tif"
211
+ # Create a vsi-based raster from scratch.
212
+ vsimem = GDALRaster(
213
+ {
214
+ "name": path,
215
+ "driver": "tif",
216
+ "width": 4,
217
+ "height": 4,
218
+ "srid": 4326,
219
+ "bands": [
220
+ {
221
+ "data": range(16),
222
+ }
223
+ ],
224
+ }
225
+ )
226
+ # The virtual file exists.
227
+ rst = GDALRaster(path)
228
+ self.assertEqual(rst.width, 4)
229
+ # Delete GDALRaster.
230
+ del vsimem
231
+ del rst
232
+ # The virtual file has been removed.
233
+ msg = 'Could not open the datasource at "/vsimem/raster.tif"'
234
+ with self.assertRaisesMessage(GDALException, msg):
235
+ GDALRaster(path)
236
+
237
+ def test_vsi_invalid_buffer_error(self):
238
+ msg = "Failed creating VSI raster from the input buffer."
239
+ with self.assertRaisesMessage(GDALException, msg):
240
+ GDALRaster(b"not-a-raster-buffer")
241
+
242
+ def test_vsi_buffer_property(self):
243
+ # Create a vsi-based raster from scratch.
244
+ rast = GDALRaster(
245
+ {
246
+ "name": "/vsimem/raster.tif",
247
+ "driver": "tif",
248
+ "width": 4,
249
+ "height": 4,
250
+ "srid": 4326,
251
+ "bands": [
252
+ {
253
+ "data": range(16),
254
+ }
255
+ ],
256
+ }
257
+ )
258
+ # Do a round trip from raster to buffer to raster.
259
+ result = GDALRaster(rast.vsi_buffer).bands[0].data()
260
+ if numpy:
261
+ result = result.flatten().tolist()
262
+ # Band data is equal to nodata value except on input block of ones.
263
+ self.assertEqual(result, list(range(16)))
264
+ # The vsi buffer is None for rasters that are not vsi based.
265
+ self.assertIsNone(self.rs.vsi_buffer)
266
+
267
+ def test_vsi_vsizip_filesystem(self):
268
+ rst_zipfile = NamedTemporaryFile(suffix=".zip")
269
+ with zipfile.ZipFile(rst_zipfile, mode="w") as zf:
270
+ zf.write(self.rs_path, "raster.tif")
271
+ rst_path = "/vsizip/" + os.path.join(rst_zipfile.name, "raster.tif")
272
+ rst = GDALRaster(rst_path)
273
+ self.assertEqual(rst.driver.name, self.rs.driver.name)
274
+ self.assertEqual(rst.name, rst_path)
275
+ self.assertIs(rst.is_vsi_based, True)
276
+ self.assertIsNone(rst.vsi_buffer)
277
+
278
+ def test_offset_size_and_shape_on_raster_creation(self):
279
+ rast = GDALRaster(
280
+ {
281
+ "datatype": 1,
282
+ "width": 4,
283
+ "height": 4,
284
+ "srid": 4326,
285
+ "bands": [
286
+ {
287
+ "data": (1,),
288
+ "offset": (1, 1),
289
+ "size": (2, 2),
290
+ "shape": (1, 1),
291
+ "nodata_value": 2,
292
+ }
293
+ ],
294
+ }
295
+ )
296
+ # Get array from raster.
297
+ result = rast.bands[0].data()
298
+ if numpy:
299
+ result = result.flatten().tolist()
300
+ # Band data is equal to nodata value except on input block of ones.
301
+ self.assertEqual(result, [2, 2, 2, 2, 2, 1, 1, 2, 2, 1, 1, 2, 2, 2, 2, 2])
302
+
303
+ def test_set_nodata_value_on_raster_creation(self):
304
+ # Create raster filled with nodata values.
305
+ rast = GDALRaster(
306
+ {
307
+ "datatype": 1,
308
+ "width": 2,
309
+ "height": 2,
310
+ "srid": 4326,
311
+ "bands": [{"nodata_value": 23}],
312
+ }
313
+ )
314
+ # Get array from raster.
315
+ result = rast.bands[0].data()
316
+ if numpy:
317
+ result = result.flatten().tolist()
318
+ # All band data is equal to nodata value.
319
+ self.assertEqual(result, [23] * 4)
320
+
321
+ def test_set_nodata_none_on_raster_creation(self):
322
+ # Create raster without data and without nodata value.
323
+ rast = GDALRaster(
324
+ {
325
+ "datatype": 1,
326
+ "width": 2,
327
+ "height": 2,
328
+ "srid": 4326,
329
+ "bands": [{"nodata_value": None}],
330
+ }
331
+ )
332
+ # Get array from raster.
333
+ result = rast.bands[0].data()
334
+ if numpy:
335
+ result = result.flatten().tolist()
336
+ # Band data is equal to zero because no nodata value has been specified.
337
+ self.assertEqual(result, [0] * 4)
338
+
339
+ def test_raster_metadata_property(self):
340
+ data = self.rs.metadata
341
+ self.assertEqual(data["DEFAULT"], {"AREA_OR_POINT": "Area"})
342
+ self.assertEqual(data["IMAGE_STRUCTURE"], {"INTERLEAVE": "BAND"})
343
+
344
+ # Create file-based raster from scratch
345
+ source = GDALRaster(
346
+ {
347
+ "datatype": 1,
348
+ "width": 2,
349
+ "height": 2,
350
+ "srid": 4326,
351
+ "bands": [{"data": range(4), "nodata_value": 99}],
352
+ }
353
+ )
354
+ # Set metadata on raster and on a band.
355
+ metadata = {
356
+ "DEFAULT": {"OWNER": "Django", "VERSION": "1.0", "AREA_OR_POINT": "Point"},
357
+ }
358
+ source.metadata = metadata
359
+ source.bands[0].metadata = metadata
360
+ self.assertEqual(source.metadata["DEFAULT"], metadata["DEFAULT"])
361
+ self.assertEqual(source.bands[0].metadata["DEFAULT"], metadata["DEFAULT"])
362
+ # Update metadata on raster.
363
+ metadata = {
364
+ "DEFAULT": {"VERSION": "2.0"},
365
+ }
366
+ source.metadata = metadata
367
+ self.assertEqual(source.metadata["DEFAULT"]["VERSION"], "2.0")
368
+ # Remove metadata on raster.
369
+ metadata = {
370
+ "DEFAULT": {"OWNER": None},
371
+ }
372
+ source.metadata = metadata
373
+ self.assertNotIn("OWNER", source.metadata["DEFAULT"])
374
+
375
+ def test_raster_info_accessor(self):
376
+ infos = self.rs.info
377
+ # Data
378
+ info_lines = [line.strip() for line in infos.split("\n") if line.strip() != ""]
379
+ for line in [
380
+ "Driver: GTiff/GeoTIFF",
381
+ "Files: {}".format(self.rs_path),
382
+ "Size is 163, 174",
383
+ "Origin = (511700.468070655711927,435103.377123198588379)",
384
+ "Pixel Size = (100.000000000000000,-100.000000000000000)",
385
+ "Metadata:",
386
+ "AREA_OR_POINT=Area",
387
+ "Image Structure Metadata:",
388
+ "INTERLEAVE=BAND",
389
+ "Band 1 Block=163x50 Type=Byte, ColorInterp=Gray",
390
+ "NoData Value=15",
391
+ ]:
392
+ self.assertIn(line, info_lines)
393
+ for line in [
394
+ r"Upper Left \( 511700.468, 435103.377\) "
395
+ r'\( 82d51\'46.1\d"W, 27d55\' 1.5\d"N\)',
396
+ r"Lower Left \( 511700.468, 417703.377\) "
397
+ r'\( 82d51\'52.0\d"W, 27d45\'37.5\d"N\)',
398
+ r"Upper Right \( 528000.468, 435103.377\) "
399
+ r'\( 82d41\'48.8\d"W, 27d54\'56.3\d"N\)',
400
+ r"Lower Right \( 528000.468, 417703.377\) "
401
+ r'\( 82d41\'55.5\d"W, 27d45\'32.2\d"N\)',
402
+ r"Center \( 519850.468, 426403.377\) "
403
+ r'\( 82d46\'50.6\d"W, 27d50\'16.9\d"N\)',
404
+ ]:
405
+ self.assertRegex(infos, line)
406
+ # CRS (skip the name because string depends on the GDAL/Proj versions).
407
+ self.assertIn("NAD83 / Florida GDL Albers", infos)
408
+
409
+ def test_compressed_file_based_raster_creation(self):
410
+ rstfile = NamedTemporaryFile(suffix=".tif")
411
+ # Make a compressed copy of an existing raster.
412
+ compressed = self.rs.warp(
413
+ {"papsz_options": {"compress": "packbits"}, "name": rstfile.name}
414
+ )
415
+ # Check physically if compression worked.
416
+ self.assertLess(os.path.getsize(compressed.name), os.path.getsize(self.rs.name))
417
+ # Create file-based raster with options from scratch.
418
+ papsz_options = {
419
+ "compress": "packbits",
420
+ "blockxsize": 23,
421
+ "blockysize": 23,
422
+ }
423
+ if GDAL_VERSION < (3, 7):
424
+ datatype = 1
425
+ papsz_options["pixeltype"] = "signedbyte"
426
+ else:
427
+ datatype = 14
428
+ compressed = GDALRaster(
429
+ {
430
+ "datatype": datatype,
431
+ "driver": "tif",
432
+ "name": rstfile.name,
433
+ "width": 40,
434
+ "height": 40,
435
+ "srid": 3086,
436
+ "origin": (500000, 400000),
437
+ "scale": (100, -100),
438
+ "skew": (0, 0),
439
+ "bands": [
440
+ {
441
+ "data": range(40 ^ 2),
442
+ "nodata_value": 255,
443
+ }
444
+ ],
445
+ "papsz_options": papsz_options,
446
+ }
447
+ )
448
+ # Check if options used on creation are stored in metadata.
449
+ # Reopening the raster ensures that all metadata has been written
450
+ # to the file.
451
+ compressed = GDALRaster(compressed.name)
452
+ self.assertEqual(
453
+ compressed.metadata["IMAGE_STRUCTURE"]["COMPRESSION"],
454
+ "PACKBITS",
455
+ )
456
+ self.assertEqual(compressed.bands[0].datatype(), datatype)
457
+ if GDAL_VERSION < (3, 7):
458
+ self.assertEqual(
459
+ compressed.bands[0].metadata["IMAGE_STRUCTURE"]["PIXELTYPE"],
460
+ "SIGNEDBYTE",
461
+ )
462
+ self.assertIn("Block=40x23", compressed.info)
463
+
464
+ def test_raster_warp(self):
465
+ # Create in memory raster
466
+ source = GDALRaster(
467
+ {
468
+ "datatype": 1,
469
+ "driver": "MEM",
470
+ "name": "sourceraster",
471
+ "width": 4,
472
+ "height": 4,
473
+ "nr_of_bands": 1,
474
+ "srid": 3086,
475
+ "origin": (500000, 400000),
476
+ "scale": (100, -100),
477
+ "skew": (0, 0),
478
+ "bands": [
479
+ {
480
+ "data": range(16),
481
+ "nodata_value": 255,
482
+ }
483
+ ],
484
+ }
485
+ )
486
+
487
+ # Test altering the scale, width, and height of a raster
488
+ data = {
489
+ "scale": [200, -200],
490
+ "width": 2,
491
+ "height": 2,
492
+ }
493
+ target = source.warp(data)
494
+ self.assertEqual(target.width, data["width"])
495
+ self.assertEqual(target.height, data["height"])
496
+ self.assertEqual(target.scale, data["scale"])
497
+ self.assertEqual(target.bands[0].datatype(), source.bands[0].datatype())
498
+ self.assertEqual(target.name, "sourceraster_copy.MEM")
499
+ result = target.bands[0].data()
500
+ if numpy:
501
+ result = result.flatten().tolist()
502
+ self.assertEqual(result, [5, 7, 13, 15])
503
+
504
+ # Test altering the name and datatype (to float)
505
+ data = {
506
+ "name": "/path/to/targetraster.tif",
507
+ "datatype": 6,
508
+ }
509
+ target = source.warp(data)
510
+ self.assertEqual(target.bands[0].datatype(), 6)
511
+ self.assertEqual(target.name, "/path/to/targetraster.tif")
512
+ self.assertEqual(target.driver.name, "MEM")
513
+ result = target.bands[0].data()
514
+ if numpy:
515
+ result = result.flatten().tolist()
516
+ self.assertEqual(
517
+ result,
518
+ [
519
+ 0.0,
520
+ 1.0,
521
+ 2.0,
522
+ 3.0,
523
+ 4.0,
524
+ 5.0,
525
+ 6.0,
526
+ 7.0,
527
+ 8.0,
528
+ 9.0,
529
+ 10.0,
530
+ 11.0,
531
+ 12.0,
532
+ 13.0,
533
+ 14.0,
534
+ 15.0,
535
+ ],
536
+ )
537
+
538
+ def test_raster_warp_nodata_zone(self):
539
+ # Create in memory raster.
540
+ source = GDALRaster(
541
+ {
542
+ "datatype": 1,
543
+ "driver": "MEM",
544
+ "width": 4,
545
+ "height": 4,
546
+ "srid": 3086,
547
+ "origin": (500000, 400000),
548
+ "scale": (100, -100),
549
+ "skew": (0, 0),
550
+ "bands": [
551
+ {
552
+ "data": range(16),
553
+ "nodata_value": 23,
554
+ }
555
+ ],
556
+ }
557
+ )
558
+ # Warp raster onto a location that does not cover any pixels of the original.
559
+ result = source.warp({"origin": (200000, 200000)}).bands[0].data()
560
+ if numpy:
561
+ result = result.flatten().tolist()
562
+ # The result is an empty raster filled with the correct nodata value.
563
+ self.assertEqual(result, [23] * 16)
564
+
565
+ def test_raster_clone(self):
566
+ rstfile = NamedTemporaryFile(suffix=".tif")
567
+ tests = [
568
+ ("MEM", "", 23), # In memory raster.
569
+ ("tif", rstfile.name, 99), # In file based raster.
570
+ ]
571
+ for driver, name, nodata_value in tests:
572
+ with self.subTest(driver=driver):
573
+ source = GDALRaster(
574
+ {
575
+ "datatype": 1,
576
+ "driver": driver,
577
+ "name": name,
578
+ "width": 4,
579
+ "height": 4,
580
+ "srid": 3086,
581
+ "origin": (500000, 400000),
582
+ "scale": (100, -100),
583
+ "skew": (0, 0),
584
+ "bands": [
585
+ {
586
+ "data": range(16),
587
+ "nodata_value": nodata_value,
588
+ }
589
+ ],
590
+ }
591
+ )
592
+ clone = source.clone()
593
+ self.assertNotEqual(clone.name, source.name)
594
+ self.assertEqual(clone._write, source._write)
595
+ self.assertEqual(clone.srs.srid, source.srs.srid)
596
+ self.assertEqual(clone.width, source.width)
597
+ self.assertEqual(clone.height, source.height)
598
+ self.assertEqual(clone.origin, source.origin)
599
+ self.assertEqual(clone.scale, source.scale)
600
+ self.assertEqual(clone.skew, source.skew)
601
+ self.assertIsNot(clone, source)
602
+
603
+ def test_raster_transform(self):
604
+ tests = [
605
+ 3086,
606
+ "3086",
607
+ SpatialReference(3086),
608
+ ]
609
+ for srs in tests:
610
+ with self.subTest(srs=srs):
611
+ # Prepare tempfile and nodata value.
612
+ rstfile = NamedTemporaryFile(suffix=".tif")
613
+ ndv = 99
614
+ # Create in file based raster.
615
+ source = GDALRaster(
616
+ {
617
+ "datatype": 1,
618
+ "driver": "tif",
619
+ "name": rstfile.name,
620
+ "width": 5,
621
+ "height": 5,
622
+ "nr_of_bands": 1,
623
+ "srid": 4326,
624
+ "origin": (-5, 5),
625
+ "scale": (2, -2),
626
+ "skew": (0, 0),
627
+ "bands": [
628
+ {
629
+ "data": range(25),
630
+ "nodata_value": ndv,
631
+ }
632
+ ],
633
+ }
634
+ )
635
+
636
+ target = source.transform(srs)
637
+
638
+ # Reload data from disk.
639
+ target = GDALRaster(target.name)
640
+ self.assertEqual(target.srs.srid, 3086)
641
+ self.assertEqual(target.width, 7)
642
+ self.assertEqual(target.height, 7)
643
+ self.assertEqual(target.bands[0].datatype(), source.bands[0].datatype())
644
+ self.assertAlmostEqual(target.origin[0], 9124842.791079799, 3)
645
+ self.assertAlmostEqual(target.origin[1], 1589911.6476407414, 3)
646
+ self.assertAlmostEqual(target.scale[0], 223824.82664250192, 3)
647
+ self.assertAlmostEqual(target.scale[1], -223824.82664250192, 3)
648
+ self.assertEqual(target.skew, [0, 0])
649
+
650
+ result = target.bands[0].data()
651
+ if numpy:
652
+ result = result.flatten().tolist()
653
+ # The reprojection of a raster that spans over a large area
654
+ # skews the data matrix and might introduce nodata values.
655
+ self.assertEqual(
656
+ result,
657
+ [
658
+ ndv,
659
+ ndv,
660
+ ndv,
661
+ ndv,
662
+ 4,
663
+ ndv,
664
+ ndv,
665
+ ndv,
666
+ ndv,
667
+ 2,
668
+ 3,
669
+ 9,
670
+ ndv,
671
+ ndv,
672
+ ndv,
673
+ 1,
674
+ 2,
675
+ 8,
676
+ 13,
677
+ 19,
678
+ ndv,
679
+ 0,
680
+ 6,
681
+ 6,
682
+ 12,
683
+ 18,
684
+ 18,
685
+ 24,
686
+ ndv,
687
+ 10,
688
+ 11,
689
+ 16,
690
+ 22,
691
+ 23,
692
+ ndv,
693
+ ndv,
694
+ ndv,
695
+ 15,
696
+ 21,
697
+ 22,
698
+ ndv,
699
+ ndv,
700
+ ndv,
701
+ ndv,
702
+ 20,
703
+ ndv,
704
+ ndv,
705
+ ndv,
706
+ ndv,
707
+ ],
708
+ )
709
+
710
+ def test_raster_transform_clone(self):
711
+ with mock.patch.object(GDALRaster, "clone") as mocked_clone:
712
+ # Create in file based raster.
713
+ rstfile = NamedTemporaryFile(suffix=".tif")
714
+ source = GDALRaster(
715
+ {
716
+ "datatype": 1,
717
+ "driver": "tif",
718
+ "name": rstfile.name,
719
+ "width": 5,
720
+ "height": 5,
721
+ "nr_of_bands": 1,
722
+ "srid": 4326,
723
+ "origin": (-5, 5),
724
+ "scale": (2, -2),
725
+ "skew": (0, 0),
726
+ "bands": [
727
+ {
728
+ "data": range(25),
729
+ "nodata_value": 99,
730
+ }
731
+ ],
732
+ }
733
+ )
734
+ # transform() returns a clone because it is the same SRID and
735
+ # driver.
736
+ source.transform(4326)
737
+ self.assertEqual(mocked_clone.call_count, 1)
738
+
739
+ def test_raster_transform_clone_name(self):
740
+ # Create in file based raster.
741
+ rstfile = NamedTemporaryFile(suffix=".tif")
742
+ source = GDALRaster(
743
+ {
744
+ "datatype": 1,
745
+ "driver": "tif",
746
+ "name": rstfile.name,
747
+ "width": 5,
748
+ "height": 5,
749
+ "nr_of_bands": 1,
750
+ "srid": 4326,
751
+ "origin": (-5, 5),
752
+ "scale": (2, -2),
753
+ "skew": (0, 0),
754
+ "bands": [
755
+ {
756
+ "data": range(25),
757
+ "nodata_value": 99,
758
+ }
759
+ ],
760
+ }
761
+ )
762
+ clone_name = rstfile.name + "_respect_name.GTiff"
763
+ target = source.transform(4326, name=clone_name)
764
+ self.assertEqual(target.name, clone_name)
765
+
766
+
767
+ class GDALBandTests(SimpleTestCase):
768
+ rs_path = os.path.join(os.path.dirname(__file__), "../data/rasters/raster.tif")
769
+
770
+ def test_band_data(self):
771
+ rs = GDALRaster(self.rs_path)
772
+ band = rs.bands[0]
773
+ self.assertEqual(band.width, 163)
774
+ self.assertEqual(band.height, 174)
775
+ self.assertEqual(band.description, "")
776
+ self.assertEqual(band.datatype(), 1)
777
+ self.assertEqual(band.datatype(as_string=True), "GDT_Byte")
778
+ self.assertEqual(band.color_interp(), 1)
779
+ self.assertEqual(band.color_interp(as_string=True), "GCI_GrayIndex")
780
+ self.assertEqual(band.nodata_value, 15)
781
+ if numpy:
782
+ data = band.data()
783
+ assert_array = numpy.loadtxt(
784
+ os.path.join(
785
+ os.path.dirname(__file__), "../data/rasters/raster.numpy.txt"
786
+ )
787
+ )
788
+ numpy.testing.assert_equal(data, assert_array)
789
+ self.assertEqual(data.shape, (band.height, band.width))
790
+
791
+ def test_band_statistics(self):
792
+ with tempfile.TemporaryDirectory() as tmp_dir:
793
+ rs_path = os.path.join(tmp_dir, "raster.tif")
794
+ shutil.copyfile(self.rs_path, rs_path)
795
+ rs = GDALRaster(rs_path)
796
+ band = rs.bands[0]
797
+ pam_file = rs_path + ".aux.xml"
798
+ smin, smax, smean, sstd = band.statistics(approximate=True)
799
+ self.assertEqual(smin, 0)
800
+ self.assertEqual(smax, 9)
801
+ self.assertAlmostEqual(smean, 2.842331288343558)
802
+ self.assertAlmostEqual(sstd, 2.3965567248965356)
803
+
804
+ smin, smax, smean, sstd = band.statistics(approximate=False, refresh=True)
805
+ self.assertEqual(smin, 0)
806
+ self.assertEqual(smax, 9)
807
+ self.assertAlmostEqual(smean, 2.828326634228898)
808
+ self.assertAlmostEqual(sstd, 2.4260526986669095)
809
+
810
+ self.assertEqual(band.min, 0)
811
+ self.assertEqual(band.max, 9)
812
+ self.assertAlmostEqual(band.mean, 2.828326634228898)
813
+ self.assertAlmostEqual(band.std, 2.4260526986669095)
814
+
815
+ # Statistics are persisted into PAM file on band close
816
+ rs = band = None
817
+ self.assertTrue(os.path.isfile(pam_file))
818
+
819
+ def _remove_aux_file(self):
820
+ pam_file = self.rs_path + ".aux.xml"
821
+ if os.path.isfile(pam_file):
822
+ os.remove(pam_file)
823
+
824
+ def test_read_mode_error(self):
825
+ # Open raster in read mode
826
+ rs = GDALRaster(self.rs_path, write=False)
827
+ band = rs.bands[0]
828
+ self.addCleanup(self._remove_aux_file)
829
+
830
+ # Setting attributes in write mode raises exception in the _flush method
831
+ with self.assertRaises(GDALException):
832
+ setattr(band, "nodata_value", 10)
833
+
834
+ def test_band_data_setters(self):
835
+ # Create in-memory raster and get band
836
+ rsmem = GDALRaster(
837
+ {
838
+ "datatype": 1,
839
+ "driver": "MEM",
840
+ "name": "mem_rst",
841
+ "width": 10,
842
+ "height": 10,
843
+ "nr_of_bands": 1,
844
+ "srid": 4326,
845
+ }
846
+ )
847
+ bandmem = rsmem.bands[0]
848
+
849
+ # Set nodata value
850
+ bandmem.nodata_value = 99
851
+ self.assertEqual(bandmem.nodata_value, 99)
852
+
853
+ # Set data for entire dataset
854
+ bandmem.data(range(100))
855
+ if numpy:
856
+ numpy.testing.assert_equal(
857
+ bandmem.data(), numpy.arange(100).reshape(10, 10)
858
+ )
859
+ else:
860
+ self.assertEqual(bandmem.data(), list(range(100)))
861
+
862
+ # Prepare data for setting values in subsequent tests
863
+ block = list(range(100, 104))
864
+ packed_block = struct.pack("<" + "B B B B", *block)
865
+
866
+ # Set data from list
867
+ bandmem.data(block, (1, 1), (2, 2))
868
+ result = bandmem.data(offset=(1, 1), size=(2, 2))
869
+ if numpy:
870
+ numpy.testing.assert_equal(result, numpy.array(block).reshape(2, 2))
871
+ else:
872
+ self.assertEqual(result, block)
873
+
874
+ # Set data from packed block
875
+ bandmem.data(packed_block, (1, 1), (2, 2))
876
+ result = bandmem.data(offset=(1, 1), size=(2, 2))
877
+ if numpy:
878
+ numpy.testing.assert_equal(result, numpy.array(block).reshape(2, 2))
879
+ else:
880
+ self.assertEqual(result, block)
881
+
882
+ # Set data from bytes
883
+ bandmem.data(bytes(packed_block), (1, 1), (2, 2))
884
+ result = bandmem.data(offset=(1, 1), size=(2, 2))
885
+ if numpy:
886
+ numpy.testing.assert_equal(result, numpy.array(block).reshape(2, 2))
887
+ else:
888
+ self.assertEqual(result, block)
889
+
890
+ # Set data from bytearray
891
+ bandmem.data(bytearray(packed_block), (1, 1), (2, 2))
892
+ result = bandmem.data(offset=(1, 1), size=(2, 2))
893
+ if numpy:
894
+ numpy.testing.assert_equal(result, numpy.array(block).reshape(2, 2))
895
+ else:
896
+ self.assertEqual(result, block)
897
+
898
+ # Set data from memoryview
899
+ bandmem.data(memoryview(packed_block), (1, 1), (2, 2))
900
+ result = bandmem.data(offset=(1, 1), size=(2, 2))
901
+ if numpy:
902
+ numpy.testing.assert_equal(result, numpy.array(block).reshape(2, 2))
903
+ else:
904
+ self.assertEqual(result, block)
905
+
906
+ # Set data from numpy array
907
+ if numpy:
908
+ bandmem.data(numpy.array(block, dtype="int8").reshape(2, 2), (1, 1), (2, 2))
909
+ numpy.testing.assert_equal(
910
+ bandmem.data(offset=(1, 1), size=(2, 2)),
911
+ numpy.array(block).reshape(2, 2),
912
+ )
913
+
914
+ # Test json input data
915
+ rsmemjson = GDALRaster(JSON_RASTER)
916
+ bandmemjson = rsmemjson.bands[0]
917
+ if numpy:
918
+ numpy.testing.assert_equal(
919
+ bandmemjson.data(), numpy.array(range(25)).reshape(5, 5)
920
+ )
921
+ else:
922
+ self.assertEqual(bandmemjson.data(), list(range(25)))
923
+
924
+ def test_band_statistics_automatic_refresh(self):
925
+ rsmem = GDALRaster(
926
+ {
927
+ "srid": 4326,
928
+ "width": 2,
929
+ "height": 2,
930
+ "bands": [{"data": [0] * 4, "nodata_value": 99}],
931
+ }
932
+ )
933
+ band = rsmem.bands[0]
934
+ # Populate statistics cache
935
+ self.assertEqual(band.statistics(), (0, 0, 0, 0))
936
+ # Change data
937
+ band.data([1, 1, 0, 0])
938
+ # Statistics are properly updated
939
+ self.assertEqual(band.statistics(), (0.0, 1.0, 0.5, 0.5))
940
+ # Change nodata_value
941
+ band.nodata_value = 0
942
+ # Statistics are properly updated
943
+ self.assertEqual(band.statistics(), (1.0, 1.0, 1.0, 0.0))
944
+
945
+ def test_band_statistics_empty_band(self):
946
+ rsmem = GDALRaster(
947
+ {
948
+ "srid": 4326,
949
+ "width": 1,
950
+ "height": 1,
951
+ "bands": [{"data": [0], "nodata_value": 0}],
952
+ }
953
+ )
954
+ self.assertEqual(rsmem.bands[0].statistics(), (None, None, None, None))
955
+
956
+ def test_band_delete_nodata(self):
957
+ rsmem = GDALRaster(
958
+ {
959
+ "srid": 4326,
960
+ "width": 1,
961
+ "height": 1,
962
+ "bands": [{"data": [0], "nodata_value": 1}],
963
+ }
964
+ )
965
+ rsmem.bands[0].nodata_value = None
966
+ self.assertIsNone(rsmem.bands[0].nodata_value)
967
+
968
+ def test_band_data_replication(self):
969
+ band = GDALRaster(
970
+ {
971
+ "srid": 4326,
972
+ "width": 3,
973
+ "height": 3,
974
+ "bands": [{"data": range(10, 19), "nodata_value": 0}],
975
+ }
976
+ ).bands[0]
977
+
978
+ # Variations for input (data, shape, expected result).
979
+ combos = (
980
+ ([1], (1, 1), [1] * 9),
981
+ (range(3), (1, 3), [0, 0, 0, 1, 1, 1, 2, 2, 2]),
982
+ (range(3), (3, 1), [0, 1, 2, 0, 1, 2, 0, 1, 2]),
983
+ )
984
+ for combo in combos:
985
+ band.data(combo[0], shape=combo[1])
986
+ if numpy:
987
+ numpy.testing.assert_equal(
988
+ band.data(), numpy.array(combo[2]).reshape(3, 3)
989
+ )
990
+ else:
991
+ self.assertEqual(band.data(), list(combo[2]))
testbed/django__django/tests/gis_tests/gdal_tests/test_srs.py ADDED
@@ -0,0 +1,403 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from unittest import skipIf
2
+
3
+ from django.contrib.gis.gdal import (
4
+ GDAL_VERSION,
5
+ AxisOrder,
6
+ CoordTransform,
7
+ GDALException,
8
+ SpatialReference,
9
+ SRSException,
10
+ )
11
+ from django.contrib.gis.geos import GEOSGeometry
12
+ from django.test import SimpleTestCase
13
+
14
+
15
+ class TestSRS:
16
+ def __init__(self, wkt, **kwargs):
17
+ self.wkt = wkt
18
+ for key, value in kwargs.items():
19
+ setattr(self, key, value)
20
+
21
+
22
+ # Some Spatial Reference examples
23
+ srlist = (
24
+ TestSRS(
25
+ 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,'
26
+ 'AUTHORITY["EPSG","7030"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6326"]],'
27
+ 'PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",'
28
+ '0.0174532925199433,AUTHORITY["EPSG","9122"]],AXIS["Latitude",NORTH],'
29
+ 'AXIS["Longitude",EAST],AUTHORITY["EPSG","4326"]]',
30
+ epsg=4326,
31
+ projected=False,
32
+ geographic=True,
33
+ local=False,
34
+ lin_name="unknown",
35
+ ang_name="degree",
36
+ lin_units=1.0,
37
+ ang_units=0.0174532925199,
38
+ auth={
39
+ None: ("EPSG", "4326"), # Top-level authority.
40
+ "GEOGCS": ("EPSG", "4326"),
41
+ "spheroid": ("EPSG", "7030"),
42
+ },
43
+ attr=(
44
+ ("DATUM", "WGS_1984"),
45
+ (("SPHEROID", 1), "6378137"),
46
+ ("primem|authority", "EPSG"),
47
+ ),
48
+ ),
49
+ TestSRS(
50
+ 'PROJCS["NAD83 / Texas South Central",'
51
+ 'GEOGCS["NAD83",DATUM["North_American_Datum_1983",'
52
+ 'SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],'
53
+ 'AUTHORITY["EPSG","6269"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],'
54
+ 'UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],'
55
+ 'AUTHORITY["EPSG","4269"]],PROJECTION["Lambert_Conformal_Conic_2SP"],'
56
+ 'PARAMETER["standard_parallel_1",30.2833333333333],'
57
+ 'PARAMETER["standard_parallel_2",28.3833333333333],'
58
+ 'PARAMETER["latitude_of_origin",27.8333333333333],'
59
+ 'PARAMETER["central_meridian",-99],PARAMETER["false_easting",600000],'
60
+ 'PARAMETER["false_northing",4000000],UNIT["metre",1,AUTHORITY["EPSG","9001"]],'
61
+ 'AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","32140"]]',
62
+ epsg=32140,
63
+ projected=True,
64
+ geographic=False,
65
+ local=False,
66
+ lin_name="metre",
67
+ ang_name="degree",
68
+ lin_units=1.0,
69
+ ang_units=0.0174532925199,
70
+ auth={
71
+ None: ("EPSG", "32140"), # Top-level authority.
72
+ "PROJCS": ("EPSG", "32140"),
73
+ "spheroid": ("EPSG", "7019"),
74
+ "unit": ("EPSG", "9001"),
75
+ },
76
+ attr=(
77
+ ("DATUM", "North_American_Datum_1983"),
78
+ (("SPHEROID", 2), "298.257222101"),
79
+ ("PROJECTION", "Lambert_Conformal_Conic_2SP"),
80
+ ),
81
+ ),
82
+ TestSRS(
83
+ 'PROJCS["NAD83 / Texas South Central (ftUS)",'
84
+ 'GEOGCS["NAD83",DATUM["North_American_Datum_1983",'
85
+ 'SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],'
86
+ 'AUTHORITY["EPSG","6269"]],'
87
+ 'PRIMEM["Greenwich",0],'
88
+ 'UNIT["Degree",0.0174532925199433]],PROJECTION["Lambert_Conformal_Conic_2SP"],'
89
+ 'PARAMETER["false_easting",1968500],'
90
+ 'PARAMETER["false_northing",13123333.3333333],'
91
+ 'PARAMETER["central_meridian",-99],'
92
+ 'PARAMETER["standard_parallel_1",28.3833333333333],'
93
+ 'PARAMETER["standard_parallel_2",30.2833333333333],'
94
+ 'PARAMETER["latitude_of_origin",27.8333333333333],'
95
+ 'UNIT["US survey foot",0.304800609601219],AXIS["Easting",EAST],'
96
+ 'AXIS["Northing",NORTH]]',
97
+ epsg=None,
98
+ projected=True,
99
+ geographic=False,
100
+ local=False,
101
+ lin_name="US survey foot",
102
+ ang_name="Degree",
103
+ lin_units=0.3048006096012192,
104
+ ang_units=0.0174532925199,
105
+ auth={
106
+ None: (None, None), # Top-level authority.
107
+ "PROJCS": (None, None),
108
+ },
109
+ attr=(
110
+ ("PROJCS|GeOgCs|spheroid", "GRS 1980"),
111
+ (("projcs", 9), "UNIT"),
112
+ (("projcs", 11), "AXIS"),
113
+ ),
114
+ ),
115
+ # This is really ESRI format, not WKT -- but the import should work the same
116
+ TestSRS(
117
+ 'LOCAL_CS["Non-Earth (Meter)",LOCAL_DATUM["Local Datum",32767],'
118
+ 'UNIT["Meter",1],AXIS["X",EAST],AXIS["Y",NORTH]]',
119
+ esri=True,
120
+ epsg=None,
121
+ projected=False,
122
+ geographic=False,
123
+ local=True,
124
+ lin_name="Meter",
125
+ ang_name="degree",
126
+ lin_units=1.0,
127
+ ang_units=0.0174532925199,
128
+ attr=(("LOCAL_DATUM", "Local Datum"),),
129
+ ),
130
+ )
131
+
132
+ # Well-Known Names
133
+ well_known = (
134
+ TestSRS(
135
+ 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,'
136
+ 'AUTHORITY["EPSG","7030"]],TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6326"]],'
137
+ 'PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],'
138
+ 'UNIT["degree",0.01745329251994328,'
139
+ 'AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]',
140
+ wk="WGS84",
141
+ name="WGS 84",
142
+ attrs=(("GEOGCS|AUTHORITY", 1, "4326"), ("SPHEROID", "WGS 84")),
143
+ ),
144
+ TestSRS(
145
+ 'GEOGCS["WGS 72",DATUM["WGS_1972",SPHEROID["WGS 72",6378135,298.26,'
146
+ 'AUTHORITY["EPSG","7043"]],AUTHORITY["EPSG","6322"]],'
147
+ 'PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],'
148
+ 'UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],'
149
+ 'AUTHORITY["EPSG","4322"]]',
150
+ wk="WGS72",
151
+ name="WGS 72",
152
+ attrs=(("GEOGCS|AUTHORITY", 1, "4322"), ("SPHEROID", "WGS 72")),
153
+ ),
154
+ TestSRS(
155
+ 'GEOGCS["NAD27",DATUM["North_American_Datum_1927",'
156
+ 'SPHEROID["Clarke 1866",6378206.4,294.9786982138982,'
157
+ 'AUTHORITY["EPSG","7008"]],AUTHORITY["EPSG","6267"]],'
158
+ 'PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],'
159
+ 'UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],'
160
+ 'AUTHORITY["EPSG","4267"]]',
161
+ wk="NAD27",
162
+ name="NAD27",
163
+ attrs=(("GEOGCS|AUTHORITY", 1, "4267"), ("SPHEROID", "Clarke 1866")),
164
+ ),
165
+ TestSRS(
166
+ 'GEOGCS["NAD83",DATUM["North_American_Datum_1983",'
167
+ 'SPHEROID["GRS 1980",6378137,298.257222101,'
168
+ 'AUTHORITY["EPSG","7019"]],AUTHORITY["EPSG","6269"]],'
169
+ 'PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],'
170
+ 'UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],'
171
+ 'AUTHORITY["EPSG","4269"]]',
172
+ wk="NAD83",
173
+ name="NAD83",
174
+ attrs=(("GEOGCS|AUTHORITY", 1, "4269"), ("SPHEROID", "GRS 1980")),
175
+ ),
176
+ TestSRS(
177
+ 'PROJCS["NZGD49 / Karamea Circuit",GEOGCS["NZGD49",'
178
+ 'DATUM["New_Zealand_Geodetic_Datum_1949",'
179
+ 'SPHEROID["International 1924",6378388,297,'
180
+ 'AUTHORITY["EPSG","7022"]],'
181
+ "TOWGS84[59.47,-5.04,187.44,0.47,-0.1,1.024,-4.5993],"
182
+ 'AUTHORITY["EPSG","6272"]],PRIMEM["Greenwich",0,'
183
+ 'AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,'
184
+ 'AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4272"]],'
185
+ 'PROJECTION["Transverse_Mercator"],'
186
+ 'PARAMETER["latitude_of_origin",-41.28991152777778],'
187
+ 'PARAMETER["central_meridian",172.1090281944444],'
188
+ 'PARAMETER["scale_factor",1],PARAMETER["false_easting",300000],'
189
+ 'PARAMETER["false_northing",700000],'
190
+ 'UNIT["metre",1,AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","27216"]]',
191
+ wk="EPSG:27216",
192
+ name="NZGD49 / Karamea Circuit",
193
+ attrs=(
194
+ ("PROJECTION", "Transverse_Mercator"),
195
+ ("SPHEROID", "International 1924"),
196
+ ),
197
+ ),
198
+ )
199
+
200
+ bad_srlist = (
201
+ "Foobar",
202
+ 'OOJCS["NAD83 / Texas South Central",GEOGCS["NAD83",'
203
+ 'DATUM["North_American_Datum_1983",'
204
+ 'SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],'
205
+ 'AUTHORITY["EPSG","6269"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],'
206
+ 'UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],'
207
+ 'AUTHORITY["EPSG","4269"]],PROJECTION["Lambert_Conformal_Conic_2SP"],'
208
+ 'PARAMETER["standard_parallel_1",30.28333333333333],'
209
+ 'PARAMETER["standard_parallel_2",28.38333333333333],'
210
+ 'PARAMETER["latitude_of_origin",27.83333333333333],'
211
+ 'PARAMETER["central_meridian",-99],PARAMETER["false_easting",600000],'
212
+ 'PARAMETER["false_northing",4000000],UNIT["metre",1,'
213
+ 'AUTHORITY["EPSG","9001"]],AUTHORITY["EPSG","32140"]]',
214
+ )
215
+
216
+
217
+ class SpatialRefTest(SimpleTestCase):
218
+ def test01_wkt(self):
219
+ "Testing initialization on valid OGC WKT."
220
+ for s in srlist:
221
+ SpatialReference(s.wkt)
222
+
223
+ def test02_bad_wkt(self):
224
+ "Testing initialization on invalid WKT."
225
+ for bad in bad_srlist:
226
+ try:
227
+ srs = SpatialReference(bad)
228
+ srs.validate()
229
+ except (SRSException, GDALException):
230
+ pass
231
+ else:
232
+ self.fail('Should not have initialized on bad WKT "%s"!')
233
+
234
+ def test03_get_wkt(self):
235
+ "Testing getting the WKT."
236
+ for s in srlist:
237
+ srs = SpatialReference(s.wkt)
238
+ # GDAL 3 strips UNIT part in the last occurrence.
239
+ self.assertEqual(
240
+ s.wkt.replace(',UNIT["Meter",1]', ""),
241
+ srs.wkt.replace(',UNIT["Meter",1]', ""),
242
+ )
243
+
244
+ def test04_proj(self):
245
+ """PROJ import and export."""
246
+ proj_parts = [
247
+ "+proj=longlat",
248
+ "+ellps=WGS84",
249
+ "+towgs84=0,0,0,0,0,0,0",
250
+ "+datum=WGS84",
251
+ "+no_defs",
252
+ ]
253
+ srs1 = SpatialReference(srlist[0].wkt)
254
+ srs2 = SpatialReference("+proj=longlat +datum=WGS84 +no_defs")
255
+ self.assertTrue(all(part in proj_parts for part in srs1.proj.split()))
256
+ self.assertTrue(all(part in proj_parts for part in srs2.proj.split()))
257
+
258
+ def test05_epsg(self):
259
+ "Test EPSG import."
260
+ for s in srlist:
261
+ if s.epsg:
262
+ srs1 = SpatialReference(s.wkt)
263
+ srs2 = SpatialReference(s.epsg)
264
+ srs3 = SpatialReference(str(s.epsg))
265
+ srs4 = SpatialReference("EPSG:%d" % s.epsg)
266
+ for srs in (srs1, srs2, srs3, srs4):
267
+ for attr, expected in s.attr:
268
+ self.assertEqual(expected, srs[attr])
269
+
270
+ def test07_boolean_props(self):
271
+ "Testing the boolean properties."
272
+ for s in srlist:
273
+ srs = SpatialReference(s.wkt)
274
+ self.assertEqual(s.projected, srs.projected)
275
+ self.assertEqual(s.geographic, srs.geographic)
276
+
277
+ def test08_angular_linear(self):
278
+ "Testing the linear and angular units routines."
279
+ for s in srlist:
280
+ srs = SpatialReference(s.wkt)
281
+ self.assertEqual(s.ang_name, srs.angular_name)
282
+ self.assertEqual(s.lin_name, srs.linear_name)
283
+ self.assertAlmostEqual(s.ang_units, srs.angular_units, 9)
284
+ self.assertAlmostEqual(s.lin_units, srs.linear_units, 9)
285
+
286
+ def test09_authority(self):
287
+ "Testing the authority name & code routines."
288
+ for s in srlist:
289
+ if hasattr(s, "auth"):
290
+ srs = SpatialReference(s.wkt)
291
+ for target, tup in s.auth.items():
292
+ self.assertEqual(tup[0], srs.auth_name(target))
293
+ self.assertEqual(tup[1], srs.auth_code(target))
294
+
295
+ def test10_attributes(self):
296
+ "Testing the attribute retrieval routines."
297
+ for s in srlist:
298
+ srs = SpatialReference(s.wkt)
299
+ for tup in s.attr:
300
+ att = tup[0] # Attribute to test
301
+ exp = tup[1] # Expected result
302
+ self.assertEqual(exp, srs[att])
303
+
304
+ def test11_wellknown(self):
305
+ "Testing Well Known Names of Spatial References."
306
+ for s in well_known:
307
+ srs = SpatialReference(s.wk)
308
+ self.assertEqual(s.name, srs.name)
309
+ for tup in s.attrs:
310
+ if len(tup) == 2:
311
+ key = tup[0]
312
+ exp = tup[1]
313
+ elif len(tup) == 3:
314
+ key = tup[:2]
315
+ exp = tup[2]
316
+ self.assertEqual(srs[key], exp)
317
+
318
+ def test12_coordtransform(self):
319
+ "Testing initialization of a CoordTransform."
320
+ target = SpatialReference("WGS84")
321
+ CoordTransform(SpatialReference(srlist[0].wkt), target)
322
+
323
+ def test13_attr_value(self):
324
+ "Testing the attr_value() method."
325
+ s1 = SpatialReference("WGS84")
326
+ with self.assertRaises(TypeError):
327
+ s1.__getitem__(0)
328
+ with self.assertRaises(TypeError):
329
+ s1.__getitem__(("GEOGCS", "foo"))
330
+ self.assertEqual("WGS 84", s1["GEOGCS"])
331
+ self.assertEqual("WGS_1984", s1["DATUM"])
332
+ self.assertEqual("EPSG", s1["AUTHORITY"])
333
+ self.assertEqual(4326, int(s1["AUTHORITY", 1]))
334
+ self.assertIsNone(s1["FOOBAR"])
335
+
336
+ def test_unicode(self):
337
+ wkt = (
338
+ 'PROJCS["DHDN / Soldner 39 Langschoß",'
339
+ 'GEOGCS["DHDN",DATUM["Deutsches_Hauptdreiecksnetz",'
340
+ 'SPHEROID["Bessel 1841",6377397.155,299.1528128,AUTHORITY["EPSG","7004"]],'
341
+ 'AUTHORITY["EPSG","6314"]],'
342
+ 'PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],'
343
+ 'UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],'
344
+ 'AUTHORITY["EPSG","4314"]],PROJECTION["Cassini_Soldner"],'
345
+ 'PARAMETER["latitude_of_origin",50.66738711],'
346
+ 'PARAMETER["central_meridian",6.28935703],'
347
+ 'PARAMETER["false_easting",0],PARAMETER["false_northing",0],'
348
+ 'UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["X",NORTH],AXIS["Y",EAST],'
349
+ 'AUTHORITY["mj10777.de","187939"]]'
350
+ )
351
+ srs = SpatialReference(wkt)
352
+ srs_list = [srs, srs.clone()]
353
+ srs.import_wkt(wkt)
354
+
355
+ for srs in srs_list:
356
+ self.assertEqual(srs.name, "DHDN / Soldner 39 Langschoß")
357
+ self.assertEqual(srs.wkt, wkt)
358
+ self.assertIn("Langschoß", srs.pretty_wkt)
359
+ self.assertIn("Langschoß", srs.xml)
360
+
361
+ @skipIf(GDAL_VERSION < (3, 0), "GDAL >= 3.0 is required")
362
+ def test_axis_order(self):
363
+ wgs84_trad = SpatialReference(4326, axis_order=AxisOrder.TRADITIONAL)
364
+ wgs84_auth = SpatialReference(4326, axis_order=AxisOrder.AUTHORITY)
365
+ # Coordinate interpretation may depend on the srs axis predicate.
366
+ pt = GEOSGeometry("POINT (992385.4472045 481455.4944650)", 2774)
367
+ pt_trad = pt.transform(wgs84_trad, clone=True)
368
+ self.assertAlmostEqual(pt_trad.x, -104.609, 3)
369
+ self.assertAlmostEqual(pt_trad.y, 38.255, 3)
370
+ pt_auth = pt.transform(wgs84_auth, clone=True)
371
+ self.assertAlmostEqual(pt_auth.x, 38.255, 3)
372
+ self.assertAlmostEqual(pt_auth.y, -104.609, 3)
373
+ # clone() preserves the axis order.
374
+ pt_auth = pt.transform(wgs84_auth.clone(), clone=True)
375
+ self.assertAlmostEqual(pt_auth.x, 38.255, 3)
376
+ self.assertAlmostEqual(pt_auth.y, -104.609, 3)
377
+
378
+ def test_axis_order_invalid(self):
379
+ msg = "SpatialReference.axis_order must be an AxisOrder instance."
380
+ with self.assertRaisesMessage(ValueError, msg):
381
+ SpatialReference(4326, axis_order="other")
382
+
383
+ @skipIf(GDAL_VERSION > (3, 0), "GDAL < 3.0 doesn't support authority.")
384
+ def test_axis_order_non_traditional_invalid(self):
385
+ msg = "AxisOrder.AUTHORITY is not supported in GDAL < 3.0."
386
+ with self.assertRaisesMessage(ValueError, msg):
387
+ SpatialReference(4326, axis_order=AxisOrder.AUTHORITY)
388
+
389
+ def test_esri(self):
390
+ srs = SpatialReference("NAD83")
391
+ pre_esri_wkt = srs.wkt
392
+ srs.to_esri()
393
+ self.assertNotEqual(srs.wkt, pre_esri_wkt)
394
+ self.assertIn('DATUM["D_North_American_1983"', srs.wkt)
395
+ srs.from_esri()
396
+ self.assertIn('DATUM["North_American_Datum_1983"', srs.wkt)
397
+
398
+ def test_srid(self):
399
+ """The srid property returns top-level authority code."""
400
+ for s in srlist:
401
+ if hasattr(s, "epsg"):
402
+ srs = SpatialReference(s.wkt)
403
+ self.assertEqual(srs.srid, s.epsg)
testbed/django__django/tests/gis_tests/gdal_tests/tests.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import unittest
2
+
3
+ from django.contrib.gis.gdal import GDAL_VERSION, gdal_full_version, gdal_version
4
+
5
+
6
+ class GDALTest(unittest.TestCase):
7
+ def test_gdal_version(self):
8
+ if GDAL_VERSION:
9
+ self.assertEqual(gdal_version(), ("%s.%s.%s" % GDAL_VERSION).encode())
10
+ else:
11
+ self.assertIn(b".", gdal_version())
12
+
13
+ def test_gdal_full_version(self):
14
+ full_version = gdal_full_version()
15
+ self.assertIn(gdal_version(), full_version)
16
+ self.assertTrue(full_version.startswith(b"GDAL"))
testbed/django__django/tests/gis_tests/geo3d/__init__.py ADDED
File without changes