Skip to content

Commit f867855

Browse files
authored
Merge pull request #166 from avsm/fix-fetch-embedding-coordinate-snapping
auto-snap coordinates to valid tile centers in fetch_embedding
2 parents 79651c3 + 4cbf01b commit f867855

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

geotessera/core.py

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
EMBEDDINGS_DIR_NAME,
1818
tile_to_geotiff_path,
1919
tile_to_zarr_path,
20+
tile_from_world,
2021
)
2122

2223
try:
@@ -1109,9 +1110,13 @@ def fetch_embedding(
11091110
) -> Tuple[np.ndarray, object, object]:
11101111
"""Fetch and dequantize a single embedding tile with CRS information.
11111112
1113+
Coordinates are automatically snapped to valid tile centers. Tiles are
1114+
0.1×0.1 degree squares centered at 0.05-degree offsets (e.g., 0.05, 0.15).
1115+
For example, coordinates (-120, 36) will be snapped to (-119.95, 36.05).
1116+
11121117
Args:
1113-
lon: Tile center longitude
1114-
lat: Tile center latitude
1118+
lon: Longitude (will be snapped to nearest tile center)
1119+
lat: Latitude (will be snapped to nearest tile center)
11151120
year: Year of embeddings
11161121
progress_callback: Optional callback for download progress
11171122
refresh: If True, force re-download even if local files exist in embeddings_dir
@@ -1122,6 +1127,13 @@ def fetch_embedding(
11221127
- crs: CRS object from rasterio (coordinate reference system)
11231128
- transform: Affine transform from rasterio
11241129
"""
1130+
# Snap coordinates to valid tile centers
1131+
tile_lon, tile_lat = tile_from_world(lon, lat)
1132+
if tile_lon != lon or tile_lat != lat:
1133+
self.logger.debug(
1134+
f"Snapped coordinates ({lon}, {lat}) to tile center ({tile_lon}, {tile_lat})"
1135+
)
1136+
lon, lat = tile_lon, tile_lat
11251137

11261138
# Fetch the files using coordinates
11271139
embedding_file = self.registry.fetch(
@@ -1164,9 +1176,12 @@ def download_tile(
11641176
) -> bool:
11651177
"""Download a single tile and save it to embeddings_dir.
11661178
1179+
Coordinates are automatically snapped to valid tile centers. Tiles are
1180+
0.1×0.1 degree squares centered at 0.05-degree offsets (e.g., 0.05, 0.15).
1181+
11671182
Args:
1168-
lon: Tile center longitude
1169-
lat: Tile center latitude
1183+
lon: Longitude (will be snapped to nearest tile center)
1184+
lat: Latitude (will be snapped to nearest tile center)
11701185
year: Year of embeddings
11711186
progress_callback: Optional callback for download progress
11721187
@@ -1177,7 +1192,18 @@ def download_tile(
11771192
>>> gt = GeoTessera(embeddings_dir="./embeddings")
11781193
>>> gt.download_tile(lon=0.15, lat=52.05, year=2024)
11791194
True
1195+
>>> # Arbitrary coordinates are snapped to tile center
1196+
>>> gt.download_tile(lon=-120, lat=36, year=2024) # snaps to (-119.95, 36.05)
1197+
True
11801198
"""
1199+
# Snap coordinates to valid tile centers
1200+
tile_lon, tile_lat = tile_from_world(lon, lat)
1201+
if tile_lon != lon or tile_lat != lat:
1202+
self.logger.debug(
1203+
f"Snapped coordinates ({lon}, {lat}) to tile center ({tile_lon}, {tile_lat})"
1204+
)
1205+
lon, lat = tile_lon, tile_lat
1206+
11811207
try:
11821208
# Download files directly to embeddings_dir (using refresh=True to force download)
11831209
# fetch() handles creating directory structure and saving to correct locations

0 commit comments

Comments
 (0)