diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/src/django_oapif/crs_utils.py b/src/django_oapif/crs_utils.py new file mode 100644 index 00000000..4720ff9b --- /dev/null +++ b/src/django_oapif/crs_utils.py @@ -0,0 +1,56 @@ +import re + +from pyproj import CRS + +# taken from https://github.com/geopython/pygeoapi/blob/953b6fa74d2ce292d8f566c4f4d3bcb4161d6e95/pygeoapi/util.py#L90 + + +CRS_AUTHORITY = [ + "AUTO", + "EPSG", + "OGC", +] +CRS_URI_PATTERN = re.compile( + ( + rf"^http://www.opengis\.net/def/crs/" + rf"(?P{'|'.join(CRS_AUTHORITY)})/" + rf"[\d|\.]+?/(?P\w+?)$" + ) +) + + +def get_crs_from_uri(uri: str) -> CRS: + """ + Get a `pyproj.CRS` instance from a CRS URI. + Author: @MTachon + + :param uri: Uniform resource identifier of the coordinate + reference system. + :type uri: str + + :raises `CRSError`: Error raised if no CRS could be identified from the + URI. + + :returns: `pyproj.CRS` instance matching the input URI. + :rtype: `pyproj.CRS` + """ + + try: + crs = CRS.from_authority(*CRS_URI_PATTERN.search(uri).groups()) + except RuntimeError: + msg = ( + f"CRS could not be identified from URI {uri!r} " + f"(Authority: {CRS_URI_PATTERN.search(uri).group('auth')!r}, " + f"Code: {CRS_URI_PATTERN.search(uri).group('code')!r})." + ) + raise RuntimeError(msg) + except AttributeError: + msg = ( + f"CRS could not be identified from URI {uri!r}. CRS URIs must " + "follow the format " + "'http://www.opengis.net/def/crs/{authority}/{version}/{code}' " + "(see https://docs.opengeospatial.org/is/18-058r1/18-058r1.html#crs-overview)." # noqa + ) + raise AttributeError(msg) + else: + return crs diff --git a/src/django_oapif/decorators.py b/src/django_oapif/decorators.py index 97aa92f1..78048fa2 100644 --- a/src/django_oapif/decorators.py +++ b/src/django_oapif/decorators.py @@ -1,4 +1,3 @@ -import re from os import getenv from typing import Any, Callable, Dict, Optional @@ -12,61 +11,9 @@ from django_oapif.mixins import OAPIFDescribeModelViewSetMixin from django_oapif.urls import oapif_router +from .crs_utils import get_crs_from_uri from .filters import BboxFilterBackend -# taken from https://github.com/geopython/pygeoapi/blob/953b6fa74d2ce292d8f566c4f4d3bcb4161d6e95/pygeoapi/util.py#L90 -CRS_AUTHORITY = [ - "AUTO", - "EPSG", - "OGC", -] - -CRS_URI_PATTERN = re.compile( - ( - rf"^http://www.opengis\.net/def/crs/" - rf"(?P{'|'.join(CRS_AUTHORITY)})/" - rf"[\d|\.]+?/(?P\w+?)$" - ) -) - - -# taken from -def get_crs_from_uri(uri: str) -> CRS: - """ - Get a `pyproj.CRS` instance from a CRS URI. - Author: @MTachon - - :param uri: Uniform resource identifier of the coordinate - reference system. - :type uri: str - - :raises `CRSError`: Error raised if no CRS could be identified from the - URI. - - :returns: `pyproj.CRS` instance matching the input URI. - :rtype: `pyproj.CRS` - """ - - try: - crs = CRS.from_authority(*CRS_URI_PATTERN.search(uri).groups()) - except RuntimeError: - msg = ( - f"CRS could not be identified from URI {uri!r} " - f"(Authority: {CRS_URI_PATTERN.search(uri).group('auth')!r}, " - f"Code: {CRS_URI_PATTERN.search(uri).group('code')!r})." - ) - raise RuntimeError(msg) - except AttributeError: - msg = ( - f"CRS could not be identified from URI {uri!r}. CRS URIs must " - "follow the format " - "'http://www.opengis.net/def/crs/{authority}/{version}/{code}' " - "(see https://docs.opengeospatial.org/is/18-058r1/18-058r1.html#crs-overview)." # noqa - ) - raise AttributeError(msg) - else: - return crs - def register_oapif_viewset( key: Optional[str] = None,