Skip to content

Commit

Permalink
move code to crs_utils (#84)
Browse files Browse the repository at this point in the history
* move code to crs_utils

* typo
  • Loading branch information
3nids authored Jun 2, 2023
1 parent 89bfaaa commit e17c242
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 54 deletions.
Empty file added src/__init__.py
Empty file.
56 changes: 56 additions & 0 deletions src/django_oapif/crs_utils.py
Original file line number Diff line number Diff line change
@@ -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<auth>{'|'.join(CRS_AUTHORITY)})/"
rf"[\d|\.]+?/(?P<code>\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
55 changes: 1 addition & 54 deletions src/django_oapif/decorators.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import re
from os import getenv
from typing import Any, Callable, Dict, Optional

Expand All @@ -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<auth>{'|'.join(CRS_AUTHORITY)})/"
rf"[\d|\.]+?/(?P<code>\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,
Expand Down

0 comments on commit e17c242

Please sign in to comment.