Skip to content

Commit c00f1b6

Browse files
authored
Merge pull request #374 from Labelbox/geo-nb
update to tiled iamge mal notebook
2 parents 8a670fd + 2a45cf2 commit c00f1b6

File tree

3 files changed

+137
-383
lines changed

3 files changed

+137
-383
lines changed

examples/model_assisted_labeling/tiled_imagery_mal.ipynb

Lines changed: 92 additions & 378 deletions
Large diffs are not rendered by default.

labelbox/data/annotation_types/data/tiled_image.py

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ class TileLayer(BaseModel):
102102
url: str
103103
name: Optional[str] = "default"
104104

105+
def asdict(self):
106+
return {"tileLayerUrl": self.url, "name": self.name}
107+
105108
@validator('url')
106109
def validate_url(cls, url):
107110
xyz_format = "/{z}/{x}/{y}"
@@ -131,13 +134,34 @@ class TiledImageData(BaseData):
131134
"""
132135
tile_layer: TileLayer
133136
tile_bounds: TiledBounds
134-
alternative_layers: List[TileLayer] = None
137+
alternative_layers: List[TileLayer] = []
135138
zoom_levels: Tuple[int, int]
136139
max_native_zoom: Optional[int] = None
137140
tile_size: Optional[int] = DEFAULT_TMS_TILE_SIZE
138141
version: Optional[int] = 2
139142
multithread: bool = True
140143

144+
def __post_init__(self):
145+
if self.max_native_zoom is None:
146+
self.max_native_zoom = self.zoom_levels[0]
147+
148+
def asdict(self):
149+
return {
150+
"tileLayerUrl": self.tile_layer.url,
151+
"bounds": [[
152+
self.tile_bounds.bounds[0].x, self.tile_bounds.bounds[0].y
153+
], [self.tile_bounds.bounds[1].x, self.tile_bounds.bounds[1].y]],
154+
"minZoom": self.zoom_levels[0],
155+
"maxZoom": self.zoom_levels[1],
156+
"maxNativeZoom": self.max_native_zoom,
157+
"epsg": self.tile_bounds.epsg.name,
158+
"tileSize": self.tile_size,
159+
"alternativeLayers": [
160+
layer.asdict() for layer in self.alternative_layers
161+
],
162+
"version": self.version
163+
}
164+
141165
def as_raster_data(self,
142166
zoom: int = 0,
143167
max_tiles: int = 32,
@@ -149,9 +173,24 @@ def as_raster_data(self,
149173
"""
150174
if self.tile_bounds.epsg == EPSG.SIMPLEPIXEL:
151175
xstart, ystart, xend, yend = self._get_simple_image_params(zoom)
152-
176+
elif self.tile_bounds.epsg == EPSG.EPSG4326:
177+
xstart, ystart, xend, yend = self._get_3857_image_params(zoom)
153178
elif self.tile_bounds.epsg == EPSG.EPSG3857:
179+
#transform to 4326
180+
transformer = EPSGTransformer.create_geo_to_geo_transformer(
181+
EPSG.EPSG3857, EPSG.EPSG4326)
182+
self.tile_bounds.bounds = [
183+
transformer(self.tile_bounds.bounds[0]),
184+
transformer(self.tile_bounds.bounds[1])
185+
]
154186
xstart, ystart, xend, yend = self._get_3857_image_params(zoom)
187+
#transform back to 3857
188+
transformer = EPSGTransformer.create_geo_to_geo_transformer(
189+
EPSG.EPSG4326, EPSG.EPSG3857)
190+
self.tile_bounds.bounds = [
191+
transformer(self.tile_bounds.bounds[0]),
192+
transformer(self.tile_bounds.bounds[1])
193+
]
155194
else:
156195
raise ValueError(f"Unsupported epsg found: {self.tile_bounds.epsg}")
157196

@@ -202,8 +241,8 @@ def _get_3857_image_params(self, zoom) -> Tuple[float, float, float, float]:
202241
1].x, self.tile_bounds.bounds[0].x
203242

204243
# Convert to zoom 0 tile coordinates
205-
xstart, ystart = self._latlng_to_tile(lat_start, lng_start, zoom)
206-
xend, yend = self._latlng_to_tile(lat_end, lng_end, zoom)
244+
xstart, ystart = self._latlng_to_tile(lat_start, lng_start)
245+
xend, yend = self._latlng_to_tile(lat_end, lng_end)
207246

208247
# Make sure that the tiles are increasing in order
209248
xstart, xend = min(xstart, xend), max(xstart, xend)

labelbox/data/annotation_types/label.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
from pydantic import BaseModel, validator
55

66
import labelbox
7+
from labelbox.data.annotation_types.data.tiled_image import TiledImageData
78
from labelbox.schema import ontology
89
from .annotation import (ClassificationAnnotation, ObjectAnnotation,
910
VideoClassificationAnnotation, VideoObjectAnnotation)
@@ -35,7 +36,7 @@ class Label(BaseModel):
3536
extra: additional context
3637
"""
3738
uid: Optional[Cuid] = None
38-
data: Union[VideoData, ImageData, TextData]
39+
data: Union[VideoData, ImageData, TextData, TiledImageData]
3940
annotations: List[Union[ClassificationAnnotation, ObjectAnnotation,
4041
VideoObjectAnnotation,
4142
VideoClassificationAnnotation, ScalarMetric,

0 commit comments

Comments
 (0)