Skip to content

preview: make tiles subdir and provide default for imagery_offset arg #81

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 11, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions label_maker/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ def class_test(value):

# download tiles
tiles = class_tiles + background_tiles
print('Downloading {} tiles to {}'.format(len(tiles), op.join(dest_folder, 'tiles')))
print('Downloading {} tiles to {}'.format(len(tiles), tiles_dir))

# get image acquisition function based on imagery string
image_function = download_tile_tms
if is_tif(imagery):
image_function = get_tile_tif

for tile in tiles:
image_function(tile, imagery, dest_folder, imagery_offset)
image_function(tile, imagery, tiles_dir, imagery_offset)
3 changes: 2 additions & 1 deletion label_maker/preview.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from label_maker.utils import class_match, download_tile_tms, get_tile_tif, is_tif

def preview(dest_folder, number, classes, imagery, ml_type, imagery_offset, **kwargs):
def preview(dest_folder, number, classes, imagery, ml_type, imagery_offset=False, **kwargs):
"""Produce imagery examples for specified classes

Parameters
Expand Down Expand Up @@ -55,6 +55,7 @@ def preview(dest_folder, number, classes, imagery, ml_type, imagery_offset, **kw
for i, cl in enumerate(classes):
# create class directory
class_dir = op.join(dest_folder, 'examples', cl.get('name'))

if not op.isdir(class_dir):
makedirs(class_dir)

Expand Down
8 changes: 4 additions & 4 deletions label_maker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ def class_match(ml_type, label, i):
return np.count_nonzero(label == i)
return None

def download_tile_tms(tile, imagery, dest_folder, *args):
def download_tile_tms(tile, imagery, folder, *args):
"""Download a satellite image tile from a tms endpoint"""
o = urlparse(imagery)
_, image_format = op.splitext(o.path)
r = requests.get(url(tile.split('-'), imagery))
tile_img = op.join(dest_folder, 'tiles', '{}{}'.format(tile, image_format))
tile_img = op.join(folder, '{}{}'.format(tile, image_format))
open(tile_img, 'wb').write(r.content)
return tile_img

def get_tile_tif(tile, imagery, dest_folder, imagery_offset):
def get_tile_tif(tile, imagery, folder, imagery_offset):
"""
Read a GeoTIFF with a window corresponding to a TMS tile

Expand Down Expand Up @@ -80,7 +80,7 @@ def get_tile_tif(tile, imagery, dest_folder, imagery_offset):
src.read(k, window=window, out=data[k - 1], boundless=True)

# save
tile_img = op.join(dest_folder, 'tiles', '{}{}'.format(tile, '.jpg'))
tile_img = op.join(folder, '{}{}'.format(tile, '.jpg'))
img = Image.fromarray(np.moveaxis(data, 0, -1), mode='RGB')
img.save(tile_img)

Expand Down
6 changes: 3 additions & 3 deletions test/unit/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_get_tile_tif(self):
if not op.isdir(tiles_dir):
makedirs(tiles_dir)

get_tile_tif(tile, 'test/fixtures/drone.tif', dest_folder, None)
get_tile_tif(tile, 'test/fixtures/drone.tif', tiles_dir, None)
test_tile = Image.open('test/tiles/{}.jpg'.format(tile))
fixture_tile = Image.open('test/fixtures/{}.jpg'.format(tile))
self.assertEqual(test_tile, fixture_tile)
Expand All @@ -66,7 +66,7 @@ def test_get_tile_tif_offset(self):
if not op.isdir(tiles_dir):
makedirs(tiles_dir)

get_tile_tif(tile, 'test/fixtures/drone.tif', dest_folder, [128, 64])
get_tile_tif(tile, 'test/fixtures/drone.tif', tiles_dir, [128, 64])
test_tile = Image.open('test/tiles/{}.jpg'.format(tile))
fixture_tile = Image.open('test/fixtures/{}_offset.jpg'.format(tile))
self.assertEqual(test_tile, fixture_tile)
Expand All @@ -80,7 +80,7 @@ def test_get_tile_vrt(self):
if not op.isdir(tiles_dir):
makedirs(tiles_dir)

get_tile_tif(tile, 'test/fixtures/drone.vrt', dest_folder, None)
get_tile_tif(tile, 'test/fixtures/drone.vrt', tiles_dir, None)
test_tile = Image.open('test/tiles/{}.jpg'.format(tile))
fixture_tile = Image.open('test/fixtures/{}.jpg'.format(tile))
self.assertEqual(test_tile, fixture_tile)
Expand Down