Skip to content

Commit

Permalink
Merge pull request #47 from kartoza/fix-cog-conversion-issue
Browse files Browse the repository at this point in the history
Fix cog conversion issue
  • Loading branch information
danangmassandy committed Jun 10, 2024
2 parents 6d0c4df + db9be3a commit c35091a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 50 deletions.
100 changes: 51 additions & 49 deletions django_project/cplus_api/utils/worker_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,53 +341,6 @@ def from_dict(cls, data: dict) -> typing.Self:
return config


def create_and_upload_output_layer(
file_path: str, scenario_task: ScenarioTask,
is_final_output: bool, group: str,
output_meta: dict = None) -> OutputLayer:
filename = os.path.basename(file_path)
if get_layer_type(file_path) == 0:
cog_name = (
f"{os.path.basename(file_path).split('.')[0]}"
f"_COG."
f"{os.path.basename(file_path).split('.')[1]}"
)
final_output_path = os.path.join(
os.path.dirname(file_path),
cog_name
)
result = subprocess.run(
(
f"gdal_translate -of COG -co COMPRESS=DEFLATE"
f" {file_path} {final_output_path}"
),
shell=True,
capture_output=True
)
if result.returncode != 0:
logger.error(result.stderr)
logger.error(f"Failed coverting raster to COG: {file_path}")
if not os.path.exists(final_output_path):
# fallback to original file
final_output_path = file_path
else:
final_output_path = file_path
output_layer = OutputLayer.objects.create(
name=filename,
created_on=timezone.now(),
owner=scenario_task.submitted_by,
layer_type=get_layer_type(file_path),
size=os.stat(final_output_path).st_size,
is_final_output=is_final_output,
scenario=scenario_task,
group=group,
output_meta={} if not output_meta else output_meta
)
with open(final_output_path, 'rb') as output_file:
output_layer.file.save(filename, output_file)
return output_layer


class WorkerScenarioAnalysisTask(ScenarioAnalysisTask):

MIN_UPDATE_PROGRESS_IN_SECONDS = 1
Expand Down Expand Up @@ -673,6 +626,55 @@ def should_update_progress(self):
self.MIN_UPDATE_PROGRESS_IN_SECONDS
)

def create_and_upload_output_layer(
self, file_path: str, scenario_task: ScenarioTask,
is_final_output: bool, group: str,
output_meta: dict = None) -> OutputLayer:
filename = os.path.basename(file_path)
if get_layer_type(file_path) == 0:
cog_name = (
f"{os.path.basename(file_path).split('.')[0]}"
f"_COG."
f"{os.path.basename(file_path).split('.')[1]}"
)
final_output_path = os.path.join(
os.path.dirname(file_path),
cog_name
)
result = subprocess.run(
(
f'gdal_translate -of COG -co COMPRESS=DEFLATE'
f' "{file_path}" {final_output_path}'
),
shell=True,
capture_output=True
)
if result.returncode != 0:
self.log_message(result.stderr.decode(), info=False)
self.log_message(
f"Failed coverting raster to COG: {file_path}",
info=False
)
if not os.path.exists(final_output_path):
# fallback to original file
final_output_path = file_path
else:
final_output_path = file_path
output_layer = OutputLayer.objects.create(
name=filename,
created_on=timezone.now(),
owner=scenario_task.submitted_by,
layer_type=get_layer_type(file_path),
size=os.stat(final_output_path).st_size,
is_final_output=is_final_output,
scenario=scenario_task,
group=group,
output_meta={} if not output_meta else output_meta
)
with open(final_output_path, 'rb') as output_file:
output_layer.file.save(filename, output_file)
return output_layer

def upload_scenario_outputs(self):
scenario_output_files, total_files = (
self.scenario_task.get_scenario_output_files()
Expand All @@ -689,7 +691,7 @@ def upload_scenario_outputs(self):
output_meta = self.output
if 'OUTPUT' in output_meta:
del output_meta['OUTPUT']
create_and_upload_output_layer(
self.create_and_upload_output_layer(
files[0], self.scenario_task,
True, None, self.output
)
Expand All @@ -698,7 +700,7 @@ def upload_scenario_outputs(self):
100 * (total_uploaded_files / total_files))
else:
for file in files:
create_and_upload_output_layer(
self.create_and_upload_output_layer(
file, self.scenario_task, False, group)
total_uploaded_files += 1
self.set_custom_progress(
Expand Down
2 changes: 1 addition & 1 deletion django_project/version/commit.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
edf56ca2892b5982b80d34a500639bedcfd4ced1
5239f6407c84d7925ead9b888451ebf8f098f9f9

0 comments on commit c35091a

Please sign in to comment.