Commit 265bb07d authored by Benjamin Pillot's avatar Benjamin Pillot
Browse files

Merge branch 'master' of https://framagit.org/espace-dev/pyrasta

 Conflicts:
	pyrasta/__init__.py
	pyrasta/tools/calculator.py
parents 66056492 c128c75f
......@@ -29,7 +29,6 @@ The easiest way to install PyRasta is by using ``pip`` in a terminal
$ pip install pyrasta
```
## Examples
### Build digital elevation model from CGIAR SRTM site
......@@ -60,4 +59,7 @@ rstats = Raster("/path/to/your/raster").zonal_stats(geopandas.GeoDataFrame.from_
stats=["mean", "median", "min", "max"],
customized_stats={"my_mean": my_mean})
```
\ No newline at end of file
```
## Author
Benjamin Pillot
\ No newline at end of file
......@@ -5,7 +5,6 @@
More detailed description.
"""
__version__ = '1.2.7'
__version__ = '1.2.8'
__author__ = 'Benjamin Pillot'
__copyright__ = 'Copyright 2020, Benjamin Pillot'
__email__ = 'benjaminpillot@riseup.net'
......@@ -10,13 +10,14 @@ import numpy as np
from pyrasta.io_.files import RasterTempFile
from pyrasta.tools import _gdal_temp_dataset, _return_raster
from pyrasta.tools.mapping import GDAL_TO_NUMPY
from pyrasta.tools.windows import get_block_windows, get_xy_block_windows
from pyrasta.utils import MP_CHUNK_SIZE, split_into_chunks
from pyrasta.utils import split_into_chunks
from tqdm import tqdm
import gdal
OP_WINDOW_SIZE = 1000
@_return_raster
def _op(raster1, out_file, raster2, op_type):
......@@ -30,7 +31,7 @@ def _op(raster1, out_file, raster2, op_type):
for band in range(1, raster1.nb_band + 1):
for window in get_block_windows(1000, raster1.x_size, raster1.y_size):
for window in get_block_windows(OP_WINDOW_SIZE, raster1.x_size, raster1.y_size):
array1 = raster1._gdal_dataset.GetRasterBand(
band).ReadAsArray(*window).astype("float32")
try:
......@@ -58,7 +59,7 @@ def _op(raster1, out_file, raster2, op_type):
def _raster_calculation(raster_class, sources, fhandle, window_size,
gdal_driver, data_type, no_data, nb_processes,
chunksize, description):
chunksize):
""" Calculate raster expression
"""
......@@ -66,14 +67,10 @@ def _raster_calculation(raster_class, sources, fhandle, window_size,
window_size = (window_size, window_size)
master_raster = sources[0]
window_gen = ([src._gdal_dataset.ReadAsArray(*w).astype(GDAL_TO_NUMPY[data_type])
for src in sources] for w in get_xy_block_windows(window_size,
master_raster.x_size,
master_raster.y_size))
width = int(master_raster.x_size /
window_size[0]) + min(1, master_raster.x_size % window_size[0])
height = int(master_raster.y_size /
window_size[1]) + min(1, master_raster.y_size % window_size[1])
window_gen = ([src._gdal_dataset.ReadAsArray(*w) for src in sources] for w
in get_xy_block_windows(window_size, master_raster.x_size, master_raster.y_size))
width = int(master_raster.x_size / window_size[0]) + min(1, master_raster.x_size % window_size[0])
height = int(master_raster.y_size / window_size[1]) + min(1, master_raster.y_size % window_size[1])
with RasterTempFile(gdal_driver.GetMetadata()['DMD_EXTENSION']) as out_file:
......@@ -82,13 +79,14 @@ def _raster_calculation(raster_class, sources, fhandle, window_size,
for win_gen in tqdm(split_into_chunks(window_gen, width),
total=height,
desc=description):
desc="Calculate raster expression"):
with mp.Pool(processes=nb_processes) as pool:
result = np.concatenate(list(pool.imap(fhandle,
win_gen,
chunksize=chunksize)),
axis=1)
list_of_arrays = list(pool.map(fhandle,
win_gen,
chunksize=chunksize))
result = np.concatenate(list_of_arrays, axis=list_of_arrays[0].ndim - 1)
if is_first_run:
if result.ndim == 2:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment