package gdal

  1. Overview
  2. Docs

Source file data_set.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
open Ctypes

type t = T.t
let t = T.t

let t_opt = T.t_opt

exception Invalid_source
exception Invalid_projection
exception Band_error
exception Copy_error
exception Overview_error
exception Wrong_data_type

let proj_err = T.err Invalid_projection
let band_err = T.err Band_error
let copy_err = T.err Copy_error
let overview_err = T.err Overview_error

let open_ = (* 'open' is a keyword in OCaml *)
  Lib.c "GDALOpen"
    (string @-> int @-> returning t)

let close =
  Lib.c "GDALClose"
    (t @-> returning void)

let of_source ?(write = false) name =
  let h = open_ name (if write then 1 else 0) in
  if h = null then
    `Error `Invalid_source
  else
    `Ok h

let of_source_exn ?write name =
  match of_source ?write name with
  | `Ok o -> o
  | `Error _ -> raise Invalid_source

let with_source ?write name f =
  match of_source ?write name with
  | `Ok h -> Lib.protect f h ~finally:close
  | `Error _ as e -> e

let with_source_exn ?write name f =
  let h = of_source_exn ?write name in
  Lib.protect f h ~finally:close

let get_driver =
  Lib.c "GDALGetDatasetDriver"
    (t @-> returning Driver.t)

let get_projection =
  Lib.c "GDALGetProjectionRef"
    (t @-> returning string)

let get_x_size =
  Lib.c "GDALGetRasterXSize"
    (t @-> returning int)

let get_y_size =
  Lib.c "GDALGetRasterYSize"
    (t @-> returning int)

let get_count =
  Lib.c "GDALGetRasterCount"
    (t @-> returning int)

let get_band =
  Lib.c "GDALGetRasterBand"
    (t @-> int @-> returning Band.t)

let get_band_data_type t i =
  let c = get_band t i in
  Band.get_data_type c

let get_band t i kind =
  let c = get_band t i in
  if Band.check_data_type c kind then
    (c, Band.Data.to_ba_kind kind)
  else
    raise Wrong_data_type

let add_band =
  Lib.c "GDALAddBand"
    (t @-> int @-> ptr string_opt @-> returning band_err)

let add_band ?(options = []) t kind =
  let i = Band.Data.to_int kind in
  let options = Lib.convert_creation_options options in
  add_band t i (Lib.creation_options_to_ptr options)

let create_copy =
  Lib.c "GDALCreateCopy" (
    Driver.t @->
    string @->
    t @->
    int @->
    ptr string_opt @-> ptr void @-> ptr void @->
    returning t
  )

let create_copy ?(strict = false) ?(options = []) src driver name =
  let options = Lib.convert_creation_options options in
  let dst =
    create_copy driver name src
      (if strict then 1 else 0)
      (Lib.creation_options_to_ptr options) null null
  in
  if dst = null then
    `Error `Invalid_source
  else
    `Ok dst

let create_copy_exn ?strict ?options src driver name =
  match create_copy ?strict ?options src driver name with
  | `Ok c -> c
  | `Error _ -> raise Invalid_source

let create =
  Lib.c "GDALCreate" (
    Driver.t @-> string @-> int @-> int @-> int @-> int @-> ptr string_opt @->
    returning t
  )

let create ?(options = []) ?bands driver name (columns, rows) =
  let nbands, kind =
    match bands with
    | None -> 0, None
    | Some (n, kind) -> n, Some kind
  in
  let options = Lib.convert_creation_options options in
  let ds =
    create
      driver name columns rows nbands (Band.Data.to_int_opt kind)
      (Lib.creation_options_to_ptr options)
  in
  if ds = null then
    `Error `Invalid_source
  else
    `Ok ds

let create_exn ?options ?bands driver name dims =
  match create ?options ?bands driver name dims with
  | `Ok c -> c
  | `Error _ -> raise Invalid_source

let copy =
  Lib.c "GDALDatasetCopyWholeRaster"
    (t @-> t @-> ptr string_opt @-> ptr void @-> ptr void @-> returning copy_err)

let copy ?(options = []) ~src ~dst =
  let options = Lib.convert_creation_options options in
  copy src dst (Lib.creation_options_to_ptr options) null null

let set_projection =
  Lib.c "GDALSetProjection"
    (t @-> string @-> returning proj_err)

let of_band =
  Lib.c "GDALGetBandDataset"
    (Band.t @-> returning t)

let of_band (band, _) =
  of_band band

let build_overviews =
  Lib.c "GDALBuildOverviews"
    (t @-> string @-> int @-> ptr int @-> int @-> ptr int @-> ptr void @-> ptr void
     @-> returning overview_err)

let build_overviews ?bands t factors resampling =
  let n_factors, factors =
    match factors with
    | [] -> 0, null |> from_voidp int
    | f ->
      let ca = CArray.of_list int f in
      CArray.length ca, CArray.start ca
  in
  let n_bands, bands =
    match bands with
    | None
    | Some [] -> 0, null |> from_voidp int
    | Some b ->
      let ca = CArray.of_list int b in
      CArray.length ca, CArray.start ca
  in
  build_overviews t resampling n_factors factors n_bands bands null null