Initialize project
[guile-gdal] / README.md
1 # Guile GDAL Bindings
2
3 [GDAL](https://gdal.org/) Scheme bindings for [Guile](https://www.gnu.org/software/guile/) programming language.
4
5 This library allows you to perform the following tasks:
6
7 * Open a raster file for reading and writing
8 * Access metadata of raster files
9 * Compute statistics of raster files
10 * Access layers of raster dataset
11 * Helper functions, providing idiomatic Scheme interface to the GDAL APIs
12 * TODO: OGR support
13
14 ## Example
15
16 Read/write raster files using GDAL binding functions or helper functions
17
18 ```
19 (use-modules (gdal))
20 (use-modules (gdal extension))
21
22 (use-modules (rnrs bytevectors))
23
24 ;; initialize GDAL by registering GDAL drivers
25
26 (all-register)
27
28 ;; read raster dataset using binding functions
29
30 (let* ((dataset (open-dataset "raster-small.txt" GA_READONLY))
31        (h-band (get-raster-band dataset 1))
32        (x-size (get-raster-band-x-size h-band))
33        (y-size (get-raster-band-y-size h-band))
34        (size (* x-size y-size))
35        (bv (make-s32vector size)))
36     (begin
37         (raster-io h-band GF_READ 0 0 x-size y-size bv x-size y-size GDT_INT32 0 0)
38         (for-each (lambda (i) (format #t "~a " (s32vector-ref bv i)))
39           (iota size))))
40
41 (newline)
42
43 ;; or read raster dataset using helper functions in the extension module
44 ;; providing more convenient way
45
46 (let* ((dataset (open-dataset "raster-small.txt" GA_READONLY))
47        (h-band (get-raster-band dataset 1))
48        (buf (make-buffer-all-from-band h-band GDT_INT32)))
49     (for-each-pixel (lambda (p) (format #t "~a " p)) buf))
50
51 ;; transform pixels using map-pixel function, returning a new binary buffer
52 ;; of type INT16 with 1 for pixels greater than 0, and 0 otherwise.
53 ;; use write-buffer-to-file to save the buffer into the disk in GeoTIFF format.
54
55 (let* ((dataset (open-dataset "raster-small.txt" GA_READONLY))
56        (h-band (get-raster-band dataset 1))
57        (buf (make-buffer-all-from-band h-band GDT_INT32)))
58     (begin
59         (define new-buf (map-pixel (lambda (p) (if (> p 0) 1 0))
60                                    buf #:buf-type GDT_INT16))
61         (write-buffer-to-file new-buf GDN_GTIFF
62                               "new-raster-small.tif" #:no-data -1)))
63 ```
64
65 See examples folder for more code samples.