Skip to content

Commit

Permalink
make terracotta python 3.12 compatible
Browse files Browse the repository at this point in the history
fix deprecation/removal of pkg_resources and rasterio is_tiled
  • Loading branch information
jkittner committed Oct 22, 2024
1 parent 4ac20dd commit 4b2ad16
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 15 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:

matrix:
os: [ubuntu-latest]
python-version: ["3.8", "3.11"]
python-version: ["3.8", "3.12"]

defaults:
run:
Expand Down Expand Up @@ -107,7 +107,7 @@ jobs:

matrix:
os: [macos-latest, windows-latest]
python-version: ["3.8", "3.11"]
python-version: ["3.8", "3.12"]

defaults:
run:
Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Framework :: Flask",
"Operating System :: Microsoft :: Windows :: Windows 10",
"Operating System :: MacOS :: MacOS X",
Expand Down Expand Up @@ -69,6 +70,7 @@
"click-spinner",
"flask",
"flask_cors",
"importlib_resources; python_version<'3.9'",
"marshmallow>=3.0.0",
"mercantile",
"numpy%s" % numpy_version,
Expand Down
15 changes: 9 additions & 6 deletions terracotta/cmaps/get_cmaps.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@

from typing import Dict
import os
from pkg_resources import resource_filename, Requirement, DistributionNotFound

import numpy as np
import sys

if sys.version_info >= (3, 9):
import importlib.resources as importlib_resources
else:
import importlib_resources


SUFFIX = "_rgba.npy"
EXTRA_CMAP_FOLDER = os.environ.get("TC_EXTRA_CMAP_FOLDER", "")

try:
PACKAGE_DIR = resource_filename(
Requirement.parse("terracotta"), "terracotta/cmaps/data"
)
except DistributionNotFound:
PACKAGE_DIR = str(importlib_resources.files("terracotta") / "cmaps/data")
except ModuleNotFoundError:
# terracotta was not installed, fall back to file system
PACKAGE_DIR = os.path.join(os.path.dirname(__file__), "data")

Expand Down
10 changes: 8 additions & 2 deletions terracotta/cog.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ def check_raster_file(src_path: str) -> ValidationInfo: # pragma: no cover

overviews = src.overviews(1)
if src.width > 512 and src.height > 512:
if not src.is_tiled:
if not (
src.block_shapes
and all(src.width != w for _, w in src.block_shapes)
):
errors.append(
"The file is greater than 512xH or 512xW, but is not tiled"
)
Expand Down Expand Up @@ -166,7 +169,10 @@ def check_raster_file(src_path: str) -> ValidationInfo: # pragma: no cover
for ix, dec in enumerate(overviews):
with rasterio.open(src_path, OVERVIEW_LEVEL=ix) as ovr_dst:
if ovr_dst.width > 512 and ovr_dst.height > 512:
if not ovr_dst.is_tiled:
if not (
ovr_dst.block_shapes
and all(ovr_dst.width != w for _, w in ovr_dst.block_shapes)
):
errors.append("Overview of index {} is not tiled".format(ix))

return errors, warnings, details
15 changes: 10 additions & 5 deletions tests/cmaps/test_get_cmap.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import pytest

import numpy as np
import sys

if sys.version_info >= (3, 9):
import importlib.resources as importlib_resources
else:
import importlib_resources


@pytest.fixture(autouse=True)
Expand All @@ -24,19 +30,18 @@ def test_get_cmap():


def test_get_cmap_filesystem(monkeypatch):
import pkg_resources
import importlib

import terracotta.cmaps.get_cmaps

def throw_error(*args, **kwargs):
raise pkg_resources.DistributionNotFound("monkeypatched")
raise ModuleNotFoundError

with monkeypatch.context() as m:
m.setattr(pkg_resources.Requirement, "parse", throw_error)
m.setattr(importlib_resources, "files", throw_error)

with pytest.raises(pkg_resources.DistributionNotFound):
pkg_resources.Requirement.parse("terracotta")
with pytest.raises(ModuleNotFoundError):
importlib_resources.files("terracotta")

importlib.reload(terracotta.cmaps.get_cmaps)

Expand Down

0 comments on commit 4b2ad16

Please sign in to comment.