Skip to content

Commit a8aac7e

Browse files
committed
refactor: epsg codes enum
1 parent c205e0d commit a8aac7e

9 files changed

Lines changed: 50 additions & 41 deletions

File tree

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from enum import Enum
2+
3+
4+
class EpsgNumber(int, Enum):
5+
NZTM_2000 = 2193
6+
"""New Zealand Transverse Mercator 2000"""
7+
WGS_1984 = 4326
8+
"""World Geodetic System 1984"""
9+
CITM_2000 = 3793
10+
"""Chatham Islands Transverse Mercator 2000"""
11+
NZVD_2016 = 7839
12+
"""New Zealand Vertical Datum 2016"""

packages/topo-imagery-gdal/src/topo_imagery_gdal/gdal/gdal_commands.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
from decimal import Decimal
44

55
from linz_logger import get_log
6+
from topo_imagery_common.epsg import EpsgNumber
67
from topo_imagery_gdal.gdal.gdal_bands import get_gdal_band_offset
7-
from topo_imagery_gdal.gdal.gdal_helper import EpsgNumber
88
from topo_imagery_gdal.gdal.gdal_presets import (
99
BASE_COG,
1010
COMPRESS_LZW,
@@ -101,7 +101,7 @@ def get_cutline_command(cutline: str | None) -> list[str]:
101101
def get_build_vrt_command(
102102
files: list[str],
103103
output: str = "output.vrt",
104-
epsg: int = 2193,
104+
epsg: int = EpsgNumber.NZTM_2000,
105105
add_alpha: bool = False,
106106
resolution: list[Decimal] | None = None,
107107
) -> list[str]:

packages/topo-imagery-gdal/src/topo_imagery_gdal/gdal/gdal_helper.py

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import json
22
import os
33
import subprocess
4-
from enum import Enum
54
from shutil import rmtree
65
from tempfile import mkdtemp
76
from typing import cast
@@ -10,6 +9,7 @@
109
from topo_imagery_common.aws.aws_helper import is_s3
1110
from topo_imagery_common.files.files_helper import get_file_name_from_path
1211
from topo_imagery_common.files.fs import copy
12+
from topo_imagery_common.epsg import EpsgNumber
1313
from topo_imagery_common.log.time_helper import time_in_ms
1414
from topo_imagery_gdal.gdal.gdalinfo import GdalInfo
1515

@@ -18,13 +18,6 @@ class GDALExecutionException(Exception):
1818
pass
1919

2020

21-
class EpsgNumber(int, Enum):
22-
NZTM_2000 = 2193
23-
"""New Zealand Transverse Mercator 2000"""
24-
WGS_1984 = 4326
25-
"""World Geodetic System 1984"""
26-
27-
2821
def get_vfs_path(path: str) -> str:
2922
"""Make the path as a GDAL Virtual File Systems path.
3023

packages/topo-imagery-gdal/src/topo_imagery_gdal/standardising.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
from tifffile import TiffFile
1212
from topo_imagery_common.aws.aws_helper import is_s3
1313
from topo_imagery_common.cli.cli_helper import TileFiles
14+
from topo_imagery_common.epsg import EpsgNumber
1415
from topo_imagery_common.files.files_helper import ContentType, is_tiff
1516
from topo_imagery_common.files.fs import exists, read, write, write_all, write_sidecars
1617
from topo_imagery_common.log.time_helper import time_in_ms
@@ -211,7 +212,7 @@ def standardising(
211212
def create_vrt(
212213
source_tiffs: list[str],
213214
target_path: str,
214-
epsg: int = 2193,
215+
epsg: int = EpsgNumber.NZTM_2000,
215216
add_alpha: bool = False,
216217
resolution: list[Decimal] | None = None,
217218
) -> str:

packages/topo-imagery-gdal/src/topo_imagery_gdal/tile/tile_index.py

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import re
22
from typing import NamedTuple
33

4+
from topo_imagery_common.epsg import EpsgNumber
45
from topo_imagery_gdal.tile.util import charcodeat
56

67
SHEET_WIDTH = 24_000
@@ -16,9 +17,9 @@
1617
CHAR_A = charcodeat("A", 0)
1718
CHAR_S = charcodeat("S", 0)
1819

19-
MAINLAND_EPSG = 2193
20+
MAINLAND_EPSG = EpsgNumber.NZTM_2000
2021
""" EPSG code of the mainland NZTM50 mapsheet grid (NZGD2000 / NZTM2000) """
21-
CHATHAM_EPSG = 3793
22+
CHATHAM_EPSG = EpsgNumber.CITM_2000
2223
""" EPSG code of the Chatham Islands mapsheet grid (NZGD2000 / CITM2000) """
2324

2425

@@ -63,41 +64,21 @@ class Bounds(NamedTuple):
6364
"CI06": Point(x=3_506_000, y=5_104_000), # Pitt Island (Rangiauria)
6465
}
6566

66-
67-
def get_chatham_mapsheet_offset(sheet_code: str) -> Point:
68-
"""Look up the origin point for a Chatham Islands mapsheet code.
69-
70-
Args:
71-
sheet_code: Chatham Islands topo 50 map sheet code eg "CI06"
72-
73-
Returns:
74-
Point: The top left point of the mapsheet, in EPSG:3793
75-
76-
Example:
77-
>>> get_chatham_mapsheet_offset("CI06")
78-
Point(x=3506000, y=5104000)
79-
"""
80-
origin = CHATHAM_SHEET_ORIGINS.get(sheet_code[:4])
81-
if origin is None:
82-
raise ValueError(f"Unknown Chatham Islands map sheet: {sheet_code}; known sheets: {sorted(CHATHAM_SHEET_ORIGINS)}")
83-
return origin
84-
85-
8667
def get_bounds_from_name(tile_name: str, target_epsg: int = MAINLAND_EPSG) -> Bounds:
8768
"""Get the origin coordinates and size of the tile from its name.
8869
8970
Args:
9071
tile_name: the tile name as `sheetCode_gridSize_tileId`
91-
target_epsg: EPSG code of the mapsheet grid the tile is named against; only
72+
target_epsg: EPSG code of the mapsheet grid for the tile.
9273
`MAINLAND_EPSG` (2193) and `CHATHAM_EPSG` (3793) are supported
9374
9475
Returns:
9576
a `Bounds` object
9677
"""
97-
if target_epsg == CHATHAM_EPSG:
98-
get_offset = get_chatham_mapsheet_offset
99-
elif target_epsg == MAINLAND_EPSG:
78+
if target_epsg == MAINLAND_EPSG:
10079
get_offset = get_mapsheet_offset
80+
elif target_epsg == CHATHAM_EPSG:
81+
get_offset = get_chatham_mapsheet_offset
10182
else:
10283
raise ValueError(
10384
f"Unsupported target EPSG:{target_epsg} for mapsheet lookup; supported: {MAINLAND_EPSG}, {CHATHAM_EPSG}"
@@ -169,6 +150,26 @@ def get_mapsheet_offset(sheet_code: str) -> Point:
169150
return Point(x=SHEET_WIDTH * x + SHEET_ORIGIN_LEFT, y=SHEET_ORIGIN_TOP - SHEET_HEIGHT * y)
170151

171152

153+
def get_chatham_mapsheet_offset(sheet_code: str) -> Point:
154+
"""Look up the origin point for a Chatham Islands mapsheet code.
155+
156+
Args:
157+
sheet_code: Chatham Islands topo 50 map sheet code eg "CI06"
158+
159+
Returns:
160+
Point: The top left point of the mapsheet, in EPSG:3793
161+
162+
Example:
163+
>>> get_chatham_mapsheet_offset("CI06")
164+
Point(x=3506000, y=5104000)
165+
"""
166+
origin = CHATHAM_SHEET_ORIGINS.get(sheet_code[:4])
167+
if origin is None:
168+
raise ValueError(f"Unknown Chatham Islands map sheet: {sheet_code}. Known sheets: {sorted(CHATHAM_SHEET_ORIGINS)}")
169+
return origin
170+
171+
172+
172173
def get_tile_offset(grid_size: int, x: int, y: int) -> Bounds:
173174
"""Get the tile offset from its coordinate and the grid size
174175

packages/topo-imagery-gdal/test/gdal/gdal_commands_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
import pytest
44
from pytest_subtests import SubTests
5+
from topo_imagery_common.epsg import EpsgNumber
56
from topo_imagery_gdal.gdal.gdal_commands import (
67
get_buffer_distance,
78
get_cutline_command,
89
get_footprint_command,
910
get_gdal_command,
1011
)
11-
from topo_imagery_gdal.gdal.gdal_helper import EpsgNumber
1212
from topo_imagery_gdal.gdal.gdal_presets import CompressionPreset, HillshadePreset
1313

1414

packages/topo-imagery-gdal/test/tile/tile_index_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from typing import Any
22

3-
# Values transcribed from https://data.linz.govt.nz/layer/50089-nz-chatham-island-linz-map-sheets-topo-150k/
3+
# Values source: https://data.linz.govt.nz/layer/50089-nz-chatham-island-linz-map-sheets-topo-150k/
44
CHATHAM_SHEET_DATA: list[dict[Any, Any]] = [
55
{"origin": {"x": 3458000, "y": 5176000}, "code": "CI01"},
66
{"origin": {"x": 3482000, "y": 5176000}, "code": "CI02"},

scripts/generate_hillshade.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from topo_imagery_common.cli.cli_helper import InputParameterError, TileFiles, load_input_files
1010
from topo_imagery_common.cli.common_args import CommonArgumentParser
1111
from topo_imagery_common.datetimes import RFC_3339_DATETIME_FORMAT
12+
from topo_imagery_common.epsg import EpsgNumber
1213
from topo_imagery_common.files.files_helper import SUFFIX_JSON, ContentType, is_tiff
1314
from topo_imagery_common.files.fs import exists, read, write, write_all
1415
from topo_imagery_common.log.time_helper import time_in_ms
@@ -112,7 +113,7 @@ def create_hillshade(
112113

113114
# COGify the hillshade output, using ZSTD compression
114115
run_gdal(
115-
get_gdal_command(CompressionPreset.DEM_ZSTD.value, 2193),
116+
get_gdal_command(CompressionPreset.DEM_ZSTD.value, EpsgNumber.NZTM_2000),
116117
input_file=hillshade_working_path,
117118
output_file=hillshade_cog_working_path,
118119
)

scripts/pdal/pdal_commands.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from linz_logger import get_log
77
from topo_imagery_common.aws.aws_helper import is_s3
8+
from topo_imagery_common.epsg import EpsgNumber
89
from topo_imagery_common.files.fs import copy
910
from topo_imagery_common.log.time_helper import time_in_ms
1011

@@ -53,7 +54,7 @@ def get_pdal_command(command: str, options: list[str]) -> list[str]:
5354
pdal_translate_add_proj_command = get_pdal_command(
5455
"translate",
5556
[
56-
"--readers.las.spatialreference=EPSG:2193+7839",
57+
f"--readers.las.spatialreference=EPSG:{EpsgNumber.NZTM_2000.value}+{EpsgNumber.NZVD_2016.value}",
5758
"--writers.las.filesource_id=0",
5859
"--writers.las.forward=all",
5960
],

0 commit comments

Comments
 (0)