Skip to content
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
10 changes: 5 additions & 5 deletions .github/workflows/python-CD.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ on:
branches: [master]
jobs:
format_and_check:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
environment: production
steps:
- uses: actions/checkout@v3
Expand All @@ -30,7 +30,7 @@ jobs:

build:
needs: format_and_check
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
environment: production
strategy:
matrix:
Expand Down Expand Up @@ -62,7 +62,7 @@ jobs:

deploy:
needs: build
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
environment: production
steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -97,7 +97,7 @@ jobs:

release:
needs: deploy
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
environment: production
steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -128,7 +128,7 @@ jobs:

deploy_docs:
needs: build
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
environment: production
steps:
- uses: actions/checkout@v3
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/python-CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ on:
branches: [develop, feature/*]
jobs:
format_and_check:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
environment: development
steps:
- uses: actions/checkout@v3
Expand All @@ -29,7 +29,7 @@ jobs:

build:
needs: format_and_check
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
environment: development
strategy:
matrix:
Expand Down
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.10.16
16 changes: 4 additions & 12 deletions Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,13 @@ verify_ssl = true
name = "pypi"

[packages]
tqdm = "*"
numpy = "*"
scikit-image = "*"
splitraster = {editable = true, path = "."}
numpy = "<2.0.0,>=1.19.0"
tqdm = "<5.0.0,>=4.40.0"
scikit-image = "<1.0.0,>=0.18.0"
pytest = "*"

[dev-packages]
ipykernel = "*"
mkdocs = "*"
black = "*"
pre-commit = "*"
pytest = "*"
ipykernel = "*"
torch = "*"
matplotlib = "*"
torchvision = "*"

[requires]
python_version = "3.10"
1,960 changes: 431 additions & 1,529 deletions Pipfile.lock

Large diffs are not rendered by default.

50 changes: 34 additions & 16 deletions notebooks/Tutorial.ipynb

Large diffs are not rendered by default.

20 changes: 9 additions & 11 deletions notebooks/Tutorial_II.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"/Users/caihaocui/.local/share/virtualenvs/split_raster-m2hVat3N/lib/python3.10/site-packages/osgeo/gdal.py:312: FutureWarning: Neither gdal.UseExceptions() nor gdal.DontUseExceptions() has been explicitly called. In GDAL 4.0, exceptions will be enabled by default.\n",
"/Users/caihaocui/.local/share/virtualenvs/split_raster-co_bDcoB/lib/python3.10/site-packages/osgeo/gdal.py:314: FutureWarning: Neither gdal.UseExceptions() nor gdal.DontUseExceptions() has been explicitly called. In GDAL 4.0, exceptions will be enabled by default.\n",
" warnings.warn(\n"
]
},
Expand All @@ -102,7 +102,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"Generating: 100%|\u001b[32m██████████\u001b[0m| 400/400 [00:00<00:00, 2127.55img/s]\n"
"Generating: 100%|\u001b[32m██████████\u001b[0m| 400/400 [00:00<00:00, 1114.16img/s]\n"
]
},
{
Expand All @@ -119,7 +119,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"Generating: 100%|\u001b[32m██████████\u001b[0m| 400/400 [00:00<00:00, 2673.49img/s]"
"Generating: 100%|\u001b[32m██████████\u001b[0m| 400/400 [00:00<00:00, 2581.73img/s]"
]
},
{
Expand Down Expand Up @@ -256,7 +256,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"Generating: 100%|\u001b[32m██████████\u001b[0m| 20/20 [00:00<00:00, 692.43img/s]"
"Generating: 100%|\u001b[32m██████████\u001b[0m| 20/20 [00:00<00:00, 227.96img/s]"
]
},
{
Expand Down Expand Up @@ -308,14 +308,14 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Latest run time 2024-03-23 16:59:37\n"
"Latest run time 2025-03-23 20:37:15\n"
]
}
],
Expand All @@ -334,11 +334,9 @@
}
],
"metadata": {
"interpreter": {
"hash": "67d32777f905cb443c7595cdac206bb322335f5bfbf8ae9544bce83f7472f717"
},
"kernelspec": {
"display_name": "Python 3.8.10 64-bit ('base': conda)",
"display_name": "split_raster-co_bDcoB",
"language": "python",
"name": "python3"
},
"language_info": {
Expand All @@ -351,7 +349,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
"version": "3.10.16"
},
"orig_nbformat": 4
},
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ numpy>=1.19.0, <2.0.0
tqdm>=4.40.0, <5.0.0
scikit-image>=0.18.0, <1.0.0
# GDAL==3.8.4 # For GIS only on MacOS `brew install gdal` and `pip install GDAL==3.8.4`
# python_version >= "3.10"
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def read_requirements():

setup(
name="splitraster",
version="0.3.5",
version="0.3.6",
author="Chris Cui",
license="MIT",
platforms="any",
Expand Down
89 changes: 42 additions & 47 deletions src/splitraster/geo.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
from tqdm import tqdm

from osgeo import gdal
from osgeo import gdal_array

from osgeo import gdal, gdal_array
import numpy as np
from pathlib import Path
import random
from typing import Tuple, Optional


def read_rasterArray(image_path):
def read_rasterArray(image_path: str) -> Tuple[np.ndarray, Tuple[float, ...], str]:
dataset = gdal.Open(image_path, gdal.GA_ReadOnly)
image = dataset.ReadAsArray() # get the rasterArray
# convert 2D raster to [1, H, W] format
Expand All @@ -19,7 +17,9 @@ def read_rasterArray(image_path):
return image, geotrans, proj


def save_rasterGeoTIF(im_data, im_geotrans, im_proj, file_name):
def save_rasterGeoTIF(
im_data: np.ndarray, im_geotrans: Tuple[float, ...], im_proj: str, file_name: str
) -> None:
if Path(file_name).is_file():
print(f"Overwrite existing file: {file_name}")

Expand All @@ -40,41 +40,28 @@ def save_rasterGeoTIF(im_data, im_geotrans, im_proj, file_name):
dataset = driver.Create(
file_name, int(im_width), int(im_height), int(im_bands), datatype
)
if dataset != None:
if dataset is not None:
dataset.SetGeoTransform(im_geotrans)
dataset.SetProjection(im_proj)
for i in range(im_bands):
dataset.GetRasterBand(i + 1).WriteArray(im_data[i])
del dataset


# def save_rasterArray(im_data, path, image_prototype_path):

# output = gdal_array.SaveArray(
# im_data, path, format="GTiff", prototype=image_prototype_path)
# return True


def save_rasterArray(im_data, file_name):
def save_rasterArray(im_data: np.ndarray, file_name: str) -> bool:
if Path(file_name).is_file():
print(f"Overwrite existing file: {file_name}")
output = gdal_array.SaveArray(im_data, file_name, format="GTiff")
gdal_array.SaveArray(im_data, file_name, format="GTiff")
return True


def count_files(folder_path):
count = 0
for path in Path(folder_path).iterdir():
if path.is_file():
count += 1
return count
def count_files(folder_path: str) -> int:
return sum(1 for path in Path(folder_path).iterdir() if path.is_file())


def padding_mul_image(img, stride):
D = img.shape[0] # this one is for (D, H, W) format Channel First.
height = img.shape[1]
width = img.shape[2]
# get the minial padding image size
def padding_mul_image(img: np.ndarray, stride: int) -> np.ndarray:
D, height, width = img.shape # (D, H, W) format Channel First.
# get the minimal padding image size
H = int(np.ceil(height / stride) * stride)
W = int(np.ceil(width / stride) * stride)

Expand All @@ -84,13 +71,17 @@ def padding_mul_image(img, stride):
padded_img[d, :, :] = np.pad(
onelayer, ((0, H - height), (0, W - width)), "reflect"
)
# padded_img = np.squeeze(padded_img) # Remove axes of length one
return padded_img


def split_image(
img_path, save_path, crop_size, repetition_rate=0, overwrite=True, ext="."
):
img_path: str,
save_path: str,
crop_size: int,
repetition_rate: float = 0,
overwrite: bool = True,
ext: Optional[str] = ".",
) -> Optional[int]:
# check input image
img, geotrans, proj = read_rasterArray(img_path)
if img is None:
Expand All @@ -104,7 +95,7 @@ def split_image(
print(f"Input Image File Shape (D, H, W):{ img.shape}")

stride = int(crop_size * (1 - repetition_rate))
print(f"{crop_size=}, {stride=}")
print(f"crop_size = {crop_size}, stride = {stride}")

padded_img = padding_mul_image(img, stride)

Expand Down Expand Up @@ -138,32 +129,36 @@ def tile_generator():
crop_img = padded_img[:, h : h + crop_size, w : w + crop_size]
crop_image_name = f"{new_name:04d}{ext}"
crop_image_path = Path(save_path) / crop_image_name
# save_rasterArray(crop_img, str(crop_image_path)) # just save the raster image
save_rasterGeoTIF(crop_img, geotrans, proj, str(crop_image_path))
new_name = new_name + 1
new_name += 1
pbar.update(1)

return n + 1


def random_crop_image(
img_path,
img_save_path,
label_path,
label_save_path,
crop_size=256,
crop_number=20,
img_ext=".tif",
label_ext=".tif",
overwrite=True,
):
img_path: str,
img_save_path: str,
label_path: str,
label_save_path: str,
crop_size: int = 256,
crop_number: int = 20,
img_ext: str = ".tif",
label_ext: str = ".tif",
overwrite: bool = True,
) -> Optional[int]:
"""Generate Random cropped image pair from the input image pairs.

Args:
img_path (str): path of input image
img_save_path (str):
img_save_path (str): path to save cropped images
label_path (str): path of input label
label_save_path (str): path to save cropped labels
crop_size (int): image tile size (H,W), i.e., 256x256
overwrite (bool, optional): [overwrite existing files]. Defaults to True.
crop_number (int): number of crops to generate
img_ext (str): extension for image files
label_ext (str): extension for label files
overwrite (bool): overwrite existing files
"""
img, geotrans, proj = read_rasterArray(label_path)
if img is None:
Expand Down Expand Up @@ -231,8 +226,8 @@ def random_crop_image(
crop_image_path = Path(label_save_path) / crop_image_name
save_rasterGeoTIF(labelCrop, geotrans, proj, str(crop_image_path))

new_name = new_name + 1 # update image name
crop_cnt = crop_cnt + 1 # add crop count
new_name += 1 # update image name
crop_cnt += 1 # add crop count
pbar.update(1)

return crop_cnt # return total crop sample pair number.
Loading