-
Notifications
You must be signed in to change notification settings - Fork 44
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
change how blocksize is defined in create CLI #283
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,10 +131,8 @@ def cogeo(): | |
) | ||
@click.option( | ||
"--overview-blocksize", | ||
default=lambda: os.environ.get("GDAL_TIFF_OVR_BLOCKSIZE", 128), | ||
help="Overview's internal tile size (default defined by " | ||
"GDAL_TIFF_OVR_BLOCKSIZE env or 128)", | ||
show_default=True, | ||
help="Overview's internal tile size (default can be defined by " | ||
"GDAL_TIFF_OVR_BLOCKSIZE env). The default is 128, or starting with GDAL 3.1 to use the same block size as the full-resolution dataset if possible).", | ||
) | ||
@click.option( | ||
"--web-optimized", "-w", is_flag=True, help="Create COGEO optimized for Web." | ||
|
@@ -251,26 +249,18 @@ def create( | |
quiet, | ||
): | ||
"""Create Cloud Optimized Geotiff.""" | ||
output_profile = cog_profiles.get(cogeo_profile) | ||
output_profile.update({"BIGTIFF": os.environ.get("BIGTIFF", "IF_SAFER")}) | ||
if creation_options: | ||
output_profile.update(creation_options) | ||
|
||
if blocksize: | ||
output_profile["blockxsize"] = blocksize | ||
output_profile["blockysize"] = blocksize | ||
|
||
if web_optimized: | ||
overview_blocksize = blocksize or 512 | ||
|
||
config.update( | ||
{ | ||
"GDAL_NUM_THREADS": threads, | ||
"GDAL_TIFF_INTERNAL_MASK": os.environ.get("GDAL_TIFF_INTERNAL_MASK", True), | ||
"GDAL_TIFF_OVR_BLOCKSIZE": str(overview_blocksize), | ||
} | ||
) | ||
|
||
output_profile = cog_profiles.get(cogeo_profile) | ||
output_profile.update({"BIGTIFF": os.environ.get("BIGTIFF", "IF_SAFER")}) | ||
if creation_options: | ||
output_profile.update(creation_options) | ||
|
||
tilematrixset: Optional[TileMatrixSet] = None | ||
if tms: | ||
with open(tms, "r") as f: | ||
|
@@ -279,6 +269,33 @@ def create( | |
elif web_optimized: | ||
tilematrixset = morecantile.tms.get("WebMercatorQuad") | ||
|
||
if tilematrixset: | ||
if not blocksize: | ||
click.secho( | ||
f"Defining `blocksize` from {tilematrixset.id} TileMatrixSet `tileWidth` and `tileHeight`", | ||
fg="yellow", | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we only print a warning if blocksize wasn't defined |
||
|
||
blocksize = min( | ||
tilematrixset.matrix(tilematrixset.minzoom).tileHeight, | ||
tilematrixset.matrix(tilematrixset.minzoom).tileWidth, | ||
) | ||
|
||
if not overview_blocksize: | ||
click.secho( | ||
f"Defining overview's `blocksize` to match the high resolution `blocksize`: {blocksize}", | ||
fg="yellow", | ||
) | ||
overview_blocksize = blocksize | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
|
||
if blocksize: | ||
output_profile["blockxsize"] = blocksize | ||
output_profile["blockysize"] = blocksize | ||
|
||
overview_blocksize = overview_blocksize or os.environ.get("GDAL_TIFF_OVR_BLOCKSIZE") | ||
if overview_blocksize: | ||
config.update({"GDAL_TIFF_OVR_BLOCKSIZE": str(overview_blocksize)}) | ||
|
||
cog_translate( | ||
input, | ||
output, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
import os | ||
|
||
import morecantile | ||
import pytest | ||
import rasterio | ||
|
||
|
@@ -513,30 +514,23 @@ def test_cogeo_validUpercaseProfile(monkeypatch, runner): | |
assert src.compression.value == "DEFLATE" | ||
|
||
|
||
def test_cogeo_info(runner): | ||
@pytest.mark.parametrize( | ||
"src_path", | ||
[ | ||
raster_path_rgb, | ||
raster_invalid_cog, | ||
raster_band_tags, | ||
raster_path_gcps, | ||
], | ||
) | ||
def test_cogeo_info(src_path, runner): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. not related to this PR main goal |
||
"""Should work as expected.""" | ||
with runner.isolated_filesystem(): | ||
result = runner.invoke(cogeo, ["info", raster_path_rgb]) | ||
assert not result.exception | ||
assert result.exit_code == 0 | ||
|
||
result = runner.invoke(cogeo, ["info", raster_path_rgb, "--json"]) | ||
assert not result.exception | ||
assert result.exit_code == 0 | ||
|
||
result = runner.invoke(cogeo, ["info", raster_invalid_cog]) | ||
assert not result.exception | ||
assert result.exit_code == 0 | ||
|
||
result = runner.invoke(cogeo, ["info", raster_path_gcps]) | ||
result = runner.invoke(cogeo, ["info", src_path]) | ||
assert not result.exception | ||
assert result.exit_code == 0 | ||
|
||
result = runner.invoke(cogeo, ["info", raster_band_tags, "--json"]) | ||
assert not result.exception | ||
assert result.exit_code == 0 | ||
|
||
result = runner.invoke(cogeo, ["info", raster_band_tags]) | ||
result = runner.invoke(cogeo, ["info", src_path, "--json"]) | ||
assert not result.exception | ||
assert result.exit_code == 0 | ||
|
||
|
@@ -577,3 +571,104 @@ def test_cogeo_zoom_level(runner): | |
with rasterio.open("output.tif") as src: | ||
_, max_zoom = get_zooms(src) | ||
assert max_zoom == 19 | ||
|
||
|
||
def test_create_web_tms(runner): | ||
"""Should work as expected.""" | ||
with runner.isolated_filesystem(): | ||
result = runner.invoke( | ||
cogeo, | ||
[ | ||
"create", | ||
raster_path_rgb, | ||
"output.tif", | ||
"--web-optimized", | ||
"--aligned-levels", | ||
2, | ||
], | ||
) | ||
assert not result.exception | ||
assert result.exit_code == 0 | ||
with rasterio.open("output.tif") as src: | ||
meta = src.profile | ||
|
||
with rasterio.open("output.tif", OVERVIEW_LEVEL=0) as src: | ||
meta_ovr = src.profile | ||
assert meta_ovr["blockysize"] == meta["blockysize"] | ||
|
||
with open("webmercator.json", "w") as f: | ||
tms = morecantile.tms.get("WebMercatorQuad") | ||
f.write(tms.model_dump_json()) | ||
|
||
result = runner.invoke( | ||
cogeo, | ||
[ | ||
"create", | ||
raster_path_rgb, | ||
"output.tif", | ||
"--aligned-levels", | ||
"2", | ||
"--tms", | ||
"webmercator.json", | ||
], | ||
) | ||
assert not result.exception | ||
assert result.exit_code == 0 | ||
with rasterio.open("output.tif") as src: | ||
meta_tms = src.profile | ||
|
||
assert meta_tms["crs"] == meta["crs"] | ||
assert meta_tms["transform"] == meta["transform"] | ||
assert meta_tms["blockysize"] == meta["blockysize"] == 256 | ||
assert meta_tms["width"] == meta["width"] | ||
assert meta_tms["height"] == meta["height"] | ||
|
||
with rasterio.open("output.tif", OVERVIEW_LEVEL=0) as src: | ||
meta_ovr = src.profile | ||
assert meta_ovr["blockysize"] == meta_tms["blockysize"] == 256 | ||
|
||
result = runner.invoke( | ||
cogeo, | ||
[ | ||
"create", | ||
raster_path_rgb, | ||
"output.tif", | ||
"--aligned-levels", | ||
"2", | ||
"--blocksize", | ||
512, | ||
"--tms", | ||
"webmercator.json", | ||
], | ||
) | ||
assert not result.exception | ||
assert result.exit_code == 0 | ||
with rasterio.open("output.tif") as src: | ||
meta_tms = src.profile | ||
|
||
with rasterio.open("output.tif", OVERVIEW_LEVEL=0) as src: | ||
meta_ovr = src.profile | ||
assert meta_ovr["blockysize"] == meta_tms["blockysize"] == 512 | ||
|
||
result = runner.invoke( | ||
cogeo, | ||
[ | ||
"create", | ||
raster_path_rgb, | ||
"output.tif", | ||
"--aligned-levels", | ||
"2", | ||
"--blocksize", | ||
512, | ||
"--overview-blocksize", | ||
128, | ||
"--tms", | ||
"webmercator.json", | ||
], | ||
) | ||
assert not result.exception | ||
assert result.exit_code == 0 | ||
|
||
with rasterio.open("output.tif", OVERVIEW_LEVEL=0) as src: | ||
meta_ovr = src.profile | ||
assert meta_ovr["blockysize"] == 128 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove default in the CLI