Skip to content

Commit 29600ba

Browse files
committed
update to file to simplify some code as well as play nicely with Pydantic without arbitrarily allowing random data types
1 parent c8bcf5c commit 29600ba

File tree

1 file changed

+22
-39
lines changed

1 file changed

+22
-39
lines changed

labelbox/data/annotation_types/data/tiled_image.py

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from enum import Enum
2-
from typing import Optional, List, Any, Dict
2+
from typing import Optional, List
33

4-
from pydantic import BaseModel, validator
4+
from pydantic import BaseModel, validator, conlist
55
import numpy as np
6-
from pyproj import Transformer, transform
6+
from pyproj import Transformer
77

88
from ..geometry import Point
99
from .base_data import BaseData
@@ -88,15 +88,11 @@ class TiledImageData(BaseData):
8888
tile_layer: TileLayer
8989
tile_bounds: TiledBounds
9090
alternative_layers: List[TileLayer] = None
91-
zoom_levels: List[int]
91+
zoom_levels: conlist(item_type=int, min_items=2, max_items=2)
9292
max_native_zoom: int = None
9393
tile_size: Optional[int]
9494
version: int = 2
9595

96-
def __post_init__(self):
97-
if self.max_native_zoom is None:
98-
self.max_native_zoom = zoom_levels[1]
99-
10096
#TODO: look further into Matt's code and how to reference the monorepo ?
10197
def _as_raster(zoom):
10298
# stitched together tiles as a RasterData object
@@ -105,58 +101,45 @@ def _as_raster(zoom):
105101
image_as_np = None
106102
return RasterData(arr=image_as_np)
107103

108-
@property #TODO
104+
#TODO
105+
@property
109106
def value(self) -> np.ndarray:
110107
return self._as_raster(self.min_zoom).value()
111108

112-
#TODO: maybe not necessary, can remove
113-
# @property
114-
# def tile_layer_url(self) -> str:
115-
# return self.tile_layer.url
116-
117-
#TODO: maybe not necessary, can remove
118-
# @property
119-
# def bounds(self) -> List[Point]:
120-
# return self.tile_bounds.bounds
121-
122-
#TODO: wondering to keep this or not since epsg may be something of interest
123-
# and they dont want to go through TiledImageData.tile_bounds.epsg?
124-
# then can call TiledimageData.epsg
125-
@property
126-
def epsg(self) -> EPSG:
127-
return self.tile_bounds.epsg
128-
129-
@validator('zoom_levels')
130-
def validate_zooms(cls, zoom_levels):
131-
if len(zoom_levels) != 2:
132-
raise AssertionError(
133-
f"zoom_levels should contain 2 values [min,max], found {len(zoom_levels)}"
134-
)
135-
136109

137110
#TODO: we will need to update the [data] package to also require pyproj
138111
class EPSGTransformer(BaseModel):
139-
"""Transformer class between different EPSG's. Useful when wanting to project
112+
"""Transformer class between different EPSG's. Useful when wanting to project
140113
in different formats.
141114
142115
Requires as input a Point object.
143116
"""
144-
class Config:
145-
arbitrary_types_allowed = True
117+
class ProjectionTransformer():
118+
"""Custom class to help represent a Transformer that will play
119+
nicely with Pydantic.
120+
"""
121+
@classmethod
122+
def __get_validators__(cls):
123+
yield cls.validate
124+
125+
@classmethod
126+
def validate(cls, v):
127+
return v
146128

147-
transform_function: Transformer = None
129+
transform_function: Optional[ProjectionTransformer] = None
148130

149-
def is_simple(self, epsg: EPSG) -> bool:
131+
def _is_simple(self, epsg: EPSG) -> bool:
150132
return epsg == EPSG.SIMPLEPIXEL
151133

152134
def geo_and_geo(self, src_epsg: EPSG, tgt_epsg: EPSG) -> None:
153-
if self.is_simple(src_epsg) or self.is_simple(tgt_epsg):
135+
if self._is_simple(src_epsg) or self._is_simple(tgt_epsg):
154136
raise Exception(
155137
f"Cannot be used for Simple transformations. Found {src_epsg} and {tgt_epsg}"
156138
)
157139
self.transform_function = Transformer.from_crs(src_epsg.value,
158140
tgt_epsg.value)
159141

142+
#TODO
160143
def geo_and_pixel(self, src_epsg, geojson):
161144
pass
162145

0 commit comments

Comments
 (0)