Skip to content

Commit

Permalink
Merge pull request #409 from kubilus1/bestchunkdupes
Browse files Browse the repository at this point in the history
Fix 'duplicate' best chunk tile matches
  • Loading branch information
kubilus1 authored Oct 8, 2023
2 parents 77b3cd2 + 7ba80e3 commit 5c0b591
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 24 deletions.
30 changes: 6 additions & 24 deletions autoortho/getortho.py
Original file line number Diff line number Diff line change
Expand Up @@ -799,10 +799,14 @@ def get_best_chunk(self, col, row, mm, zoom):
col_offset = col % scalefactor
row_offset = row % scalefactor

log.debug(f"Col_Offset: {col_offset}, Row_Offset: {row_offset}, Scale_Factor: {scalefactor}")

# Pixel width
w_p = 256 >> diff
h_p = 256 >> diff

log.debug(f"Pixel Size: {w_p}x{h_p}")

# Load image to crop
img_p = AoImage.load_from_memory(c.data)
if not img_p:
Expand All @@ -812,37 +816,15 @@ def get_best_chunk(self, col, row, mm, zoom):

# Crop
crop_img = AoImage.new('RGBA', (w_p, h_p), (0,255,0))
img_p.crop(crop_img, (col_offset, row_offset))
img_p.crop(crop_img, (col_offset * w_p, row_offset * h_p))
chunk_img = crop_img.scale(scalefactor)

return chunk_img

log.debug(f"No best chunk found for {col}x{row}x{zoom}!")
return False


def _get_best_chunk(self, x, y, mm):
for i in range(mm+1, 5):
if i in self.imgs:
# We have an image already
img = self.imgs[i]

# Difference between requested mm and found image mm level
mmdiff = i - mm

mm4_x = x >> mmdiff
mm4_y = y >> mmdiff
scalefactor = 1 << mmdiff

mm_w = 256 >> mmdiff
mm_h = 256 >> mmdiff

log.debug(f"GET_IMG: {self} Crop: {mm4_x}x{mm4_y} w:{mm_w} h:{mm_h}")
crop_img = AoImage.new('RGBA', (mm_w, mm_h), (0,255,0))
img.crop(crop_img, (mm4_x, mm4_y))
chunk_img = crop_img.scale(scalefactor)
return chunk_img
return False

#@profile
@locked
def get_mipmap(self, mipmap=0):
Expand Down
16 changes: 16 additions & 0 deletions autoortho/test_getortho.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,3 +338,19 @@ def test_get_best_chunk(tmpdir):
ret = tile3.get_best_chunk(18408, 26857, 0, 16)
assert not ret


@pytest.mark.parametrize("mm", [4,3,2,1])
def test_get_best_chunks_all(mm, tmpdir):
tile = getortho.Tile(17408, 25856, 'BI', 16, cache_dir=tmpdir)

# Verify we get a match
tile.get_img(mm)

for x in range(16):
for y in range(16):
ret = tile.get_best_chunk(17408+x, 25856+y, 0, 16)
assert(ret)
ret.write_jpg(os.path.join(tmpdir, f"best_{mm}_{x}_{y}.jpg"))

#assert True == False

0 comments on commit 5c0b591

Please sign in to comment.