Skip to content

Commit 7a64a76

Browse files
author
frankfeng
committed
Fix coverage map bug
1 parent 718cc62 commit 7a64a76

File tree

1 file changed

+78
-65
lines changed

1 file changed

+78
-65
lines changed

geotessera/core.py

Lines changed: 78 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -238,77 +238,90 @@ def _ensure_block_loaded(self, year: int, lon: float, lat: float):
238238
# Update available embeddings cache
239239
self._parse_available_embeddings()
240240

241-
def _load_all_blocks(self):
242-
"""Load all available block registries to build complete embedding list.
241+
def _load_all_blocks(self):
242+
"""Load all available block registries to build complete embedding list.
243+
244+
This method is used when a complete listing of all embeddings is needed,
245+
such as for generating coverage maps. It parses the master registry to
246+
find all block files and loads them.
247+
"""
248+
try:
249+
cache_path = self._cache_dir if self._cache_dir else pooch.os_cache("geotessera")
250+
registry_file = Path(cache_path) / "registry.txt"
243251

244-
This method is used when a complete listing of all embeddings is needed,
245-
such as for generating coverage maps. It parses the master registry to
246-
find all block files and loads them.
247-
"""
248-
try:
249-
cache_path = self._cache_dir if self._cache_dir else pooch.os_cache("geotessera")
250-
registry_file = Path(cache_path) / "registry.txt"
251-
252-
if not registry_file.exists():
253-
print("Warning: Master registry not found")
254-
return
255-
256-
# Parse registry.txt to find all block registry files
257-
block_files = []
258-
with open(registry_file, 'r') as f:
259-
for line in f:
260-
line = line.strip()
261-
if line and not line.startswith('#'):
262-
parts = line.split(' ', 1)
263-
if len(parts) == 2:
264-
filename = parts[0]
265-
# Look for block registry files (format: registryYYYY_BXXX_BYYY.txt)
266-
if filename.startswith('registry') and '_B' in filename and filename.endswith('.txt'):
267-
block_files.append(filename)
268-
269-
print(f"Found {len(block_files)} block registry files to load")
252+
if not registry_file.exists():
253+
print("Warning: Master registry not found")
254+
return
255+
256+
# Parse registry.txt to find all block registry files
257+
block_files = []
258+
with open(registry_file, 'r') as f:
259+
for line in f:
260+
line = line.strip()
261+
if line and not line.startswith('#'):
262+
parts = line.split(' ', 1)
263+
if len(parts) == 2:
264+
filename = parts[0]
265+
# Look for block registry files (format: registry_YYYY_lonXXX_latYYY.txt)
266+
if filename.startswith('registry_') and '_lon' in filename and '_lat' in filename and filename.endswith('.txt'):
267+
block_files.append(filename)
268+
269+
print(f"Found {len(block_files)} block registry files to load")
270+
271+
# Load each block registry
272+
for i, block_file in enumerate(block_files):
273+
if (i + 1) % 100 == 0: # Progress indicator every 100 blocks
274+
print(f"Loading block registries: {i + 1}/{len(block_files)}")
270275

271-
# Load each block registry
272-
for i, block_file in enumerate(block_files):
273-
if (i + 1) % 100 == 0: # Progress indicator every 100 blocks
274-
print(f"Loading block registries: {i + 1}/{len(block_files)}")
276+
try:
277+
# Download the block registry file
278+
registry_url = f"{TESSERA_BASE_URL}/{self.version}/registry/{block_file}"
279+
registry_hash = self._get_registry_hash(block_file)
275280

276-
try:
277-
# Download the block registry file
278-
registry_url = f"{TESSERA_BASE_URL}/{self.version}/registry/{block_file}"
279-
registry_hash = self._get_registry_hash(block_file)
280-
281-
downloaded_file = pooch.retrieve(
282-
url=registry_url,
283-
known_hash=registry_hash,
284-
fname=block_file,
285-
path=self._registry_base_dir,
286-
progressbar=False # Don't show progress for individual files
287-
)
281+
downloaded_file = pooch.retrieve(
282+
url=registry_url,
283+
known_hash=registry_hash,
284+
fname=block_file,
285+
path=self._registry_base_dir,
286+
progressbar=False # Don't show progress for individual files
287+
)
288+
289+
# Load the registry into the pooch instance
290+
self._pooch.load_registry(downloaded_file)
291+
292+
# Mark this block as loaded
293+
# Parse filename format: registry_YYYY_lonXXX_latYYY.txt
294+
# Examples: registry_2024_lon-15_lat10.txt, registry_2024_lon130_lat45.txt
295+
parts = block_file.replace('.txt', '').split('_')
296+
if len(parts) >= 4:
297+
year = int(parts[1]) # parts[0] is "registry", parts[1] is year
288298

289-
# Load the registry into the pooch instance
290-
self._pooch.load_registry(downloaded_file)
299+
# Extract lon and lat values
300+
lon_part = None
301+
lat_part = None
302+
for j, part in enumerate(parts):
303+
if part.startswith('lon'):
304+
lon_part = part[3:] # Remove 'lon' prefix
305+
elif part.startswith('lat'):
306+
lat_part = part[3:] # Remove 'lat' prefix
291307

292-
# Mark this block as loaded (extract year and coordinates from filename)
293-
# Format: registryYYYY_BXXX_BYYY.txt
294-
parts = block_file.replace('registry', '').replace('.txt', '').split('_')
295-
if len(parts) == 3:
296-
year = int(parts[0])
297-
block_lon = int(parts[1].replace('B', ''))
298-
block_lat = int(parts[2].replace('B', ''))
308+
if lon_part and lat_part:
309+
# Convert to block coordinates (assuming these are already block coordinates)
310+
block_lon = int(lon_part)
311+
block_lat = int(lat_part)
299312
self._loaded_blocks.add((year, block_lon, block_lat))
300-
301-
except Exception as e:
302-
print(f"Warning: Failed to load block registry {block_file}: {e}")
303-
continue
304-
305-
# Update available embeddings cache
306-
self._parse_available_embeddings()
307-
print(f"Loaded {len(self._available_embeddings)} total embeddings")
308-
309-
except Exception as e:
310-
print(f"Error loading all blocks: {e}")
311-
313+
314+
except Exception as e:
315+
print(f"Warning: Failed to load block registry {block_file}: {e}")
316+
continue
317+
318+
# Update available embeddings cache
319+
self._parse_available_embeddings()
320+
print(f"Loaded {len(self._available_embeddings)} total embeddings")
321+
322+
except Exception as e:
323+
print(f"Error loading all blocks: {e}")
324+
312325
def _ensure_tile_block_loaded(self, lon: float, lat: float):
313326
"""Ensure registry data for a specific tile block is loaded.
314327

0 commit comments

Comments
 (0)