|
| 1 | +from enum import Enum |
| 2 | +from typing import Optional, List |
| 3 | + |
| 4 | +from pydantic import BaseModel, validator |
| 5 | + |
| 6 | +from ..geometry import Point |
| 7 | +from .base_data import BaseData |
| 8 | + |
| 9 | + |
| 10 | +class EPSG(Enum): |
| 11 | + """ Provides the EPSG for tiled image assets that are currently supported. |
| 12 | + |
| 13 | + SIMPLEPIXEL is Simple that can be used to obtain the pixel space coordinates |
| 14 | +
|
| 15 | + >>> epsg = EPSG() |
| 16 | + """ |
| 17 | + SIMPLEPIXEL = 1 |
| 18 | + EPSG4326 = 2 |
| 19 | + EPSG3857 = 3 |
| 20 | + EPSG3395 = 4 |
| 21 | + |
| 22 | + |
| 23 | +class TiledBounds(BaseModel): |
| 24 | + """ Bounds for a tiled image asset related to the relevant epsg. |
| 25 | +
|
| 26 | + Bounds should be Point objects |
| 27 | +
|
| 28 | + If version of asset is 2, these should be [[lat,lng],[lat,lng]] |
| 29 | + If version of asset is 1, these should be [[lng,lat]],[lng,lat]] |
| 30 | +
|
| 31 | + >>> bounds = TiledBounds( |
| 32 | + epsg=EPSG.4326, |
| 33 | + bounds=[Point(x=0, y=0),Point(x=100, y=100)] |
| 34 | + ) |
| 35 | + """ |
| 36 | + epsg: EPSG |
| 37 | + bounds: List[Point] |
| 38 | + |
| 39 | + @validator('bounds') |
| 40 | + def validate_bounds(cls, bounds): |
| 41 | + first_bound = bounds[0] |
| 42 | + second_bound = bounds[1] |
| 43 | + |
| 44 | + if first_bound == second_bound: |
| 45 | + raise AssertionError(f"Bounds cannot be equal, contains {bounds}") |
| 46 | + return bounds |
| 47 | + |
| 48 | + |
| 49 | +class TileLayer(BaseModel): |
| 50 | + """ Url that contains the tile layer. Must be in the format: |
| 51 | +
|
| 52 | + https://c.tile.openstreetmap.org/{z}/{x}/{y}.png |
| 53 | +
|
| 54 | + >>> layer = TileLayer( |
| 55 | + url="https://c.tile.openstreetmap.org/{z}/{x}/{y}.png", |
| 56 | + name="slippy map tile" |
| 57 | + ) |
| 58 | + """ |
| 59 | + url: str |
| 60 | + name: Optional[str] = "default" |
| 61 | + |
| 62 | + @validator('url') |
| 63 | + def validate_url(cls, url): |
| 64 | + xyz_format = "/{z}/{x}/{y}" |
| 65 | + if xyz_format not in url: |
| 66 | + raise AssertionError(f"{url} needs to contain {xyz_format}") |
| 67 | + return url |
| 68 | + |
| 69 | + |
| 70 | +class TiledImageData(BaseData): |
| 71 | + """ Represents tiled imagery |
| 72 | +
|
| 73 | + If specified version is 2, converts bounds from [lng,lat] to [lat,lng] |
| 74 | + """ |
| 75 | + tile_layer: TileLayer |
| 76 | + alternative_layers: list[TileLayer] |
| 77 | + tile_bounds: TiledBounds |
| 78 | + zoom_levels: List[int] |
| 79 | + max_native_zoom: int = zoom_levels[1] |
| 80 | + tile_size: Optional[int] |
| 81 | + version: int = 2 |
| 82 | + |
| 83 | + # @property #TODO |
| 84 | + # def value(self): |
| 85 | + # Return self._as_raster(self.min_zoom).value() |
| 86 | + |
| 87 | + # def _as_raster(zoom): #TODO |
| 88 | + # stitched together tiles as a RasterData object |
| 89 | + # Return result |
| 90 | + |
| 91 | + @property |
| 92 | + def tile_layer_url(self): |
| 93 | + return self.tile_layer.url |
| 94 | + |
| 95 | + @property |
| 96 | + def bounds(self): |
| 97 | + return self.tile_bounds.bounds |
| 98 | + |
| 99 | + @property |
| 100 | + def epsg(self): |
| 101 | + return self.tile_bounds.epsg |
| 102 | + |
| 103 | + @validator('zoom_levels') |
| 104 | + def validate_zooms(cls, zoom_levels): |
| 105 | + if len(zoom_levels) != 2: |
| 106 | + raise AssertionError( |
| 107 | + f"zoom_levels should only contain min and max, found {len(zoom_levels)}" |
| 108 | + ) |
0 commit comments