def place_file(self, id, source_path):
target_dir = os.path.join(IMAGERY_PATH, id)
if not os.path.exists(target_dir):
os.mkdir(target_dir)
output_file = os.path.abspath(os.path.join(target_dir, 'index.tif'))
# rewrite with gdal_translate
gdal_translate = [
'gdal_translate',
source_path,
output_file,
'-co', 'TILED=yes',
'-co', 'COMPRESS=DEFLATE',
'-co', 'PREDICTOR=2',
'-co', 'BLOCKXSIZE=512',
'-co', 'BLOCKYSIZE=512',
'-co', 'NUM_THREADS=ALL_CPUS',
]
started_at = datetime.utcnow()
self.update_state(state='RUNNING',
meta={
'name': 'preprocess',
'started_at': started_at.isoformat(),
'status': 'Rewriting imagery'
})
try:
returncode = subprocess.call(gdal_translate, timeout=TASK_TIMEOUT)
except subprocess.TimeoutExpired as e:
raise Exception(json.dumps({
'name': 'preprocess',
'started_at': started_at.isoformat(),
'command': ' '.join(gdal_translate),
'status': 'Timed out'
}))
if returncode != 0:
raise Exception(json.dumps({
'name': 'preprocess',
'started_at': started_at.isoformat(),
'command': ' '.join(gdal_translate),
'return_code': returncode,
'status': 'Failed'
}))
if not source_path.startswith(('/vsicurl', 'http://', 'https://')):
# delete original
os.unlink(source_path)
return {
'name': 'preprocess',
'completed_at': datetime.utcnow().isoformat(),
'started_at': started_at,
'status': 'Image pre-processing completed'
}
评论列表
文章目录