Metadata-Version: 2.1
Name: up42-blockutils
Version: 0.0.3
Summary: Utilities to build blocks in UP42.
Home-page: https://www.up42.com
Author: UP42
Author-email: support@up42.com
License: UNKNOWN
Platform: UNKNOWN
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Requires-Python: >=3.6
Description-Content-Type: text/markdown

<p align="center">
    <strong>(BETA) Python package with utilities to build blocks in UP42.</strong>
</p>

<p align="center">
    <a href="https://pypi.org/project/up42-blockutils/" title="up42-blockutils on pypi"><img src="https://img.shields.io/pypi/v/up42-blockutils"></a>
    <img src="coverage.svg">
    <a href="https://twitter.com/UP42Official" title="UP42 on Twitter"><img src="https://img.shields.io/twitter/follow/UP42Official.svg?style=social"></a>
</p>

<p align="center">
    <b>
      <a href="https://up42.github.io/up42-blockutils/"> Documentation</a> &nbsp; • &nbsp;
      <a href="http://www.up42.com">UP42.com</a> &nbsp; • &nbsp;
      <a href="#support">Support</a>
    </b>
</p>

## Highlights

- Python package with utilities to build blocks in UP42.
- For UP42 partners and customers interested in creating blocks!
- Base classes for data and processing blocks.
- Facilities to easily create tests, ensuring production ready blocks.

## Overview

The UP42 `blockutils` package provides several modules with specific concerns:

- **[Blocks](blocks.md)**: Base classes for data and processing blocks.
- **[Common](common.md)**: Directory and query/parameter input handling.
- **[Logging](logging.md)**: Standard logging facility.
- **[Data Path](datapath.md)**: Input and output file handling.
- **[Exceptions](exceptions.md)**: Shared exceptions between blocks.
- **[Format](format.md)**: Exotic file handling (DIMAP and NETCDF).
- **[Geometry](geometry.md)**: Generic recurrent geometry operations.
- **[Windows](windows.md)**: Raster windowed read and write helper, useful for large file handling.
- **[STAC](stac.md)**: Handling STAC type queries.
- **[Synthetic Image](syntheticimage.md)**: Image generator, especially useful for mock testing.

## Install

The package requires Python > 3.6.

```bash
pip install up42-blockutils
```

## Quickstart

The example below shows a minimal processing block that takes a raster file and returns
the raster values to the power of 2. In this example we make use of the `logging`,
`blocks`, `exceptions`, `datapath` and `windows` modules.

```python3
from pathlib import Path
import rasterio as rio
from geojson import FeatureCollection
import blockutils

logger = blockutils.logging.getLogger(__name__)


class AProcessingBlock(blockutils.blocks.ProcessingBlock):
    def process(
        self, input_fc: FeatureCollection, process_dir: Path = Path("/tmp")
    ) -> FeatureCollection:
        output_fc = FeatureCollection([])

        if not input_fc.features:
            raise blockutils.exceptions.UP42Error(
                blockutils.exceptions.SupportedErrors.NO_INPUT_ERROR
            )

        for feat in input_fc:
            logger.info(f"Processing {feat}...")
            input_path = Path(blockutils.datapath.get_data_path(feat))
            with rio.open(input_path) as src:
                src_win = blockutils.windows.WindowsUtil(src)
                (
                    output_name,
                    output_path,
                ) = blockutils.datapath.get_output_filename_and_path(
                    input_path.name, postfix="processed"
                )
                dst_meta = src.meta.copy()
                with rio.open(output_path, "w", **dst_meta) as dst:
                    for win in src_win.windows_regular():
                        exp = src.read(window=win) ** 2
                        dst.write(exp, window=win)

                out_feat = self.get_metadata(feat)
                out_feat = blockutils.datapath.set_data_path(out_feat, output_name)
                logger.info(f"Processed {out_feat}...")
                output_fc.append(out_feat)
            return output_fc


AProcessingBlock().run()
```

## Support

For any issues or help please contact us via Email **[support@up42.com](mailto:support@up42.com)**.


