Skip to content

Commit 1dcc3e1

Browse files
committed
Improved file handling for raster parser.
The fixes should make the package more stable in windows environments.
1 parent c45f1c9 commit 1dcc3e1

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

raster/tiles/parser.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -202,21 +202,19 @@ def reproject_rasterfile(self):
202202

203203
# Compress reprojected raster file and store it
204204
if self.rasterlayer.store_reprojected:
205-
dest = tempfile.NamedTemporaryFile(dir=self.tmpdir, suffix='.zip')
206-
dest_zip = zipfile.ZipFile(dest.name, 'w', allowZip64=True)
207-
dest_zip.write(
208-
filename=self.dataset.name,
209-
arcname=os.path.basename(self.dataset.name),
210-
compress_type=zipfile.ZIP_DEFLATED,
211-
)
212-
dest_zip.close()
213-
214-
# Store zip file in reprojected raster model
215-
self.rasterlayer.reprojected.rasterfile = File(
216-
open(dest_zip.filename, 'rb'),
217-
name=os.path.basename(dest_zip.filename)
218-
)
219-
self.rasterlayer.reprojected.save()
205+
with tempfile.NamedTemporaryFile(dir=self.tmpdir, suffix='.zip') as dest:
206+
with zipfile.ZipFile(dest.name, 'w', allowZip64=True) as dest_zip:
207+
dest_zip.write(
208+
filename=self.dataset.name,
209+
arcname=os.path.basename(self.dataset.name),
210+
compress_type=zipfile.ZIP_DEFLATED,
211+
)
212+
# Store zip file in reprojected raster model
213+
self.rasterlayer.reprojected.rasterfile = File(
214+
open(dest.name, 'rb'),
215+
name=os.path.basename(dest_zip.filename)
216+
)
217+
self.rasterlayer.reprojected.save()
220218

221219
self.log('Finished transforming raster.')
222220

@@ -340,7 +338,7 @@ def process_quadrant(self, indexrange, zoom):
340338

341339
# Compute quadrant bounds and create destination file
342340
bounds = utils.tile_bounds(indexrange[0], indexrange[1], zoom)
343-
dest_file = tempfile.NamedTemporaryFile(dir=self.tmpdir, suffix='.tif')
341+
dest_file = tempfile.NamedTemporaryFile(dir=self.tmpdir, suffix='.tif', delete=False)
344342

345343
# Snap dataset to the quadrant
346344
snapped_dataset = self.dataset.warp({
@@ -410,6 +408,9 @@ def process_quadrant(self, indexrange, zoom):
410408
if len(batch):
411409
RasterTile.objects.bulk_create(batch)
412410

411+
# Remove quadrant raster tempfile.
412+
os.remove(dest_file.name)
413+
413414
def push_histogram(self, data):
414415
"""
415416
Add data to band level histogram.

0 commit comments

Comments
 (0)