Skip to content

Commit

Permalink
Merge pull request #152 from cogeotiff/minMaxZoom
Browse files Browse the repository at this point in the history
add min/max zoom in cogeo info output
  • Loading branch information
vincentsarago committed Aug 18, 2020
2 parents db8dabc + 6c499ff commit b9b5730
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 17 deletions.
8 changes: 8 additions & 0 deletions CHANGES.txt
@@ -1,3 +1,11 @@
2.0a6 (2020-08-18)
------------------
- fix bug in cogeo.info when CRS in not set.
- add minzoom/maxzoom in cogeo.info output.

Breaking Changes:
- rio_cogeo.utils.get_max_zoom renamed rio_cogeo.utils.get_zooms and now return min/max zoom.

2.0a5 (2020-07-31)
------------------
- move most of the cogeo info code in rio_cogeo.cogeo.cog_info api
Expand Down
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -159,6 +159,8 @@ Geo
Origin: (699960.0, 3600000.0)
Resolution: (10.0, -10.0)
BoundingBox: (699960.0, 3490200.0, 809760.0, 3600000.0)
MinZoom: 10
MaxZoom: 19

IFD
Id Size BlockSize Decimation
Expand Down
25 changes: 19 additions & 6 deletions rio_cogeo/cogeo.py
Expand Up @@ -6,7 +6,7 @@
import tempfile
import warnings
from contextlib import ExitStack, contextmanager
from typing import Any, Dict, List, Tuple
from typing import Any, Dict, List, Optional, Tuple

import click
import rasterio
Expand Down Expand Up @@ -503,16 +503,29 @@ def cog_info(src_path: str, **kwargs: Any) -> Dict:
"Scales": src_dst.scales,
"Offsets": src_dst.offsets,
}
crs = (
f"EPSG:{src_dst.crs.to_epsg()}"
if src_dst.crs.to_epsg()
else src_dst.crs.to_wkt()
)
try:
crs = (
f"EPSG:{src_dst.crs.to_epsg()}"
if src_dst.crs.to_epsg()
else src_dst.crs.to_wkt()
)
except AttributeError:
crs = None

minzoom: Optional[int] = None
maxzoom: Optional[int] = None
try:
minzoom, maxzoom = utils.get_zooms(src_dst)
except Exception:
pass

geo = {
"CRS": crs,
"BoundingBox": tuple(src_dst.bounds),
"Origin": (src_dst.transform.c, src_dst.transform.f),
"Resolution": (src_dst.transform.a, src_dst.transform.e),
"MinZoom": minzoom,
"MaxZoom": maxzoom,
}

ifd_raw = [
Expand Down
4 changes: 3 additions & 1 deletion rio_cogeo/scripts/cli.py
Expand Up @@ -313,7 +313,9 @@ def info(input, to_json):
{click.style("Crs:", bold=True):<{sep}} {metadata['GEO']['CRS']}
{click.style("Origin:", bold=True):<{sep}} {metadata['GEO']['Origin']}
{click.style("Resolution:", bold=True):<{sep}} {metadata['GEO']['Resolution']}
{click.style("BoundingBox:", bold=True):<{sep}} {metadata['GEO']['BoundingBox']}"""
{click.style("BoundingBox:", bold=True):<{sep}} {metadata['GEO']['BoundingBox']}
{click.style("MinZoom:", bold=True):<{sep}} {metadata['GEO']['MinZoom']}
{click.style("MaxZoom:", bold=True):<{sep}} {metadata['GEO']['MaxZoom']}"""
)

