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 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335
|
"""
The transformer module is for performing cartographic transformations.
"""
__all__ = [
"AreaOfInterest",
"Transformer",
"TransformerGroup",
"itransform",
"transform",
]
import threading
import warnings
from abc import ABC, abstractmethod
from array import array
from collections.abc import Iterable, Iterator
from dataclasses import dataclass
from itertools import chain, islice
from pathlib import Path
from typing import Any, overload
from pyproj import CRS
from pyproj._compat import cstrencode
from pyproj._context import _clear_proj_error
from pyproj._crs import AreaOfUse, CoordinateOperation
from pyproj._transformer import ( # noqa: F401 pylint: disable=unused-import
AreaOfInterest,
_Transformer,
_TransformerGroup,
)
from pyproj.datadir import get_user_data_dir
from pyproj.enums import ProjVersion, TransformDirection, WktVersion
from pyproj.exceptions import ProjError
from pyproj.sync import _download_resource_file
from pyproj.utils import _convertback, _copytobuffer
class TransformerMaker(ABC):
"""
.. versionadded:: 3.1.0
Base class for generating new instances
of the Cython _Transformer class for
thread safety in the Transformer class.
"""
@abstractmethod
def __call__(self) -> _Transformer:
"""
Returns
-------
_Transformer
"""
raise NotImplementedError
@dataclass(frozen=True)
class TransformerUnsafe(TransformerMaker):
"""
.. versionadded:: 3.1.0
Returns the original Cython _Transformer
and is not thread-safe.
"""
transformer: _Transformer
def __call__(self) -> _Transformer:
"""
Returns
-------
_Transformer
"""
return self.transformer
@dataclass(frozen=True)
class TransformerFromCRS( # pylint: disable=too-many-instance-attributes
TransformerMaker
):
"""
.. versionadded:: 3.1.0
.. versionadded:: 3.4.0 force_over
Generates a Cython _Transformer class from input CRS data.
"""
crs_from: bytes
crs_to: bytes
always_xy: bool
area_of_interest: AreaOfInterest | None
authority: str | None
accuracy: str | None
allow_ballpark: bool | None
force_over: bool = False
only_best: bool | None = None
def __call__(self) -> _Transformer:
"""
Returns
-------
_Transformer
"""
return _Transformer.from_crs(
self.crs_from,
self.crs_to,
always_xy=self.always_xy,
area_of_interest=self.area_of_interest,
authority=self.authority,
accuracy=self.accuracy,
allow_ballpark=self.allow_ballpark,
force_over=self.force_over,
only_best=self.only_best,
)
@dataclass(frozen=True)
class TransformerFromPipeline(TransformerMaker):
"""
.. versionadded:: 3.1.0
Generates a Cython _Transformer class from input pipeline data.
"""
proj_pipeline: bytes
def __call__(self) -> _Transformer:
"""
Returns
-------
_Transformer
"""
return _Transformer.from_pipeline(self.proj_pipeline)
class TransformerGroup(_TransformerGroup):
"""
The TransformerGroup is a set of possible transformers from one CRS to another.
.. versionadded:: 2.3.0
.. warning:: CoordinateOperation and Transformer objects
returned are not thread-safe.
From PROJ docs::
The operations are sorted with the most relevant ones first: by
descending area (intersection of the transformation area with the
area of interest, or intersection of the transformation with the
area of use of the CRS), and by increasing accuracy. Operations
with unknown accuracy are sorted last, whatever their area.
"""
def __init__(
self,
crs_from: Any,
crs_to: Any,
always_xy: bool = False,
area_of_interest: AreaOfInterest | None = None,
authority: str | None = None,
accuracy: float | None = None,
allow_ballpark: bool = True,
allow_superseded: bool = False,
) -> None:
"""Get all possible transformations from a :obj:`pyproj.crs.CRS`
or input used to create one.
.. versionadded:: 3.4.0 authority, accuracy, allow_ballpark
.. versionadded:: 3.6.0 allow_superseded
Parameters
----------
crs_from: pyproj.crs.CRS or input used to create one
Projection of input data.
crs_to: pyproj.crs.CRS or input used to create one
Projection of output data.
always_xy: bool, default=False
If true, the transform method will accept as input and return as output
coordinates using the traditional GIS order, that is longitude, latitude
for geographic CRS and easting, northing for most projected CRS.
area_of_interest: :class:`.AreaOfInterest`, optional
The area of interest to help order the transformations based on the
best operation for the area.
authority: str, optional
When not specified, coordinate operations from any authority will be
searched, with the restrictions set in the
authority_to_authority_preference database table related to the
authority of the source/target CRS themselves. If authority is set
to “any”, then coordinate operations from any authority will be
searched. If authority is a non-empty string different from "any",
then coordinate operations will be searched only in that authority
namespace (e.g. EPSG).
accuracy: float, optional
The minimum desired accuracy (in metres) of the candidate
coordinate operations.
allow_ballpark: bool, default=True
Set to False to disallow the use of Ballpark transformation
in the candidate coordinate operations. Default is to allow.
allow_superseded: bool, default=False
Set to True to allow the use of superseded (but not deprecated)
transformations in the candidate coordinate operations. Default is
to disallow.
"""
super().__init__(
CRS.from_user_input(crs_from)._crs,
CRS.from_user_input(crs_to)._crs,
always_xy=always_xy,
area_of_interest=area_of_interest,
authority=authority,
accuracy=-1 if accuracy is None else accuracy,
allow_ballpark=allow_ballpark,
allow_superseded=allow_superseded,
)
for iii, transformer in enumerate(self._transformers):
# pylint: disable=unsupported-assignment-operation
self._transformers[iii] = Transformer(TransformerUnsafe(transformer))
@property
def transformers(self) -> list["Transformer"]:
"""
list[:obj:`Transformer`]:
List of available :obj:`Transformer`
associated with the transformation.
"""
return self._transformers
@property
def unavailable_operations(self) -> list[CoordinateOperation]:
"""
list[:obj:`pyproj.crs.CoordinateOperation`]:
List of :obj:`pyproj.crs.CoordinateOperation` that are not
available due to missing grids.
"""
return self._unavailable_operations
@property
def best_available(self) -> bool:
"""
bool: If True, the best possible transformer is available.
"""
return self._best_available
def download_grids(
self,
directory: str | Path | None = None,
open_license: bool = True,
verbose: bool = False,
) -> None:
"""
.. versionadded:: 3.0.0
Download missing grids that can be downloaded automatically.
.. warning:: There are cases where the URL to download the grid is missing.
In those cases, you can enable enable
:ref:`debugging-internal-proj` and perform a
transformation. The logs will show the grids PROJ searches for.
Parameters
----------
directory: str or Path, optional
The directory to download the grids to.
Defaults to :func:`pyproj.datadir.get_user_data_dir`
open_license: bool, default=True
If True, will only download grids with an open license.
verbose: bool, default=False
If True, will print information about grids downloaded.
"""
if directory is None:
directory = get_user_data_dir(True)
# pylint: disable=not-an-iterable
for unavailable_operation in self.unavailable_operations:
for grid in unavailable_operation.grids:
if (
not grid.available
and grid.url.endswith(grid.short_name)
and grid.direct_download
and (grid.open_license or not open_license)
):
_download_resource_file(
file_url=grid.url,
short_name=grid.short_name,
directory=directory,
verbose=verbose,
)
elif not grid.available and verbose:
warnings.warn(f"Skipped: {grid}")
def __repr__(self) -> str:
return (
f"<TransformerGroup: best_available={self.best_available}>\n"
f"- transformers: {len(self.transformers)}\n"
f"- unavailable_operations: {len(self.unavailable_operations)}"
)
class TransformerLocal(threading.local):
"""
Threading local instance for cython _Transformer class.
For more details, see:
https://github.com/pyproj4/pyproj/issues/782
"""
def __init__(self):
self.transformer = None # Initialises in each thread
super().__init__()
class Transformer:
"""
The Transformer class is for facilitating re-using
transforms without needing to re-create them. The goal
is to make repeated transforms faster.
Additionally, it provides multiple methods for initialization.
.. versionadded:: 2.1.0
"""
def __init__(
self,
transformer_maker: TransformerMaker | None = None,
) -> None:
if not isinstance(transformer_maker, TransformerMaker):
_clear_proj_error()
raise ProjError(
"Transformer must be initialized using: 'from_crs' or 'from_pipeline'."
)
self._local = TransformerLocal()
self._local.transformer = transformer_maker()
self._transformer_maker = transformer_maker
def __getstate__(self) -> dict[str, Any]:
return {"_transformer_maker": self._transformer_maker}
def __setstate__(self, state: dict[str, Any]):
self.__dict__.update(state)
self._local = TransformerLocal()
self._local.transformer = self._transformer_maker()
@property
def _transformer(self):
"""
The Cython _Transformer object for this thread.
Returns
-------
_Transformer
"""
if self._local.transformer is None:
self._local.transformer = self._transformer_maker()
return self._local.transformer
@property
def name(self) -> str:
"""
str: Name of the projection.
"""
return self._transformer.id
@property
def description(self) -> str:
"""
str: Description of the projection.
"""
return self._transformer.description
@property
def definition(self) -> str:
"""
str: Definition of the projection.
"""
return self._transformer.definition
@property
def has_inverse(self) -> bool:
"""
bool: True if an inverse mapping exists.
"""
return self._transformer.has_inverse
@property
def accuracy(self) -> float:
"""
float: Expected accuracy of the transformation. -1 if unknown.
"""
return self._transformer.accuracy
@property
def area_of_use(self) -> AreaOfUse:
"""
.. versionadded:: 2.3.0
Returns
-------
AreaOfUse:
The area of use object with associated attributes.
"""
return self._transformer.area_of_use
@property
def remarks(self) -> str:
"""
.. versionadded:: 2.4.0
Returns
-------
str:
Remarks about object.
"""
return self._transformer.remarks
@property
def scope(self) -> str:
"""
.. versionadded:: 2.4.0
Returns
-------
str:
Scope of object.
"""
return self._transformer.scope
@property
def operations(self) -> tuple[CoordinateOperation] | None:
"""
.. versionadded:: 2.4.0
Returns
-------
tuple[CoordinateOperation]:
The operations in a concatenated operation.
"""
return self._transformer.operations
def get_last_used_operation(self) -> "Transformer":
"""
.. versionadded:: 3.4.0
.. note:: Requires PROJ 9.1+
See: :c:func:`proj_trans_get_last_used_operation`
Returns
-------
Transformer:
The operation used in the transform call.
"""
return Transformer(
TransformerUnsafe(self._transformer.get_last_used_operation())
)
@property
def is_network_enabled(self) -> bool:
"""
.. versionadded:: 3.0.0
Returns
-------
bool:
If the network is enabled.
"""
return self._transformer.is_network_enabled
@property
def source_crs(self) -> CRS | None:
"""
.. versionadded:: 3.3.0
Returns
-------
CRS | None:
The source CRS of a CoordinateOperation.
"""
return (
None
if self._transformer.source_crs is None
else CRS(self._transformer.source_crs)
)
@property
def target_crs(self) -> CRS | None:
"""
.. versionadded:: 3.3.0
Returns
-------
CRS | None:
The target CRS of a CoordinateOperation.
"""
return (
None
if self._transformer.target_crs is None
else CRS(self._transformer.target_crs)
)
@staticmethod
def from_proj(
proj_from: Any,
proj_to: Any,
always_xy: bool = False,
area_of_interest: AreaOfInterest | None = None,
) -> "Transformer":
"""Make a Transformer from a :obj:`pyproj.Proj` or input used to create one.
.. deprecated:: 3.4.1 :meth:`~Transformer.from_crs` is preferred.
.. versionadded:: 2.2.0 always_xy
.. versionadded:: 2.3.0 area_of_interest
Parameters
----------
proj_from: :obj:`pyproj.Proj` or input used to create one
Projection of input data.
proj_to: :obj:`pyproj.Proj` or input used to create one
Projection of output data.
always_xy: bool, default=False
If true, the transform method will accept as input and return as output
coordinates using the traditional GIS order, that is longitude, latitude
for geographic CRS and easting, northing for most projected CRS.
area_of_interest: :class:`.AreaOfInterest`, optional
The area of interest to help select the transformation.
Returns
-------
Transformer
"""
# pylint: disable=import-outside-toplevel
from pyproj import Proj
if not isinstance(proj_from, Proj):
proj_from = Proj(proj_from)
if not isinstance(proj_to, Proj):
proj_to = Proj(proj_to)
return Transformer.from_crs(
proj_from.crs,
proj_to.crs,
always_xy=always_xy,
area_of_interest=area_of_interest,
)
@staticmethod
def from_crs(
crs_from: Any,
crs_to: Any,
always_xy: bool = False,
area_of_interest: AreaOfInterest | None = None,
authority: str | None = None,
accuracy: float | None = None,
allow_ballpark: bool | None = None,
force_over: bool = False,
only_best: bool | None = None,
) -> "Transformer":
"""Make a Transformer from a :obj:`pyproj.crs.CRS` or input used to create one.
See:
- :c:func:`proj_create_crs_to_crs`
- :c:func:`proj_create_crs_to_crs_from_pj`
.. versionadded:: 2.2.0 always_xy
.. versionadded:: 2.3.0 area_of_interest
.. versionadded:: 3.1.0 authority, accuracy, allow_ballpark
.. versionadded:: 3.4.0 force_over
.. versionadded:: 3.5.0 only_best
Parameters
----------
crs_from: pyproj.crs.CRS or input used to create one
Projection of input data.
crs_to: pyproj.crs.CRS or input used to create one
Projection of output data.
always_xy: bool, default=False
If true, the transform method will accept as input and return as output
coordinates using the traditional GIS order, that is longitude, latitude
for geographic CRS and easting, northing for most projected CRS.
area_of_interest: :class:`.AreaOfInterest`, optional
The area of interest to help select the transformation.
authority: str, optional
When not specified, coordinate operations from any authority will be
searched, with the restrictions set in the
authority_to_authority_preference database table related to the
authority of the source/target CRS themselves. If authority is set
to “any”, then coordinate operations from any authority will be
searched. If authority is a non-empty string different from "any",
then coordinate operations will be searched only in that authority
namespace (e.g. EPSG).
accuracy: float, optional
The minimum desired accuracy (in metres) of the candidate
coordinate operations.
allow_ballpark: bool, optional
Set to False to disallow the use of Ballpark transformation
in the candidate coordinate operations. Default is to allow.
force_over: bool, default=False
If True, it will to force the +over flag on the transformation.
Requires PROJ 9+.
only_best: bool, optional
Can be set to True to cause PROJ to error out if the best
transformation known to PROJ and usable by PROJ if all grids known and
usable by PROJ were accessible, cannot be used. Best transformation should
be understood as the transformation returned by
:c:func:`proj_get_suggested_operation` if all known grids were
accessible (either locally or through network).
Note that the default value for this option can be also set with the
:envvar:`PROJ_ONLY_BEST_DEFAULT` environment variable, or with the
``only_best_default`` setting of :ref:`proj-ini`.
The only_best kwarg overrides the default value if set.
Requires PROJ 9.2+.
Returns
-------
Transformer
"""
return Transformer(
TransformerFromCRS(
cstrencode(CRS.from_user_input(crs_from).srs),
cstrencode(CRS.from_user_input(crs_to).srs),
always_xy=always_xy,
area_of_interest=area_of_interest,
authority=authority,
accuracy=accuracy if accuracy is None else str(accuracy),
allow_ballpark=allow_ballpark,
force_over=force_over,
only_best=only_best,
)
)
@staticmethod
def from_pipeline(proj_pipeline: str) -> "Transformer":
"""Make a Transformer from a PROJ pipeline string.
:ref:`pipeline`
See:
- :c:func:`proj_create`
- :c:func:`proj_create_from_database`
.. versionadded:: 3.1.0 AUTH:CODE string support (e.g. EPSG:1671)
Allowed input:
- a PROJ string
- a WKT string
- a PROJJSON string
- an object code (e.g. "EPSG:1671"
"urn:ogc:def:coordinateOperation:EPSG::1671")
- an object name. e.g "ITRF2014 to ETRF2014 (1)".
In that case as uniqueness is not guaranteed,
heuristics are applied to determine the appropriate best match.
- a OGC URN combining references for concatenated operations
(e.g. "urn:ogc:def:coordinateOperation,coordinateOperation:EPSG::3895,
coordinateOperation:EPSG::1618")
Parameters
----------
proj_pipeline: str
Projection pipeline string.
Returns
-------
Transformer
"""
return Transformer(TransformerFromPipeline(cstrencode(proj_pipeline)))
@overload
def transform( # noqa: E704 pylint: disable=invalid-name
self,
xx: Any,
yy: Any,
radians: bool = False,
errcheck: bool = False,
direction: TransformDirection | str = TransformDirection.FORWARD,
inplace: bool = False,
) -> tuple[Any, Any]: ...
@overload
def transform( # noqa: E704 pylint: disable=invalid-name
self,
xx: Any,
yy: Any,
zz: Any,
radians: bool = False,
errcheck: bool = False,
direction: TransformDirection | str = TransformDirection.FORWARD,
inplace: bool = False,
) -> tuple[Any, Any, Any]: ...
@overload
def transform( # noqa: E704 pylint: disable=invalid-name
self,
xx: Any,
yy: Any,
zz: Any,
tt: Any,
radians: bool = False,
errcheck: bool = False,
direction: TransformDirection | str = TransformDirection.FORWARD,
inplace: bool = False,
) -> tuple[Any, Any, Any, Any]: ...
def transform( # pylint: disable=invalid-name
self,
xx,
yy,
zz=None,
tt=None,
radians=False,
errcheck=False,
direction=TransformDirection.FORWARD,
inplace=False,
):
"""
Transform points between two coordinate systems.
See: :c:func:`proj_trans_generic`
.. versionadded:: 2.1.1 errcheck
.. versionadded:: 2.2.0 direction
.. versionadded:: 3.2.0 inplace
Accepted numeric scalar or array:
- :class:`int`
- :class:`float`
- :class:`numpy.floating`
- :class:`numpy.integer`
- :class:`list`
- :class:`tuple`
- :class:`array.array`
- :class:`numpy.ndarray`
- :class:`xarray.DataArray`
- :class:`pandas.Series`
Parameters
----------
xx: scalar or array
Input x coordinate(s).
yy: scalar or array
Input y coordinate(s).
zz: scalar or array, optional
Input z coordinate(s).
tt: scalar or array, optional
Input time coordinate(s).
radians: bool, default=False
If True, will expect input data to be in radians and will return radians
if the projection is geographic. Otherwise, it uses degrees.
Ignored for pipeline transformations with pyproj 2,
but will work in pyproj 3.
errcheck: bool, default=False
If True, an exception is raised if the errors are found in the process.
If False, ``inf`` is returned for errors.
direction: pyproj.enums.TransformDirection, optional
The direction of the transform.
Default is :attr:`pyproj.enums.TransformDirection.FORWARD`.
inplace: bool, default=False
If True, will attempt to write the results to the input array
instead of returning a new array. This will fail if the input
is not an array in C order with the double data type.
Example
--------
>>> from pyproj import Transformer
>>> transformer = Transformer.from_crs("EPSG:4326", "EPSG:3857")
>>> x3, y3 = transformer.transform(33, 98)
>>> f"{x3:.3f} {y3:.3f}"
'10909310.098 3895303.963'
>>> pipeline_str = (
... "+proj=pipeline +step +proj=longlat +ellps=WGS84 "
... "+step +proj=unitconvert +xy_in=rad +xy_out=deg"
... )
>>> pipe_trans = Transformer.from_pipeline(pipeline_str)
>>> xt, yt = pipe_trans.transform(2.1, 0.001)
>>> f"{xt:.3f} {yt:.3f}"
'2.100 0.001'
>>> transproj = Transformer.from_crs(
... {"proj":'geocent', "ellps":'WGS84', "datum":'WGS84'},
... "EPSG:4326",
... always_xy=True,
... )
>>> xpj, ypj, zpj = transproj.transform(
... -2704026.010,
... -4253051.810,
... 3895878.820,
... radians=True,
... )
>>> f"{xpj:.3f} {ypj:.3f} {zpj:.3f}"
'-2.137 0.661 -20.531'
>>> transprojr = Transformer.from_crs(
... "EPSG:4326",
... {"proj":'geocent', "ellps":'WGS84', "datum":'WGS84'},
... always_xy=True,
... )
>>> xpjr, ypjr, zpjr = transprojr.transform(xpj, ypj, zpj, radians=True)
>>> f"{xpjr:.3f} {ypjr:.3f} {zpjr:.3f}"
'-2704026.010 -4253051.810 3895878.820'
>>> transformer = Transformer.from_crs("EPSG:4326", 4326)
>>> xeq, yeq = transformer.transform(33, 98)
>>> f"{xeq:.0f} {yeq:.0f}"
'33 98'
"""
try:
# function optimized for point data
return self._transformer._transform_point(
inx=xx,
iny=yy,
inz=zz,
intime=tt,
direction=direction,
radians=radians,
errcheck=errcheck,
)
except TypeError:
pass
# process inputs, making copies that support buffer API.
inx, x_data_type = _copytobuffer(xx, inplace=inplace)
iny, y_data_type = _copytobuffer(yy, inplace=inplace)
if zz is not None:
inz, z_data_type = _copytobuffer(zz, inplace=inplace)
else:
inz = None
if tt is not None:
intime, t_data_type = _copytobuffer(tt, inplace=inplace)
else:
intime = None
# call pj_transform. inx,iny,inz buffers modified in place.
self._transformer._transform(
inx=inx,
iny=iny,
inz=inz,
intime=intime,
direction=direction,
radians=radians,
errcheck=errcheck,
)
# if inputs were lists, tuples or floats, convert back.
outx = _convertback(x_data_type, inx)
outy = _convertback(y_data_type, iny)
return_data: tuple[Any, ...] = (outx, outy)
if zz is not None:
return_data += (_convertback(z_data_type, inz),)
if tt is not None:
return_data += (_convertback(t_data_type, intime),)
return return_data
def itransform(
self,
points: Any,
switch: bool = False,
time_3rd: bool = False,
radians: bool = False,
errcheck: bool = False,
direction: TransformDirection | str = TransformDirection.FORWARD,
) -> Iterator[Iterable]:
"""
Iterator/generator version of the function pyproj.Transformer.transform.
See: :c:func:`proj_trans_generic`
.. versionadded:: 2.1.1 errcheck
.. versionadded:: 2.2.0 direction
Parameters
----------
points: list
List of point tuples.
switch: bool, default=False
If True x, y or lon,lat coordinates of points are switched to y, x
or lat, lon. Default is False.
time_3rd: bool, default=False
If the input coordinates are 3 dimensional and the 3rd dimension is time.
radians: bool, default=False
If True, will expect input data to be in radians and will return radians
if the projection is geographic. Otherwise, it uses degrees.
Ignored for pipeline transformations with pyproj 2,
but will work in pyproj 3.
errcheck: bool, default=False
If True, an exception is raised if the errors are found in the process.
If False, ``inf`` is returned for errors.
direction: pyproj.enums.TransformDirection, optional
The direction of the transform.
Default is :attr:`pyproj.enums.TransformDirection.FORWARD`.
Example
--------
>>> from pyproj import Transformer
>>> transformer = Transformer.from_crs(4326, 2100)
>>> points = [(22.95, 40.63), (22.81, 40.53), (23.51, 40.86)]
>>> for pt in transformer.itransform(points): '{:.3f} {:.3f}'.format(*pt)
'2221638.801 2637034.372'
'2212924.125 2619851.898'
'2238294.779 2703763.736'
>>> pipeline_str = (
... "+proj=pipeline +step +proj=longlat +ellps=WGS84 "
... "+step +proj=unitconvert +xy_in=rad +xy_out=deg"
... )
>>> pipe_trans = Transformer.from_pipeline(pipeline_str)
>>> for pt in pipe_trans.itransform([(2.1, 0.001)]):
... '{:.3f} {:.3f}'.format(*pt)
'2.100 0.001'
>>> transproj = Transformer.from_crs(
... {"proj":'geocent', "ellps":'WGS84', "datum":'WGS84'},
... "EPSG:4326",
... always_xy=True,
... )
>>> for pt in transproj.itransform(
... [(-2704026.010, -4253051.810, 3895878.820)],
... radians=True,
... ):
... '{:.3f} {:.3f} {:.3f}'.format(*pt)
'-2.137 0.661 -20.531'
>>> transprojr = Transformer.from_crs(
... "EPSG:4326",
... {"proj":'geocent', "ellps":'WGS84', "datum":'WGS84'},
... always_xy=True,
... )
>>> for pt in transprojr.itransform(
... [(-2.137, 0.661, -20.531)],
... radians=True
... ):
... '{:.3f} {:.3f} {:.3f}'.format(*pt)
'-2704214.394 -4254414.478 3894270.731'
>>> transproj_eq = Transformer.from_crs(
... 'EPSG:4326',
... '+proj=longlat +datum=WGS84 +no_defs +type=crs',
... always_xy=True,
... )
>>> for pt in transproj_eq.itransform([(-2.137, 0.661)]):
... '{:.3f} {:.3f}'.format(*pt)
'-2.137 0.661'
"""
point_it = iter(points) # point iterator
# get first point to check stride
try:
fst_pt = next(point_it)
except StopIteration:
raise ValueError("iterable must contain at least one point") from None
stride = len(fst_pt)
if stride not in (2, 3, 4):
raise ValueError("points can contain up to 4 coordinates")
if time_3rd and stride != 3:
raise ValueError("'time_3rd' is only valid for 3 coordinates.")
# create a coordinate sequence generator etc. x1,y1,z1,x2,y2,z2,....
# chain so the generator returns the first point that was already acquired
coord_gen = chain(
fst_pt, (coords[c] for coords in point_it for c in range(stride))
)
while True:
# create a temporary buffer storage for
# the next 64 points (64*stride*8 bytes)
buff = array("d", islice(coord_gen, 0, 64 * stride))
if len(buff) == 0:
break
self._transformer._transform_sequence(
stride,
buff,
switch=switch,
direction=direction,
time_3rd=time_3rd,
radians=radians,
errcheck=errcheck,
)
yield from zip(*([iter(buff)] * stride))
def transform_bounds(
self,
left: float,
bottom: float,
right: float,
top: float,
densify_pts: int = 21,
radians: bool = False,
errcheck: bool = False,
direction: TransformDirection | str = TransformDirection.FORWARD,
) -> tuple[float, float, float, float]:
"""
.. versionadded:: 3.1.0
See: :c:func:`proj_trans_bounds`
Transform boundary densifying the edges to account for nonlinear
transformations along these edges and extracting the outermost bounds.
If the destination CRS is geographic and right < left then the bounds
crossed the antimeridian. In this scenario there are two polygons,
one on each side of the antimeridian. The first polygon should be
constructed with (left, bottom, 180, top) and the second with
(-180, bottom, top, right).
To construct the bounding polygons with shapely::
def bounding_polygon(left, bottom, right, top):
if right < left:
return shapely.geometry.MultiPolygon(
[
shapely.geometry.box(left, bottom, 180, top),
shapely.geometry.box(-180, bottom, right, top),
]
)
return shapely.geometry.box(left, bottom, right, top)
Parameters
----------
left: float
Minimum bounding coordinate of the first axis in source CRS
(or the target CRS if using the reverse direction).
bottom: float
Minimum bounding coordinate of the second axis in source CRS.
(or the target CRS if using the reverse direction).
right: float
Maximum bounding coordinate of the first axis in source CRS.
(or the target CRS if using the reverse direction).
top: float
Maximum bounding coordinate of the second axis in source CRS.
(or the target CRS if using the reverse direction).
densify_points: uint, default=21
Number of points to add to each edge to account for nonlinear edges
produced by the transform process. Large numbers will produce worse
performance.
radians: bool, default=False
If True, will expect input data to be in radians and will return radians
if the projection is geographic. Otherwise, it uses degrees.
errcheck: bool, default=False
If True, an exception is raised if the errors are found in the process.
If False, ``inf`` is returned for errors.
direction: pyproj.enums.TransformDirection, optional
The direction of the transform.
Default is :attr:`pyproj.enums.TransformDirection.FORWARD`.
Returns
-------
left, bottom, right, top: float
Outermost coordinates in target coordinate reference system.
"""
return self._transformer._transform_bounds(
left=left,
bottom=bottom,
right=right,
top=top,
densify_pts=densify_pts,
radians=radians,
errcheck=errcheck,
direction=direction,
)
def to_proj4(
self,
version: ProjVersion | str = ProjVersion.PROJ_5,
pretty: bool = False,
) -> str:
"""
Convert the projection to a PROJ string.
.. versionadded:: 3.1.0
Parameters
----------
version: pyproj.enums.ProjVersion
The version of the PROJ string output.
Default is :attr:`pyproj.enums.ProjVersion.PROJ_5`.
pretty: bool, default=False
If True, it will set the output to be a multiline string.
Returns
-------
str:
The PROJ string.
"""
return self._transformer.to_proj4(version=version, pretty=pretty)
def to_wkt(
self,
version: WktVersion | str = WktVersion.WKT2_2019,
pretty: bool = False,
) -> str:
"""
Convert the projection to a WKT string.
Version options:
- WKT2_2015
- WKT2_2015_SIMPLIFIED
- WKT2_2019
- WKT2_2019_SIMPLIFIED
- WKT1_GDAL
- WKT1_ESRI
Parameters
----------
version: pyproj.enums.WktVersion, optional
The version of the WKT output.
Default is :attr:`pyproj.enums.WktVersion.WKT2_2019`.
pretty: bool, default=False
If True, it will set the output to be a multiline string.
Returns
-------
str:
The WKT string.
"""
return self._transformer.to_wkt(version=version, pretty=pretty)
def to_json(self, pretty: bool = False, indentation: int = 2) -> str:
"""
Convert the projection to a JSON string.
.. versionadded:: 2.4.0
Parameters
----------
pretty: bool, default=False
If True, it will set the output to be a multiline string.
indentation: int, default=2
If pretty is True, it will set the width of the indentation.
Returns
-------
str:
The JSON string.
"""
return self._transformer.to_json(pretty=pretty, indentation=indentation)
def to_json_dict(self) -> dict:
"""
Convert the projection to a JSON dictionary.
.. versionadded:: 2.4.0
Returns
-------
dict:
The JSON dictionary.
"""
return self._transformer.to_json_dict()
def __str__(self) -> str:
return self.definition
def __repr__(self) -> str:
return (
f"<{self._transformer.type_name}: {self.name}>\n"
f"Description: {self.description}\n"
f"Area of Use:\n{self.area_of_use or '- undefined'}"
)
def __eq__(self, other: object) -> bool:
if not isinstance(other, Transformer):
return False
return self._transformer.__eq__(other._transformer)
def is_exact_same(self, other: Any) -> bool:
"""
Check if the Transformer objects are the exact same.
If it is not a Transformer, then it returns False.
Parameters
----------
other: Any
Returns
-------
bool
"""
if not isinstance(other, Transformer):
return False
return self._transformer.is_exact_same(other._transformer)
def transform( # pylint: disable=invalid-name
p1: Any,
p2: Any,
x: Any,
y: Any,
z: Any | None = None,
tt: Any | None = None,
radians: bool = False,
errcheck: bool = False,
always_xy: bool = False,
):
"""
.. versionadded:: 2.2.0 always_xy
.. deprecated:: 2.6.1
This function is deprecated. See: :ref:`upgrade_transformer`
x2, y2, z2 = transform(p1, p2, x1, y1, z1)
Transform points between two coordinate systems defined by the
Proj instances p1 and p2.
The points x1,y1,z1 in the coordinate system defined by p1 are
transformed to x2,y2,z2 in the coordinate system defined by p2.
z1 is optional, if it is not set it is assumed to be zero (and
only x2 and y2 are returned). If the optional keyword
'radians' is True (default is False), then all input and
output coordinates will be in radians instead of the default
of degrees for geographic input/output projections.
If the optional keyword 'errcheck' is set to True an
exception is raised if the transformation is
invalid. By default errcheck=False and ``inf`` is returned for an
invalid transformation (and no exception is raised).
If `always_xy` is toggled, the transform method will accept as
input and return as output coordinates using the traditional GIS order,
that is longitude, latitude for geographic CRS and easting,
northing for most projected CRS.
In addition to converting between cartographic and geographic
projection coordinates, this function can take care of datum
shifts (which cannot be done using the __call__ method of the
Proj instances). It also allows for one of the coordinate
systems to be geographic (proj = 'latlong').
x,y and z can be numpy or regular python arrays, python
lists/tuples or scalars. Arrays are fastest. For projections in
geocentric coordinates, values of x and y are given in meters.
z is always meters.
"""
warnings.warn(
(
"This function is deprecated. "
"See: https://pyproj4.github.io/pyproj/stable/"
"gotchas.html#upgrading-to-pyproj-2-from-pyproj-1"
),
FutureWarning,
stacklevel=2,
)
return Transformer.from_proj(p1, p2, always_xy=always_xy).transform(
xx=x, yy=y, zz=z, tt=tt, radians=radians, errcheck=errcheck
)
def itransform( # pylint: disable=invalid-name
p1: Any,
p2: Any,
points: Iterable[Iterable],
switch: bool = False,
time_3rd: bool = False,
radians: bool = False,
errcheck: bool = False,
always_xy: bool = False,
):
"""
.. versionadded:: 2.2.0 always_xy
.. deprecated:: 2.6.1
This function is deprecated. See: :ref:`upgrade_transformer`
points2 = itransform(p1, p2, points1)
Iterator/generator version of the function pyproj.transform.
Transform points between two coordinate systems defined by the
Proj instances p1 and p2. This function can be used as an alternative
to pyproj.transform when there is a need to transform a big number of
coordinates lazily, for example when reading and processing from a file.
Points1 is an iterable/generator of coordinates x1,y1(,z1) or lon1,lat1(,z1)
in the coordinate system defined by p1. Points2 is an iterator that returns tuples
of x2,y2(,z2) or lon2,lat2(,z2) coordinates in the coordinate system defined by p2.
z are provided optionally.
Points1 can be:
- a tuple/list of tuples/lists i.e. for 2d points: [(xi,yi),(xj,yj),....(xn,yn)]
- a Nx3 or Nx2 2d numpy array where N is the point number
- a generator of coordinates (xi,yi) for 2d points or (xi,yi,zi) for 3d
If optional keyword 'switch' is True (default is False) then x, y or lon,lat
coordinates of points are switched to y, x or lat, lon.
If the optional keyword 'radians' is True (default is False),
then all input and output coordinates will be in radians instead
of the default of degrees for geographic input/output projections.
If the optional keyword 'errcheck' is set to True an
exception is raised if the transformation is
invalid. By default errcheck=False and ``inf`` is returned for an
invalid transformation (and no exception is raised).
If `always_xy` is toggled, the transform method will accept as
input and return as output coordinates using the traditional GIS order,
that is longitude, latitude for geographic CRS and easting, northing
for most projected CRS.
Example usage:
>>> from pyproj import Proj, itransform
>>> # projection 1: WGS84
>>> # (defined by epsg code 4326)
>>> p1 = Proj('epsg:4326', preserve_units=False)
>>> # projection 2: GGRS87 / Greek Grid
>>> p2 = Proj('epsg:2100', preserve_units=False)
>>> # Three points with coordinates lon, lat in p1
>>> points = [(22.95, 40.63), (22.81, 40.53), (23.51, 40.86)]
>>> # transform this point to projection 2 coordinates.
>>> for pt in itransform(p1,p2,points, always_xy=True): '%6.3f %7.3f' % pt
'411050.470 4497928.574'
'399060.236 4486978.710'
'458553.243 4523045.485'
>>> for pt in itransform(4326, 4326, [(30, 60)]):
... '{:.0f} {:.0f}'.format(*pt)
'30 60'
"""
warnings.warn(
(
"This function is deprecated. "
"See: https://pyproj4.github.io/pyproj/stable/"
"gotchas.html#upgrading-to-pyproj-2-from-pyproj-1"
),
FutureWarning,
stacklevel=2,
)
return Transformer.from_proj(p1, p2, always_xy=always_xy).itransform(
points, switch=switch, time_3rd=time_3rd, radians=radians, errcheck=errcheck
)
|