Initialize project
[guile-gdal] / examples / raster / write-raster.scm
1 #!/usr/bin/env -S guile -s
2 !#
3
4 (add-to-load-path "../..")
5
6 (use-modules (gdal))
7 (use-modules (rnrs bytevectors))
8 (use-modules (gdal extension))
9
10 (all-register)
11
12 ;; we get the dataset handle of the file "raster-small.txt" in read-only
13 ;; mode, and then get the handle of the first band. make-buffer-all-from-band
14 ;; function reads all the pixels in the input file into the memory in INT32
15 ;; format.
16
17 ;; map-pixel function, here, returns a new binary buffer of type INT16
18 ;; with 1 for pixels greater than 0, and 0 otherwise.
19
20 ;; for-each-pixel method prints all the values of
21 ;; the new raster into the console, and write-buffer-to-file saves the buffer
22 ;; into the disk in GeoTIFF format.
23
24 (let* ((dataset (open-dataset "raster-small.txt" GA_READONLY))
25        (h-band (get-raster-band dataset 1))
26        (buf (make-buffer-all-from-band h-band GDT_INT32)))
27     (begin
28         (define new-buf (map-pixel (lambda (p) (if (> p 0) 1 0))
29                                    buf #:buf-type GDT_INT16))
30         (for-each-pixel (lambda (p) (format #t "~a " p)) new-buf)
31         (write-buffer-to-file new-buf GDN_GTIFF
32                               "new-raster-small.tif" #:no-data -1)))
33
34 (newline)