buteo

Geospatial Analysis Meets AI

Buteo is a toolbox designed to simplify the process of working with geospatial data in machine learning. It includes tools for reading, writing, and processing geospatial data, as well as tools for creating labels from vector data and generating patches from geospatial data. Buteo makes it easy to ingest data, create training data, and perform inference on geospatial data.

Please note that Buteo is under active development, and its API may not be entirely stable. When using, please pin the version of Buteo you are using to avoid breaking changes. Feel free to report any bugs or suggest improvements.

Author: Casper Fibaek (github.com/casperfibaek)

Dependencies
numba (https://numba.pydata.org/)
gdal (https://gdal.org/)

Installation
Using pip:

pip install gdal
pip install buteo

Using conda:

conda install gdal
pip install buteo

Quickstart

Reproject (and other functions) to references. (Vector and raster)

import buteo as beo

OUTDIR = "path/to/output/dir"

vector_file_correct_projection = "path/to/vector/file.gpkg"
raster_files_wrong_projection = "path/to/raster/files/*.tif:glob"

paths_to_reprojected_rasters = beo.reproject_raster(
    raster_files_with_wrong_projection,
    vector_file_with_correct_projection,
    out_path=outdir
)

paths_to_reprojected_rasters
>>> [path/to/output/dir/file1.tif, path/to/output/dir/file2.tif, ...]

Align, stack, and make patches from rasters

import buteo as beo

SRCDIR = "path/to/src/dir/"

paths_to_aligned_rasters_in_memory = beo.align_rasters(
    SRCDIR + "*.tif:glob",
)

stacked_numpy_arrays = beo.raster_to_array(
    paths_to_aligned_rasters_in_memory,
)

patches = beo.array_to_patches(
    stacked_numpy_arrays,
    256,
    n_offsets=1, # 1 overlap at 1/2 patch size (128)
)

# patches_nr, height, width, channels
patches
>>> np.ndarray([10000, 256, 256, 9])

Predict a raster using a model

import buteo as beo

RASTER_PATH = "path/to/raster/raster.tif"
RASTER_OUT_PATH = "path/to/raster/raster_pred.tif"

array = beo.raster_to_array(RASTER_PATH)

callback = model.predict # from pytorch, keras, etc..

# Predict the raster using overlaps, and borders.
# Merge using different methods. (median, mad, mean, mode, ...)
predicted = predict_array(
    array,
    callback,
    tile_size=256,
)

# Write the predicted raster to disk
beo.array_to_raster(
    predicted,
    reference=RASTER_PATH,
    out_path=RASTER_OUT_PATH,
)
# Path to the predicted raster
>>> "path/to/raster/raster_pred.tif"


Example Colabs
Create labels from OpenStreetMap data Open All Collab
Scheduled cleaning of geospatial data Open All Collab
Clip and remove noise from rasters Open All Collab
Sharpen nightlights data Open All Collab
Filters and morphological operations Open All Collab


The toolbox is being developed by ESA-Philab, NIRAS, and Aalborg University.

Dependencies

gdal numba

optional: orfeo-toolbox esa-snap

  1"""# Geospatial Analysis Meets AI
  2
  3Buteo is a toolbox designed to simplify the process of working with geospatial data in machine learning. It includes tools for reading, writing, and processing geospatial data, as well as tools for creating labels from vector data and generating patches from geospatial data. Buteo makes it easy to ingest data, create training data, and perform inference on geospatial data.
  4
  5Please note that Buteo is under active development, and its API may not be entirely stable.
  6When using, please pin the version of Buteo you are using to avoid breaking changes.
  7Feel free to report any bugs or suggest improvements.
  8
  9Author: *Casper Fibaek* (github.com/casperfibaek) </br>
 10
 11**Dependencies** </br>
 12`numba` (https://numba.pydata.org/) </br>
 13`gdal` (https://gdal.org/) </br>
 14
 15**Installation** </br>
 16Using pip:
 17```
 18pip install gdal
 19pip install buteo
 20```
 21Using conda:
 22```
 23conda install gdal
 24pip install buteo
 25```
 26
 27**Quickstart**
 28
 29### Reproject (and other functions) to references. (Vector and raster)
 30```python
 31import buteo as beo
 32
 33OUTDIR = "path/to/output/dir"
 34
 35vector_file_correct_projection = "path/to/vector/file.gpkg"
 36raster_files_wrong_projection = "path/to/raster/files/*.tif:glob"
 37
 38paths_to_reprojected_rasters = beo.reproject_raster(
 39    raster_files_with_wrong_projection,
 40    vector_file_with_correct_projection,
 41    out_path=outdir
 42)
 43
 44paths_to_reprojected_rasters
 45>>> [path/to/output/dir/file1.tif, path/to/output/dir/file2.tif, ...]
 46```
 47
 48### Align, stack, and make patches from rasters
 49```python
 50import buteo as beo
 51
 52SRCDIR = "path/to/src/dir/"
 53
 54paths_to_aligned_rasters_in_memory = beo.align_rasters(
 55    SRCDIR + "*.tif:glob",
 56)
 57
 58stacked_numpy_arrays = beo.raster_to_array(
 59    paths_to_aligned_rasters_in_memory,
 60)
 61
 62patches = beo.array_to_patches(
 63    stacked_numpy_arrays,
 64    256,
 65    n_offsets=1, # 1 overlap at 1/2 patch size (128)
 66)
 67
 68# patches_nr, height, width, channels
 69patches
 70>>> np.ndarray([10000, 256, 256, 9])
 71```
 72
 73### Predict a raster using a model
 74```python
 75import buteo as beo
 76
 77RASTER_PATH = "path/to/raster/raster.tif"
 78RASTER_OUT_PATH = "path/to/raster/raster_pred.tif"
 79
 80array = beo.raster_to_array(RASTER_PATH)
 81
 82callback = model.predict # from pytorch, keras, etc..
 83
 84# Predict the raster using overlaps, and borders.
 85# Merge using different methods. (median, mad, mean, mode, ...)
 86predicted = predict_array(
 87    array,
 88    callback,
 89    tile_size=256,
 90)
 91
 92# Write the predicted raster to disk
 93beo.array_to_raster(
 94    predicted,
 95    reference=RASTER_PATH,
 96    out_path=RASTER_OUT_PATH,
 97)
 98# Path to the predicted raster
 99>>> "path/to/raster/raster_pred.tif"
100```
101
102</br>
103
104| Example Colabs                        |                                                                                                                                                                                                               |
105|---------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
106| Create labels from OpenStreetMap data | [![Open All Collab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/casperfibaek/buteo/blob/master/examples/create_labels_from_osm_data.ipynb)            |
107| Scheduled cleaning of geospatial data | [![Open All Collab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/casperfibaek/buteo/blob/master/examples/rocket_example.ipynb)                         |
108| Clip and remove noise from rasters    | [![Open All Collab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/casperfibaek/buteo/blob/master/examples/clip_and_remove_noise_raster.ipynb)           |
109| Sharpen nightlights data              | [![Open All Collab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/casperfibaek/buteo/blob/master/examples/process_nightlights.ipynb)                    |
110| Filters and morphological operations  | [![Open All Collab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/casperfibaek/buteo/blob/master/examples/filters_and_morphology.ipynb)                 |
111
112</br>
113
114The toolbox is being developed by ESA-Philab, NIRAS, and Aalborg University.
115
116# Dependencies
117gdal
118numba
119
120optional:
121orfeo-toolbox
122esa-snap
123"""
124from .utils import *
125from .raster import *
126from .vector import *
127from .array import *
128from .eo import *
129from .ai import *
130from .bindings import *
131
132# import os; os.environ["GDAL_PAM_ENABLED"] = "FALSE"
133
134try:
135    from osgeo import gdal
136    gdal.UseExceptions()
137except ModuleNotFoundError:
138    print("GDAL not installed. Some functions may not work.")
139
140__version__ = "0.9.64"