|
3 | 3 | # Authors: Tom Kralidis <tomkralidis@gmail.com> |
4 | 4 | # Just van den Broecke <justb4@gmail.com> |
5 | 5 | # |
| 6 | +# Copyright (c) 2026 Joana Simoes |
6 | 7 | # Copyright (c) 2025 Tom Kralidis |
7 | 8 | # Copyright (c) 2025 Just van den Broecke |
8 | 9 | # |
|
47 | 48 | shape as geojson_to_geom, |
48 | 49 | mapping as geom_to_geojson |
49 | 50 | ) |
| 51 | +from urllib.parse import urlparse |
50 | 52 |
|
51 | 53 | LOGGER = logging.getLogger(__name__) |
52 | 54 |
|
@@ -120,6 +122,96 @@ def get_supported_crs_list( |
120 | 122 | return supported_crs_list |
121 | 123 |
|
122 | 124 |
|
| 125 | +def get_uri(crs: str) -> str: |
| 126 | + """ |
| 127 | + Parse a uri from a uri or a curie |
| 128 | +
|
| 129 | + :param crs: Uniform resource identifier of the coordinate |
| 130 | + reference system. In accordance with |
| 131 | + https://docs.ogc.org/pol/09-048r5.html#_naming_rule |
| 132 | + Or a safe, or unsafe curie |
| 133 | + https://docs.ogc.org/DRAFTS/20-024.html#conventions-curies |
| 134 | +
|
| 135 | + :raises `CRSError`: Error raised if no CRS could be identified from the |
| 136 | + URI. |
| 137 | +
|
| 138 | + :returns: `crs uri` matching the input CRS. |
| 139 | + """ |
| 140 | + |
| 141 | + try: |
| 142 | + crs = crs.lower() |
| 143 | + result = urlparse(crs) |
| 144 | + |
| 145 | + # If it is a uri, check if it is valid |
| 146 | + LOGGER.debug(f'Attempt to parse a uri: {crs}') |
| 147 | + |
| 148 | + if result.scheme not in ['http', 'https']: |
| 149 | + raise CRSError('Invalid uri scheme') |
| 150 | + if result.netloc is None or result.netloc != 'www.opengis.net': |
| 151 | + raise CRSError('Invalid uri prefix') |
| 152 | + |
| 153 | + path_el = [p for p in result.path.split('/') if p] |
| 154 | + |
| 155 | + # Check if the path uri contains the relevant fragments |
| 156 | + if len(path_el |
| 157 | + ) != 5 or path_el[0] != 'def' or path_el[1] != 'crs' or path_el[2] not in ['epsg', 'ogc']: # noqa |
| 158 | + raise CRSError('Invalid uri fragments') |
| 159 | + |
| 160 | + return crs |
| 161 | + |
| 162 | + except CRSError: |
| 163 | + try: |
| 164 | + # Parse safe CURIE |
| 165 | + curie = crs.strip('[]') |
| 166 | + LOGGER.debug(f'Attempt to parse a curie: {curie}') |
| 167 | + |
| 168 | + # We support all EPSG codes and CRS84 |
| 169 | + if curie not in ['crs:84', 'ogc:crs84']: |
| 170 | + [curie_auth, curie_code] = curie.split(':') |
| 171 | + if len(curie.split(':')) != 2 or (curie_auth != 'epsg'): |
| 172 | + raise CRSError('Unsupported CRS') |
| 173 | + |
| 174 | + return f'http://www.opengis.net/def/crs/EPSG/0/{curie_code}' |
| 175 | + else: |
| 176 | + return 'http://www.opengis.net/def/crs/OGC/1.3/CRS84' |
| 177 | + |
| 178 | + except CRSError as e: |
| 179 | + return e |
| 180 | + |
| 181 | + |
| 182 | +def get_curie(crs: str) -> str: |
| 183 | + """ |
| 184 | + Get a WMS compatible CRS CURIE from a uri |
| 185 | +
|
| 186 | + :param crs: Uniform resource identifier of the coordinate |
| 187 | + reference system. In accordance with |
| 188 | + https://docs.ogc.org/pol/09-048r5.html#_naming_rule |
| 189 | +
|
| 190 | + :raises `CRSError`: Error raised if no CRS could be identified from the |
| 191 | + URI. |
| 192 | +
|
| 193 | + :returns: `WMS CURIE` matching the input uri. |
| 194 | + """ |
| 195 | + |
| 196 | + try: |
| 197 | + if not crs.startswith(("http://", "https://")): |
| 198 | + raise CRSError('Not an uri') |
| 199 | + |
| 200 | + crs = crs.lower() |
| 201 | + path_el = [p for p in crs.split('/') if p] |
| 202 | + |
| 203 | + # We support all EPSG codes and CRS84 |
| 204 | + if path_el[4] == 'epsg': |
| 205 | + return f'EPSG:{path_el[6]}' |
| 206 | + elif path_el[6] != 'crs84': |
| 207 | + raise CRSError('Unsupported CRS') |
| 208 | + |
| 209 | + return 'CRS:84' |
| 210 | + |
| 211 | + except CRSError as e: |
| 212 | + return e |
| 213 | + |
| 214 | + |
123 | 215 | def get_crs(crs: Union[str, pyproj.CRS]) -> pyproj.CRS: |
124 | 216 | """ |
125 | 217 | Get a `pyproj.CRS` instance from a CRS. |
|
0 commit comments