click.echo(
Expand Down
12 changes: 8 additions & 4 deletions rio_cogeo/utils.py
Expand Up @@ -2,7 +2,7 @@

import math
import warnings
from typing import Dict
from typing import Dict, Tuple

import mercantile
from rasterio.crs import CRS
Expand Down Expand Up @@ -66,7 +66,7 @@ def zoom_for_pixelsize(pixel_size, max_z=24, tilesize=256):
return max_z - 1


def get_max_zoom(src_dst, lat=0.0, tilesize=256):
def get_zooms(src_dst, lat=0.0, tilesize=256) -> Tuple[int, int]:
"""
Calculate raster max zoom level.
Expand Down Expand Up @@ -98,7 +98,11 @@ def get_max_zoom(src_dst, lat=0.0, tilesize=256):
corrected_resolution = native_resolution * latitude_correction_factor

max_zoom = zoom_for_pixelsize(corrected_resolution, tilesize=tilesize)
return max_zoom

ovr_resolution = corrected_resolution * max(h, w) / tilesize
min_zoom = zoom_for_pixelsize(ovr_resolution, tilesize=tilesize)

return (min_zoom, max_zoom)


def get_maximum_overview_level(src_dst, minsize=512):
Expand Down Expand Up @@ -165,7 +169,7 @@ def get_web_optimized_params(
center = [(bounds[0] + bounds[2]) / 2, (bounds[1] + bounds[3]) / 2]

lat = 0 if latitude_adjustment else center[1]
max_zoom = get_max_zoom(src_dst, lat=lat, tilesize=tilesize)
_, max_zoom = get_zooms(src_dst, lat=lat, tilesize=tilesize)

extrema = tile_extrema(bounds, max_zoom)

Expand Down
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -27,7 +27,7 @@

setup(
name="rio-cogeo",
version="2.0a5",
version="2.0a6",
python_requires=">=3",
description=u"CloudOptimized GeoTIFF (COGEO) creation plugin for rasterio",
long_description=readme,
Expand Down
Binary file added tests/fixtures/slc.tif
Binary file not shown.
5 changes: 5 additions & 0 deletions tests/test_cli.py
Expand Up @@ -21,6 +21,7 @@
raster_invalid_cog = os.path.join(
os.path.dirname(__file__), "fixtures", "validate", "image_dec.tif"
)
raster_path_gcps = os.path.join(os.path.dirname(__file__), "fixtures", "slc.tif")


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -501,3 +502,7 @@ def test_cogeo_info(runner):
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])
assert not result.exception
assert result.exit_code == 0
12 changes: 7 additions & 5 deletions tests/test_web.py
Expand Up @@ -14,7 +14,7 @@

from rio_cogeo.cogeo import cog_translate
from rio_cogeo.profiles import cog_profiles
from rio_cogeo.utils import get_max_zoom
from rio_cogeo.utils import get_zooms

raster_path_web = os.path.join(os.path.dirname(__file__), "fixtures", "image_web.tif")
raster_path_north = os.path.join(
Expand Down Expand Up @@ -49,7 +49,8 @@ def test_cog_translate_webZooms():
config=config,
)
with rasterio.open("cogeo.tif") as out_dst:
assert get_max_zoom(out_dst) == 8
_, maxzoom = get_zooms(out_dst)
assert maxzoom == 8

cog_translate(
raster_path_north,
Expand All @@ -61,7 +62,8 @@ def test_cog_translate_webZooms():
config=config,
)
with rasterio.open("cogeo.tif") as out_dst:
assert get_max_zoom(out_dst) == 10
_, maxzoom = get_zooms(out_dst)
assert maxzoom == 10


def test_cog_translate_web():
Expand Down Expand Up @@ -93,7 +95,7 @@ def test_cog_translate_web():
ts = blocks[0][0]
assert not out_dst.width % ts
assert not out_dst.height % ts
max_zoom = get_max_zoom(out_dst)
_, max_zoom = get_zooms(out_dst)

bounds = list(
transform_bounds(
Expand Down Expand Up @@ -150,7 +152,7 @@ def test_cog_translate_Internal():
ts = blocks[0][0]
assert not out_dst.width % ts
assert not out_dst.height % ts
max_zoom = get_max_zoom(out_dst)
_, max_zoom = get_zooms(out_dst)

bounds = list(
transform_bounds(
Expand Down

0 comments on commit b9b5730

Please sign in to comment.