Skip to content
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

Fix overviews #22

Merged
merged 3 commits into from
Dec 13, 2021
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

### Fixed

- Colormap must be applied before `gdal_translate` is called. This ensures that the correct settings are used e.g. NEAREST vs CUBIC for generating overviews.[#22](https://github.com/stactools-packages/nrcan-landcover/pull/22)
- create-item and create-collection shouldn't call save on the STAC object.
- Metadata URL on STAC Item.

Expand Down
97 changes: 54 additions & 43 deletions src/stactools/nrcan_landcover/cog.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import logging
import os
from glob import glob
from shutil import copyfile
from subprocess import CalledProcessError, check_output
from tempfile import TemporaryDirectory
from zipfile import ZipFile
Expand All @@ -11,6 +12,7 @@
from stactools.nrcan_landcover.constants import (
COLOUR_MAP,
JSONLD_HREF,
NO_DATA_VALUE,
TILING_PIXEL_SIZE,
)
from stactools.nrcan_landcover.utils import get_metadata
Expand Down Expand Up @@ -149,50 +151,59 @@ def create_cog(
str: The path to the output COG.
"""

output = None
try:
if dry_run:
logger.info("Would have read TIFF, created COG, and written COG")
else:
logger.info("Converting TIFF to COG")
logger.debug(f"input_path: {input_path}")
logger.debug(f"output_path: {output_path}")
cmd = [
"gdal_translate",
"-of",
"COG",
"-co",
"NUM_THREADS=ALL_CPUS",
"-co",
"BLOCKSIZE=512",
"-co",
"COMPRESS=DEFLATE",
"-co",
"LEVEL=9",
"-co",
"PREDICTOR=YES",
"-co",
"OVERVIEWS=IGNORE_EXISTING",
"-a_nodata",
"0",
input_path,
output_path,
]

try:
output = check_output(cmd)
except CalledProcessError as e:
output = e.output
raise
finally:
logger.info(f"output: {str(output)}")
with rasterio.open(output_path, "r+") as dataset:
dataset.write_colormap(1, COLOUR_MAP)
with TemporaryDirectory() as tmp_dir:
# The colormap must be applied before processing.
# This ensures that the correct defaults are used.
# e.g. NEAREST rather than CUBIC for calculating overviews.
# So we copy the input file to a temp dir and apply it before
# running gdal_translate.
tmp_path = os.path.join(tmp_dir, os.path.basename(input_path))
copyfile(input_path, tmp_path)
with rasterio.open(tmp_path, "r+") as dataset:
dataset.write_colormap(1, COLOUR_MAP)
output = None
try:
if dry_run:
logger.info(
"Would have read TIFF, created COG, and written COG")
else:
logger.info("Converting TIFF to COG")
logger.debug(f"input_path: {input_path}")
logger.debug(f"output_path: {output_path}")
cmd = [
"gdal_translate",
"-of",
"COG",
"-co",
"NUM_THREADS=ALL_CPUS",
"-co",
"BLOCKSIZE=512",
"-co",
"COMPRESS=DEFLATE",
"-co",
"LEVEL=9",
"-co",
"PREDICTOR=YES",
"-co",
"OVERVIEWS=IGNORE_EXISTING",
"-a_nodata",
str(NO_DATA_VALUE),
tmp_path,
output_path,
]

except Exception:
logger.error("Failed to process {}".format(output_path))
try:
output = check_output(cmd)
except CalledProcessError as e:
output = e.output
raise
finally:
logger.info(f"output: {str(output)}")

if raise_on_fail:
raise
except Exception:
logger.error("Failed to process {}".format(output_path))

if raise_on_fail:
raise

return output_path
2 changes: 2 additions & 0 deletions src/stactools/nrcan_landcover/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@
19: "Snow and Ice",
}

NO_DATA_VALUE = 0

TILING_PIXEL_SIZE = (10001, 10001)

FULL_DATASET_BBOX = [-2600030.0, -885090.0, 3100000.0, 3914940.0]
5 changes: 3 additions & 2 deletions src/stactools/nrcan_landcover/stac.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
LANDCOVER_TITLE,
LICENSE,
LICENSE_LINK,
NO_DATA_VALUE,
NRCAN_PROVIDER,
THUMBNAIL_HREF,
)
Expand Down Expand Up @@ -247,7 +248,7 @@ def create_item(metadata: Dict[str, Any],
# Raster Extension
cog_asset_raster = RasterExtension.ext(cog_asset, add_if_missing=True)
cog_asset_raster.bands = [
RasterBand.create(nodata=0,
RasterBand.create(nodata=NO_DATA_VALUE,
sampling=Sampling.AREA,
data_type=DataType.UINT8,
spatial_resolution=30)
Expand Down Expand Up @@ -378,7 +379,7 @@ def create_collection(
"title":
"Land cover of Canada COG",
"raster:bands": [
RasterBand.create(nodata=0,
RasterBand.create(nodata=NO_DATA_VALUE,
sampling=Sampling.AREA,
data_type=DataType.UINT8,
spatial_resolution=30).to_dict()
Expand Down