-
Notifications
You must be signed in to change notification settings - Fork 213
/
test_crs.py
1618 lines (1245 loc) · 50.7 KB
/
test_crs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import concurrent.futures
import json
import platform
from unittest.mock import patch
import numpy
import pytest
import pyproj
from pyproj import CRS
from pyproj._crs import AuthorityMatchInfo
from pyproj.crs import (
CoordinateOperation,
CoordinateSystem,
Datum,
Ellipsoid,
PrimeMeridian,
)
from pyproj.crs.enums import CoordinateOperationType, DatumType
from pyproj.enums import ProjVersion, WktVersion
from pyproj.exceptions import CRSError
from pyproj.transformer import TransformerGroup
from test.conftest import (
PROJ_GTE_82,
PROJ_GTE_91,
PROJ_GTE_901,
RGF93toWSG84,
assert_can_pickle,
grids_available,
)
class CustomCRS(object):
def to_wkt(self):
return CRS.from_epsg(4326).to_wkt()
def test_from_proj4_json():
json_str = '{"proj": "longlat", "ellps": "WGS84", "datum": "WGS84"}'
proj = CRS.from_string(json_str)
with pytest.warns(UserWarning):
assert proj.to_proj4(4) == "+proj=longlat +datum=WGS84 +no_defs +type=crs"
assert proj.to_proj4(5) == "+proj=longlat +datum=WGS84 +no_defs +type=crs"
# Test with invalid JSON code
with pytest.raises(CRSError):
assert CRS.from_string("{foo: bar}")
def test_from_proj4():
proj = CRS.from_proj4("+proj=longlat +datum=WGS84 +no_defs +type=crs")
with pytest.warns(UserWarning):
assert proj.to_proj4() == "+proj=longlat +datum=WGS84 +no_defs +type=crs"
def test_from_proj4__invalid():
# Test with invalid JSON code
with pytest.raises(CRSError):
assert CRS.from_proj4(CRS(3857).to_wkt())
def test_from_epsg():
proj = CRS.from_epsg(4326)
assert proj.to_epsg() == 4326
# Test with invalid EPSG code
with pytest.raises(CRSError):
assert CRS.from_epsg(0)
def test_from_epsg_string():
proj = CRS.from_string("epsg:4326")
assert proj.to_epsg() == 4326
# Test with invalid EPSG code
with pytest.raises(CRSError):
assert CRS.from_string("epsg:xyz")
def test_from_epsg_int_like_string():
proj = CRS.from_string("4326")
assert proj.to_epsg() == 4326
# Test with invalid EPSG code
with pytest.raises(CRSError):
assert CRS.from_string("0")
def test_from_string():
wgs84_crs = CRS.from_string("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
with pytest.warns(UserWarning):
assert wgs84_crs.to_proj4() == "+proj=longlat +datum=WGS84 +no_defs +type=crs"
# Make sure this doesn't get handled using the from_epsg()
# even though 'epsg' is in the string
with pytest.warns(FutureWarning):
epsg_init_crs = CRS.from_string("+init=epsg:26911 +units=m +no_defs=True")
with pytest.warns(UserWarning):
assert (
epsg_init_crs.to_proj4()
== "+proj=utm +zone=11 +datum=NAD83 +units=m +no_defs +type=crs"
)
def test_from_numpy():
crs_numpy = numpy.array([4326])[0]
proj = CRS.from_user_input(crs_numpy)
assert proj.to_epsg() == 4326
# Test with invalid EPSG code
with pytest.raises(CRSError):
crs_numpy = numpy.array([0])[0]
assert CRS.from_epsg(crs_numpy)
def test_from_string__invalid():
with pytest.raises(CRSError, match="CRS input is not a string"):
CRS.from_string(4326)
def test_initialize_projparams_with_kwargs():
crs_mixed_args = CRS("+proj=utm +zone=10", ellps="WGS84")
crs_positional = CRS("+proj=utm +zone=10 +ellps=WGS84")
assert crs_mixed_args.is_exact_same(crs_positional)
def test_bare_parameters():
"""Make sure that bare parameters (e.g., no_defs) are handled properly,
even if they come in with key=True. This covers interaction with pyproj,
which makes presents bare parameters as key=<bool>."""
# Example produced by pyproj
proj = CRS.from_string(
"+proj=lcc +lon_0=-95 +ellps=GRS80 +y_0=0 +no_defs=True "
"+x_0=0 +units=m +lat_2=77 +lat_1=49 +lat_0=0"
)
with pytest.warns(UserWarning):
assert "+no_defs" in proj.to_proj4(4)
# TODO: THIS DOES NOT WORK
proj = CRS.from_string(
"+lon_0=-95 +ellps=GRS80 +proj=lcc +y_0=0 +no_defs=False "
"+x_0=0 +units=m +lat_2=77 +lat_1=49 +lat_0=0"
)
# assert "+no_defs" not in proj.to_proj4(4)
def test_is_geographic():
assert CRS("EPSG:4326").is_geographic is True
assert CRS("EPSG:3857").is_geographic is False
wgs84_crs = CRS.from_string("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
assert wgs84_crs.is_geographic is True
nad27_crs = CRS.from_string("+proj=longlat +ellps=clrk66 +datum=NAD27 +no_defs")
assert nad27_crs.is_geographic is True
lcc_crs = CRS.from_string(
"+lon_0=-95 +ellps=GRS80 +y_0=0 +no_defs=True +proj=lcc +x_0=0 "
"+units=m +lat_2=77 +lat_1=49 +lat_0=0"
)
assert lcc_crs.is_geographic is False
def test_is_projected():
assert CRS("EPSG:3857").is_projected is True
lcc_crs = CRS.from_string(
"+lon_0=-95 +ellps=GRS80 +y_0=0 +no_defs=True +proj=lcc +x_0=0 "
"+units=m +lat_2=77 +lat_1=49 +lat_0=0"
)
assert CRS.from_user_input(lcc_crs).is_projected is True
wgs84_crs = CRS.from_string("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
assert CRS.from_user_input(wgs84_crs).is_projected is False
def test_is_compound():
assert CRS("EPSG:4326+5773").is_compound
assert not CRS("EPSG:4326").is_compound
def test_is_same_crs():
crs1 = CRS("urn:ogc:def:crs:OGC::CRS84")
crs2 = CRS("EPSG:3857")
assert crs1 == crs1
assert crs1 != crs2
wgs84_crs = CRS.from_string("+proj=longlat +ellps=WGS84 +datum=WGS84")
assert crs1 == wgs84_crs
# Make sure that same projection with different parameter are not equal
lcc_crs1 = CRS.from_string(
"+lon_0=-95 +ellps=GRS80 +y_0=0 +no_defs=True +proj=lcc "
"+x_0=0 +units=m +lat_2=77 +lat_1=49 +lat_0=0"
)
lcc_crs2 = CRS.from_string(
"+lon_0=-95 +ellps=GRS80 +y_0=0 +no_defs=True +proj=lcc "
"+x_0=0 +units=m +lat_2=77 +lat_1=45 +lat_0=0"
)
assert lcc_crs1 != lcc_crs2
def test_to_proj4():
with pytest.warns(UserWarning):
assert (
CRS("EPSG:4326").to_proj4(4)
== "+proj=longlat +datum=WGS84 +no_defs +type=crs"
)
def test_empty_json():
with pytest.raises(CRSError):
CRS.from_string("{}")
with pytest.raises(CRSError):
CRS.from_string("[]")
with pytest.raises(CRSError):
CRS.from_string("")
def test_has_wkt_property():
with pytest.warns(FutureWarning):
assert (
CRS({"init": "EPSG:4326"})
.to_wkt("WKT1_GDAL")
.startswith('GEOGCS["WGS 84",DATUM')
)
def test_to_wkt_pretty():
crs = CRS.from_epsg(4326)
assert "\n" in crs.to_wkt(pretty=True)
assert "\n" not in crs.to_wkt()
def test_repr():
with pytest.warns(FutureWarning):
assert repr(CRS({"init": "EPSG:4326"})) == (
"<Geographic 2D CRS: +init=epsg:4326 +type=crs>\n"
"Name: WGS 84\n"
"Axis Info [ellipsoidal]:\n"
"- lon[east]: Longitude (degree)\n"
"- lat[north]: Latitude (degree)\n"
"Area of Use:\n"
"- name: World.\n"
"- bounds: (-180.0, -90.0, 180.0, 90.0)\n"
"Datum: World Geodetic System 1984 ensemble\n"
"- Ellipsoid: WGS 84\n"
"- Prime Meridian: Greenwich\n"
)
def test_repr__long():
with pytest.warns(FutureWarning):
wkt_str = 'GEOGCRS["WGS 84",ENSEMBLE["World Geodetic System 1'
assert repr(CRS(CRS({"init": "EPSG:4326"}).to_wkt())) == (
f"<Geographic 2D CRS: {wkt_str} ...>\n"
"Name: WGS 84\n"
"Axis Info [ellipsoidal]:\n"
"- lon[east]: Longitude (degree)\n"
"- lat[north]: Latitude (degree)\n"
"Area of Use:\n"
"- name: World.\n"
"- bounds: (-180.0, -90.0, 180.0, 90.0)\n"
"Datum: World Geodetic System 1984 ensemble\n"
"- Ellipsoid: WGS 84\n"
"- Prime Meridian: Greenwich\n"
)
def test_repr_epsg():
assert repr(CRS(CRS("EPSG:4326").to_wkt())) == (
"<Geographic 2D CRS: EPSG:4326>\n"
"Name: WGS 84\n"
"Axis Info [ellipsoidal]:\n"
"- Lat[north]: Geodetic latitude (degree)\n"
"- Lon[east]: Geodetic longitude (degree)\n"
"Area of Use:\n"
"- name: World.\n"
"- bounds: (-180.0, -90.0, 180.0, 90.0)\n"
"Datum: World Geodetic System 1984 ensemble\n"
"- Ellipsoid: WGS 84\n"
"- Prime Meridian: Greenwich\n"
)
def test_repr__undefined():
datum_name = "unknown"
if PROJ_GTE_901:
datum_name = f"{datum_name} using nadgrids=@null"
assert repr(
CRS(
"+proj=merc +a=6378137.0 +b=6378137.0 +nadgrids=@null"
" +lon_0=0.0 +x_0=0.0 +y_0=0.0 +units=m +no_defs"
)
) == (
"<Bound CRS: +proj=merc +a=6378137.0 +b=6378137.0 +nadgrids=@nu ...>\n"
"Name: unknown\n"
"Axis Info [cartesian]:\n"
"- E[east]: Easting (metre)\n"
"- N[north]: Northing (metre)\n"
"Area of Use:\n"
"- undefined\n"
"Coordinate Operation:\n"
"- name: unknown to WGS84\n"
"- method: NTv2\n"
f"Datum: {datum_name}\n"
"- Ellipsoid: unknown\n"
"- Prime Meridian: Greenwich\n"
"Source CRS: unknown\n"
)
def test_repr_compound():
assert repr(CRS.from_epsg(3901)) == (
"<Compound CRS: EPSG:3901>\n"
"Name: KKJ / Finland Uniform Coordinate System + N60 height\n"
"Axis Info [cartesian|vertical]:\n"
"- X[north]: Northing (metre)\n"
"- Y[east]: Easting (metre)\n"
"- H[up]: Gravity-related height (metre)\n"
"Area of Use:\n"
"- name: Finland - onshore.\n"
"- bounds: (19.24, 59.75, 31.59, 70.09)\n"
"Datum: Kartastokoordinaattijarjestelma (1966)\n"
"- Ellipsoid: International 1924\n"
"- Prime Meridian: Greenwich\n"
"Sub CRS:\n"
"- KKJ / Finland Uniform Coordinate System\n"
"- N60 height\n"
)
def test_axis_info_compound():
assert [axis.direction for axis in CRS.from_epsg(3901).axis_info] == [
"north",
"east",
"up",
]
def test_dunder_str():
with pytest.warns(FutureWarning):
assert str(CRS({"init": "EPSG:4326"})) == CRS({"init": "EPSG:4326"}).srs
def test_epsg():
with pytest.warns(FutureWarning):
assert CRS({"init": "EPSG:4326"}).to_epsg(20) == 4326
assert CRS({"init": "EPSG:4326"}).to_epsg() is None
assert CRS.from_user_input(4326).to_epsg() == 4326
assert CRS.from_epsg(4326).to_epsg() == 4326
assert CRS.from_user_input("epsg:4326").to_epsg() == 4326
def test_datum():
datum = CRS.from_epsg(4326).datum
assert "\n" in repr(datum)
datum_wkt = 'ENSEMBLE["World Geodetic System 1984 ensemble"'
assert repr(datum).startswith(datum_wkt)
assert datum.to_wkt().startswith(datum_wkt)
assert datum == datum
assert datum.is_exact_same(datum)
def test_datum_horizontal():
assert CRS.from_epsg(5972).datum == CRS.from_epsg(25832).datum
def test_datum_unknown():
crs = CRS(
"+proj=omerc +lat_0=-36.10360962430914 "
"+lonc=147.06322917270154 +alpha=-54.786229796129035 "
"+k=1 +x_0=0 +y_0=0 +gamma=0 +ellps=WGS84 "
"+towgs84=0,0,0,0,0,0,0 +units=m +no_defs"
)
datum_name = "Unknown based on WGS84 ellipsoid"
if PROJ_GTE_901:
datum_name = f"{datum_name} using towgs84=0,0,0,0,0,0,0"
assert crs.datum.name == datum_name
def test_epsg__not_found():
assert CRS("+proj=longlat +datum=WGS84 +no_defs +towgs84=0,0,0").to_epsg(0) is None
assert (
CRS.from_string("+proj=longlat +datum=WGS84 +no_defs +towgs84=0,0,0").to_epsg()
is None
)
def test_epsg__no_code_available():
lcc_crs = CRS.from_string(
"+lon_0=-95 +ellps=GRS80 +y_0=0 +no_defs=True +proj=lcc "
"+x_0=0 +units=m +lat_2=77 +lat_1=49 +lat_0=0"
)
assert lcc_crs.to_epsg() is None
def test_crs_OSR_equivalence():
crs1 = CRS.from_string("+proj=longlat +datum=WGS84 +no_defs")
crs2 = CRS.from_string("+proj=latlong +datum=WGS84 +no_defs")
with pytest.warns(FutureWarning):
crs3 = CRS({"init": "EPSG:4326"})
assert crs1 == crs2
# these are not equivalent in proj.4 now as one uses degrees and the othe radians
assert crs1 == crs3
def test_crs_OSR_no_equivalence():
crs1 = CRS.from_string("+proj=longlat +datum=WGS84 +no_defs")
crs2 = CRS.from_string("+proj=longlat +datum=NAD27 +no_defs")
assert crs1 != crs2
def test_init_from_wkt():
wgs84 = CRS.from_string("+proj=longlat +datum=WGS84 +no_defs")
from_wkt = CRS(wgs84.to_wkt())
assert wgs84.to_wkt() == from_wkt.to_wkt()
def test_init_from_wkt_invalid():
with pytest.raises(CRSError):
CRS("trash-54322")
with pytest.raises(CRSError):
CRS("")
def test_from_wkt():
wgs84 = CRS.from_string("+proj=longlat +datum=WGS84 +no_defs")
from_wkt = CRS.from_wkt(wgs84.to_wkt())
assert wgs84.to_wkt() == from_wkt.to_wkt()
def test_from_wkt_invalid():
with pytest.raises(CRSError), pytest.warns(UserWarning):
CRS.from_wkt(CRS(4326).to_proj4())
def test_from_user_input_epsg():
with pytest.warns(UserWarning):
assert "+proj=longlat" in CRS.from_user_input("EPSG:4326").to_proj4(4)
def test_from_esri_wkt():
projection_string = (
'PROJCS["USA_Contiguous_Albers_Equal_Area_Conic_USGS_version",'
'GEOGCS["GCS_North_American_1983",DATUM["D_North_American_1983",'
'SPHEROID["GRS_1980",6378137.0,298.257222101]],'
'PRIMEM["Greenwich",0.0],'
'UNIT["Degree",0.0174532925199433]],'
'PROJECTION["Albers"],'
'PARAMETER["false_easting",0.0],'
'PARAMETER["false_northing",0.0],'
'PARAMETER["central_meridian",-96.0],'
'PARAMETER["standard_parallel_1",29.5],'
'PARAMETER["standard_parallel_2",45.5],'
'PARAMETER["latitude_of_origin",23.0],'
'UNIT["Meter",1.0],'
'VERTCS["NAVD_1988",'
'VDATUM["North_American_Vertical_Datum_1988"],'
'PARAMETER["Vertical_Shift",0.0],'
'PARAMETER["Direction",1.0],UNIT["Centimeter",0.01]]]'
)
proj_crs_str = CRS.from_string(projection_string)
proj_crs_wkt = CRS(projection_string)
with pytest.warns(UserWarning):
assert proj_crs_str.to_proj4() == proj_crs_wkt.to_proj4()
assert proj_crs_str.to_proj4(4) == (
"+proj=aea +lat_0=23 +lon_0=-96 +lat_1=29.5 "
"+lat_2=45.5 +x_0=0 +y_0=0 +datum=NAD83 +units=m +no_defs +type=crs"
)
def test_compound_crs():
wkt = """COMPD_CS["unknown",GEOGCS["WGS 84",DATUM["WGS_1984",
SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],
TOWGS84[0,0,0,0,0,0,0],AUTHORITY["EPSG","6326"]],
PRIMEM["Greenwich",0],UNIT["degree",0.0174532925199433],
AUTHORITY["EPSG","4326"]],VERT_CS["unknown",
VERT_DATUM["unknown",2005],UNIT["metre",1.0,
AUTHORITY["EPSG","9001"]],AXIS["Up",UP]]]"""
assert CRS(wkt).to_wkt("WKT1_GDAL").startswith('COMPD_CS["unknown",GEOGCS["WGS 84"')
def test_ellipsoid():
crs1 = CRS.from_epsg(4326)
assert f"{crs1.ellipsoid.inverse_flattening:.3f}" == "298.257"
assert f"{crs1.ellipsoid.semi_major_metre:.3f}" == "6378137.000"
assert f"{crs1.ellipsoid.semi_minor_metre:.3f}" == "6356752.314"
def test_ellipsoid__semi_minor_not_computed():
cc = CRS("+proj=geos +lon_0=-89.5 +a=6378137.0 +b=6356752.31 h=12345")
assert cc.datum.ellipsoid.semi_minor_metre == 6356752.31
assert cc.datum.ellipsoid.semi_major_metre == 6378137.0
assert not cc.datum.ellipsoid.is_semi_minor_computed
def test_area_of_use():
crs1 = CRS.from_epsg(4326)
assert crs1.area_of_use.bounds == (-180.0, -90.0, 180.0, 90.0)
assert crs1.area_of_use.name == "World."
def test_from_user_input_custom_crs_class():
assert CRS.from_user_input(CustomCRS()) == CRS.from_epsg(4326)
def test_non_crs_error():
with pytest.raises(CRSError, match="Input is not a CRS"):
CRS(
"+proj=pipeline +ellps=GRS80 +step +proj=merc "
"+step +proj=axisswap +order=2,1"
)
def test_sub_crs():
crs = CRS.from_epsg(5972)
sub_crs_list = crs.sub_crs_list
assert len(sub_crs_list) == 2
assert sub_crs_list[0] == CRS.from_epsg(25832)
assert sub_crs_list[1] == CRS.from_epsg(5941)
assert crs.is_projected
assert crs.is_vertical
assert not crs.is_geographic
def test_sub_crs__none():
assert CRS.from_epsg(4326).sub_crs_list == []
def test_coordinate_system():
crs = CRS.from_epsg(26915)
assert repr(crs.coordinate_system).startswith("CS[Cartesian")
assert crs.coordinate_system.name == "cartesian"
assert crs.coordinate_system.name == str(crs.coordinate_system)
assert crs.coordinate_system.axis_list == crs.axis_info
assert len(crs.coordinate_system.axis_list) == 2
def test_coordinate_system_geog():
crs = CRS.from_epsg(4326)
assert repr(crs.coordinate_system).startswith("CS[ellipsoidal")
assert crs.coordinate_system.name == "ellipsoidal"
assert crs.coordinate_system.name == str(crs.coordinate_system)
assert crs.coordinate_system.axis_list == crs.axis_info
assert repr(crs.coordinate_system.axis_list) == (
"[Axis(name=Geodetic latitude, abbrev=Lat, direction=north, "
"unit_auth_code=EPSG, unit_code=9122, unit_name=degree), "
"Axis(name=Geodetic longitude, abbrev=Lon, direction=east, "
"unit_auth_code=EPSG, unit_code=9122, unit_name=degree)]"
)
def test_coordinate_operation():
crs = CRS.from_epsg(26915)
assert repr(crs.coordinate_operation) == (
"<Coordinate Operation: Conversion>\n"
"Name: UTM zone 15N\n"
"Method: Transverse Mercator\n"
"Area of Use:\n"
"- name: Between 96°W and 90°W, northern hemisphere between equator and 84°N, "
"onshore and offshore.\n"
"- bounds: (-96.0, 0.0, -90.0, 84.0)"
)
assert crs.coordinate_operation.method_name == "Transverse Mercator"
assert crs.coordinate_operation.name == str(crs.coordinate_operation)
assert crs.coordinate_operation.method_auth_name == "EPSG"
assert crs.coordinate_operation.method_code == "9807"
assert crs.coordinate_operation.is_instantiable == 1
assert crs.coordinate_operation.has_ballpark_transformation == 0
assert crs.coordinate_operation.accuracy == -1.0
assert repr(crs.coordinate_operation.params) == (
"[Param(name=Latitude of natural origin, auth_name=EPSG, code=8801, "
"value=0.0, unit_name=degree, unit_auth_name=EPSG, "
"unit_code=9102, unit_category=angular), "
"Param(name=Longitude of natural origin, auth_name=EPSG, code=8802, "
"value=-93.0, unit_name=degree, unit_auth_name=EPSG, "
"unit_code=9102, unit_category=angular), "
"Param(name=Scale factor at natural origin, auth_name=EPSG, code=8805, "
"value=0.9996, unit_name=unity, unit_auth_name=EPSG, "
"unit_code=9201, unit_category=scale), "
"Param(name=False easting, auth_name=EPSG, code=8806, value=500000.0, "
"unit_name=metre, unit_auth_name=EPSG, unit_code=9001, unit_category=linear), "
"Param(name=False northing, auth_name=EPSG, code=8807, value=0.0, "
"unit_name=metre, unit_auth_name=EPSG, unit_code=9001, unit_category=linear)]"
)
assert crs.coordinate_operation.grids == []
def test_coordinate_operation_grids():
cc = CoordinateOperation.from_epsg(1312)
if not cc.grids[0].full_name:
assert (
repr(cc.grids)
== "[Grid(short_name=NTv1_0.gsb, full_name=, package_name=, url=, "
"direct_download=False, open_license=False, available=False)]"
)
else:
assert (
repr(cc.grids)
== "[Grid(short_name=NTv1_0.gsb, full_name=NTv1_0.gsb, package_name=, "
"url=, direct_download=False, open_license=False, available=False)]"
)
@pytest.mark.grid
def test_coordinate_operation_grids__alternative_grid_name():
cc = CoordinateOperation.from_epsg(1312, True)
assert len(cc.grids) == 1
grid = cc.grids[0]
assert grid.direct_download is True
assert grid.open_license is True
assert grid.short_name == "ca_nrc_ntv1_can.tif"
if grids_available(grid.short_name):
assert grid.available is True
if PROJ_GTE_91:
assert grid.full_name == ""
else:
assert grid.full_name.endswith(grid.short_name)
else:
assert grid.available is False
assert grid.full_name == ""
assert grid.package_name == ""
assert grid.url == "https://cdn.proj.org/ca_nrc_ntv1_can.tif"
def test_coordinate_operation__missing():
crs = CRS.from_epsg(4326)
assert crs.coordinate_operation is None
def test_coordinate_operation__from_epsg():
cc = CoordinateOperation.from_epsg(16031)
assert cc.method_auth_name == "EPSG"
assert cc.method_code == "9807"
def test_coordinate_operation__from_authority():
cc = CoordinateOperation.from_authority("EPSG", 16031)
assert cc.method_auth_name == "EPSG"
assert cc.method_code == "9807"
@pytest.mark.parametrize(
"user_input",
[
1671,
("EPSG", 1671),
"urn:ogc:def:coordinateOperation:EPSG::1671",
CoordinateOperation.from_epsg(1671),
CoordinateOperation.from_epsg(1671).to_json_dict(),
RGF93toWSG84,
],
)
def test_coordinate_operation__from_user_input(user_input):
assert CoordinateOperation.from_user_input(
user_input
) == CoordinateOperation.from_epsg(1671)
def test_coordinate_operation__from_user_input__invalid():
with pytest.raises(CRSError, match="Invalid coordinate operation"):
CoordinateOperation.from_user_input({})
def test_coordinate_operation__from_epsg__empty():
with pytest.raises(CRSError, match="Invalid authority"):
CoordinateOperation.from_epsg(1)
def test_coordinate_operation__from_authority__empty():
with pytest.raises(CRSError, match="Invalid authority"):
CoordinateOperation.from_authority("BOB", 4326)
def test_datum__from_epsg():
if PROJ_GTE_82:
datum_wkt = (
'ENSEMBLE["World Geodetic System 1984 ensemble",'
'MEMBER["World Geodetic System 1984 (Transit)",ID["EPSG",1166]],'
'MEMBER["World Geodetic System 1984 (G730)",ID["EPSG",1152]],'
'MEMBER["World Geodetic System 1984 (G873)",ID["EPSG",1153]],'
'MEMBER["World Geodetic System 1984 (G1150)",ID["EPSG",1154]],'
'MEMBER["World Geodetic System 1984 (G1674)",ID["EPSG",1155]],'
'MEMBER["World Geodetic System 1984 (G1762)",ID["EPSG",1156]],'
'MEMBER["World Geodetic System 1984 (G2139)",ID["EPSG",1309]],'
'ELLIPSOID["WGS 84",6378137,298.257223563,LENGTHUNIT["metre",1],'
'ID["EPSG",7030]],ENSEMBLEACCURACY[2.0],ID["EPSG",6326]]'
)
else:
datum_wkt = (
'ENSEMBLE["World Geodetic System 1984 ensemble",'
'MEMBER["World Geodetic System 1984 (Transit)",ID["EPSG",1166]],'
'MEMBER["World Geodetic System 1984 (G730)",ID["EPSG",1152]],'
'MEMBER["World Geodetic System 1984 (G873)",ID["EPSG",1153]],'
'MEMBER["World Geodetic System 1984 (G1150)",ID["EPSG",1154]],'
'MEMBER["World Geodetic System 1984 (G1674)",ID["EPSG",1155]],'
'MEMBER["World Geodetic System 1984 (G1762)",ID["EPSG",1156]],'
'ELLIPSOID["WGS 84",6378137,298.257223563,LENGTHUNIT["metre",1],'
'ID["EPSG",7030]],ENSEMBLEACCURACY[2.0],ID["EPSG",6326]]'
)
assert Datum.from_epsg("6326").to_wkt() == datum_wkt
def test_datum__from_authority():
dt = Datum.from_authority("EPSG", 6326)
assert dt.name == "World Geodetic System 1984 ensemble"
def test_datum__from_epsg__invalid():
with pytest.raises(CRSError, match="Invalid authority"):
Datum.from_epsg(1)
def test_datum__from_authority__invalid():
with pytest.raises(CRSError, match="Invalid authority"):
Datum.from_authority("BOB", 1)
@pytest.mark.parametrize(
"user_input",
[
6326,
("EPSG", 6326),
"urn:ogc:def:ensemble:EPSG::6326",
Datum.from_epsg(6326),
Datum.from_epsg(6326).to_json_dict(),
"World Geodetic System 1984",
],
)
def test_datum__from_user_input(user_input):
assert Datum.from_user_input(user_input) == Datum.from_epsg(6326)
def test_datum__from_user_input__invalid():
with pytest.raises(CRSError, match="Invalid datum"):
Datum.from_user_input({})
def test_prime_meridian__from_epsg():
assert PrimeMeridian.from_epsg(8903).to_wkt() == (
'PRIMEM["Paris",2.5969213,ANGLEUNIT["grad",0.0157079632679489],ID["EPSG",8903]]'
)
def test_prime_meridian__from_authority():
assert PrimeMeridian.from_authority("EPSG", 8903).name == "Paris"
def test_prime_meridian__from_epsg__invalid():
with pytest.raises(CRSError, match="Invalid authority"):
PrimeMeridian.from_epsg(1)
def test_prime_meridian__from_authority__invalid():
with pytest.raises(CRSError, match="Invalid authority"):
PrimeMeridian.from_authority("Bob", 1)
@pytest.mark.parametrize(
"user_input",
[
8901,
("EPSG", 8901),
"urn:ogc:def:meridian:EPSG::8901",
PrimeMeridian.from_epsg(8901),
PrimeMeridian.from_epsg(8901).to_json_dict(),
"Greenwich",
],
)
def test_prime_meridian__from_user_input(user_input):
assert PrimeMeridian.from_user_input(user_input) == PrimeMeridian.from_epsg(8901)
def test_prime_meridian__from_user_input__invalid():
with pytest.raises(CRSError, match="Invalid prime meridian"):
PrimeMeridian.from_user_input({})
def test_ellipsoid__from_epsg():
assert Ellipsoid.from_epsg(7030).to_wkt() == (
'ELLIPSOID["WGS 84",6378137,298.257223563,'
'LENGTHUNIT["metre",1],ID["EPSG",7030]]'
)
def test_ellipsoid__from_authority():
assert Ellipsoid.from_authority("EPSG", 7030).name == "WGS 84"
def test_ellipsoid__from_epsg__invalid():
with pytest.raises(CRSError, match="Invalid authority"):
Ellipsoid.from_epsg(1)
def test_ellipsoid__from_authority__invalid():
with pytest.raises(CRSError, match="Invalid authority"):
Ellipsoid.from_authority("BOB", 1)
@pytest.mark.parametrize(
"user_input",
[
7001,
("EPSG", 7001),
"urn:ogc:def:ellipsoid:EPSG::7001",
Ellipsoid.from_epsg(7001),
Ellipsoid.from_epsg(7001).to_json_dict(),
"Airy 1830",
],
)
def test_ellipsoid__from_user_input(user_input):
assert Ellipsoid.from_user_input(user_input) == Ellipsoid.from_epsg(7001)
def test_ellipsoid__from_user_input__invalid():
with pytest.raises(CRSError, match="Invalid ellipsoid"):
Ellipsoid.from_user_input({})
CS_JSON_DICT = {
"$schema": "https://proj.org/schemas/v0.2/projjson.schema.json",
"type": "CoordinateSystem",
"subtype": "Cartesian",
"axis": [
{"name": "Easting", "abbreviation": "E", "direction": "east", "unit": "metre"},
{
"name": "Northing",
"abbreviation": "N",
"direction": "north",
"unit": "metre",
},
],
}
@pytest.mark.parametrize(
"user_input",
[
CS_JSON_DICT,
json.dumps(CS_JSON_DICT),
CoordinateSystem.from_json_dict(CS_JSON_DICT),
],
)
def test_coordinate_system__from_user_input(user_input):
assert CoordinateSystem.from_user_input(
user_input
) == CoordinateSystem.from_json_dict(CS_JSON_DICT)
@pytest.mark.parametrize(
"user_input",
[
7001,
("EPSG", 7001),
"urn:ogc:def:ellipsoid:EPSG::7001",
Ellipsoid.from_epsg(7001),
Ellipsoid.from_epsg(7001).to_json_dict(),
],
)
def test_coordinate_system__from_user_input__invalid(user_input):
with pytest.raises(CRSError, match="Invalid"):
CoordinateSystem.from_user_input(user_input)
def test_bound_crs_is_geographic():
assert CRS(
"proj=longlat datum=WGS84 no_defs ellps=WGS84 towgs84=0,0,0"
).is_geographic
def test_coordinate_operation_towgs84_three():
crs = CRS("+proj=latlong +ellps=GRS80 +towgs84=-199.87,74.79,246.62")
assert crs.coordinate_operation.towgs84 == [-199.87, 74.79, 246.62]
def test_coordinate_operation_towgs84_seven():
crs = CRS(
"+proj=tmerc +lat_0=0 +lon_0=15 +k=0.9996 +x_0=2520000 +y_0=0 "
"+ellps=intl +towgs84=-122.74,-34.27,-22.83,-1.884,-3.400,-3.030,-15.62"
)
assert crs.coordinate_operation.towgs84 == [
-122.74,
-34.27,
-22.83,
-1.884,
-3.4,
-3.03,
-15.62,
]
def test_axis_info_bound():
crs = CRS(
"+proj=tmerc +lat_0=0 +lon_0=15 +k=0.9996 +x_0=2520000 +y_0=0 "
"+ellps=intl +towgs84=-122.74,-34.27,-22.83,-1.884,-3.400,-3.030,-15.62"
)
assert [axis.direction for axis in crs.axis_info] == ["east", "north"]
def test_coordinate_operation_towgs84_missing():
crs = CRS("epsg:3004")
assert crs.coordinate_operation.towgs84 == []
@pytest.mark.parametrize(
"wkt_version_str, wkt_version_enum",
[
("WKT1_GDAL", WktVersion.WKT1_GDAL),
("WKT2_2018", WktVersion.WKT2_2018),
("WKT2_2018_SIMPLIFIED", WktVersion.WKT2_2018_SIMPLIFIED),
("WKT2_2019", WktVersion.WKT2_2019),
("WKT2_2019_SIMPLIFIED", WktVersion.WKT2_2019_SIMPLIFIED),
("WKT2_2015", WktVersion.WKT2_2015),
("WKT2_2015_SIMPLIFIED", WktVersion.WKT2_2015_SIMPLIFIED),
],
)
def test_to_wkt_enum(wkt_version_str, wkt_version_enum):
crs = CRS.from_epsg(4326)
assert crs.to_wkt(wkt_version_str) == crs.to_wkt(wkt_version_enum)
def test_to_wkt_enum__invalid():
crs = CRS.from_epsg(4326)
with pytest.raises(ValueError, match="Invalid value"):
crs.to_wkt("WKT_INVALID")
@pytest.mark.parametrize(
"wkt_version",
["WKT2_2015", "WKT2_2015_SIMPLIFIED", "WKT1_GDAL", "WKT1_ESRI"],
)
def test_to_wkt_none_warning(wkt_version):
wkt_string = (
'PROJCRS["unknown",BASEGEOGCRS["unknown",DATUM["unknown",'
'ELLIPSOID["WGS 84",6378137,298.257223563,LENGTHUNIT["metre",1,'
'ID["EPSG",9001]]]],PRIMEM["Greenwich",0,ANGLEUNIT["degree",0.0174532925199],'
'ID["EPSG",8901]]],CONVERSION["unknown",METHOD["Equidistant Cylindrical",'
'ID["EPSG",1028]],PARAMETER["Latitude of 1st standard parallel",0,'
'ANGLEUNIT["degree",0.0174532925199433],ID["EPSG",8823]],'
'PARAMETER["Longitude of natural origin",0,ANGLEUNIT["degree",0.0174532925199],'
'ID["EPSG",8802]],PARAMETER["False easting",0,'
'LENGTHUNIT["unknown",111319.490793274],ID["EPSG",8806]],'
'PARAMETER["False northing",0,LENGTHUNIT["unknown",111319.490793274],'
'ID["EPSG",8807]]],CS[Cartesian,3],AXIS["(E)",east,ORDER[1],'
'LENGTHUNIT["unknown",111319.490793274]],AXIS["(N)",north,ORDER[2],'
'LENGTHUNIT["unknown",111319.490793274]],AXIS["ellipsoidal height (h)",up,'
'ORDER[3],LENGTHUNIT["metre",1,ID["EPSG",9001]]]]'
)
crs = CRS.from_wkt(wkt_string)
with pytest.warns(FutureWarning, match="CRS cannot be converted to a WKT string"):
assert crs.to_wkt(version=wkt_version) is None
def test_to_proj4_none_warning():
crs = CRS("EPSG:4326")
with patch("pyproj.crs.crs.CRS._crs") as crs_mock, pytest.warns(
FutureWarning, match="CRS cannot be converted to a PROJ string"
):
crs_mock.to_proj4.return_value = None
assert crs.to_proj4() is None
def test_to_json_none_warning():
crs = CRS("EPSG:4326")
with patch("pyproj.crs.crs.CRS._crs") as crs_mock, pytest.warns(
FutureWarning, match="CRS cannot be converted to a PROJ JSON string"
):
crs_mock.to_json.return_value = None
assert crs.to_json() is None
def test_to_proj4_enum():
crs = CRS.from_epsg(4326)
with pytest.warns(UserWarning):
assert crs.to_proj4(4) == crs.to_proj4(ProjVersion.PROJ_4)
assert crs.to_proj4(5) == crs.to_proj4(ProjVersion.PROJ_5)
def test_datum_equals():
datum = Datum.from_epsg(6326)
assert datum == 6326
assert not datum != 6326
assert datum != "invalid"
@pytest.mark.parametrize(
"input_str",
[
"urn:ogc:def:ensemble:EPSG::6326",
"World Geodetic System 1984",
],
)
def test_datum__from_string(input_str):
dd = Datum.from_string(input_str)
assert dd.name == "World Geodetic System 1984 ensemble"
assert dd.type_name == "Datum Ensemble"
@pytest.mark.parametrize(