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

feat: add pixel_resolution, rename block_resolution #123

Merged
merged 1 commit into from
Dec 4, 2023
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
11 changes: 11 additions & 0 deletions raster_loader/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ def import_error_rasterio(): # pragma: no cover
raise ImportError(msg)


def import_error_rio_cogeo(): # pragma: no cover
msg = (
"Rasterio is not installed.\n"
"Please install rio_cogeo to use this function.\n"
"See https://cogeotiff.github.io/rio-cogeo/\n"
"for installation instructions.\n"
"Alternatively, run `pip install rio-cogeo` to install from pypi."
)
raise ImportError(msg)


def import_error_quadbin(): # pragma: no cover
msg = (
"Quadbin is not installed.\n"
Expand Down
41 changes: 25 additions & 16 deletions raster_loader/io.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import sys
import math
import time
from functools import partial
import json
import sys
import pyproj
import shapely
import numpy as np
import pandas as pd

from typing import Iterable
from typing import Callable
from typing import List
from typing import Tuple

from affine import Affine
import numpy as np
import pandas as pd
import pyproj
import shapely
from functools import partial

try:
import rio_cogeo
Expand Down Expand Up @@ -45,6 +46,7 @@
from raster_loader.geo import raster_bounds
from raster_loader.errors import (
import_error_bigquery,
import_error_rio_cogeo,
import_error_rasterio,
import_error_quadbin,
IncompatibleRasterException,
Expand Down Expand Up @@ -111,6 +113,10 @@ def rasterio_metadata(
file_path: str,
bands_info: List[Tuple[int, str]],
):
"""Requires rio_cogeo."""
if not _has_rio_cogeo: # pragma: no cover
import_error_rio_cogeo()

"""Requires rasterio."""
if not _has_rasterio: # pragma: no cover
import_error_rasterio()
Expand All @@ -125,8 +131,10 @@ def rasterio_metadata(
error_not_google_compatible()

metadata = {}
width = raster_info["Profile"]["Width"]
height = raster_info["Profile"]["Height"]
resolution = raster_info["GEO"]["MaxZoom"]
metadata["resolution"] = resolution
metadata["block_resolution"] = resolution
metadata["minresolution"] = resolution
metadata["maxresolution"] = resolution
metadata["nodata"] = raster_info["Profile"]["Nodata"]
Expand All @@ -153,8 +161,6 @@ def rasterio_metadata(
bounds_coords = list(bounds_polygon.bounds)
center_coords = list(*bounds_polygon.centroid.coords)
center_coords.append(resolution)
width = raster_info["Profile"]["Width"]
height = raster_info["Profile"]["Height"]
# assuming all windows have the same dimensions
a_window = next(raster_dataset.block_windows())
block_width = a_window[1].width
Expand All @@ -171,6 +177,9 @@ def rasterio_metadata(
metadata["block_height"] = block_height
metadata["num_blocks"] = int(width * height / block_width / block_height)
metadata["num_pixels"] = width * height
metadata["pixel_resolution"] = resolution + math.log(
block_width * block_height, 4
)

return metadata

Expand Down Expand Up @@ -707,10 +716,10 @@ def check_metadata_is_compatible(metadata, old_metadata):
ValueError
If the metadata is not compatible.
"""
if metadata["resolution"] != old_metadata["resolution"]:
if metadata["block_resolution"] != old_metadata["block_resolution"]:
raise ValueError(
"Cannot append records to a table with a different resolution "
f"({metadata['resolution']} != {old_metadata['resolution']})."
"Cannot append records to a table with a different block_resolution "
f"({metadata['block_resolution']} != {old_metadata['block_resolution']})."
)
if metadata["nodata"] != old_metadata["nodata"]:
raise ValueError(
Expand Down Expand Up @@ -753,15 +762,15 @@ def update_metadata(metadata, old_metadata):
metadata["center"] = (
(metadata["bounds"][0] + metadata["bounds"][2]) / 2,
(metadata["bounds"][1] + metadata["bounds"][3]) / 2,
metadata["resolution"],
metadata["block_resolution"],
)
metadata["num_blocks"] += old_metadata["num_blocks"]
metadata["num_pixels"] += old_metadata["num_pixels"]
w, s, _ = quadbin.utils.point_to_tile(
metadata["bounds"][0], metadata["bounds"][1], metadata["resolution"]
metadata["bounds"][0], metadata["bounds"][1], metadata["block_resolution"]
)
e, n, _ = quadbin.utils.point_to_tile(
metadata["bounds"][2], metadata["bounds"][3], metadata["resolution"]
metadata["bounds"][2], metadata["bounds"][3], metadata["block_resolution"]
)
metadata["height"] = (s - n) * metadata["block_height"]
metadata["width"] = (e - w) * metadata["block_width"]
Expand Down
10 changes: 5 additions & 5 deletions raster_loader/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ def test_rasterio_to_bigquery_appending_rows():
metadata = json.loads([x for x in list(result.metadata) if x][0])

assert metadata == {
"resolution": 5,
"block_resolution": 5,
"minresolution": 5,
"maxresolution": 5,
"nodata": None,
Expand Down Expand Up @@ -192,7 +192,7 @@ def test_rasterio_to_bigquery_appending_rows():
"nodata": None,
"num_blocks": 2,
"num_pixels": 131072,
"resolution": 5,
"block_resolution": 5,
"width": 512,
}

Expand Down Expand Up @@ -459,7 +459,7 @@ def test_rasterio_to_bigquery_overwrite(*args, **kwargs):
"raster_loader.io.get_metadata",
return_value={
"bounds": [0, 0, 0, 0],
"resolution": 5,
"block_resolution": 5,
"nodata": None,
"block_width": 256,
"block_height": 256,
Expand Down Expand Up @@ -595,7 +595,7 @@ def test_rasterio_to_bigquery_invalid_raster(*args, **kwargs):
"raster_loader.io.get_metadata",
return_value={
"bounds": [0, 0, 0, 0],
"resolution": 5,
"block_resolution": 5,
"nodata": None,
"block_width": 256,
"block_height": 256,
Expand Down Expand Up @@ -626,7 +626,7 @@ def test_rasterio_to_bigquery_valid_raster(*args, **kwargs):
@patch("raster_loader.io.ask_yes_no_question", return_value=True)
@patch(
"raster_loader.io.get_metadata",
return_value={"bounds": [0, 0, 0, 0], "resolution": 1},
return_value={"bounds": [0, 0, 0, 0], "block_resolution": 1},
)
def test_append_with_different_resolution(*args, **kwargs):
from raster_loader.io import rasterio_to_bigquery
Expand Down
Loading