splitraster 0.3.5__tar.gz → 0.3.6__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
 
2
2
  The MIT License (MIT)
3
- Copyright (c) 2021, Your name (or your organization/company/team)
3
+ Copyright (c) 2024, Caihao Cui
4
4
 
5
5
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
6
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: splitraster
3
- Version: 0.3.5
3
+ Version: 0.3.6
4
4
  Summary: Provide good support for deep learning and computer vision tasks by creating a tiled output from an input raster dataset.
5
5
  Home-page: https://github.com/cuicaihao/split_raster
6
6
  Author: Chris Cui
@@ -27,6 +27,12 @@ License-File: LICENSE
27
27
 
28
28
  Provide good support for deep learning and computer vision tasks by creating a tiled output from an input raster dataset.
29
29
 
30
+ Find more details:
31
+
32
+ - Document: [split_raster](https://cuicaihao.github.io/split_raster/)
33
+ - Pypi: [splitraster](https://pypi.org/project/splitraster/)
34
+
35
+
30
36
  ## Use the packages
31
37
 
32
38
  ```bash
@@ -2,11 +2,13 @@
2
2
 
3
3
  [![image](https://img.shields.io/pypi/pyversions/splitraster)](https://python.org/pypi/splitraster)
4
4
  [![image](https://img.shields.io/pypi/v/splitraster?color=g)](https://python.org/pypi/splitraster)
5
- [![CI](https://img.shields.io/github/actions/workflow/status/cuicaihao/split_raster/python-app.yml?branch=master)](https://github.com/cuicaihao/split_raster/actions/workflows/python-app.yml)
6
- [![image](https://img.shields.io/pypi/dm/splitraster?color=blue)](https://python.org/pypi/splitraster)
5
+ [![CD](https://img.shields.io/github/actions/workflow/status/cuicaihao/split_raster/python-CD.yml?branch=master)](https://github.com/cuicaihao/split_raster/actions/workflows/python-CD.yml)
6
+ [![DMonth](https://pepy.tech/badge/splitraster/month)](https://pepy.tech/project/splitraster)
7
+ [![DTotal](https://pepy.tech/badge/splitraster?)](https://pepy.tech/project/splitraster)
7
8
  [![image](https://img.shields.io/github/license/cuicaihao/split_raster?color=blue)](https://python.org/pypi/splitraster)
8
9
 
9
- - Document: [split_raster](https://cuicaihao.github.io/split_raster/)
10
+
11
+ - Document: [splitraster](https://cuicaihao.github.io/split_raster/)
10
12
  - Pypi: [splitraster](https://pypi.org/project/splitraster/)
11
13
 
12
14
 
@@ -34,7 +36,7 @@ This tutorial will show you how to use the package to split a large image into s
34
36
  pip install splitraster
35
37
  ```
36
38
 
37
- ## Try Sample code
39
+ ## Try Sample code
38
40
 
39
41
  The sample image can be found in the GitHub repo.
40
42
 
@@ -85,14 +87,39 @@ Generating: 100%|█████████████████████
85
87
  49 tiles sample of ./data/raw/GT.png are added at ./data/processed/GT
86
88
  ```
87
89
 
90
+ Check Notebook for Details: [Tutorial](notebooks/Tutorial.ipynb)
91
+
92
+
93
+ ## GIS TIFF Image
94
+
88
95
  You can also work with Remote Sensing (GeoTIFF) Satellite images such as Multispectral Images which have more bands or channels. All the codes will be the same, but with a small difference. Replace the `io` with the `geo` module.
89
96
 
90
97
  This feature also needs you to install the `gdal` package with the following command in your python environment.
91
- This package is not in the required packages due to many users may not use this function.
92
98
 
99
+ This package is not in the required packages due to many users may not use this function.
100
+
101
+ However, if you do, please consider create the conda environment as follows for your application.
102
+
103
+ ```bash
104
+ conda create -n split_raster_py310 python=3.10 -y
105
+ conda activate split_raster_py310
106
+ conda install gdal -y
107
+ conda install ipykernel -y
108
+ pip install --upgrade pip
109
+ pip install splitraster
110
+ ```
111
+
112
+ On a Mac, you can install these using Homebrew:
93
113
  ```bash
94
- conda install -c conda-forge gdal
114
+ brew install gdal
95
115
  ```
116
+ then, you can install the Python GDAL package:
117
+
118
+ ```bash
119
+ pip install GDAl
120
+ ```
121
+ Please note that installing GDAL can be complex due to its system dependencies. If you encounter issues, you may need to consult the GDAL documentation or seek help from the community.
122
+
96
123
 
97
124
  Sample Code:
98
125
 
@@ -111,6 +138,9 @@ n = geo.split_image(input_image_path, save_path, crop_size,
111
138
  print(f"{n} tiles sample of {input_image_path} are added at {save_path}")
112
139
  ```
113
140
 
141
+ Check Notebook for Details: [Tutorial_II](notebooks/Tutorial_II.ipynb)
142
+
143
+
114
144
  ## Random Sampling Code
115
145
 
116
146
  The basic implementation is still the same as the above. Just replace the 'split_image' method to 'rand_crop_image'.
@@ -142,14 +172,9 @@ n = geo.random_crop_image(input_tif_image_path, input_save_image_path, gt_tif_i
142
172
  print(f"{n} sample paris of {input_tif_image_path, gt_tif_image_path} are added at {input_save_image_path, gt_save_image_path}.")
143
173
 
144
174
  ```
145
- ## Future Update:
146
-
147
- - [x] Add Random Sampling feature.
148
- - [ ] ~~Create a GUI with Qt and generate an executable file~~ (Not a good idea, since it is not a good practice to use GUI in a package).
149
- - [ ] Add Sample Balancing feature, which can be used to balance the sample number of different classes, especially for the unbalanced dataset.
150
-
151
175
 
152
176
  ## Update Log
177
+ - 2024-Mar-23 Update github actions enable full CI/CD workflow, also add python 3.12 for build and test (https://pypi.org/project/splitraster/0.3.5/)
153
178
  - 2023-Mar-19 Update github actions and add tutorial for the package.
154
179
  - 2022-Dec-16 Upgrade the package to support python 3.8, 3.9, 3.10, 3.11 (https://pypi.org/project/splitraster/0.3.3).
155
180
  - 2022-Jan-16 Fix bugs to make package suitable for python 3.7. Publish new version at(https://pypi.org/project/splitraster/0.3.2/) .
@@ -160,54 +185,4 @@ If you run into issues or have questions, please [open an issue](https://github.
160
185
 
161
186
  If you are interested in contributing to `splitraster`, please see our [contributing guidelines](../CONTRIBUTING.md).
162
187
 
163
-
164
- ## Project Organization
165
-
166
- ├── LICENSE
167
- ├── Makefile <- Makefile with commands like `make data` or `make train`
168
- ├── README.md <- The top-level README for developers using this project.
169
- ├── data
170
- │   ├── external <- Data from third party sources.
171
- │   ├── interim <- Intermediate data that has been transformed.
172
- │   ├── processed <- The final, canonical data sets for modeling.
173
- │   └── raw <- The original, immutable data dump.
174
-
175
- ├── docs <- A default Sphinx project; see sphinx-doc.org for details
176
-
177
- ├── models <- Trained and serialized models, model predictions, or model summaries
178
-
179
- ├── notebooks <- Jupyter notebooks. Naming convention is a number (for ordering),
180
- │ the creator's initials, and a short `-` delimited description, e.g.
181
- │ `1.0-jqp-initial-data-exploration`.
182
-
183
- ├── references <- Data dictionaries, manuals, and all other explanatory materials.
184
-
185
- ├── reports <- Generated analysis as HTML, PDF, LaTeX, etc.
186
- │   └── figures <- Generated graphics and figures to be used in reporting
187
-
188
- ├── requirements.txt <- The requirements file for reproducing the analysis environment, e.g.
189
- │ generated with `pip freeze > requirements.txt`
190
-
191
- ├── setup.py <- makes project pip installable (pip install -e .) so src can be imported
192
- ├── src <- Source code for use in this project.
193
- │   ├── __init__.py <- Makes src a Python module
194
- │ │
195
- │   ├── data <- Scripts to download or generate data
196
- │   │   └── make_dataset.py
197
- │ │
198
- │   ├── features <- Scripts to turn raw data into features for modeling
199
- │   │   └── build_features.py
200
- │ │
201
- │   ├── models <- Scripts to train models and then use trained models to make
202
- │ │ │ predictions
203
- │   │   ├── predict_model.py
204
- │   │   └── train_model.py
205
- │ │
206
- │   └── visualization <- Scripts to create exploratory and results oriented visualizations
207
- │   └── visualize.py
208
-
209
- └── tox.ini <- tox file with settings for running tox; see tox.readthedocs.io
210
-
211
- ---
212
-
213
188
  <p><small>Project based on the <a target="_blank" href="https://drivendata.github.io/cookiecutter-data-science/">cookiecutter data science project template</a>. #cookiecutterdatascience</small></p>
@@ -1,9 +1,16 @@
1
1
  from setuptools import find_packages, setup
2
2
  from pathlib import Path
3
3
 
4
+
5
+ def read_requirements():
6
+ with open("requirements.txt", "r") as req_file:
7
+ requirements = req_file.read().splitlines()
8
+ return requirements
9
+
10
+
4
11
  setup(
5
12
  name="splitraster",
6
- version="0.3.5",
13
+ version="0.3.6",
7
14
  author="Chris Cui",
8
15
  license="MIT",
9
16
  platforms="any",
@@ -19,12 +26,7 @@ setup(
19
26
  ),
20
27
  python_requires=">=3.7, <3.13",
21
28
  keywords="split raster tiling ",
22
- install_requires=[
23
- "tqdm>=4.40.0, <5.0.0",
24
- "numpy>=1.19.0, <2.0.0",
25
- "scikit-image>=0.18.0, <1.0.0",
26
- # 'gdal>=3.3.0' # too many local issues for this gdal python binding.
27
- ],
29
+ install_requires=read_requirements(),
28
30
  classifiers=[
29
31
  "License :: OSI Approved :: MIT License",
30
32
  "Operating System :: OS Independent",
@@ -1,14 +1,12 @@
1
1
  from tqdm import tqdm
2
-
3
- from osgeo import gdal
4
- from osgeo import gdal_array
5
-
2
+ from osgeo import gdal, gdal_array
6
3
  import numpy as np
7
4
  from pathlib import Path
8
5
  import random
6
+ from typing import Tuple, Optional
9
7
 
10
8
 
11
- def read_rasterArray(image_path):
9
+ def read_rasterArray(image_path: str) -> Tuple[np.ndarray, Tuple[float, ...], str]:
12
10
  dataset = gdal.Open(image_path, gdal.GA_ReadOnly)
13
11
  image = dataset.ReadAsArray() # get the rasterArray
14
12
  # convert 2D raster to [1, H, W] format
@@ -19,7 +17,9 @@ def read_rasterArray(image_path):
19
17
  return image, geotrans, proj
20
18
 
21
19
 
22
- def save_rasterGeoTIF(im_data, im_geotrans, im_proj, file_name):
20
+ def save_rasterGeoTIF(
21
+ im_data: np.ndarray, im_geotrans: Tuple[float, ...], im_proj: str, file_name: str
22
+ ) -> None:
23
23
  if Path(file_name).is_file():
24
24
  print(f"Overwrite existing file: {file_name}")
25
25
 
@@ -40,7 +40,7 @@ def save_rasterGeoTIF(im_data, im_geotrans, im_proj, file_name):
40
40
  dataset = driver.Create(
41
41
  file_name, int(im_width), int(im_height), int(im_bands), datatype
42
42
  )
43
- if dataset != None:
43
+ if dataset is not None:
44
44
  dataset.SetGeoTransform(im_geotrans)
45
45
  dataset.SetProjection(im_proj)
46
46
  for i in range(im_bands):
@@ -48,33 +48,20 @@ def save_rasterGeoTIF(im_data, im_geotrans, im_proj, file_name):
48
48
  del dataset
49
49
 
50
50
 
51
- # def save_rasterArray(im_data, path, image_prototype_path):
52
-
53
- # output = gdal_array.SaveArray(
54
- # im_data, path, format="GTiff", prototype=image_prototype_path)
55
- # return True
56
-
57
-
58
- def save_rasterArray(im_data, file_name):
51
+ def save_rasterArray(im_data: np.ndarray, file_name: str) -> bool:
59
52
  if Path(file_name).is_file():
60
53
  print(f"Overwrite existing file: {file_name}")
61
- output = gdal_array.SaveArray(im_data, file_name, format="GTiff")
54
+ gdal_array.SaveArray(im_data, file_name, format="GTiff")
62
55
  return True
63
56
 
64
57
 
65
- def count_files(folder_path):
66
- count = 0
67
- for path in Path(folder_path).iterdir():
68
- if path.is_file():
69
- count += 1
70
- return count
58
+ def count_files(folder_path: str) -> int:
59
+ return sum(1 for path in Path(folder_path).iterdir() if path.is_file())
71
60
 
72
61
 
73
- def padding_mul_image(img, stride):
74
- D = img.shape[0] # this one is for (D, H, W) format Channel First.
75
- height = img.shape[1]
76
- width = img.shape[2]
77
- # get the minial padding image size
62
+ def padding_mul_image(img: np.ndarray, stride: int) -> np.ndarray:
63
+ D, height, width = img.shape # (D, H, W) format Channel First.
64
+ # get the minimal padding image size
78
65
  H = int(np.ceil(height / stride) * stride)
79
66
  W = int(np.ceil(width / stride) * stride)
80
67
 
@@ -84,13 +71,17 @@ def padding_mul_image(img, stride):
84
71
  padded_img[d, :, :] = np.pad(
85
72
  onelayer, ((0, H - height), (0, W - width)), "reflect"
86
73
  )
87
- # padded_img = np.squeeze(padded_img) # Remove axes of length one
88
74
  return padded_img
89
75
 
90
76
 
91
77
  def split_image(
92
- img_path, save_path, crop_size, repetition_rate=0, overwrite=True, ext="."
93
- ):
78
+ img_path: str,
79
+ save_path: str,
80
+ crop_size: int,
81
+ repetition_rate: float = 0,
82
+ overwrite: bool = True,
83
+ ext: Optional[str] = ".",
84
+ ) -> Optional[int]:
94
85
  # check input image
95
86
  img, geotrans, proj = read_rasterArray(img_path)
96
87
  if img is None:
@@ -104,7 +95,7 @@ def split_image(
104
95
  print(f"Input Image File Shape (D, H, W):{ img.shape}")
105
96
 
106
97
  stride = int(crop_size * (1 - repetition_rate))
107
- print(f"{crop_size=}, {stride=}")
98
+ print(f"crop_size = {crop_size}, stride = {stride}")
108
99
 
109
100
  padded_img = padding_mul_image(img, stride)
110
101
 
@@ -138,32 +129,36 @@ def split_image(
138
129
  crop_img = padded_img[:, h : h + crop_size, w : w + crop_size]
139
130
  crop_image_name = f"{new_name:04d}{ext}"
140
131
  crop_image_path = Path(save_path) / crop_image_name
141
- # save_rasterArray(crop_img, str(crop_image_path)) # just save the raster image
142
132
  save_rasterGeoTIF(crop_img, geotrans, proj, str(crop_image_path))
143
- new_name = new_name + 1
133
+ new_name += 1
144
134
  pbar.update(1)
145
135
 
146
136
  return n + 1
147
137
 
148
138
 
149
139
  def random_crop_image(
150
- img_path,
151
- img_save_path,
152
- label_path,
153
- label_save_path,
154
- crop_size=256,
155
- crop_number=20,
156
- img_ext=".tif",
157
- label_ext=".tif",
158
- overwrite=True,
159
- ):
140
+ img_path: str,
141
+ img_save_path: str,
142
+ label_path: str,
143
+ label_save_path: str,
144
+ crop_size: int = 256,
145
+ crop_number: int = 20,
146
+ img_ext: str = ".tif",
147
+ label_ext: str = ".tif",
148
+ overwrite: bool = True,
149
+ ) -> Optional[int]:
160
150
  """Generate Random cropped image pair from the input image pairs.
161
151
 
162
152
  Args:
163
153
  img_path (str): path of input image
164
- img_save_path (str):
154
+ img_save_path (str): path to save cropped images
155
+ label_path (str): path of input label
156
+ label_save_path (str): path to save cropped labels
165
157
  crop_size (int): image tile size (H,W), i.e., 256x256
166
- overwrite (bool, optional): [overwrite existing files]. Defaults to True.
158
+ crop_number (int): number of crops to generate
159
+ img_ext (str): extension for image files
160
+ label_ext (str): extension for label files
161
+ overwrite (bool): overwrite existing files
167
162
  """
168
163
  img, geotrans, proj = read_rasterArray(label_path)
169
164
  if img is None:
@@ -231,8 +226,8 @@ def random_crop_image(
231
226
  crop_image_path = Path(label_save_path) / crop_image_name
232
227
  save_rasterGeoTIF(labelCrop, geotrans, proj, str(crop_image_path))
233
228
 
234
- new_name = new_name + 1 # update image name
235
- crop_cnt = crop_cnt + 1 # add crop count
229
+ new_name += 1 # update image name
230
+ crop_cnt += 1 # add crop count
236
231
  pbar.update(1)
237
232
 
238
233
  return crop_cnt # return total crop sample pair number.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: splitraster
3
- Version: 0.3.5
3
+ Version: 0.3.6
4
4
  Summary: Provide good support for deep learning and computer vision tasks by creating a tiled output from an input raster dataset.
5
5
  Home-page: https://github.com/cuicaihao/split_raster
6
6
  Author: Chris Cui
@@ -27,6 +27,12 @@ License-File: LICENSE
27
27
 
28
28
  Provide good support for deep learning and computer vision tasks by creating a tiled output from an input raster dataset.
29
29
 
30
+ Find more details:
31
+
32
+ - Document: [split_raster](https://cuicaihao.github.io/split_raster/)
33
+ - Pypi: [splitraster](https://pypi.org/project/splitraster/)
34
+
35
+
30
36
  ## Use the packages
31
37
 
32
38
  ```bash
File without changes
@@ -1,3 +1,3 @@
1
- tqdm<5.0.0,>=4.40.0
2
1
  numpy<2.0.0,>=1.19.0
2
+ tqdm<5.0.0,>=4.40.0
3
3
  scikit-image<1.0.0,>=0.18.0