Use html for the code block in README
[guile-gdal] / gdal.scm
1 (define-module (gdal)
2   #:use-module (system foreign)
3   #:use-module (rnrs bytevectors)
4   #:use-module (ice-9 q)
5   #:use-module (srfi srfi-9)
6   #:use-module (srfi srfi-1)
7   #:use-module (srfi srfi-11)
8   #:use-module (srfi srfi-4 gnu)
9   #:use-module (gdal config)
10   #:use-module (gdal internal)
11   #:re-export (GDT_UNKNOWN
12                GDT_BYTE
13                GDT_UINT16
14                GDT_INT16
15                GDT_UINT32
16                GDT_INT32
17                GDT_FLOAT32
18                GDT_FLOAT64
19                GDT_CINT16
20                GDT_CINT32
21                GDT_CFLOAT32
22                GDT_CFLOAT64
23                GDT_TYPECOUNT))
24
25 ;;------------------------------------------------------------------------------
26
27 ;;; Enums
28
29 ;;------------------------------------------------------------------------------
30
31 ;;; GDALAsyncStatusType enums
32 (define-public GARIO_PENDING 0)
33 (define-public GARIO_UPDATE 1)
34 (define-public GARIO_ERROR 2)
35 (define-public GARIO_COMPLETE 3)
36 (define-public GARIO_TYPECOUNT 4)
37
38 ;;; GDALColorInterp enums
39 (define-public GCI_UNDEFINED 0)
40 (define-public GCI_GRAY_INDEX 1)
41 (define-public GCI_PALETTE_INDEX 2)
42 (define-public GCI_RED_BAND 3)
43 (define-public GCI_GREEN_BAND 4)
44 (define-public GCI_BLUE_BAND 5)
45 (define-public GCI_ALPHA_BAND 6)
46 (define-public GCI_HUE_BAND 7)
47 (define-public GCI_SATURATION_BAND 8)
48 (define-public GCI_LIGHTNESS_BAND 9)
49 (define-public GCI_CYAN_BAND 10)
50 (define-public GCI_MAGENTA_BAND 11)
51 (define-public GCI_YELLOW_BAND 12)
52 (define-public GCI_BLACK_BAND 13)
53 (define-public GCI_YCBCR_Y_BAND 14)
54 (define-public GCI_YCBCR_CB_Band 15)
55 (define-public GCI_YCBCR_CR_Band 16)
56 (define-public GCI_MAX 16)
57
58 ;;; GDALPaletteInterp enums
59 (define-public GPI_GRAY 0)
60 (define-public GPI_RGB 1)
61 (define-public GPI_CMYK 2)
62 (define-public GPI_HLS 3)
63
64 ;;; GDALAccess enums
65 (define-public GA_READONLY 0)
66 (define-public GA_UPDATE 1)
67
68 ;;; CPLErr enums
69 (define-public CE_NONE 0)
70 (define-public CE_DEBUG 1)
71 (define-public CE_WARNING 2)
72 (define-public CE_FAILURE 3)
73 (define-public CE_FATAL 4)
74
75 ;;; GDALRWFlag enums
76 (define-public GF_READ 0)
77 (define-public GF_WRITE 1)
78
79 ;;; GDALRIOResampleAlg enums
80 (define-public GRIORA_NEAREST_NEIGHBOUR 0)
81 (define-public GRIORA_BILINEAR 1)
82 (define-public GRIORA_CUBIC 2)
83 (define-public GRIORA_CUBIC_SPLINE 3)
84 (define-public GRIORA_LANCZOS 4)
85 (define-public GRIORA_AVERAGE 5)
86 (define-public GRIORA_MODE 6)
87 (define-public GRIORA_GAUSS 7)
88
89 ;;; GDALRIOResampleAlg v2 enums
90 (define-public GRIORAv2_NEAREST_NEIGHBOUR 0)
91 (define-public GRIORAv2_BILINEAR 1)
92 (define-public GRIORAv2_CUBIC 2)
93 (define-public GRIORAv2_CUBIC_SPLINE 3)
94 (define-public GRIORAv2_LANCZOS 4)
95 (define-public GRIORAv2_AVERAGE 5)
96 (define-public GRIORAv2_MODE 6)
97 (define-public GRIORAv2_GAUSS 7)
98 (define-public GRIORAv2_AVERAGE_MAGPHASE 8)
99 (define-public GRIORAv2_NONE 9)
100
101 (define *grioeav2-to-string*
102   `((,GRIORAv2_NEAREST_NEIGHBOUR . ,"NEAREST")
103     (,GRIORAv2_BILINEAR . ,"BILINEAR")
104     (,GRIORAv2_CUBIC . ,"CUBIC")
105     (,GRIORAv2_CUBIC_SPLINE . ,"CUBICSPLINE")
106     (,GRIORAv2_LANCZOS . ,"LANCZOS")
107     (,GRIORAv2_AVERAGE . ,"AVERAGE")
108     (,GRIORAv2_MODE . ,"MODE")
109     (,GRIORAv2_GAUSS . ,"GAUSS")
110     (,GRIORAv2_AVERAGE_MAGPHASE . ,"AVERAGE_MAGPHASE")
111     (,GRIORAv2_NONE . ,"NONE")))
112
113 ;;; Enums of known driver short names
114 (define-public GDN_AAIGRID "AAIGrid")
115 (define-public GDN_ACE2 "ACE2")
116 (define-public GDN_ADRG "ADRG")
117 (define-public GDN_AERONAVFAA "AeronavFAA")
118 (define-public GDN_AIG "AIG")
119 (define-public GDN_AIRSAR "AirSAR")
120 (define-public GDN_AMIGOCLOUD "AmigoCloud")
121 (define-public GDN_ARCGEN "ARCGEN")
122 (define-public GDN_ARG "ARG")
123 (define-public GDN_AVCBIN "AVCBin")
124 (define-public GDN_AVCE00 "AVCE00")
125 (define-public GDN_BAG "BAG")
126 (define-public GDN_BIGGIF "BIGGIF")
127 (define-public GDN_BLX "BLX")
128 (define-public GDN_BMP "BMP")
129 (define-public GDN_BNA "BNA")
130 (define-public GDN_BSB "BSB")
131 (define-public GDN_BT "BT")
132 (define-public GDN_CAD "CAD")
133 (define-public GDN_CALS "CALS")
134 (define-public GDN_CARTO "Carto")
135 (define-public GDN_CEOS "CEOS")
136 (define-public GDN_CLOUDANT "Cloudant")
137 (define-public GDN_COASP "COASP")
138 (define-public GDN_COSAR "COSAR")
139 (define-public GDN_COUCHDB "CouchDB")
140 (define-public GDN_CPG "CPG")
141 (define-public GDN_CSV "CSV")
142 (define-public GDN_CSW "CSW")
143 (define-public GDN_CTABLE2 "CTable2")
144 (define-public GDN_CTG "CTG")
145 (define-public GDN_DERIVED "DERIVED")
146 (define-public GDN_DGN "DGN")
147 (define-public GDN_DIMAP "DIMAP")
148 (define-public GDN_DIPEX "DIPEx")
149 (define-public GDN_DODS "DODS")
150 (define-public GDN_DOQ1 "DOQ1")
151 (define-public GDN_DOQ2 "DOQ2")
152 (define-public GDN_DTED "DTED")
153 (define-public GDN_DXF "DXF")
154 (define-public GDN_E00GRID "E00GRID")
155 (define-public GDN_ECRGTOC "ECRGTOC")
156 (define-public GDN_EDIGEO "EDIGEO")
157 (define-public GDN_EHDR "EHdr")
158 (define-public GDN_EIR "EIR")
159 (define-public GDN_ELAS "ELAS")
160 (define-public GDN_ELASTICSEARCH "ElasticSearch")
161 (define-public GDN_ENVI "ENVI")
162 (define-public GDN_EPSILON "EPSILON")
163 (define-public GDN_ERS "ERS")
164 (define-public GDN_ESAT "ESAT")
165 (define-public GDN_ESRI_SHAPEFILE "ESRI Shapefile")
166 (define-public GDN_ESRIJSON "ESRIJSON")
167 (define-public GDN_FAST "FAST")
168 (define-public GDN_FIT "FIT")
169 (define-public GDN_FUJIBAS "FujiBAS")
170 (define-public GDN_GENBIN "GenBin")
171 (define-public GDN_GEOCONCEPT "Geoconcept")
172 (define-public GDN_GEOJSON "GeoJSON")
173 (define-public GDN_GEOMEDIA "Geomedia")
174 (define-public GDN_GEORSS "GeoRSS")
175 (define-public GDN_GFF "GFF")
176 (define-public GDN_GFT "GFT")
177 (define-public GDN_GIF "GIF")
178 (define-public GDN_GML "GML")
179 (define-public GDN_GMLAS "GMLAS")
180 (define-public GDN_GMT "GMT")
181 (define-public GDN_GNMDATABASE "GNMDatabase")
182 (define-public GDN_GNMFILE "GNMFile")
183 (define-public GDN_GPKG "GPKG")
184 (define-public GDN_GPSBABEL "GPSBabel")
185 (define-public GDN_GPSTRACKMAKER "GPSTrackMaker")
186 (define-public GDN_GPX "GPX")
187 (define-public GDN_GRASSASCIIGRID "GRASSASCIIGrid")
188 (define-public GDN_GRIB "GRIB")
189 (define-public GDN_GS7BG "GS7BG")
190 (define-public GDN_GSAG "GSAG")
191 (define-public GDN_GSBG "GSBG")
192 (define-public GDN_GSC "GSC")
193 (define-public GDN_GTIFF "GTiff")
194 (define-public GDN_GTX "GTX")
195 (define-public GDN_GXF "GXF")
196 (define-public GDN_HDF4 "HDF4")
197 (define-public GDN_HDF4IMAGE "HDF4Image")
198 (define-public GDN_HDF5 "HDF5")
199 (define-public GDN_HDF5IMAGE "HDF5Image")
200 (define-public GDN_HF2 "HF2")
201 (define-public GDN_HFA "HFA")
202 (define-public GDN_HTF "HTF")
203 (define-public GDN_HTTP "HTTP")
204 (define-public GDN_IDA "IDA")
205 (define-public GDN_IDRISI "Idrisi")
206 (define-public GDN_ILWIS "ILWIS")
207 (define-public GDN_INGR "INGR")
208 (define-public GDN_INTERLIS_1 "Interlis 1")
209 (define-public GDN_INTERLIS_2 "Interlis 2")
210 (define-public GDN_IRIS "IRIS")
211 (define-public GDN_ISCE "ISCE")
212 (define-public GDN_ISIS2 "ISIS2")
213 (define-public GDN_ISIS3 "ISIS3")
214 (define-public GDN_JAXAPALSAR "JAXAPALSAR")
215 (define-public GDN_JDEM "JDEM")
216 (define-public GDN_JML "JML")
217 (define-public GDN_JP2OPENJPEG "JP2OpenJPEG")
218 (define-public GDN_JPEG "JPEG")
219 (define-public GDN_JPEGLS "JPEGLS")
220 (define-public GDN_KML "KML")
221 (define-public GDN_KMLSUPEROVERLAY "KMLSUPEROVERLAY")
222 (define-public GDN_KRO "KRO")
223 (define-public GDN_L1B "L1B")
224 (define-public GDN_LAN "LAN")
225 (define-public GDN_LCP "LCP")
226 (define-public GDN_LEVELLER "Leveller")
227 (define-public GDN_LIBKML "LIBKML")
228 (define-public GDN_LOSLAS "LOSLAS")
229 (define-public GDN_MAP "MAP")
230 (define-public GDN_MAPINFO_FILE "MapInfo File")
231 (define-public GDN_MBTILES "MBTiles")
232 (define-public GDN_MEM "MEM")
233 (define-public GDN_MEMORY "Memory")
234 (define-public GDN_MFF "MFF")
235 (define-public GDN_MFF2 "MFF2")
236 (define-public GDN_MRF "MRF")
237 (define-public GDN_MSGN "MSGN")
238 (define-public GDN_MSSQLSPATIAL "MSSQLSpatial")
239 (define-public GDN_MVT "MVT")
240 (define-public GDN_MYSQL "MySQL")
241 (define-public GDN_NAS "NAS")
242 (define-public GDN_NDF "NDF")
243 (define-public GDN_NETCDF "netCDF")
244 (define-public GDN_NGSGEOID "NGSGEOID")
245 (define-public GDN_NITF "NITF")
246 (define-public GDN_NTV2 "NTv2")
247 (define-public GDN_NWT_GRC "NWT_GRC")
248 (define-public GDN_NWT_GRD "NWT_GRD")
249 (define-public GDN_ODBC "ODBC")
250 (define-public GDN_ODS "ODS")
251 (define-public GDN_OGR_DODS "OGR_DODS")
252 (define-public GDN_OGR_GMT "OGR_GMT")
253 (define-public GDN_OGR_OGDI "OGR_OGDI")
254 (define-public GDN_OGR_PDS "OGR_PDS")
255 (define-public GDN_OGR_SDTS "OGR_SDTS")
256 (define-public GDN_OGR_VRT "OGR_VRT")
257 (define-public GDN_OPENAIR "OpenAir")
258 (define-public GDN_OPENFILEGDB "OpenFileGDB")
259 (define-public GDN_OSM "OSM")
260 (define-public GDN_OZI "OZI")
261 (define-public GDN_PAUX "PAux")
262 (define-public GDN_PCIDSK "PCIDSK")
263 (define-public GDN_PCRASTER "PCRaster")
264 (define-public GDN_PDF "PDF")
265 (define-public GDN_PDS "PDS")
266 (define-public GDN_PDS4 "PDS4")
267 (define-public GDN_PGDUMP "PGDUMP")
268 (define-public GDN_PGEO "PGeo")
269 (define-public GDN_PLMOSAIC "PLMOSAIC")
270 (define-public GDN_PLSCENES "PLSCENES")
271 (define-public GDN_PNG "PNG")
272 (define-public GDN_PNM "PNM")
273 (define-public GDN_POSTGISRASTER "PostGISRaster")
274 (define-public GDN_POSTGRESQL "PostgreSQL")
275 (define-public GDN_PRF "PRF")
276 (define-public GDN_R "R")
277 (define-public GDN_RASTERLITE "Rasterlite")
278 (define-public GDN_RDA "RDA")
279 (define-public GDN_REC "REC")
280 (define-public GDN_RIK "RIK")
281 (define-public GDN_RMF "RMF")
282 (define-public GDN_ROI_PAC "ROI_PAC")
283 (define-public GDN_RPFTOC "RPFTOC")
284 (define-public GDN_RRASTER "RRASTER")
285 (define-public GDN_RS2 "RS2")
286 (define-public GDN_RST "RST")
287 (define-public GDN_S57 "S57")
288 (define-public GDN_SAFE "SAFE")
289 (define-public GDN_SAGA "SAGA")
290 (define-public GDN_SAR_CEOS "SAR_CEOS")
291 (define-public GDN_SDTS "SDTS")
292 (define-public GDN_SEGUKOOA "SEGUKOOA")
293 (define-public GDN_SEGY "SEGY")
294 (define-public GDN_SELAFIN "Selafin")
295 (define-public GDN_SENTINEL2 "SENTINEL2")
296 (define-public GDN_SGI "SGI")
297 (define-public GDN_SNODAS "SNODAS")
298 (define-public GDN_SOSI "SOSI")
299 (define-public GDN_SQLITE "SQLite")
300 (define-public GDN_SRP "SRP")
301 (define-public GDN_SRTMHGT "SRTMHGT")
302 (define-public GDN_SUA "SUA")
303 (define-public GDN_SVG "SVG")
304 (define-public GDN_SXF "SXF")
305 (define-public GDN_TERRAGEN "Terragen")
306 (define-public GDN_TIGER "TIGER")
307 (define-public GDN_TIL "TIL")
308 (define-public GDN_TOPOJSON "TopoJSON")
309 (define-public GDN_TSX "TSX")
310 (define-public GDN_UK_NTF "UK .NTF")
311 (define-public GDN_USGSDEM "USGSDEM")
312 (define-public GDN_VDV "VDV")
313 (define-public GDN_VFK "VFK")
314 (define-public GDN_VICAR "VICAR")
315 (define-public GDN_VRT "VRT")
316 (define-public GDN_WALK "Walk")
317 (define-public GDN_WASP "WAsP")
318 (define-public GDN_WCS "WCS")
319 (define-public GDN_WEBP "WEBP")
320 (define-public GDN_WFS "WFS")
321 (define-public GDN_WFS3 "WFS3")
322 (define-public GDN_WMS "WMS")
323 (define-public GDN_WMTS "WMTS")
324 (define-public GDN_XLS "XLS")
325 (define-public GDN_XLSX "XLSX")
326 (define-public GDN_XPLANE "XPlane")
327 (define-public GDN_XPM "XPM")
328 (define-public GDN_XYZ "XYZ")
329 (define-public GDN_ZMAP "ZMap")
330
331 ;;; OGRwkbGeometryType enums
332 (define-public WKB_UNKNOWN 0)
333 (define-public WKB_POINT 1)
334 (define-public WKB_LINE_STRING 2)
335 (define-public WKB_POLYGON 3)
336 (define-public WKB_MULTI_POINT 4)
337 (define-public WKB_MULTI_LINE_STRING 5)
338 (define-public WKB_MULTI_POLYGON 6)
339 (define-public WKB_GEOMETRY_COLLECTION 7)
340 (define-public WKB_COMPOUND_CURVE 9)
341 (define-public WKB_CURVE_POLYGON 10)
342 (define-public WKB_MULTI_CURVE 11)
343 (define-public WKB_MULTI_SURFACE 12)
344 (define-public WKB_CURVE 13)
345 (define-public WKB_SURFACE 14)
346 (define-public WKB_POLYHEDRAL_SURFACE 15)
347 (define-public WKB_TIN 16)
348 (define-public WKB_TRIANGLE 17)
349 (define-public WKB_NONE 100)
350 (define-public WKB_LINEAR_RING 101)
351 (define-public WKB_CIRCULAR_STRING_Z 1008)
352 (define-public WKB_COMPOUND_CURVE_Z 1009)
353 (define-public WKB_CURVE_POLYGON_Z 1010)
354 (define-public WKB_MULTI_CURVE_Z 1011)
355 (define-public WKB_MULTI_SURFACE_Z 1012)
356 (define-public WKB_CURVE_Z 1013)
357 (define-public WKB_SURFACE_Z 1014)
358 (define-public WKB_POLYHEDRAL_SURFACE_Z 1015)
359 (define-public WKB_TIN_Z 1016)
360 (define-public WKB_TRIANGLE_Z 1017)
361 (define-public WKB_POINT_M 2001)
362 (define-public WKB_LINE_STRING_M 2002)
363 (define-public WKB_POLYGON_M 2003)
364 (define-public WKB_MULTI_POINT_M 2004)
365 (define-public WKB_MULTI_LINE_STRING_M 2005)
366 (define-public WKB_MULTI_POLYGON_M 2006)
367 (define-public WKB_GEOMETRY_COLLECTION_M 2007)
368 (define-public WKB_CIRCULAR_STRING_M 2008)
369 (define-public WKB_COMPOUND_CURVE_M 2009)
370 (define-public WKB_CURVE_POLYGON_M 2010)
371 (define-public WKB_MULTI_CURVE_M 2011)
372 (define-public WKB_MULTI_SURFACE_M 2012)
373 (define-public WKB_CURVE_M 2013)
374 (define-public WKB_SURFACE_M 2014)
375 (define-public WKB_POLYHEDRAL_SURFACE_M 2015)
376 (define-public WKB_TIN_M 2016)
377 (define-public WKB_TRIANGLE_M 2017)
378 (define-public WKB_POINT_ZM 3001)
379 (define-public WKB_LINE_STRING_ZM 3002)
380 (define-public WKB_POLYGON_ZM 3003)
381 (define-public WKB_MULTI_POINT_ZM 3004)
382 (define-public WKB_MULTI_LINE_STRING_ZM 3005)
383 (define-public WKB_MULTI_POLYGON_ZM 3006)
384 (define-public WKB_GEOMETRY_COLLECTION_ZM 3007)
385 (define-public WKB_CIRCULAR_STRING_ZM 3008)
386 (define-public WKB_COMPOUND_CURVE_ZM 3009)
387 (define-public WKB_CURVE_POLYGON_ZM 3010)
388 (define-public WKB_MULTI_CURVE_ZM 3011)
389 (define-public WKB_MULTI_SURFACE_ZM 3012)
390 (define-public WKB_CURVE_ZM 3013)
391 (define-public WKB_SURFACE_ZM 3014)
392 (define-public WKB_POLYHEDRAL_SURFACE_ZM 3015)
393 (define-public WKB_TIN_ZM 3016)
394 (define-public WKB_TRIANGLE_ZM 3017)
395 (define-public WKB_POINT_25D #x80000001)
396 (define-public WKB_LINE_STRING_25D #x80000002)
397 (define-public WKB_POLYGON_25D #x80000003)
398 (define-public WKB_MULTI_POINT_25D #x80000004)
399 (define-public WKB_MULTI_LINE_STRING_25D #x80000005)
400 (define-public WKB_MULTI_POLYGON_25D #x80000006)
401 (define-public WKB_GEOMETRY_COLLECTION_25D #x80000007)
402
403 ;;; GMF Enums
404 (define-public GMF_ALL_VALID #x01)
405 (define-public GMF_PER_DATASET #x02)
406 (define-public GMF_ALPHA #x04)
407 (define-public GMF_NODATA #x08)
408
409 ;;; GDAL_OF Enums
410 (define-public GDAL_OF_READONLY #x00)
411 (define-public GDAL_OF_UPDATE #x01)
412 (define-public GDAL_OF_ALL #x00)
413 (define-public GDAL_OF_RASTER #x02)
414 (define-public GDAL_OF_VECTOR #x04)
415 (define-public GDAL_OF_GNM #x08)
416 (define-public GDAL_OF_MULTIDIM_RASTER #x10)
417 (define-public GDAL_OF_KIND_MASK #x1e)
418 (define-public GDAL_OF_SHARED #x20)
419 (define-public GDAL_OF_VERBOSE_ERROR #x40)
420 (define-public GDAL_OF_INTERNAL #x80)
421 (define-public GDAL_OF_DEFAULT_BLOCK_ACCESS 0)
422 (define-public GDAL_OF_ARRAY_BLOCK_ACCESS #x100)
423 (define-public GDAL_OF_HASHSET_BLOCK_ACCESS #x200)
424 (define-public GDAL_OF_BLOCK_ACCESS_MASK #x300)
425
426 ;;; ODs Enums
427 (define-public ODSC_CREATE_LAYER "CreateLayer")
428 (define-public ODSC_DELETE_LAYER "DeleteLayer")
429 (define-public ODSC_CREATE_GEOM_FIELD_AFTER_CREATE_LAYER
430   "CreateGeomFieldAfterCreateLayer")
431 (define-public ODSC_CURVE_GEOMETRIES "CurveGeometries")
432 (define-public ODSC_TRANSACTIONS "Transactions")
433 (define-public ODSC_EMULATED_TRANSACTIONS "EmulatedTransactions")
434 (define-public ODSC_RANDOM_LAYER_READ "RandomLayerRead")
435 (define-public ODSC_RANDOM_LAYER_WRITE "RandomLayerWrite")
436
437 ;;; OGRERR Enums
438 (define-public OGRERR_NONE 0)
439 (define-public OGRERR_NOT_ENOUGH_DATA 1)
440 (define-public OGRERR_NOT_ENOUGH_MEMORY 2)
441 (define-public OGRERR_UNSUPPORTED_GEOMETRY_TYPE 3)
442 (define-public OGRERR_UNSUPPORTED_OPERATION 4)
443 (define-public OGRERR_CORRUPT_DATA 5)
444 (define-public OGRERR_FAILURE 6)
445 (define-public OGRERR_UNSUPPORTED_SRS 7)
446 (define-public OGRERR_INVALID_HANDLE 8)
447 (define-public OGRERR_NON_EXISTING_FEATURE 9)
448
449 ;;; RAT Enums
450 (define-public GFU_GENERIC 0)
451 (define-public GFU_PIXEL_COUNT 1)
452 (define-public GFU_NAME 2)
453 (define-public GFU_MIN 3)
454 (define-public GFU_MAX 4)
455 (define-public GFU_MIN_MAX 5)
456 (define-public GFU_RED 6)
457 (define-public GFU_GREEN 7)
458 (define-public GFU_BLUE 8)
459 (define-public GFU_ALPHA 9)
460 (define-public GFU_RED_MIN 10)
461 (define-public GFU_GREEN_MIN 11)
462 (define-public GFU_BLUE_MIN 12)
463 (define-public GFU_ALPHA_MIN 13)
464 (define-public GFU_RED_MAX 14)
465 (define-public GFU_GREEN_MAX 15)
466 (define-public GFU_BLUE_MAX 16)
467 (define-public GFU_ALPHA_MAX 17)
468
469 ;;; GFT Enums
470 (define-public GFT_INTEGER 0)
471 (define-public GFT_REAL 1)
472 (define-public GFT_STRING 2)
473
474 ;;; GRTT Enums
475 (define-public GRTT_THEMATIC 0)
476 (define-public GRTT_ATHEMATIC 1)
477
478 ;;------------------------------------------------------------------------------
479
480 ;;; Structures
481
482 ;;------------------------------------------------------------------------------
483
484 ;;; GCP
485
486 (define-record-type <gcp>
487   (%make-gcp id info pixel line x y z)
488   gcp?
489   (id gcp-id set-gcp-id!)
490   (info gcp-info set-gcp-info!)
491   (pixel gcp-pixel set-gcp-pixel!)
492   (line gcp-line set-gcp-line!)
493   (x gcp-x set-gcp-x!)
494   (y gcp-y set-gcp-y!)
495   (z gcp-z set-gcp-z!))
496
497 (define* (make-gcp #:key
498                    (id "")
499                    (info "")
500                    (pixel 0.0)
501                    (line 0.0)
502                    (x 0.0)
503                    (y 0.0)
504                    (z 0.0))
505   (%make-gcp id info pixel line x y z))
506
507 (export make-gcp
508         gcp?
509         gcp-id
510         set-gcp-id!
511         gcp-info
512         set-gcp-info!
513         gcp-pixel
514         set-gcp-pixel!
515         gcp-line
516         set-gcp-line!
517         gcp-x
518         set-gcp-x!
519         gcp-y
520         set-gcp-y!
521         gcp-z
522         set-gcp-z!)
523
524 (define gcp-types (list '* '* double double double double double))
525
526 (define (gcp->pointer record)
527   (if (gcp? record)
528     (make-c-struct
529      gcp-types
530      (list (string->pointer (gcp-id record))
531            (string->pointer (gcp-info record))
532            (gcp-pixel record)
533            (gcp-line record)
534            (gcp-x record)
535            (gcp-y record)
536            (gcp-z record)))
537     %null-pointer))
538
539 (define (pointer->gcp pointer)
540   (let ((lst (parse-c-struct pointer gcp-types)))
541     (make-gcp #:id (pointer->string (list-ref lst 0))
542               #:info (pointer->string (list-ref lst 1))
543               #:pixel (list-ref lst 2)
544               #:line (list-ref lst 3)
545               #:x (list-ref lst 4)
546               #:y (list-ref lst 5)
547               #:z (list-ref lst 6))))
548
549 (define (gcp-list->pointer lst)
550   (struct-list->pointer lst (sizeof gcp-types) gcp->pointer))
551
552 (define (pointer->gcp-list pointer count)
553   (pointer->struct-list pointer count (sizeof gcp-types) pointer->gcp))
554
555 (export pointer->gcp-list)
556 ;;; GDALRasterIOExtraArg
557
558 (define-record-type <grioea>
559   (%make-grioea version resample-alg progress-callback progress-data
560                 is-fp-window-valid x-off y-off x-size y-size)
561   grioea?
562   (version grioea-version set-grioea-version!)
563   (resample-alg grioea-resample-alg set-grioea-resample-alg!)
564   (progress-callback grioea-progress-callback set-grioea-progress-callback!)
565   (progress-data grioea-progress-data set-grioea-progress-data!)
566   (is-fp-window-valid grioea-is-fp-window-valid set-grioea-is-fp-window-valid!)
567   (x-off grioea-x-off set-grioea-x-off!)
568   (y-off grioea-y-off set-grioea-y-off!)
569   (x-size grioea-x-size set-grioea-x-size!)
570   (y-size grioea-y-size set-grioea-y-size!))
571
572 (define (gdal-progress-func progress-callback)
573   (if (null? progress-callback)
574     %null-pointer
575     (procedure->pointer int
576                         (lambda (complete message progress-arg)
577                                 (progress-callback complete
578                                                    (pointer->string message)
579                                                    progress-arg))
580                         (list double '* '*))))
581
582 (define* (make-grioea #:key
583                       (version 1)
584                       (resample-alg GRIORA_NEAREST_NEIGHBOUR)
585                       (progress-callback '())
586                       (progress-data %null-pointer)
587                       (is-fp-window-valid #f)
588                       (x-off 0.0)
589                       (y-off 0.0)
590                       (x-size 0.0)
591                       (y-size 0.0))
592   (%make-grioea version resample-alg
593                 (gdal-progress-func progress-callback)
594                 progress-data
595                 (boolean->c-bool is-fp-window-valid)
596                 x-off y-off x-size y-size))
597
598 (export make-grioea
599         grioea?
600         grioea-version
601         set-grioea-version!
602         grioea-resample-alg
603         set-grioea-resample-alg!
604         grioea-progress-callback
605         set-grioea-progress-callback!
606         grioea-progress-data
607         set-grioea-progress-data!
608         grioea-is-fp-window-valid
609         set-grioea-is-fp-window-valid!
610         grioea-x-off
611         set-grioea-x-off!
612         grioea-y-off
613         set-grioea-y-off!
614         grioea-x-size
615         set-grioea-x-size!
616         grioea-y-size
617         set-grioea-y-size!)
618
619 (define (grioea->foreign-pointer record)
620   (if (grioea? record)
621     (make-c-struct
622      (list int int '* '* int double double double double)
623      (list (grioea-version record)
624            (grioea-resample-alg record)
625            (grioea-progress-callback record)
626            (grioea-progress-data record)
627            (grioea-is-fp-window-valid record)
628            (grioea-x-off record)
629            (grioea-y-off record)
630            (grioea-x-size record)
631            (grioea-y-size record)))
632     %null-pointer))
633
634 (define (gdal-term-progress complete message arg)
635   "Simple progress report to terminal.
636
637 This progress reporter prints simple progress report to the terminal window.
638 The progress report generally looks something like this:
639
640 17.0...33.0...50.0...67.0...83.0...100.0.
641
642 Use it when you create grioea record via \"make-grioea\" function:
643
644   Sample:
645
646 (make-grioea #:progress-callback gdal-term-progress)"
647   (let ((perc (round (* complete 100))))
648     (display perc)
649     (if (= perc 100)
650       (newline)
651       (display "..."))
652     1))
653
654 (export gdal-term-progress)
655
656 ;;------------------------------------------------------------------------------
657
658 ;;; GDAL Function Bindings
659
660 ;;------------------------------------------------------------------------------
661
662 (define-gdal-foreign %gdal-all-register
663   void "GDALAllRegister" '() 20)
664
665 (define (all-register)
666   "Register all known configured GDAL drivers."
667   (%gdal-all-register))
668
669 (export all-register)
670
671 ;;------------------------------------------------------------------------------
672
673 (define-gdal-foreign %cpl-error-reset
674   void "CPLErrorReset" '() 20)
675
676 (define (reset-error)
677   "Erase any traces of previous errors.
678
679 This is normally used to ensure that an error which has been recovered from
680 does not appear to be still in play with high level functions."
681   (%cpl-error-reset))
682
683 (export reset-error)
684
685 ;;------------------------------------------------------------------------------
686
687 (define-gdal-foreign %cpl-get-last-error-msg
688   '* "CPLGetLastErrorMsg" '() 20)
689
690 (define (get-last-error-message)
691     "Get the last error message.
692
693 Fetches the last error message posted with CPLError(), that hasn't been cleared
694 by reset-error."
695   (let ((ptr (%cpl-get-last-error-msg)))
696     (if (null-pointer? ptr)
697              ""
698         (pointer->string ptr))))
699
700 (export get-last-error-message)
701
702 ;;------------------------------------------------------------------------------
703
704 (define-gdal-foreign %gdal-data-type-size
705   int "GDALGetDataTypeSize" (list int) 20)
706
707 (define (get-data-type-size data-type)
708     "Get data type size in bits.
709
710 Deprecated.
711
712 Returns the size of a GDT_* type in bits, not bytes!
713 Use get-data-type-size-bytes for bytes. Use get-data-type-size-bits for bits.
714
715 Parameters:
716     data-type: type, such as GDT_BYTE."
717   (let ((result (%gdal-data-type-size data-type)))
718     (if (zero? result)
719         (error "failed to recognize data type")
720         result)))
721
722 (export get-data-type-size)
723
724 ;;------------------------------------------------------------------------------
725
726 (define-gdal-foreign %gdal-data-type-size-bits
727   int "GDALGetDataTypeSizeBits" (list int) 20)
728
729 (define (get-data-type-size-bits data-type)
730     "Get data type size in bits.
731
732 Returns the size of a GDT_* type in bits, not bytes!
733 Use get-data-type-size-bytes for bytes.
734
735 Parameters:
736     data-type: type, such as GDT_BYTE."
737   (let ((result (%gdal-data-type-size-bits data-type)))
738     (if (zero? result)
739         (error "failed to recognize data type")
740         result)))
741
742 (export get-data-type-size-bits)
743
744 ;;------------------------------------------------------------------------------
745
746 (define-gdal-foreign %gdal-data-type-size-bytes
747   int "GDALGetDataTypeSizeBytes" (list int) 20)
748
749 (define (get-data-type-size-bytes data-type)
750     "Get data type size in bytes.
751
752 Returns the size of a GDT_* type in bytes. In contrast, get-data-type-size and
753 get-data-type-size-bits return the size in bits.
754
755 Parameters:
756     data-type: type, such as GDT_BYTE."
757   (let ((result (%gdal-data-type-size-bytes data-type)))
758     (if (zero? result)
759         (error "failed to recognize data type")
760         result)))
761
762 (export get-data-type-size-bytes)
763
764 ;;------------------------------------------------------------------------------
765
766 (define-gdal-foreign %gdal-data-type-is-complex
767   int "GDALDataTypeIsComplex" (list int) 20)
768
769 (define (data-type-is-complex? data-type)
770     "Is data type complex?
771
772 Returns #t if the passed type is complex (one of GDT_CINT16, GDT_CINT32,
773 GDT_CFLOAT32 or GDT_CFLOAT64), that is it consists of a real and imaginary
774 component.
775
776 Parameters:
777     data-type: type, such as GDT_BYTE."
778   (let ((result (%gdal-data-type-is-complex data-type)))
779     (if (= result 1)
780              #t
781         #f)))
782
783 (export data-type-is-complex?)
784
785 ;;------------------------------------------------------------------------------
786
787 (define-gdal-foreign %gdal-data-type-is-integer
788   int "GDALDataTypeIsInteger" (list int) 23)
789
790 (define (data-type-is-integer? data-type)
791     "Is data type integer? (might be complex)
792
793 Returns #t if the passed type is integer (one of GDT_BYTE, GDT_INT16,
794 GDT_UINT16, GDT_INT32, GDT_UINT32, GDT_CINT16 or GDT_CINT32).
795
796 Parameters:
797     data-type: type, such as GDT_BYTE."
798   (let ((result (%gdal-data-type-is-integer data-type)))
799     (if (= result 1)
800              #t
801         #f)))
802
803 (export data-type-is-integer?)
804
805 ;;------------------------------------------------------------------------------
806
807 (define-gdal-foreign %gdal-data-type-is-floating
808   int "GDALDataTypeIsFloating" (list int) 23)
809
810 (define (data-type-is-floating? data-type)
811     "Is data type floating? (might be complex)
812
813 Returns #t if the passed type is floating (one of GDT_FLOAT32, GDT_FLOAT64,
814 GDT_CFLOAT32, GDT_CFLOAT64).
815
816 Parameters:
817     data-type: type, such as GDT_BYTE."
818   (let ((result (%gdal-data-type-is-floating data-type)))
819     (if (= result 1)
820              #t
821         #f)))
822
823 (export data-type-is-floating?)
824
825 ;;------------------------------------------------------------------------------
826
827 (define-gdal-foreign %gdal-data-type-is-signed
828   int "GDALDataTypeIsSigned" (list int) 23)
829
830 (define (data-type-is-signed? data-type)
831     "Is data type signed?
832
833 Returns #t if the passed type is signed.
834
835 Parameters:
836     data-type: type, such as GDT_BYTE."
837   (let ((result (%gdal-data-type-is-signed data-type)))
838     (if (= result 1)
839              #t
840         #f)))
841
842 (export data-type-is-signed?)
843
844 ;;------------------------------------------------------------------------------
845
846 (define-gdal-foreign %gdal-get-data-type-name
847   '* "GDALGetDataTypeName" (list int) 20)
848
849 (define (get-data-type-name data-type)
850     "Get name of data type.
851
852 Returns a symbolic name for the data type. This is essentially the enumerated
853 item name with the GDT_ prefix removed. So GDT_BYTE returns 'Byte'.
854
855 Parameters:
856     data-type: type to get name of."
857   (let ((ptr (%gdal-get-data-type-name data-type)))
858     (if (null-pointer? ptr)
859              (error "failed to recognize data type")
860         (pointer->string ptr))))
861
862 (export get-data-type-name)
863
864 ;;------------------------------------------------------------------------------
865
866 (define-gdal-foreign %gdal-get-data-type-by-name
867   int "GDALGetDataTypeByName" (list '*) 20)
868
869 (define (get-data-type-by-name name)
870     "Get data type by symbolic name.
871
872 Returns a data type corresponding to the given symbolic name. This function is
873 opposite to the get-data-type-name.
874
875 Parameters:
876     name: string containing the symbolic name of the type."
877   (let ((result (%gdal-get-data-type-by-name (string->pointer name))))
878     (if (zero? result)
879              (error "failed to recognize data type")
880         result)))
881
882 (export get-data-type-by-name)
883
884 ;;------------------------------------------------------------------------------
885
886 (define-gdal-foreign %gdal-data-type-union
887   int "GDALDataTypeUnion" (list int int) 20)
888
889 (define (data-type-union data-type-1 data-type-2)
890     "Return the smallest data type that can fully express both input data
891 types.
892
893 Parameters:
894     data-type-1: first data type.
895     data-type-2: second data type."
896   (let ((result (%gdal-data-type-union data-type-1 data-type-2)))
897     (if (zero? result)
898              (error "failed to recognize data type")
899         result)))
900
901 (export data-type-union)
902
903 ;;------------------------------------------------------------------------------
904
905 (define-gdal-foreign %gdal-data-type-union-with-value
906   int "GDALDataTypeUnionWithValue" (list int double int) 23)
907
908 (define (data-type-union-with-value data-type value is-complex)
909     "Union a data type with the one found for a value.
910
911 Parameters:
912     data-type: the first data type
913     value: the value for which to find a data type and union with 'data-type'
914     is-complex: boolean, #t if the value is complex."
915   (let ((result (%gdal-data-type-union-with-value
916                  data-type value (boolean->c-bool is-complex))))
917     (if (zero? result)
918              (error "failed to union data type")
919         result)))
920
921 (export data-type-union-with-value)
922
923 ;;------------------------------------------------------------------------------
924
925 (define-gdal-foreign %gdal-find-data-type
926   int "GDALFindDataType" (list int int int int) 23)
927
928 (define (find-data-type n-bits is-signed is-floating is-complex)
929     "Finds the smallest data type able to support the given requirements.
930
931 Parameters:
932     n-bits:     number of bits necessary
933     is-signed: if negative values are necessary
934     is-floating: if non-integer values necessary
935     is-complex: if complex values are necessary."
936   (let ((result (%gdal-find-data-type n-bits (boolean->c-bool is-signed)
937                  (boolean->c-bool is-floating) (boolean->c-bool is-complex))))
938     (if (zero? result)
939              (error "failed to find data type")
940         result)))
941
942 (export find-data-type)
943
944 ;;------------------------------------------------------------------------------
945
946 (define-gdal-foreign %gdal-find-data-type-for-value
947   int "GDALFindDataTypeForValue" (list double int) 23)
948
949 (define (find-data-type-for-value value is-complex)
950     "Finds the smallest data type able to support the provided value.
951
952 Parameters:
953     value: double       value to support
954     is-complex: is the value complex."
955   (let ((result (%gdal-find-data-type-for-value value
956                                                 (boolean->c-bool is-complex))))
957     (if (zero? result)
958              (error "failed to find data type")
959         result)))
960
961 (export find-data-type-for-value)
962
963 ;;------------------------------------------------------------------------------
964
965 (define-gdal-foreign %gdal-adjust-data-type-to-value
966   double "GDALAdjustValueToDataType" (list int double '* '*) 20)
967
968 (define (adjust-data-type-to-value data-type value)
969     "Adjust a value to the output data type.
970
971 Adjustment consist in clamping to minimum/maxmimum values of the data type and
972 rounding for integral types.
973
974 Returns multiple (3) values as adjusted double value,
975 boolean value to indicate if clamping has been made, and boolean value to
976 indicate if rounding has been made.
977
978 Parameters:
979     data-type: target data type
980     value: double value to adjust."
981   (let* ((p-clamped (make-bytevector (sizeof int)))
982         (p-rounded (make-bytevector (sizeof int)))
983         (result (%gdal-adjust-data-type-to-value
984                  data-type
985                  value
986                  (bytevector->pointer p-clamped)
987                  (bytevector->pointer p-rounded))))
988     (values
989      result
990      (c-bool->boolean (bytevector-sint-ref p-clamped
991                                            0
992                                            (native-endianness)
993                                            (sizeof int)))
994      (c-bool->boolean (bytevector-sint-ref p-rounded
995                                            0
996                                            (native-endianness)
997                                            (sizeof int))))))
998
999 (export adjust-data-type-to-value)
1000
1001 ;;------------------------------------------------------------------------------
1002
1003 (define-gdal-foreign %gdal-get-non-complex-data-type
1004   int "GDALGetNonComplexDataType" (list int) 23)
1005
1006 (define (get-non-complex-data-type data-type)
1007     "Return the base data type for the specified input.
1008
1009 If the input data type is complex this function returns the base type i.e.
1010 the data type of the real and imaginary parts (non-complex). If the input data
1011 type is already non-complex, then it is returned unchanged.
1012
1013 Parameters:
1014     data-type: type, such as GDT_CFLOAT32."
1015   (if (data-type-valid? data-type)
1016     (%gdal-get-non-complex-data-type data-type)
1017     (error "data-type is out of bounds")))
1018
1019 (export get-non-complex-data-type)
1020
1021 ;;------------------------------------------------------------------------------
1022
1023 (define-gdal-foreign %gdal-data-type-is-conversion-lossy
1024   int "GDALDataTypeIsConversionLossy" (list int int) 23)
1025
1026 (define (data-type-is-conversion-lossy? data-type-from data-type-to)
1027     "Is conversion from data-type-from to data-type-to potentially lossy.
1028
1029 Parameters:
1030     data-type-from: input datatype
1031     data-type-to: output datatype."
1032   (if (and (data-type-valid? data-type-from) (data-type-valid? data-type-to))
1033     (c-bool->boolean (%gdal-data-type-is-conversion-lossy
1034                       data-type-from
1035                       data-type-to))
1036     (error "data-type is out of bounds")))
1037
1038 (export data-type-is-conversion-lossy?)
1039
1040 ;;------------------------------------------------------------------------------
1041
1042 (define-gdal-foreign %gdal-get-async-status-type-name
1043   '* "GDALGetAsyncStatusTypeName" (list int) 20)
1044
1045 (define (get-async-status-type-name async-status-type)
1046     "Get name of AsyncStatus data type.
1047
1048 Returns a symbolic name for the AsyncStatus data type. This is essentially the
1049 the enumerated item name with the GARIO_ prefix removed. So GARIO_COMPLETE
1050 returns 'COMPLETE'.
1051
1052 Parameters:
1053     async-status-type: type to get name of."
1054     (let ((ptr (%gdal-get-async-status-type-name async-status-type)))
1055       (if (null-pointer? ptr)
1056              (error "failed to recognize async status type")
1057         (pointer->string ptr))))
1058
1059 (export get-async-status-type-name)
1060
1061 ;;------------------------------------------------------------------------------
1062
1063 (define-gdal-foreign %gdal-get-async-status-type-by-name
1064   int "GDALGetAsyncStatusTypeByName" (list '*) 20)
1065
1066 (define (get-async-status-type-by-name name)
1067     "Get AsyncStatusType by symbolic name.
1068
1069 Returns a data type corresponding to the given symbolic name. This function is
1070 opposite to the get-async-status-type-name.
1071
1072 Parameters:
1073     name: string containing the symbolic name of the type."
1074     (let ((result (%gdal-get-async-status-type-by-name (string->pointer name))))
1075       (if (= GARIO_ERROR result)
1076              (error "failed to recognize async status type")
1077         result)))
1078
1079 (export get-async-status-type-by-name)
1080
1081 ;;------------------------------------------------------------------------------
1082
1083 (define-gdal-foreign %gdal-get-color-interpretation-name
1084   '* "GDALGetColorInterpretationName" (list int) 20)
1085
1086 (define (get-color-interpretation-name color-interpretation-type)
1087     "Get name of color interpretation.
1088
1089 Returns a symbolic name for the color interpretation. This is derived from the
1090 enumerated item name with the GCI_ prefix removed, but there are some
1091 variations. So GCI_GRAY_INDEX returns 'Gray' and GCI_RED_BAND returns 'Red'.
1092
1093 Parameters:
1094     color-interpretation-type: color interpretation to get name of."
1095     (let ((ptr (%gdal-get-color-interpretation-name
1096                 color-interpretation-type)))
1097       (if (null-pointer? ptr)
1098              (error "failed to recognize color interpretation type")
1099         (pointer->string ptr))))
1100
1101 (export get-color-interpretation-name)
1102
1103 ;;------------------------------------------------------------------------------
1104
1105 (define-gdal-foreign %gdal-get-color-interpretation-by-name
1106   int "GDALGetColorInterpretationByName" (list '*) 20)
1107
1108 (define (get-color-interpretation-by-name name)
1109     "Get color interpretation by symbolic name.
1110
1111 Returns a color interpretation corresponding to the given symbolic name. This
1112 function is opposite to the get-color-interpretation-name.
1113
1114 Parameters:
1115     name: string containing the symbolic name of the color interpretation."
1116     (let ((result (%gdal-get-color-interpretation-by-name
1117                    (string->pointer name))))
1118       (if (= GCI_UNDEFINED result)
1119              (error "failed to recognize color interpretation type")
1120         result)))
1121
1122 (export get-color-interpretation-by-name)
1123
1124 ;;------------------------------------------------------------------------------
1125
1126 (define-gdal-foreign %gdal-get-palette-interpretation-name
1127   '* "GDALGetPaletteInterpretationName" (list int) 20)
1128
1129 (define (get-palette-interpretation-name palette-interpretation-type)
1130     "Get name of palette interpretation.
1131
1132 Returns a symbolic name for the palette interpretation. This is the the
1133 enumerated item name with the GPI_ prefix removed. So GPI_Gray returns 'Gray'.
1134
1135 Parameters:
1136     palette-interpretation-type: palette interpretation to get name of."
1137     (let ((ptr (%gdal-get-palette-interpretation-name
1138                 palette-interpretation-type)))
1139       (if (null-pointer? ptr)
1140              (error "failed to recognize palette interpretation type")
1141         (pointer->string ptr))))
1142
1143 (export get-palette-interpretation-name)
1144
1145 ;;------------------------------------------------------------------------------
1146
1147 (define-gdal-foreign %gdal-create
1148   '* "GDALCreate" (list '* '* int int int int '*) 20)
1149
1150 (define (create-dataset driver file-name x-size y-size n-bands band-type)
1151     "Create a new dataset with this driver.
1152
1153 Parameters:
1154     driver: gdal driver
1155     file-name: the name of the dataset to create
1156     x-size: width of created raster in pixels
1157     y-size: height of created raster in pixels
1158     n-bands: number of bands
1159     band-type: type of raster."
1160   (let ((ptr (%gdal-create driver
1161                            (string->pointer file-name)
1162                            x-size
1163                            y-size
1164                            n-bands
1165                            band-type
1166                            %null-pointer)))
1167     (if (null-pointer? ptr)
1168              (error "failed to create GDAL dataset")
1169         ptr)))
1170
1171 (export create-dataset)
1172
1173 ;;------------------------------------------------------------------------------
1174
1175 (define-gdal-foreign %gdal-create-copy
1176   '* "GDALCreateCopy" (list '* '* '* int '* '* '*) 20)
1177
1178 (define (copy-dataset driver file-name source-dataset is-strict)
1179     "Create a copy of a dataset.
1180
1181 Parameters:
1182     driver: GDAL driver
1183     file-name:  the name for the new dataset
1184     source-dataset: the dataset being duplicated.
1185     is-strict: #t if the copy must be strictly equivalent, or more normally
1186 #f indicating that the copy may adapt as needed for the output format."
1187   (let ((ptr (%gdal-create-copy driver
1188                                 (string->pointer file-name)
1189                                 source-dataset
1190                                 (boolean->c-bool is-strict)
1191                                 %null-pointer
1192                                 %null-pointer
1193                                 %null-pointer)))
1194     (if (null-pointer? ptr)
1195              (error "failed to create GDAL dataset")
1196         ptr)))
1197
1198 (export copy-dataset)
1199
1200 ;;------------------------------------------------------------------------------
1201
1202 (define-gdal-foreign %gdal-identify-driver
1203   '* "GDALIdentifyDriver" (list '* '*) 20)
1204
1205 (define (identify-driver file-name)
1206     "Identify the driver that can open a raster file.
1207
1208 Parameters:
1209     file-name: the name of the file to access. In the case of exotic drivers
1210 this may not refer to a physical file, but instead contain information for the
1211 driver on how to access a dataset."
1212     (let ((ptr (%gdal-identify-driver
1213                 (string->pointer file-name)
1214                 %null-pointer)))
1215       (if (null-pointer? ptr)
1216              (error "failed to identify driver")
1217         ptr)))
1218
1219 (export identify-driver)
1220
1221 ;;------------------------------------------------------------------------------
1222
1223 (define-gdal-foreign %gdal-open
1224   '* "GDALOpen" (list '* int) 20)
1225
1226 (define (open-dataset file-name access)
1227     "Open a raster file as a GDALDataset pointer.
1228
1229 Parameters:
1230     file-name: the name of the file to access. In the case of exotic drivers
1231 this may not refer to a physical file, but instead contain information for the
1232 driver on how to access a dataset.
1233     access: the desired access, either GA_UPDATE or GA_READONLY. Many drivers
1234 support only read only access."
1235     (let ((ptr (%gdal-open
1236                 (string->pointer file-name)
1237                 access)))
1238       (if (null-pointer? ptr)
1239              (error "failed to open dataset")
1240         ptr)))
1241
1242 (export open-dataset)
1243
1244 ;;------------------------------------------------------------------------------
1245
1246 (define-gdal-foreign %gdal-open-ex
1247   '* "GDALOpenEx" (list '* int '* '* '*) 20)
1248
1249 (define* (open-dataset-ex file-name flags #:key
1250                           (allowed-drivers '())
1251                           (open-options '())
1252                           (sibling-files '()))
1253     "Open a raster or vector file as a GDALDataset.
1254
1255 Parameters:
1256     file-name: the name of the file to access. In the case of exotic drivers
1257 this may not refer to a physical file, but instead contain information for the
1258 driver on how to access a dataset.
1259     flags: a combination of GDAL_OF_ flags that may be combined through
1260 logical or operator.
1261     allowed-drivers (optional): empty list '() to consider all candidate
1262 drivers, or a list of strings with the driver short names that must be
1263 considered.
1264     open-options (optional): empty list '(), or a list of strings with open
1265 options passed to candidate drivers.
1266     sibling-files (optional): empty list '() or a list of strings that are
1267 filenames that are auxiliary to the main filename."
1268     (let ((ptr (%gdal-open-ex
1269                 (string->pointer file-name)
1270                 flags
1271                 (string-list->pointerpointer allowed-drivers)
1272                 (string-list->pointerpointer open-options)
1273                 (string-list->pointerpointer sibling-files))))
1274       (if (null-pointer? ptr)
1275              (error "failed to open dataset")
1276         ptr)))
1277
1278 (export open-dataset-ex)
1279
1280 ;;------------------------------------------------------------------------------
1281
1282 (define-gdal-foreign %gdal-open-shared
1283   '* "GDALOpenShared" (list '* int) 20)
1284
1285 (define (open-shared-dataset file-name access)
1286     "Open a raster file as a GDALDataset pointer.
1287
1288 Parameters:
1289     file-name: the name of the file to access. In the case of exotic drivers
1290 this may not refer to a physical file, but instead contain information for the
1291 driver on how to access a dataset.
1292     access: the desired access, either GA_UPDATE or GA_READONLY. Many drivers
1293 support only read only access."
1294     (let ((ptr (%gdal-open-shared
1295                 (string->pointer file-name)
1296                 access)))
1297       (if (null-pointer? ptr)
1298              (error "failed to open dataset")
1299         ptr)))
1300
1301 (export open-shared-dataset)
1302
1303 ;;------------------------------------------------------------------------------
1304
1305 (define-gdal-foreign %gdal-get-driver-by-name
1306   '* "GDALGetDriverByName" (list '*) 20)
1307
1308 (define (get-driver-by-name driver-short-name)
1309     "Fetch a driver based on the short name.
1310
1311 Parameters:
1312     driver-short-name: the short name of the driver, such as 'GTiff' as a
1313 string or GDN_GTIFF as an enum (see GDN_*), being searched for."
1314     (let ((ptr (%gdal-get-driver-by-name
1315                 (string->pointer driver-short-name))))
1316       (if (null-pointer? ptr)
1317              (error "failed to find driver")
1318         ptr)))
1319
1320 (export get-driver-by-name)
1321
1322 ;;------------------------------------------------------------------------------
1323
1324 (define-gdal-foreign %gdal-get-driver-count
1325   int "GDALGetDriverCount" '() 20)
1326
1327 (define (get-driver-count)
1328     "Fetch the number of registered drivers."
1329     (%gdal-get-driver-count))
1330
1331 (export get-driver-count)
1332
1333 ;;------------------------------------------------------------------------------
1334
1335 (define-gdal-foreign %gdal-get-driver
1336   '* "GDALGetDriver" (list int) 20)
1337
1338 (define (get-driver driver-index)
1339     "Fetch driver by index.
1340
1341 Parameters:
1342     driver-index: the driver index from 0 to (1- (get-driver-count))."
1343     (let ((ptr (%gdal-get-driver driver-index)))
1344       (if (null-pointer? ptr)
1345              (error "failed to find driver")
1346         ptr)))
1347
1348 (export get-driver)
1349
1350 ;;------------------------------------------------------------------------------
1351
1352 (define-gdal-foreign %gdal-destroy-driver
1353   void "GDALDestroyDriver" (list '*) 20)
1354
1355 (define (destroy-driver driver)
1356     "Destroy a GDALDriver.
1357
1358 Parameters:
1359     driver: the driver to destroy"
1360     (%gdal-destroy-driver driver))
1361
1362 (export destroy-driver)
1363
1364 ;;------------------------------------------------------------------------------
1365
1366 (define-gdal-foreign %gdal-delete-dataset
1367   int "GDALDeleteDataset" (list '* '*) 20)
1368
1369 (define (delete-dataset driver file-name)
1370     "Delete named dataset.
1371
1372 Parameters:
1373     driver: the driver to use for deleting file-name
1374     file-name: name of dataset to delete."
1375     (let ((result (%gdal-delete-dataset driver (string->pointer file-name))))
1376       (unless (= result CE_NONE)
1377         (error "failed to delete dataset"))))
1378
1379 (export delete-dataset)
1380
1381 ;;------------------------------------------------------------------------------
1382
1383 (define-gdal-foreign %gdal-rename-dataset
1384   int "GDALRenameDataset" (list '* '* '*) 20)
1385
1386 (define (rename-dataset driver new-file-name old-file-name)
1387     "Rename a dataset.
1388
1389 Parameters:
1390     driver: the driver to use for deleting file-name
1391     new-file-name: new name for the dataset
1392     old-file-name: old name for the dataset."
1393     (let ((result (%gdal-rename-dataset
1394                    driver
1395                    (string->pointer new-file-name)
1396                    (string->pointer old-file-name))))
1397       (unless (= result CE_NONE)
1398         (error "failed to rename dataset"))))
1399
1400 (export rename-dataset)
1401
1402 ;;------------------------------------------------------------------------------
1403
1404 (define-gdal-foreign %gdal-copy-dataset-files
1405   int "GDALCopyDatasetFiles" (list '* '* '*) 20)
1406
1407 (define (copy-dataset-files driver new-file-name old-file-name)
1408     "Copy all the files associated with a dataset.
1409
1410 Parameters:
1411     driver: the driver to use for deleting file-name
1412     new-file-name: new name for the dataset
1413     old-file-name: old name for the dataset."
1414     (let ((result (%gdal-copy-dataset-files
1415                    driver
1416                    (string->pointer new-file-name)
1417                    (string->pointer old-file-name))))
1418       (unless (= result CE_NONE)
1419         (error "failed to copy dataset files"))))
1420
1421 (export copy-dataset-files)
1422
1423 ;;------------------------------------------------------------------------------
1424
1425 (define-gdal-foreign %gdal-gcps-to-geo-transform
1426   int "GDALGCPsToGeoTransform" (list int '* '* int) 20)
1427
1428 (define (gcps-to-geo-transform gcp-lst is-approx-ok)
1429   "Generate Geotransform from GCPs.
1430
1431 Parameters:
1432     gcp-list: list of GCPs
1433     is-approx-ok: If #f the function will fail if the geotransform is not
1434 essentially an exact fit (within 0.25 pixel) for all GCPs."
1435   (cond ((not (pair? gcp-lst))
1436          (error "input is not a list or empty"))
1437     ((not (= (count gcp? gcp-lst) (length gcp-lst)))
1438      (error "input list has at least one non-gcp record"))
1439     (else (let* ((bv-gcps (make-bytevector (* 6 (sizeof double))))
1440                  (result (%gdal-gcps-to-geo-transform
1441                           (length gcp-lst)
1442                           (gcp-list->pointer gcp-lst)
1443                           (bytevector->pointer bv-gcps)
1444                           (boolean->c-bool is-approx-ok))))
1445             (if (c-bool->boolean result)
1446               (let ((ne (native-endianness))
1447                     (coef-max-index 5)
1448                     (coefs-q (make-q)))
1449                 (do ((i 0 (1+ i)))
1450                   ((> i coef-max-index))
1451                   (enq! coefs-q
1452                         (bytevector-ieee-double-ref bv-gcps
1453                                                     (* i (sizeof double)) ne)))
1454                 (car coefs-q))
1455               #f)))))
1456
1457 (export gcps-to-geo-transform)
1458
1459 ;;------------------------------------------------------------------------------
1460
1461 (define-gdal-foreign %gdal-inv-geo-transform
1462   int "GDALInvGeoTransform" (list '* '*) 20)
1463
1464 (define (inv-geo-transform gt-lst)
1465   "Invert Geotransform.
1466
1467 Parameters:
1468     gt-lst: list of input geotransform (six doubles)."
1469   (cond ((not (pair? gt-lst))
1470          (error "input is not a list or empty"))
1471     ((not (= 6 (length gt-lst)))
1472      (error "insufficient number of coefficients in the list (6 doubles)"))
1473     (else (let* ((bv-gp-out (make-bytevector (* 6 (sizeof double))))
1474                  (coef-max-index 5)
1475                  (result (%gdal-inv-geo-transform
1476                           (list->pointer gt-lst double)
1477                           (bytevector->pointer bv-gp-out))))
1478             (if (c-bool->boolean result)
1479               (let ((coefs-q (make-q)))
1480                 (do ((i 0 (1+ i)))
1481                   ((> i coef-max-index))
1482                   (enq! coefs-q (bytevector-ieee-double-native-ref
1483                                  bv-gp-out
1484                                  (* i (sizeof double)))))
1485                 (car coefs-q))
1486               #f)))))
1487
1488 (export inv-geo-transform)
1489
1490 ;;------------------------------------------------------------------------------
1491
1492 (define-gdal-foreign %gdal-apply-geo-transform
1493   int "GDALApplyGeoTransform" (list '* double double '* '*) 20)
1494
1495 (define (apply-geo-transform gt-lst pixel line)
1496   "Apply GeoTransform to x/y coordinate.
1497
1498 Parameters:
1499     gt-lst: list of input geotransform (six doubles)
1500     pixel: input pixel location
1501     line: input line location."
1502
1503   (cond ((not (pair? gt-lst))
1504          (error "input is not a list or empty"))
1505     ((not (= 6 (length gt-lst)))
1506      (error "insufficient number of coefficients in the list (6 doubles)"))
1507     (else (let ((bv-geo-x (make-bytevector (sizeof double)))
1508                 (bv-geo-y (make-bytevector (sizeof double))))
1509             (%gdal-apply-geo-transform
1510              (list->pointer gt-lst double)
1511              pixel
1512              line
1513              (bytevector->pointer bv-geo-x)
1514              (bytevector->pointer bv-geo-y))
1515             `(,(bytevector-ieee-double-native-ref bv-geo-x 0)
1516               .
1517               ,(bytevector-ieee-double-native-ref bv-geo-y 0))))))
1518
1519 (export apply-geo-transform)
1520
1521 ;;------------------------------------------------------------------------------
1522
1523 (define-gdal-foreign %gdal-compose-geo-transforms
1524   void "GDALComposeGeoTransforms" (list '* '* '*) 20)
1525
1526 (define (compose-geo-transforms gt1-lst gt2-lst)
1527   "Compose two geotransforms.
1528
1529 Parameters:
1530     gt1-lst: list of first geotransform (six doubles)
1531     gt2-lst: list of first geotransform (six doubles)"
1532   (cond ((not (pair? gt1-lst))
1533          (error "input gt1-lst is not a list or empty"))
1534     ((not (pair? gt2-lst))
1535      (error "input gt2-lst is not a list or empty"))
1536     ((not (= 6 (length gt1-lst)))
1537      (error "insufficient number of coefficients in the list gt1-lst"))
1538     ((not (= 6 (length gt2-lst)))
1539       (error "insufficient number of coefficients in the list gt2-lst"))
1540     (else (let ((bv-gt-out (make-bytevector (* 6 (sizeof double)))))
1541             (%gdal-compose-geo-transforms
1542              (list->pointer gt1-lst double)
1543              (list->pointer gt2-lst double)
1544              (bytevector->pointer bv-gt-out))
1545              (let ((coefs-q (make-q)))
1546                (do ((i 0 (1+ i)))
1547                  ((> i 5))
1548                  (enq! coefs-q (bytevector-ieee-double-native-ref
1549                                 bv-gt-out
1550                                 (* i (sizeof double)))))
1551                (car coefs-q))))))
1552
1553 (export compose-geo-transforms)
1554
1555 ;;------------------------------------------------------------------------------
1556
1557 (define-gdal-foreign %gdal-get-metadata-domain-list
1558   '* "GDALGetMetadataDomainList" (list '*) 20)
1559
1560 (define (get-metadata-domain-list h-object)
1561   "Fetch list of metadata domains.
1562
1563 Returns a string list of metadata domains.
1564
1565 Parameters:
1566     h-object: a handle representing various GDAL objects.
1567 See https://www.gdal.org/classGDALMajorObject.html for more information."
1568   (pointerpointer->string-list
1569    (%gdal-get-metadata-domain-list h-object)))
1570
1571 (export get-metadata-domain-list)
1572
1573 ;;------------------------------------------------------------------------------
1574
1575 (define-gdal-foreign %gdal-get-metadata
1576   '* "GDALGetMetadata" (list '* '*) 20)
1577
1578 (define (get-metadata h-object domain)
1579   "Return a string list of metadata which is owned by the object, and may
1580 change at any time. It is formatted as a \"Name=value\" list.
1581
1582 Parameters:
1583     h-object: a handle representing various GDAL objects.
1584 See https://www.gdal.org/classGDALMajorObject.html for more information
1585     domain: the domain of interest. Use empty string \"\" for the default
1586 domain."
1587   (pointerpointer->string-list
1588    (%gdal-get-metadata h-object (string->pointer domain))))
1589
1590 (export get-metadata)
1591
1592 ;;------------------------------------------------------------------------------
1593
1594 (define-gdal-foreign %gdal-set-metadata
1595   int "GDALSetMetadata" (list '* '* '*) 20)
1596
1597 (define (set-metadata h-object metadata domain)
1598     "Sets a string list of a metadata where each member is formatted as
1599 a \"Name=value\".
1600
1601 Parameters:
1602     h-object: a handle representing various GDAL objects.
1603 See https://www.gdal.org/classGDALMajorObject.html for more information
1604     metadata: the metadata in name=value string list format to apply.
1605     domain: the domain of interest. Use empty string \"\" for the default
1606 domain."
1607     (let ((result (%gdal-set-metadata
1608                    h-object
1609                    (string-list->pointerpointer metadata)
1610                    (string->pointer domain))))
1611       (unless (= result CE_NONE)
1612         (error "failed to set metadata"))))
1613
1614 (export set-metadata)
1615
1616 ;;------------------------------------------------------------------------------
1617
1618 (define-gdal-foreign %gdal-close
1619   void "GDALClose" (list '*) 20)
1620
1621 (define (close-dataset h-ds)
1622     "Close GDAL dataset.
1623
1624 Parameters:
1625     h-ds: the dataset to close."
1626     (%gdal-close h-ds))
1627
1628 (export close-dataset)
1629
1630 ;;------------------------------------------------------------------------------
1631
1632 (define-gdal-foreign %gdal-get-metadata-item
1633   '* "GDALGetMetadataItem" (list '* '* '*) 20)
1634
1635 (define (get-metadata-item h-object name domain)
1636     "Fetch single metadata item.
1637
1638 Parameters:
1639     h-object: a handle representing various GDAL objects
1640     name: the key for the metadata item to fetch
1641     domain: the domain to fetch for, use \"\" for the default domain."
1642     (let ((ptr (%gdal-get-metadata-item
1643                  h-object
1644                  (string->pointer name)
1645                  (string->pointer domain))))
1646       (if (null-pointer? ptr)
1647           ""
1648           (pointer->string ptr))))
1649
1650 (export get-metadata-item)
1651
1652 ;;------------------------------------------------------------------------------
1653
1654 (define-gdal-foreign %gdal-set-metadata-item
1655   int "GDALSetMetadataItem" (list '* '* '* '*) 20)
1656
1657 (define (set-metadata-item h-object name value domain)
1658     "Sets a single metadata item which is formatted as \"Name=value\".
1659
1660 Parameters:
1661     h-object: a handle representing various GDAL objects.
1662 See https://www.gdal.org/classGDALMajorObject.html for more information
1663     name: the key for the metadata item to fetch.
1664     value: the value to assign to the key.
1665     domain: the domain to set within, use \"\" for the default domain."
1666     (let ((result (%gdal-set-metadata-item
1667                    h-object
1668                    (string->pointer name)
1669        (string->pointer value)
1670                    (string->pointer domain))))
1671       (unless (= result CE_NONE)
1672         (error "failed to set metadata item"))))
1673
1674 (export set-metadata-item)
1675
1676 ;;------------------------------------------------------------------------------
1677
1678 (define-gdal-foreign %gdal-get-description
1679   '* "GDALGetDescription" (list '*) 20)
1680
1681 (define (get-description h-object)
1682     "Fetch object description.
1683
1684 Parameters:
1685     h-object: a handle representing various GDAL objects.
1686 See https://www.gdal.org/classGDALMajorObject.html for more information."
1687     (pointer->string (%gdal-get-description h-object)))
1688
1689 (export get-description)
1690
1691 ;;------------------------------------------------------------------------------
1692
1693 (define-gdal-foreign %gdal-set-description
1694   void "GDALSetDescription" (list '* '*) 20)
1695
1696 (define (set-description h-object description)
1697     "Set object description.
1698
1699 Parameters:
1700     h-object: a handle representing various GDAL objects.
1701 See https://www.gdal.org/classGDALMajorObject.html for more information.
1702     description: new description."
1703     (%gdal-set-description h-object (string->pointer description)))
1704
1705 (export set-description)
1706
1707 ;;------------------------------------------------------------------------------
1708
1709 (define-gdal-foreign %gdal-get-dataset-driver
1710   '* "GDALGetDatasetDriver" (list '*) 20)
1711
1712 (define (get-dataset-driver h-dataset)
1713     "Fetch the driver to which this dataset relates.
1714
1715 Parameters:
1716     h-dataset: a handle representing GDALDataset."
1717     (%gdal-get-dataset-driver h-dataset))
1718
1719 (export get-dataset-driver)
1720
1721 ;;------------------------------------------------------------------------------
1722
1723 (define-gdal-foreign %gdal-get-file-list
1724   '* "GDALGetFileList" (list '*) 20)
1725
1726 (define (get-file-list h-dataset)
1727     "Fetch files forming dataset.
1728
1729 Parameters:
1730     h-dataset: a handle representing GDALDataset."
1731     (pointerpointer->string-list (%gdal-get-file-list h-dataset)))
1732
1733 (export get-file-list)
1734
1735 ;;------------------------------------------------------------------------------
1736
1737 (define-gdal-foreign %gdal-get-raster-x-size
1738   int "GDALGetRasterXSize" (list '*) 20)
1739
1740 (define (get-raster-x-size h-dataset)
1741     "Fetch raster width in pixels.
1742
1743 Parameters:
1744     h-dataset: a handle representing GDALDataset."
1745     (%gdal-get-raster-x-size h-dataset))
1746
1747 (export get-raster-x-size)
1748
1749 ;;------------------------------------------------------------------------------
1750
1751 (define-gdal-foreign %gdal-get-raster-y-size
1752   int "GDALGetRasterYSize" (list '*) 20)
1753
1754 (define (get-raster-y-size h-dataset)
1755     "Fetch raster height in pixels.
1756
1757 Parameters:
1758     h-dataset: a handle representing GDALDataset."
1759     (%gdal-get-raster-y-size h-dataset))
1760
1761 (export get-raster-y-size)
1762
1763 ;;------------------------------------------------------------------------------
1764
1765 (define-gdal-foreign %gdal-get-raster-band-x-size
1766   int "GDALGetRasterBandXSize" (list '*) 20)
1767
1768 (define (get-raster-band-x-size h-band)
1769     "Fetch raster width in pixels for the band.
1770
1771 Parameters:
1772     h-band: a handle representing GDALRasterBandH."
1773     (%gdal-get-raster-band-x-size h-band))
1774
1775 (export get-raster-band-x-size)
1776
1777 ;;------------------------------------------------------------------------------
1778
1779 (define-gdal-foreign %gdal-get-raster-band-y-size
1780   int "GDALGetRasterBandYSize" (list '*) 20)
1781
1782 (define (get-raster-band-y-size h-band)
1783     "Fetch raster height in pixels for the band.
1784
1785 Parameters:
1786     h-band: a handle representing GDALRasterBandH."
1787     (%gdal-get-raster-band-y-size h-band))
1788
1789 (export get-raster-band-y-size)
1790
1791 ;;------------------------------------------------------------------------------
1792
1793 (define-gdal-foreign %gdal-get-raster-count
1794   int "GDALGetRasterCount" (list '*) 20)
1795
1796 (define (get-raster-count h-dataset)
1797     "Fetch the number of raster bands on this dataset.
1798
1799 Parameters:
1800     h-dataset: a handle representing GDALDataset."
1801     (%gdal-get-raster-count h-dataset))
1802
1803 (export get-raster-count)
1804
1805 ;;------------------------------------------------------------------------------
1806
1807 (define-gdal-foreign %gdal-get-raster-band
1808   '* "GDALGetRasterBand" (list '* int) 20)
1809
1810 (define (get-raster-band h-dataset band-id)
1811     "Fetch a band object for a dataset.
1812
1813 Parameters:
1814     h-dataset: a handle representing GDALDataset.
1815     band-id: the index number of the band to fetch, from 1 to get-raster-count."
1816     (%gdal-get-raster-band h-dataset band-id))
1817
1818 (export get-raster-band)
1819
1820 ;;------------------------------------------------------------------------------
1821
1822 (define-gdal-foreign %gdal-add-band
1823   int "GDALAddBand" (list '* int '*) 20)
1824
1825 (define (add-band h-dataset type options)
1826     "Add a band to a dataset.
1827
1828 Parameters:
1829     h-dataset: a handle representing GDALDataset.
1830     type: the data type of the pixels in the new band.
1831     options: the list of NAME=VALUE option strings. The supported options are
1832 format specific. Empty list '() may be passed by default."
1833   (let ((result (%gdal-add-band h-dataset
1834                                 type
1835                                 (string-list->pointerpointer
1836                                  options))))
1837     (unless (= result CE_NONE)
1838       (error "failed to add band"))))
1839
1840 (export add-band)
1841
1842 ;;------------------------------------------------------------------------------
1843
1844 (define-gdal-foreign %gdal-raster-io
1845   int "GDALRasterIO" (list '* int int int int int '*
1846                              int int int int int) 20)
1847
1848 (define (raster-io h-band rw-flag x-off y-off x-size y-size data
1849                    buf-x-size buf-y-size buf-type pixel-space line-space)
1850     "Read/write a region of image data for this band.
1851
1852 Parameters:
1853     h-band: a handle representing GDALRasterBandH.
1854     rw-flag: either GF_READ to read, or GF_WRITE to write a region of data.
1855     x-off: the pixel offset to the top left corner of the region of the band.
1856     y-off: the line offset to the top left corner of the region of the band.
1857     x-size: the width of the region of the band.
1858     y-size: the height of the region of the band.
1859     data: the bytevector buffer into which the data should be read, or from
1860 which it should be written. This buffer must contain at least
1861 (* buf-x-size buf-y-size) words of type buf-type. It is organized in left to
1862 right, top to bottom pixel order. Spacing is controlled by the pixel-space, and
1863 line-space parameters.
1864     buf-x-size: the width of the buffer image into which the desired region is
1865 to be read, or from which it is to be written.
1866     buf-y-size: the height of the buffer image into which the desired region is
1867 to be read, or from which it is to be written.
1868     buf-type: the type of the pixel values to be returned. The pixel values
1869 will automatically be translated to/from the GDALRasterBand data type as needed.
1870     pixel-space: the byte offset from the start of one pixel value in data to
1871 the start of the next pixel value within a scanline. If defaulted (0) the size
1872 of the datatype buf-type is used.
1873     line-space: the byte offset from the start of one scanline in pData to the
1874 start of the next. If defaulted (0) the size of the datatype
1875 (* buf-type buf-x-size) is used."
1876   (let ((result (%gdal-raster-io h-band rw-flag x-off y-off x-size y-size
1877                                  (bytevector->pointer data)
1878                                  buf-x-size buf-y-size buf-type
1879                                  pixel-space line-space)))
1880     (unless (= result CE_NONE)
1881       (error "failed to read/write data for this band"))))
1882
1883 (export raster-io)
1884
1885 ;;------------------------------------------------------------------------------
1886
1887 (define-gdal-foreign %gdal-dataset-raster-io-ex
1888   int "GDALDatasetRasterIOEx" (list '* int int int int int '*
1889                                     int int int int '* int64 int64
1890                                     int64 '*) 20)
1891
1892 (define (dataset-raster-io-ex h-dataset rw-flag x-off y-off x-size y-size data
1893                               buf-x-size buf-y-size buf-type band-count
1894                               band-map pixel-space line-space band-space
1895                               extra-arg)
1896     "Read/write a region of image data for multiple bands.
1897
1898 Parameters:
1899     h-dataset: a handle representing GDALDatasetH.
1900     rw-flag: either GF_READ to read, or GF_WRITE to write a region of data.
1901     x-off: the pixel offset to the top left corner of the region of the band.
1902     y-off: the line offset to the top left corner of the region of the band.
1903     x-size: the width of the region of the band.
1904     y-size: the height of the region of the band.
1905     data: the bytevector buffer into which the data should be read, or from
1906 which it should be written. This buffer must contain at least
1907 (* buf-x-size buf-y-size band-count) words of type buf-type. It is organized
1908 in left to right, top to bottom pixel order. Spacing is controlled by the
1909 pixel-space, and line-space parameters.
1910     buf-x-size: the width of the buffer image into which the desired region is
1911 to be read, or from which it is to be written.
1912     buf-y-size: the height of the buffer image into which the desired region is
1913 to be read, or from which it is to be written.
1914     buf-type: the type of the pixel values to be returned. The pixel values
1915 will automatically be translated to/from the GDALRasterBand data type as needed.
1916     band-count: the number of bands being read or written.
1917     band-map: the list of band-count numbers being read/written. Note band
1918 numbers are 1 based. This may be empty list '() to select the first band-count
1919 bands.
1920     pixel-space: the byte offset from the start of one pixel value in data to
1921 the start of the next pixel value within a scanline. If defaulted (0) the size
1922 of the datatype buf-type is used.
1923     line-space: the byte offset from the start of one scanline in pData to the
1924 start of the next. If defaulted (0) the size of the datatype
1925 (* buf-type buf-x-size) is used.
1926     band-space: the byte offset from the start of one bands data to the start
1927 of the next. If defaulted (0) the value will be (* line-space buf-y-size)
1928 implying band sequential organization of the data buffer.
1929     extra-arg: (new in GDAL 2.0) pointer to a GDALRasterIOExtraArg structure
1930 constructed with \"make-grioea\" function which creates grioea record
1931 with additional arguments to specify resampling and progress callback, or
1932 #f for default behaviour. The GDAL_RASTERIO_RESAMPLING configuration option can
1933 also be defined to override the default resampling to one of BILINEAR, CUBIC,
1934 CUBICSPLINE, LANCZOS, AVERAGE or MODE."
1935   (let ((result (%gdal-dataset-raster-io-ex h-dataset rw-flag x-off y-off x-size
1936                                             y-size (bytevector->pointer data)
1937                                             buf-x-size buf-y-size buf-type
1938                                             band-count
1939                                             (list->pointer band-map int)
1940                                             pixel-space line-space band-space
1941                                             (grioea->foreign-pointer extra-arg))
1942                 ))
1943     (unless (= result CE_NONE)
1944       (error "failed to read/write data for this band"))))
1945
1946 (export dataset-raster-io-ex)
1947
1948 ;;------------------------------------------------------------------------------
1949
1950 (define-gdal-foreign %gdal-dataset-raster-io
1951   int "GDALDatasetRasterIO" (list '* int int int int int '*
1952                                   int int int int '* int int int) 20)
1953
1954 (define (dataset-raster-io h-dataset rw-flag x-off y-off x-size y-size data
1955                            buf-x-size buf-y-size buf-type band-count
1956                            band-map pixel-space line-space band-space)
1957     "Read/write a region of image data for multiple bands. Use
1958 dataset-raster-io-ex if 64 bit spacings or extra arguments (resampling
1959 resolution, progress callback, etc. are needed).
1960
1961 Parameters:
1962     h-dataset: a handle representing GDALDatasetH.
1963     rw-flag: either GF_READ to read, or GF_WRITE to write a region of data.
1964     x-off: the pixel offset to the top left corner of the region of the band.
1965     y-off: the line offset to the top left corner of the region of the band.
1966     x-size: the width of the region of the band.
1967     y-size: the height of the region of the band.
1968     data: the bytevector buffer into which the data should be read, or from
1969 which it should be written. This buffer must contain at least
1970 (* buf-x-size buf-y-size band-count) words of type buf-type. It is organized
1971 in left to right, top to bottom pixel order. Spacing is controlled by the
1972 pixel-space, and line-space parameters.
1973     buf-x-size: the width of the buffer image into which the desired region is
1974 to be read, or from which it is to be written.
1975     buf-y-size: the height of the buffer image into which the desired region is
1976 to be read, or from which it is to be written.
1977     buf-type: the type of the pixel values to be returned. The pixel values
1978 will automatically be translated to/from the GDALRasterBand data type as needed.
1979     band-count: the number of bands being read or written.
1980     band-map: the list of band-count numbers being read/written. Note band
1981 numbers are 1 based. This may be empty list '() to select the first band-count
1982 bands.
1983     pixel-space: the byte offset from the start of one pixel value in data to
1984 the start of the next pixel value within a scanline. If defaulted (0) the size
1985 of the datatype buf-type is used.
1986     line-space: the byte offset from the start of one scanline in pData to the
1987 start of the next. If defaulted (0) the size of the datatype
1988 (* buf-type buf-x-size) is used.
1989     band-space: the byte offset from the start of one bands data to the start
1990 of the next. If defaulted (0) the value will be (* line-space buf-y-size)
1991 implying band sequential organization of the data buffer."
1992   (let ((result (%gdal-dataset-raster-io h-dataset rw-flag x-off y-off x-size
1993                                          y-size (bytevector->pointer data)
1994                                          buf-x-size buf-y-size buf-type
1995                                          band-count
1996                                          (list->pointer band-map int)
1997                                          pixel-space line-space band-space)))
1998     (unless (= result CE_NONE)
1999       (error "failed to read/write data for this band"))))
2000
2001 (export dataset-raster-io)
2002
2003 ;;------------------------------------------------------------------------------
2004
2005 (define-gdal-foreign %gdal-begin-async-reader
2006   '* "GDALBeginAsyncReader" (list '* int int int int '* int int
2007                                    int int '* int int int '*) 20)
2008
2009 (define (begin-async-reader h-dataset x-off y-off x-size y-size data
2010                             buf-x-size buf-y-size buf-type band-count
2011                             band-map pixel-space line-space band-space
2012                             options)
2013     "Set up an asynchronous data request and return handle representing the
2014 request.
2015
2016 Parameters:
2017     h-dataset: a handle representing GDALDatasetH.
2018     x-off: the pixel offset to the top left corner of the region of the band.
2019     y-off: the line offset to the top left corner of the region of the band.
2020     x-size: the width of the region of the band.
2021     y-size: the height of the region of the band.
2022     data: the bytevector buffer into which the data should be read, or from
2023 which it should be written. This buffer must contain at least
2024 (* buf-x-size buf-y-size band-count) words of type buf-type. It is organized
2025 in left to right, top to bottom pixel order. Spacing is controlled by the
2026 pixel-space, and line-space parameters.
2027     buf-x-size: the width of the buffer image into which the desired region is
2028 to be read, or from which it is to be written.
2029     buf-y-size: the height of the buffer image into which the desired region is
2030 to be read, or from which it is to be written.
2031     buf-type: the type of the pixel values to be returned. The pixel values
2032 will automatically be translated to/from the GDALRasterBand data type as needed.
2033     band-count: the number of bands being read or written.
2034     band-map: the list of band-count numbers being read/written. Note band
2035 numbers are 1 based. This may be empty list '() to select the first band-count
2036 bands.
2037     pixel-space: the byte offset from the start of one pixel value in data to
2038 the start of the next pixel value within a scanline. If defaulted (0) the size
2039 of the datatype buf-type is used.
2040     line-space: the byte offset from the start of one scanline in pData to the
2041 start of the next. If defaulted (0) the size of the datatype
2042 (* buf-type buf-x-size) is used.
2043     band-space: the byte offset from the start of one bands data to the start
2044 of the next. If defaulted (0) the value will be (* line-space buf-y-size)
2045 implying band sequential organization of the data buffer.
2046     options: driver specific control options in a string list or empty
2047 list '(). Consult driver documentation for options supported."
2048   (%gdal-begin-async-reader h-dataset x-off y-off x-size y-size
2049                             (bytevector->pointer data) buf-x-size buf-y-size
2050                             buf-type band-count (list->pointer band-map int)
2051                             pixel-space line-space band-space
2052                             (string-list->pointerpointer options)))
2053
2054 (export begin-async-reader)
2055
2056 ;;------------------------------------------------------------------------------
2057
2058 (define-gdal-foreign %gdal-end-async-reader
2059   void "GDALEndAsyncReader" (list '* '*) 20)
2060
2061 (define (end-async-reader h-dataset h-async-reader)
2062     "End asynchronous request.
2063
2064 Parameters:
2065     h-dataset: handle representing GDALDataset.
2066     h-async-reader: handle representing async reader request."
2067     (%gdal-end-async-reader h-dataset h-async-reader))
2068
2069 (export end-async-reader)
2070
2071 ;;------------------------------------------------------------------------------
2072
2073 (define-gdal-foreign %gdal-raster-io-ex
2074   int "GDALRasterIOEx" (list '* int int int int int '*
2075                              int int int int64 int64 '*) 20)
2076
2077 (define (raster-io-ex h-band rw-flag x-off y-off x-size y-size data
2078                       buf-x-size buf-y-size buf-type pixel-space
2079                       line-space extra-arg)
2080     "Read/write a region of image data for this band.
2081
2082 Parameters:
2083     h-band: a handle representing GDALRasterBandH.
2084     rw-flag: either GF_READ to read, or GF_WRITE to write a region of data.
2085     x-off: the pixel offset to the top left corner of the region of the band.
2086     y-off: the line offset to the top left corner of the region of the band.
2087     x-size: the width of the region of the band.
2088     y-size: the height of the region of the band.
2089     data: the bytevector buffer into which the data should be read, or from
2090 which it should be written. This buffer must contain at least
2091 (* buf-x-size buf-y-size) words of type buf-type. It is organized in left to
2092 right, top to bottom pixel order. Spacing is controlled by the pixel-space, and
2093 line-space parameters.
2094     buf-x-size: the width of the buffer image into which the desired region is
2095 to be read, or from which it is to be written.
2096     buf-y-size: the height of the buffer image into which the desired region is
2097 to be read, or from which it is to be written.
2098     buf-type: the type of the pixel values to be returned. The pixel values
2099 will automatically be translated to/from the GDALRasterBand data type as needed.
2100     pixel-space: the byte offset from the start of one pixel value in data to
2101 the start of the next pixel value within a scanline. If defaulted (0) the size
2102 of the datatype buf-type is used.
2103     line-space: the byte offset from the start of one scanline in pData to the
2104 start of the next. If defaulted (0) the size of the datatype
2105 (* buf-type buf-x-size) is used.
2106   extra-arg: (new in GDAL 2.0) pointer to a GDALRasterIOExtraArg structure
2107 constructed with \"make-grioea\" function which creates grioea record
2108 with additional arguments to specify resampling and progress callback, or
2109 #f for default behaviour. The GDAL_RASTERIO_RESAMPLING configuration option can
2110 also be defined to override the default resampling to one of BILINEAR, CUBIC,
2111 CUBICSPLINE, LANCZOS, AVERAGE or MODE."
2112   (let ((result (%gdal-raster-io-ex h-band rw-flag x-off y-off x-size y-size
2113                                     (bytevector->pointer data)
2114                                     buf-x-size buf-y-size buf-type
2115                                     pixel-space line-space
2116                                     (grioea->foreign-pointer extra-arg))))
2117     (unless (= result CE_NONE)
2118       (error "failed to read/write data for this band"))))
2119
2120 (export raster-io-ex)
2121
2122 ;;------------------------------------------------------------------------------
2123
2124 (define-gdal-foreign %gdal-dataset-advise-read
2125   int "GDALDatasetAdviseRead" (list '* int int int int
2126                                     int int int int '* '*) 20)
2127
2128 (define (dataset-advise-read h-dataset x-off y-off x-size y-size
2129                             buf-x-size buf-y-size buf-type band-count
2130                             band-map options)
2131     "Advise driver of upcoming read requests. Return CE_FAILURE if the request
2132 is invalid and CE_NONE if it works or is ignored.
2133
2134 Parameters:
2135     h-dataset: a handle representing GDALDatasetH.
2136     x-off: the pixel offset to the top left corner of the region of the band.
2137     y-off: the line offset to the top left corner of the region of the band.
2138     x-size: the width of the region of the band.
2139     y-size: the height of the region of the band.
2140     buf-x-size: the width of the buffer image into which the desired region is
2141 to be read, or from which it is to be written.
2142     buf-y-size: the height of the buffer image into which the desired region is
2143 to be read, or from which it is to be written.
2144     buf-type: the type of the pixel values to be returned. The pixel values
2145 will automatically be translated to/from the GDALRasterBand data type as needed.
2146     band-count: the number of bands being read or written.
2147     band-map: the list of band-count numbers being read/written. Note band
2148 numbers are 1 based. This may be empty list '() to select the first band-count
2149 bands.
2150     options: driver specific control options in a string list or empty
2151 list '(). Consult driver documentation for options supported."
2152   (%gdal-dataset-advise-read h-dataset x-off y-off x-size y-size
2153                              buf-x-size buf-y-size buf-type band-count
2154                              (list->pointer band-map int)
2155                              (string-list->pointerpointer options)))
2156
2157 (export dataset-advise-read)
2158
2159 ;;------------------------------------------------------------------------------
2160
2161 (define-gdal-foreign %gdal-get-projection-ref
2162   '* "GDALGetProjectionRef" (list '*) 20)
2163
2164 (define (get-projection-ref h-dataset)
2165     "Fetch the projection definition string for this dataset.
2166
2167 The returned string defines the projection coordinate system of the image in
2168 OpenGIS WKT format.
2169
2170 When a projection definition is not available an empty string is returned.
2171
2172 Parameters:
2173     h-dataset: a handle representing GDALDataset."
2174     (pointer->string (%gdal-get-projection-ref h-dataset)))
2175
2176 (export get-projection-ref)
2177
2178 ;;------------------------------------------------------------------------------
2179
2180 (define-gdal-foreign %gdal-get-spatial-ref
2181   '* "GDALGetSpatialRef" (list '*) 30)
2182
2183 (define (get-spatial-ref h-dataset)
2184     "Fetch the spatial reference for this dataset. Available since GDAL 3.0.
2185
2186 Parameters:
2187     h-dataset: a handle representing GDALDataset."
2188     (%gdal-get-spatial-ref h-dataset))
2189
2190 (export get-spatial-ref)
2191
2192 ;;------------------------------------------------------------------------------
2193
2194 (define-gdal-foreign %gdal-set-projection
2195   int "GDALSetProjection" (list '* '*) 20)
2196
2197 (define (set-projection h-dataset projection)
2198     "Set the projection reference string in OGC WKT or PROJ.4 format for this
2199 dataset.
2200
2201 Parameters:
2202     h-dataset: a handle representing GDALDataset.
2203     projection: projection reference string."
2204     (let ((result (%gdal-set-projection h-dataset
2205                                         (string->pointer projection))))
2206       (unless (= result CE_NONE)
2207         (error "failed to set projection"))))
2208
2209 (export set-projection)
2210
2211 ;;------------------------------------------------------------------------------
2212
2213 (define-gdal-foreign %gdal-set-spatial-ref
2214   int "GDALSetSpatialRef" (list '* '*) 30)
2215
2216 (define (set-spatial-ref h-dataset srs)
2217     "Set the spatial reference system for this dataset. Available since
2218 GDAL 3.0.
2219
2220 Parameters:
2221     h-dataset: a handle representing GDALDataset.
2222     srs: spatial reference system object. %null-pointer can potentially be
2223 passed for drivers that support unsetting the SRS."
2224     (let ((result (%gdal-set-spatial-ref h-dataset srs)))
2225       (unless (= result CE_NONE)
2226         (error "failed to set spatial reference"))))
2227
2228 (export set-spatial-ref)
2229
2230 ;;------------------------------------------------------------------------------
2231
2232 (define-gdal-foreign %gdal-get-geo-transform
2233   int "GDALGetGeoTransform" (list '* '*) 20)
2234
2235 (define (get-geo-transform h-dataset)
2236   "Return a list of transformation coefficients.
2237
2238 Parameters:
2239     h-dataset: a handle representing GDALDataset."
2240
2241   (let* ((bv-gp-out (make-bytevector (* 6 (sizeof double))))
2242          (coef-max-index 5)
2243          (result (%gdal-get-geo-transform
2244                   h-dataset
2245                   (bytevector->pointer bv-gp-out))))
2246     (if (= result CE_FAILURE)
2247       (error "failed to fetch transform")
2248       (let ((coefs-q (make-q)))
2249         (do ((i 0 (1+ i)))
2250           ((> i coef-max-index))
2251           (enq! coefs-q (bytevector-ieee-double-native-ref
2252                          bv-gp-out
2253                          (* i (sizeof double)))))
2254         (car coefs-q)))))
2255
2256 (export get-geo-transform)
2257
2258 ;;------------------------------------------------------------------------------
2259
2260 (define-gdal-foreign %gdal-set-geo-transform
2261   int "GDALSetGeoTransform" (list '* '*) 20)
2262
2263 (define (set-geo-transform h-dataset transform)
2264   "Set the affine transformation coefficients.
2265
2266 Parameters:
2267     h-dataset: a handle representing GDALDataset.
2268     transform: a list of transformation coefficients."
2269   (cond ((not (pair? transform))
2270          (error "transform is not a list or empty"))
2271     ((not (= 6 (length transform)))
2272      (error "insufficient number of coefficients in the list (6 doubles)"))
2273     (else (let ((result (%gdal-set-geo-transform
2274                          h-dataset
2275                          (list->pointer transform double))))
2276             (unless (= result CE_NONE)
2277               (error "failed to set transform"))))))
2278
2279 (export set-geo-transform)
2280
2281 ;;------------------------------------------------------------------------------
2282
2283 (define-gdal-foreign %gdal-get-gcp-count
2284   int "GDALGetGCPCount" (list '*) 20)
2285
2286 (define (get-gcp-count h-dataset)
2287     "Return number of GCPs for this dataset.
2288
2289 Parameters:
2290     h-dataset: a handle representing GDALDataset."
2291     (%gdal-get-gcp-count h-dataset))
2292
2293 (export get-gcp-count)
2294
2295 ;;------------------------------------------------------------------------------
2296
2297 (define-gdal-foreign %gdal-get-gcp-projection
2298   '* "GDALGetGCPProjection" (list '*) 20)
2299
2300 (define (get-gcp-projection h-dataset)
2301     "Return internal projection string for GCPs or empty string if there are
2302 no GCPs.
2303
2304 Parameters:
2305     h-dataset: a handle representing GDALDataset."
2306     (pointer->string (%gdal-get-gcp-projection h-dataset)))
2307
2308 (export get-gcp-projection)
2309
2310 ;;------------------------------------------------------------------------------
2311
2312 (define-gdal-foreign %gdal-get-gcp-spatial-ref
2313   '* "GDALGetGCPSpatialRef" (list '*) 30)
2314
2315 (define (get-gcp-spatial-ref h-dataset)
2316     "Return a pointer to an internal object of output spatial reference system
2317 for GCPs. Available since GDAL 3.0.
2318
2319 Parameters:
2320     h-dataset: a handle representing GDALDataset."
2321     (%gdal-get-gcp-spatial-ref h-dataset))
2322
2323 (export get-gcp-spatial-ref)
2324
2325 ;;------------------------------------------------------------------------------
2326
2327 (define-gdal-foreign %gdal-get-gcps
2328   '* "GDALGetGCPs" (list '*) 20)
2329
2330 (define (get-gcps h-dataset)
2331   "Return a list of internal GCP structure.
2332
2333 Parameters:
2334     h-dataset: a handle representing GDALDataset."
2335   (pointer->gcp-list (%gdal-get-gcps h-dataset) (get-gcp-count h-dataset)))
2336
2337 (export get-gcps)
2338
2339 ;;------------------------------------------------------------------------------
2340
2341 (define-gdal-foreign %gdal-set-gcps
2342   int "GDALSetGCPs" (list '* int '* '*) 20)
2343
2344 (define* (set-gcps h-dataset gcp-lst #:key (projection ""))
2345   "Assign GCPs.
2346
2347 Parameters:
2348     h-dataset: a handle representing GDALDataset.
2349     gc-lst: list of GCP structures where each structure is created by
2350 \"make-gcp\" function.
2351     projection (optional): string of the new OGC WKT coordinate system to
2352 assign for the GCP output coordinates."
2353   (let ((result (%gdal-set-gcps h-dataset
2354                                 (length gcp-lst)
2355                                 (gcp-list->pointer gcp-lst)
2356                                 (string->pointer projection))))
2357     (unless (= result CE_NONE)
2358       (error "failed to set GCPs"))))
2359
2360 (export set-gcps)
2361
2362 ;;------------------------------------------------------------------------------
2363
2364 (define-gdal-foreign %gdal-set-gcps2
2365   int "GDALSetGCPs2" (list '* int '* '*) 30)
2366
2367 (define* (set-gcps2 h-dataset gcp-lst #:key (spatial-ref %null-pointer))
2368   "Assign GCPs. Available since GDAL 3.0.
2369
2370 Parameters:
2371     h-dataset: a handle representing GDALDataset.
2372     gc-lst: list of GCP structures where each structure is created by
2373 \"make-gcp\" function.
2374     spatial-ref (optional): the new coordinate reference system to assign for
2375 the GCP output coordinates."
2376   (let ((result (%gdal-set-gcps2 h-dataset
2377                                 (length gcp-lst)
2378                                 (gcp-list->pointer gcp-lst)
2379                                 spatial-ref)))
2380     (unless (= result CE_NONE)
2381       (error "failed to set GCPs"))))
2382
2383 (export set-gcps2)
2384
2385 ;;------------------------------------------------------------------------------
2386
2387 (define-gdal-foreign %gdal-get-internal-handle
2388   '* "GDALGetInternalHandle" (list '* '*) 20)
2389
2390 (define (get-internal-handle h-dataset request)
2391     "Fetch a format specific internally meaningful handle.
2392
2393 Parameters:
2394     h-dataset: a handle representing GDALDataset.
2395     request: the handle name desired. The meaningful names will be specific to
2396 the file format."
2397     (%gdal-get-internal-handle h-dataset (string->pointer request)))
2398
2399 (export get-internal-handle)
2400
2401 ;;------------------------------------------------------------------------------
2402
2403 (define-gdal-foreign %gdal-reference-dataset
2404   int "GDALReferenceDataset" (list '*) 20)
2405
2406 (define (reference-dataset h-dataset)
2407     "Add one to dataset reference count.
2408
2409 Parameters:
2410     h-dataset: a handle representing GDALDataset."
2411     (%gdal-reference-dataset h-dataset))
2412
2413 (export reference-dataset)
2414
2415 ;;------------------------------------------------------------------------------
2416
2417 (define-gdal-foreign %gdal-dereference-dataset
2418   int "GDALDereferenceDataset" (list '*) 20)
2419
2420 (define (dereference-dataset h-dataset)
2421     "Subtract one from dataset reference count.
2422
2423 Parameters:
2424     h-dataset: a handle representing GDALDataset."
2425     (%gdal-dereference-dataset h-dataset))
2426
2427 (export dereference-dataset)
2428
2429 ;;------------------------------------------------------------------------------
2430
2431 (define-gdal-foreign %gdal-release-dataset
2432   int "GDALReleaseDataset" (list '*) 20)
2433
2434 (define (release-dataset h-dataset)
2435     "Drop a reference to this object, and destroy if no longer referenced.
2436
2437 Parameters:
2438     h-dataset: a handle representing GDALDataset."
2439     (%gdal-release-dataset h-dataset))
2440
2441 (export release-dataset)
2442
2443 ;;------------------------------------------------------------------------------
2444
2445 (define-gdal-foreign %gdal-build-overviews
2446   int "GDALBuildOverviews" (list '* '* int '* int '* '* '*) 20)
2447
2448 (define* (build-overviews h-dataset resampling overview-list band-list
2449                           #:key (progress-callback '())
2450                           (progress-data %null-pointer))
2451     "Build raster overview(s).
2452
2453 Parameters:
2454     h-dataset: a handle representing GDALDatasetH.
2455     resampling: one of GRIORAv2 enums controlling the downsampling method
2456 applied.
2457     overview-list: the list of overview decimation factors to build, or '() to
2458 clean overviews.
2459     band-list: list of band numbers to build overviews. Build for all bands if
2460 this is '().
2461     progress-callback (optional): a function to call to report progress.
2462     progress-data (optional): application data to pass to the progress
2463 function."
2464   (let ((result (%gdal-build-overviews h-dataset
2465                                        (string->pointer (assv-ref
2466                                           *grioeav2-to-string* resampling))
2467                                        (length overview-list)
2468                                        (list->pointer overview-list int)
2469                                        (length band-list)
2470                                        (list->pointer band-list int)
2471                                        (gdal-progress-func progress-callback)
2472                                        progress-data)))
2473     (when (= result CE_FAILURE)
2474       (error "failed to build overview"))))
2475
2476 (export build-overviews)
2477
2478 ;;------------------------------------------------------------------------------
2479
2480 (define-gdal-foreign %gdal-get-open-datasets
2481   void "GDALGetOpenDatasets" (list '* '*) 20)
2482
2483 (define (get-open-datasets)
2484   "Return a list of all open GDAL dataset handles."
2485   (let ((bv-ds (make-bytevector (sizeof '*)))
2486         (bv-count (make-bytevector (sizeof '*))))
2487     (%gdal-get-open-datasets (bytevector->pointer bv-ds)
2488                              (bytevector->pointer bv-count))
2489     (pointerpointer->list (bytevector->pointer bv-ds)
2490                           dereference-pointer
2491                           (bytevector-sint-ref
2492                            bv-count 0
2493                            (native-endianness)
2494                            (sizeof int)))))
2495
2496 (export get-open-datasets)
2497
2498 ;;------------------------------------------------------------------------------
2499
2500 (define-gdal-foreign %gdal-get-access
2501   int "GDALGetAccess" (list '*) 20)
2502
2503 (define (get-access h-dataset)
2504     "Return access flag.
2505
2506 Parameters:
2507     h-dataset: a handle representing GDALDataset."
2508     (%gdal-get-access h-dataset))
2509 (export get-access)
2510
2511 ;;------------------------------------------------------------------------------
2512
2513 (define-gdal-foreign %gdal-flush-cache
2514   void "GDALFlushCache" (list '*) 20)
2515
2516 (define (flush-cache h-dataset)
2517     "Flush all write cached data to disk.
2518
2519 Parameters:
2520     h-dataset: a handle representing GDALDataset."
2521     (%gdal-flush-cache h-dataset))
2522
2523 (export flush-cache)
2524
2525 ;;------------------------------------------------------------------------------
2526
2527 (define-gdal-foreign %gdal-create-dataset-mask-band
2528   int "GDALCreateDatasetMaskBand" (list '* int) 20)
2529
2530 (define (create-dataset-mask-band h-dataset flags)
2531   "Adds a mask band to the dataset.
2532
2533 Parameters:
2534     h-dataset: a handle representing GDALDataset.
2535     flags: 0 or combination of GMF_PER_DATASET / GMF_ALPHA. GMF_PER_DATASET
2536 will be always set, even if not explicitly specified."
2537   (let ((result (%gdal-create-dataset-mask-band h-dataset flags)))
2538     (unless (= result CE_NONE)
2539       (error "failed to create mask band"))))
2540
2541 (export create-dataset-mask-band)
2542
2543 ;;------------------------------------------------------------------------------
2544
2545 (define-gdal-foreign %gdal-dataset-copy-whole-raster
2546   int "GDALDatasetCopyWholeRaster" (list '* '* '* '* '*) 20)
2547
2548 (define* (dataset-copy-whole-raster src-dataset dst-dataset options
2549                                     #:key (progress-callback '())
2550                                     (progress-data %null-pointer))
2551   "Copy all dataset raster data.
2552
2553 Currently the only options value supported are :
2554
2555     \"INTERLEAVE=PIXEL\" to force pixel interleaved operation
2556     \"COMPRESSED=YES\" to force alignment on target dataset block sizes to
2557 achieve best compression.
2558     \"SKIP_HOLES=YES\" to skip chunks for which \"get-data-coverage-status\"
2559 returns GDAL_DATA_COVERAGE_STATUS_EMPTY (GDAL >= 2.2)
2560
2561 Parameters:
2562     src-dataset: the source dataset
2563     dst-dataset: the destination dataset.
2564     options: a list of strings for transfer hints in Name=Value format.
2565     progress-callback (optional): a function to call to report progress.
2566     progress-data (optional): application data to pass to the progress
2567 function."
2568   (let ((result (%gdal-dataset-copy-whole-raster src-dataset
2569                                                  dst-dataset
2570                                                  (string-list->pointerpointer
2571                                                   options)
2572                                                  (gdal-progress-func
2573                                                   progress-callback)
2574                                                  progress-data)))
2575     (unless (= result CE_NONE)
2576       (error "failed to copy raster dataset"))))
2577
2578 (export dataset-copy-whole-raster)
2579
2580 ;;------------------------------------------------------------------------------
2581
2582 (define-gdal-foreign %gdal-raster-band-copy-whole-raster
2583   int "GDALRasterBandCopyWholeRaster" (list '* '* '* '* '*) 20)
2584
2585 (define* (raster-band-copy-whole-raster src-band dst-band options
2586                                         #:key (progress-callback '())
2587                                         (progress-data %null-pointer))
2588   "Copy a whole raster band.
2589
2590 Currently the only options value supported are :
2591
2592     \"COMPRESSED=YES\" to force alignment on target dataset block sizes to
2593 achieve best compression.
2594     \"SKIP_HOLES=YES\" to skip chunks for which \"get-data-coverage-status\"
2595 returns GDAL_DATA_COVERAGE_STATUS_EMPTY (GDAL >= 2.2)
2596
2597 Parameters:
2598     src-band: the source band.
2599     dst-band: the destination band.
2600     options: a list of strings for transfer hints in Name=Value format.
2601     progress-callback (optional): a function to call to report progress.
2602     progress-data (optional): application data to pass to the progress
2603 function."
2604   (let ((result (%gdal-raster-band-copy-whole-raster
2605                  src-band
2606                  dst-band
2607                 (string-list->pointerpointer options)
2608                 (gdal-progress-func progress-callback)
2609                 progress-data)))
2610     (unless (= result CE_NONE)
2611       (error "failed to copy raster band"))))
2612
2613 (export raster-band-copy-whole-raster)
2614
2615 ;;------------------------------------------------------------------------------
2616
2617 (define-gdal-foreign %gdal-regenerage-overviews
2618   int "GDALRegenerateOverviews" (list '* int '* '* '* '*) 20)
2619
2620 (define* (regenerage-overviews src-band band-list resampling
2621                                #:key (progress-callback '())
2622                                (progress-data %null-pointer))
2623   "Generate downsampled overviews.
2624
2625 Parameters:
2626     src-band: the source (base level) band.
2627     band-list: the list of downsampled bands to be generated.
2628     resampling: one of GRIORAv2 enums controlling the downsampling method
2629 applied.
2630     progress-callback (optional): a function to call to report progress.
2631     progress-data (optional): application data to pass to the progress
2632 function."
2633   (let ((result (%gdal-regenerage-overviews
2634                  src-band
2635                  (length band-list)
2636                  (list->pointer band-list '*)
2637                  (string->pointer (assv-ref
2638                                    *grioeav2-to-string* resampling))
2639                  (gdal-progress-func progress-callback)
2640                  progress-data)))
2641     (when (= result CE_FAILURE)
2642       (error "failed to regenerate overview"))))
2643
2644 (export regenerage-overviews)
2645
2646 ;;------------------------------------------------------------------------------
2647
2648 (define-gdal-foreign %gdal-dataset-get-layer-count
2649   int "GDALDatasetGetLayerCount" (list '*) 20)
2650
2651 (define (dataset-get-layer-count h-dataset)
2652     "Get the number of layers in this dataset.
2653
2654 Parameters:
2655     h-dataset: a dataset handle."
2656     (%gdal-dataset-get-layer-count h-dataset))
2657
2658 (export dataset-get-layer-count)
2659
2660 ;;------------------------------------------------------------------------------
2661
2662 (define-gdal-foreign %gdal-dataset-get-layer
2663   '* "GDALDatasetGetLayer" (list '* int) 20)
2664
2665 (define (dataset-get-layer h-dataset layer)
2666     "Fetch a layer by index. Return the layer handle or %null-pointer if layer
2667 is out of range or an error occurs.
2668
2669 Parameters:
2670     h-dataset: a dataset handle.
2671     layer: a layer number between 0 and (1- dataset-get-layer-count)"
2672     (%gdal-dataset-get-layer h-dataset layer))
2673
2674 (export dataset-get-layer)
2675
2676 ;;------------------------------------------------------------------------------
2677
2678 (define-gdal-foreign %gdal-dataset-get-layer-by-name
2679   '* "GDALDatasetGetLayerByName" (list '* '*) 20)
2680
2681 (define (dataset-get-layer-by-name h-dataset name)
2682     "Fetch a layer by name. Return the layer handle or %null-pointer if layer
2683 is not found or an error occurs.
2684
2685 Parameters:
2686     h-dataset: a dataset handle.
2687     name: the layer name of the layer to fetch."
2688     (%gdal-dataset-get-layer-by-name h-dataset (string->pointer name)))
2689
2690 (export dataset-get-layer-by-name)
2691
2692 ;;------------------------------------------------------------------------------
2693
2694 (define-gdal-foreign %gdal-dataset-delete-layer
2695   int "GDALDatasetDeleteLayer" (list '* int) 20)
2696
2697 (define (dataset-delete-layer h-dataset layer)
2698   "Delete the indicated layer from the datasource.
2699
2700 Parameters:
2701     h-dataset: a dataset handle.
2702     layer: the index of the layer to delete."
2703   (let ((result (%gdal-dataset-delete-layer h-dataset layer)))
2704     (unless (= result CE_NONE)
2705       (error "failed to delete layer"))))
2706
2707 (export dataset-delete-layer)
2708
2709 ;;------------------------------------------------------------------------------
2710
2711 (define-gdal-foreign %gdal-dataset-create-layer
2712   '* "GDALDatasetCreateLayer" (list '* '* '* int '*) 20)
2713
2714 (define* (dataset-create-layer h-dataset name
2715                                #:key (spatial-ref %null-pointer)
2716                                           (type 0)
2717                                           (options '()))
2718     "This function attempts to create a new layer on the dataset with the
2719 indicated name, coordinate system, geometry type. Return an handle to the layer
2720 or %null-pointer is returned on failure.
2721
2722 Parameters:
2723     h-dataset: a dataset handle.
2724     name: the name for the new layer. This should ideally not match any
2725 existing layer on the datasource.
2726     spatial-ref (optional): the coordinate system handle to use for the new
2727 layer. Default is %null-pointer where no coordinate system is available.
2728     type (optional): the geometry type for the layer. Default is WKB_UNKNOWN
2729 providing no constraints on the types geometry to be written. Use the module
2730 (gdal ogr) to access WKB enums.
2731     options (optional): a list of strings in name=value format. Default is
2732 empty list. Options are driver specific"
2733     (%gdal-dataset-create-layer h-dataset
2734                                 (string->pointer name)
2735                                 spatial-ref
2736                                 type
2737                                 (string-list->pointerpointer options)))
2738
2739 (export dataset-create-layer)
2740
2741 ;;------------------------------------------------------------------------------
2742
2743 (define-gdal-foreign %gdal-dataset-copy-layer
2744   '* "GDALDatasetCopyLayer" (list '* '* '* '*) 20)
2745
2746 (define* (dataset-copy-layer h-dataset layer name
2747                              #:key (options '()))
2748   "Duplicate an existing layer. Return an handle to the layer or %null-pointer
2749 if an error occurs.
2750
2751 Parameters:
2752     h-dataset: a dataset handle.
2753     layer: source layer.
2754     name: the name for the new layer. This should ideally not match any
2755 existing layer on the datasource.
2756     options (optional): a list of strings in name=value format. Default is
2757 empty list. Options are driver specific"
2758   (%gdal-dataset-copy-layer h-dataset
2759                             layer
2760                             (string->pointer name)
2761                             (string-list->pointerpointer options)))
2762
2763 (export dataset-copy-layer)
2764
2765 ;;------------------------------------------------------------------------------
2766
2767 (define-gdal-foreign %gdal-dataset-reset-reading
2768   void "GDALDatasetResetReading" (list '*) 20)
2769
2770 (define (dataset-reset-reading h-dataset)
2771     "Reset feature reading to start on the first feature.
2772
2773 Parameters:
2774     h-dataset: a dataset handle."
2775     (%gdal-dataset-reset-reading h-dataset))
2776
2777 (export dataset-reset-reading)
2778
2779 ;;------------------------------------------------------------------------------
2780
2781 (define-gdal-foreign %gdal-dataset-get-next-feature
2782   '* "GDALDatasetGetNextFeature" (list '* '* '* '* '*) 20)
2783
2784 (define* (dataset-get-next-feature h-dataset #:key (progress-callback '())
2785                                    (progress-data %null-pointer))
2786   "Fetch the next available feature from this dataset. Return multiple values
2787 of feature in a list, belonging layer pointer which the object belongs to and
2788 a double variable for the precentage progress in [0, 1] range, respectively.
2789 Otherwise returns empty list if no more features are available.
2790
2791 Parameters:
2792     h-dataset: a dataset handle.
2793     progress-callback (optional): a function to call to report progress.
2794     progress-data (optional): application data to pass to the progress
2795 function."
2796   (let* ((layer (make-bytevector (sizeof '*)))
2797          (progress (make-bytevector (sizeof double)))
2798          (result (%gdal-dataset-get-next-feature
2799                  h-dataset
2800                  (bytevector->pointer layer)
2801                  (bytevector->pointer progress)
2802                  (gdal-progress-func progress-callback)
2803                  progress-data)))
2804     (if (null-pointer? result)
2805       '()
2806       (list (dereference-pointer (bytevector->pointer layer))
2807             (bytevector-ieee-double-ref progress 0 (native-endianness))))))
2808
2809 (export dataset-get-next-feature)
2810
2811 ;;------------------------------------------------------------------------------
2812
2813 (define-gdal-foreign %gdal-dataset-test-capability
2814   int "GDALDatasetTestCapability" (list '* '*) 20)
2815
2816 (define (dataset-test-capability h-dataset cap)
2817     "Test if capability is available. Return #t if capability available
2818 otherwise #f.
2819
2820 - ODSC_CREATE_LAYER: True if this datasource can create new layers.
2821 - ODSC_DELETE_LAYER: True if this datasource can delete existing layers.
2822 - ODSC_CREATE_GEOM_FIELD_AFTER_CREATE_LAYER: True if the layers of this
2823 datasource support create-geom-field just after layer creation.
2824 - ODSC_CURVE_GEOMETRIES: True if this datasource supports curve geometries.
2825 - ODSC_TRANSACTIONS: True if this datasource supports (efficient) transactions.
2826 - ODSC_EMULATED_TRANSACTIONS: True if this datasource supports transactions
2827 through emulation.
2828 - ODSC_RANDOM_LAYER_READ: True if this datasource has a dedicated
2829 get-next-feature implementation, potentially returning features from layers in
2830 a non sequential way.
2831 - ODSC_RANDOM_LAYER_WRITE: True if this datasource supports calling
2832 create-feature on layers in a non sequential way.
2833
2834 Parameters:
2835     h-dataset: a dataset handle.
2836     cap: ODCS enum for the capability to test."
2837     (c-bool->boolean (%gdal-dataset-test-capability h-dataset
2838                                                     (string->pointer cap))))
2839
2840 (export dataset-test-capability)
2841
2842 ;;------------------------------------------------------------------------------
2843
2844 (define-gdal-foreign %gdal-dataset-execute-sql
2845   '* "GDALDatasetExecuteSQL" (list '* '* '* '*) 20)
2846
2847 (define* (dataset-execute-sql h-dataset statement #:key
2848                               (spatial-filter %null-pointer)
2849                               (dialect %null-pointer))
2850     "Execute an SQL statement against the data store. Return a pointer
2851 of an OGRLayer containing the results of the query. Deallocate with
2852 \"release-result-set\".
2853
2854 Parameters:
2855     h-dataset: the dataset handle.
2856     statement: the SQL statement to execute.
2857     spatial-filter: geometry which represents a spatial filter. Default is
2858 %null-pointer.
2859     dialect: a string that allows control of the statement dialect. By default
2860 the OGR SQL engine will be used."
2861     (%gdal-dataset-execute-sql h-dataset
2862                                (string->pointer statement)
2863                                spatial-filter
2864                                (if (null-pointer? dialect)
2865                                 dialect
2866                                 (string->pointer dialect))))
2867
2868 (export dataset-execute-sql)
2869
2870 ;;------------------------------------------------------------------------------
2871
2872 (define-gdal-foreign %gdal-dataset-release-result-set
2873   void "GDALDatasetReleaseResultSet" (list '* '*) 20)
2874
2875 (define (dataset-release-result-set h-dataset h-layer)
2876     "Release results of dataset-execute-sql.
2877
2878 Parameters:
2879     h-dataset: a dataset handle.
2880     h-layer: the result of a previous dataset-execute-sql call."
2881     (%gdal-dataset-release-result-set h-dataset h-layer))
2882
2883 (export dataset-release-result-set)
2884
2885 ;;------------------------------------------------------------------------------
2886
2887 (define-gdal-foreign %gdal-dataset-get-style-table
2888   '* "GDALDatasetGetStyleTable" (list '*) 20)
2889
2890 (define (dataset-get-style-table h-dataset)
2891     "Return the OGRStyleTableH handle of dataset style table which should not
2892 be modified or freed by the caller.
2893
2894 Parameters:
2895     h-dataset: a dataset handle."
2896     (%gdal-dataset-get-style-table h-dataset))
2897
2898 (export dataset-get-style-table)
2899
2900 ;;------------------------------------------------------------------------------
2901
2902 (define-gdal-foreign %gdal-dataset-set-style-table-directly
2903   void "GDALDatasetSetStyleTableDirectly" (list '* '*) 20)
2904
2905 (define (dataset-set-style-table-directly h-dataset h-style-table)
2906     "Set dataset style table.
2907
2908 Parameters:
2909     h-dataset: a dataset handle.
2910     h-style-table: the style table handle to set."
2911     (%gdal-dataset-set-style-table-directly h-dataset h-style-table))
2912
2913 (export dataset-set-style-table-directly)
2914
2915 ;;------------------------------------------------------------------------------
2916
2917 (define-gdal-foreign %gdal-dataset-set-style-table
2918   void "GDALDatasetSetStyleTable" (list '* '*) 20)
2919
2920 (define (dataset-set-style-table h-dataset h-style-table)
2921     "Set dataset style table. This function operate exactly as
2922 dataset-set-style-table-directly except that it assumes ownership of the
2923 passed table.
2924
2925 Parameters:
2926     h-dataset: a dataset handle.
2927     h-style-table: the style table handle to set."
2928     (%gdal-dataset-set-style-table h-dataset h-style-table))
2929
2930 (export dataset-set-style-table)
2931
2932 ;;------------------------------------------------------------------------------
2933
2934 (define-gdal-foreign %gdal-dataset-start-transaction
2935   int "GDALDatasetStartTransaction" (list '* int) 20)
2936
2937 (define (dataset-start-transaction h-dataset force)
2938   "For datasources which support transactions, dataset-start-transaction
2939 creates a transaction. Return OGRERR_NONE on success. If starting the
2940 transaction fails, will return OGRERR_FAILURE. Datasources which do not
2941 support transactions will always return OGRERR_UNSUPPORTED_OPERATION.
2942
2943 Parameters:
2944     h-dataset: a handle representing GDALDataset.
2945     force: a boolean value that can be set to #t if an emulation, possibly
2946 slow, of a transaction mechanism is acceptable."
2947   (%gdal-dataset-start-transaction h-dataset
2948                                    (boolean->c-bool force)))
2949
2950 (export dataset-start-transaction)
2951
2952 ;;------------------------------------------------------------------------------
2953
2954 (define-gdal-foreign %gdal-dataset-commit-transaction
2955   int "GDALDatasetCommitTransaction" (list '*) 20)
2956
2957 (define (dataset-commit-transaction h-dataset)
2958   "For datasources which support transactions, dataset-commit-transaction
2959 commits a transaction. Return OGRERR_NONE on success. If no transaction is
2960 active, or the commit fails, will return OGRERR_FAILURE. Datasources which do
2961 not support transactions will always return OGRERR_UNSUPPORTED_OPERATION.
2962
2963 Parameters:
2964     h-dataset: a handle representing GDALDataset."
2965   (%gdal-dataset-commit-transaction h-dataset))
2966
2967 (export dataset-commit-transaction)
2968
2969 ;;------------------------------------------------------------------------------
2970
2971 (define-gdal-foreign %gdal-dataset-rollback-transaction
2972   int "GDALDatasetRollbackTransaction" (list '*) 20)
2973
2974 (define (dataset-rollback-transaction h-dataset)
2975   "For datasources which support transactions, dataset-rollback-transaction
2976 will roll back a datasource to its state before the start of the current
2977 transaction. Return OGRERR_NONE on success. If no transaction is active, or
2978 the rollback fails, will return OGRERR_FAILURE. Datasources which do not
2979 support transactions will always return OGRERR_UNSUPPORTED_OPERATION.
2980
2981 Parameters:
2982     h-dataset: a handle representing GDALDataset."
2983   (%gdal-dataset-rollback-transaction h-dataset))
2984
2985 (export dataset-rollback-transaction)
2986
2987 ;;------------------------------------------------------------------------------
2988
2989 (define-gdal-foreign %gdal-get-raster-data-type
2990   int "GDALGetRasterDataType" (list '*) 20)
2991
2992 (define (get-raster-data-type h-band)
2993   "Fetch the pixel data type for this band.
2994
2995 Parameters:
2996     h-band: a handle representing GDALRasterBandH."
2997   (%gdal-get-raster-data-type h-band))
2998
2999 (export get-raster-data-type)
3000
3001 ;;------------------------------------------------------------------------------
3002
3003 (define-gdal-foreign %gdal-get-block-size
3004   void "GDALGetBlockSize" (list '* '* '*) 20)
3005
3006 (define (get-block-size h-band)
3007   "Fetch the \"natural\" block size of this band as values of x size and
3008 y size, respectively.
3009
3010 Parameters:
3011     h-band: a handle representing GDALRasterBandH."
3012   (let* ((bv-x-size (make-bytevector (sizeof int)))
3013          (bv-y-size (make-bytevector (sizeof int))))
3014     (%gdal-get-block-size h-band
3015                           (bytevector->pointer bv-x-size)
3016                           (bytevector->pointer bv-y-size))
3017     (values
3018      (bytevector-sint-ref bv-x-size 0 (native-endianness) (sizeof int))
3019      (bytevector-sint-ref bv-y-size 0 (native-endianness) (sizeof int)))))
3020
3021 (export get-block-size)
3022
3023 ;;------------------------------------------------------------------------------
3024
3025 (define-gdal-foreign %gdal-get-actual-block-size
3026   int "GDALGetActualBlockSize" (list '* int int '* '*) 20)
3027
3028 (define (get-actual-block-size h-band x-block-off y-block-off)
3029   "Retrieve the actual block size for a given block offset as values of the
3030 number of valid pixels in the x direction and y direction, respectively.
3031
3032 Parameters:
3033     h-band: a handle representing GDALRasterBandH.
3034     x-block-off:        the horizontal block offset for which to calculate the
3035 number of valid pixels, with zero indicating the left most block, 1 the next
3036 block and so forth.
3037     y-block-off: the vertical block offset, with zero indicating the left most
3038 block, 1 the next block and so forth."
3039   (let* ((bv-x-size (make-bytevector (sizeof int)))
3040          (bv-y-size (make-bytevector (sizeof int))))
3041     (%gdal-get-actual-block-size h-band x-block-off y-block-off
3042                                  (bytevector->pointer bv-x-size)
3043                                  (bytevector->pointer bv-y-size))
3044     (values
3045      (bytevector-sint-ref bv-x-size 0 (native-endianness) (sizeof int))
3046      (bytevector-sint-ref bv-y-size 0 (native-endianness) (sizeof int)))))
3047
3048 (export get-actual-block-size)
3049
3050 ;;------------------------------------------------------------------------------
3051
3052 (define-gdal-foreign %gdal-raster-advise-read
3053   int "GDALRasterAdviseRead" (list '* int int int int int int int '*) 20)
3054
3055 (define (raster-advise-read h-band x-off y-off x-size y-size
3056                             buf-x-size buf-y-size buf-type options)
3057     "Advise driver of upcoming read requests. Return CE_FAILURE if the request
3058 is invalid and CE_NONE if it works or is ignored.
3059
3060 Parameters:
3061     h-band: a handle representing GDALRasterBandH.
3062     x-off: the pixel offset to the top left corner of the region of the band.
3063     y-off: the line offset to the top left corner of the region of the band.
3064     x-size: the width of the region of the band.
3065     y-size: the height of the region of the band.
3066     buf-x-size: the width of the buffer image into which the desired region is
3067 to be read, or from which it is to be written.
3068     buf-y-size: the height of the buffer image into which the desired region is
3069 to be read, or from which it is to be written.
3070     buf-type: the type of the pixel values to be returned. The pixel values
3071 will automatically be translated to/from the GDALRasterBand data type as needed.
3072     options: driver specific control options in a string list or empty
3073 list '(). Consult driver documentation for options supported."
3074   (%gdal-raster-advise-read h-band x-off y-off x-size y-size
3075                             buf-x-size buf-y-size buf-type
3076                             (string-list->pointerpointer options)))
3077
3078 (export raster-advise-read)
3079
3080 ;;------------------------------------------------------------------------------
3081
3082 (define-gdal-foreign %gdal-read-block
3083   int "GDALReadBlock" (list '* int int '*) 20)
3084
3085 (define (read-block h-band x-block-off y-block-off data)
3086   "Read a block of image data efficiently.
3087
3088 Parameters:
3089     h-band: a handle representing GDALRasterBandH.
3090     x-block-off: the horizontal block offset, with zero indicating the left
3091 most block, 1 the next block and so forth
3092     y-block-off: the vertical block offset, with zero indicating the top most
3093 block, 1 the next block and so forth.
3094     data: the bytevector buffer into which the data should be read. The buffer
3095 must be large enough to hold (* block-x-size block-y-size) words of type
3096 raster-data-type."
3097   (let ((result (%gdal-read-block h-band x-block-off y-block-off
3098                                   (bytevector->pointer data))))
3099     (unless (= result CE_NONE)
3100       (error "failed to read a block of data for this band"))))
3101
3102 (export read-block)
3103
3104 ;;------------------------------------------------------------------------------
3105
3106 (define-gdal-foreign %gdal-write-block
3107   int "GDALWriteBlock" (list '* int int '*) 20)
3108
3109 (define (write-block h-band x-block-off y-block-off data)
3110   "Write a block of image data efficiently.
3111
3112 Parameters:
3113     h-band: a handle representing GDALRasterBandH.
3114     x-block-off: the horizontal block offset, with zero indicating the left
3115 most block, 1 the next block and so forth
3116     y-block-off: the vertical block offset, with zero indicating the top most
3117 block, 1 the next block and so forth.
3118     data: the bytevector buffer from which the data will be written. The buffer
3119 must be large enough to hold (* block-x-size block-y-size) words of type
3120 raster-data-type."
3121   (let ((result (%gdal-write-block h-band x-block-off y-block-off
3122                                    (bytevector->pointer data))))
3123     (unless (= result CE_NONE)
3124       (error "failed to write a block of data for this band"))))
3125
3126 (export write-block)
3127
3128 ;;------------------------------------------------------------------------------
3129
3130 (define-gdal-foreign %gdal-get-raster-access
3131   int "GDALGetRasterAccess" (list '*) 20)
3132
3133 (define (get-raster-access h-band)
3134   "Find out if we have update permission for this band.
3135
3136 Parameters:
3137     h-band: a handle representing GDALRasterBandH."
3138   (%gdal-get-raster-access h-band))
3139
3140 (export get-raster-access)
3141
3142 ;;------------------------------------------------------------------------------
3143
3144 (define-gdal-foreign %gdal-get-band-number
3145   int "GDALGetBandNumber" (list '*) 20)
3146
3147 (define (get-band-number h-band)
3148   "Fetch the band number for this band.
3149
3150 Parameters:
3151     h-band: a handle representing GDALRasterBandH."
3152   (%gdal-get-band-number h-band))
3153
3154 (export get-band-number)
3155
3156 ;;------------------------------------------------------------------------------
3157
3158 (define-gdal-foreign %gdal-get-band-dataset
3159   '* "GDALGetBandDataset" (list '*) 20)
3160
3161 (define (get-band-dataset h-band)
3162   "Fetch the owning dataset handle for this band.
3163
3164 Parameters:
3165     h-band: a handle representing GDALRasterBandH."
3166   (%gdal-get-band-dataset h-band))
3167
3168 (export get-band-dataset)
3169
3170 ;;------------------------------------------------------------------------------
3171
3172 (define-gdal-foreign %gdal-get-raster-color-interpretation
3173   '* "GDALGetRasterColorInterpretation" (list '*) 20)
3174
3175 (define (get-raster-color-interpretation h-band)
3176   "Fetch the handle of GDALColorInterp to figure out how this band is
3177 interpreted as color.
3178
3179 Parameters:
3180     h-band: a handle representing GDALRasterBandH."
3181   (%gdal-get-raster-color-interpretation h-band))
3182
3183 (export get-raster-color-interpretation)
3184
3185 ;;------------------------------------------------------------------------------
3186
3187 (define-gdal-foreign %gdal-set-raster-color-interpretation
3188   int "GDALSetRasterColorInterpretation" (list '* '*) 20)
3189
3190 (define (set-raster-color-interpretation h-band color-interp)
3191   "Set color interpretation of a band.
3192
3193 Parameters:
3194     h-band: a handle representing GDALRasterBandH.
3195     color-interp: a handle of   the new color interpretation to apply to this
3196 band."
3197   (let ((result (%gdal-set-raster-color-interpretation h-band
3198                                                        color-interp)))
3199     (unless (= result CE_NONE)
3200       (error "failed to set color interpretation"))))
3201
3202 (export set-raster-color-interpretation)
3203
3204 ;;------------------------------------------------------------------------------
3205
3206 (define-gdal-foreign %gdal-get-raster-color-table
3207   '* "GDALGetRasterColorTable" (list '*) 20)
3208
3209 (define (get-raster-color-table h-band)
3210   "Fetch the color table associated with band.
3211
3212 Parameters:
3213     h-band: a handle representing GDALRasterBandH."
3214   (%gdal-get-raster-color-table h-band))
3215
3216 (export get-raster-color-table)
3217
3218 ;;------------------------------------------------------------------------------
3219
3220 (define-gdal-foreign %gdal-set-raster-color-table
3221   int "GDALSetRasterColorTable" (list '* '*) 20)
3222
3223 (define (set-raster-color-table h-band color-table)
3224   "Set the raster color table of a band.
3225
3226 Parameters:
3227     h-band: a handle representing GDALRasterBandH.
3228     color-table: the color table to apply. This may be %null-pointer to clear
3229 the color table (where supported)."
3230   (let ((result (%gdal-set-raster-color-table h-band
3231                                               color-table)))
3232     (unless (= result CE_NONE)
3233       (error "failed to set color table"))))
3234
3235 (export set-raster-color-table)
3236
3237 ;;------------------------------------------------------------------------------
3238
3239 (define-gdal-foreign %gdal-has-arbitrary-overviews
3240   int "GDALHasArbitraryOverviews" (list '*) 20)
3241
3242 (define (has-arbitrary-overviews h-band)
3243   "Check for arbitrary overviews. Return #t if arbitrary overviews available
3244 (efficiently), otherwise #f.
3245
3246 Parameters:
3247     h-band: a handle representing GDALRasterBandH."
3248   (c-bool->boolean (%gdal-has-arbitrary-overviews h-band)))
3249
3250 (export has-arbitrary-overviews)
3251
3252 ;;------------------------------------------------------------------------------
3253
3254 (define-gdal-foreign %gdal-get-overview-count
3255   int "GDALGetOverviewCount" (list '*) 20)
3256
3257 (define (get-overview-count h-band)
3258   "Return the number of overview layers available.
3259
3260 Parameters:
3261     h-band: a handle representing GDALRasterBandH."
3262   (%gdal-get-overview-count h-band))
3263
3264 (export get-overview-count)
3265
3266 ;;------------------------------------------------------------------------------
3267
3268 (define-gdal-foreign %gdal-get-overview
3269   '* "GDALGetOverview" (list '* int) 20)
3270
3271 (define (get-overview h-band id)
3272     "Fetch overview raster band object.
3273
3274 Parameters:
3275     h-band: a handle representing GDALRasterBandH.
3276     id: overview index between 0 and (- (get-overview-count h-band) 1)"
3277     (%gdal-get-overview h-band id))
3278
3279 (export get-overview)
3280
3281 ;;------------------------------------------------------------------------------
3282
3283 (define-gdal-foreign %gdal-get-raster-no-data-value
3284   double "GDALGetRasterNoDataValue" (list '* '*) 20)
3285
3286 (define (get-raster-no-data-value h-band)
3287   "Fetch the no data value as double for this band, or report an error
3288 to indicate if no value is actually associated with this layer.
3289
3290 Parameters:
3291     h-band: a handle representing GDALRasterBandH."
3292   (let* ((bv-success (make-bytevector (sizeof int)))
3293          (no-data (%gdal-get-raster-no-data-value
3294                    h-band
3295                    (bytevector->pointer bv-success))))
3296     (if (c-bool->boolean (bytevector-sint-ref bv-success
3297                                               0
3298                                               (native-endianness)
3299                                               (sizeof int)))
3300       no-data
3301       (error "failed to fetch no data value"))))
3302
3303 (export get-raster-no-data-value)
3304
3305 ;;------------------------------------------------------------------------------
3306
3307 (define-gdal-foreign %gdal-set-raster-no-data-value
3308   int "GDALSetRasterNoDataValue" (list '* double) 20)
3309
3310 (define (set-raster-no-data-value h-band no-data)
3311   "Set the no data value for this band.
3312
3313 Parameters:
3314     h-band: a handle representing GDALRasterBandH.
3315     no-data: the value to set."
3316   (let* ((result (%gdal-set-raster-no-data-value
3317                   h-band
3318                   no-data)))
3319     (unless (= result CE_NONE)
3320       (error "failed to set no data"))))
3321
3322 (export set-raster-no-data-value)
3323
3324 ;;------------------------------------------------------------------------------
3325
3326 (define-gdal-foreign %gdal-delete-raster-no-data-value
3327   int "GDALDeleteRasterNoDataValue" (list '*) 20)
3328
3329 (define (delete-raster-no-data-value h-band)
3330     "Remove the no data value for this band.
3331
3332 Parameters:
3333     h-band: a handle representing GDALRasterBandH."
3334     (let ((result (%gdal-delete-raster-no-data-value h-band)))
3335       (unless (= result CE_NONE)
3336         (error "failed to delete no-data value"))))
3337
3338 (export delete-raster-no-data-value)
3339
3340 ;;------------------------------------------------------------------------------
3341
3342 (define-gdal-foreign %gdal-get-raster-category-names
3343   '* "GDALGetRasterCategoryNames" (list '*) 20)
3344
3345 (define (get-raster-category-names h-band)
3346     "Fetch the list of category names for this raster as a list of strings.
3347
3348 Parameters:
3349     h-band: a handle representing GDALRasterBandH."
3350     (pointerpointer->string-list (%gdal-get-raster-category-names h-band)))
3351
3352 (export get-raster-category-names)
3353
3354 ;;------------------------------------------------------------------------------
3355
3356 (define-gdal-foreign %gdal-set-raster-category-names
3357   int "GDALSetRasterCategoryNames" (list '* '*) 20)
3358
3359 (define* (set-raster-category-names h-band #:key (category-names '()))
3360     "Set the category names for this band.
3361
3362 Parameters:
3363     h-band: a handle representing GDALRasterBandH.
3364     category-names (optional): a list of strings for category names. Default is
3365 empty list '() that clears the existing list."
3366     (let ((result (%gdal-set-raster-category-names
3367                    h-band
3368                    (string-list->pointerpointer category-names))))
3369       (unless (= result CE_NONE)
3370         (error "failed to set category names"))))
3371
3372 (export set-raster-category-names)
3373
3374 ;;------------------------------------------------------------------------------
3375
3376 (define-gdal-foreign %gdal-get-raster-minimum
3377   double "GDALGetRasterMinimum" (list '* '*) 20)
3378
3379 (define (get-raster-minimum h-band)
3380   "Return values of minimum value for this band (excluding no data pixels) and
3381 a boolean, to indicate if the returned value is a tight minimum or not,
3382 respectively.
3383
3384 Parameters:
3385     h-band: a handle representing GDALRasterBandH."
3386   (let* ((bv-success (make-bytevector (sizeof int)))
3387          (minimum (%gdal-get-raster-minimum
3388                    h-band
3389                    (bytevector->pointer bv-success))))
3390     (values minimum (c-bool->boolean
3391                      (bytevector-sint-ref bv-success
3392                                           0
3393                                           (native-endianness)
3394                                           (sizeof int))))))
3395
3396 (export get-raster-minimum)
3397
3398 ;;------------------------------------------------------------------------------
3399
3400 (define-gdal-foreign %gdal-get-raster-maximum
3401   double "GDALGetRasterMaximum" (list '* '*) 20)
3402
3403 (define (get-raster-maximum h-band)
3404   "Return values of maximum value for this band (excluding no data pixels) and
3405 a boolean, to indicate if the returned value is a tight maximum or not,
3406 respectively.
3407
3408 Parameters:
3409     h-band: a handle representing GDALRasterBandH."
3410   (let* ((bv-success (make-bytevector (sizeof int)))
3411          (maximum (%gdal-get-raster-maximum
3412                    h-band
3413                    (bytevector->pointer bv-success))))
3414     (values maximum (c-bool->boolean
3415                      (bytevector-sint-ref bv-success
3416                                           0
3417                                           (native-endianness)
3418                                           (sizeof int))))))
3419
3420 (export get-raster-maximum)
3421
3422 ;;------------------------------------------------------------------------------
3423
3424 (define-gdal-foreign %gdal-get-raster-statistics
3425   int "GDALGetRasterStatistics" (list '* int int '* '* '* '*) 20)
3426
3427 (define (get-raster-statistics h-band approx-ok force)
3428   "Fetch image statistics as multiple values of minimum, maximum, mean and
3429 standard deviation, respectively.
3430
3431 Parameters:
3432     h-band: a handle representing GDALRasterBandH.
3433     approx-ok: a boolean value. If #t statistics may be computed based on
3434 overviews or a subset of all tiles.
3435     force: a boolean value. if #f statistics will only be returned if it can
3436 be done without rescanning the image."
3437   (let* ((bv-min (make-bytevector (sizeof double)))
3438          (bv-max (make-bytevector (sizeof double)))
3439          (bv-mean (make-bytevector (sizeof double)))
3440          (bv-std-dev (make-bytevector (sizeof double)))
3441          (result (%gdal-get-raster-statistics
3442                   h-band
3443                   (boolean->c-bool approx-ok)
3444                   (boolean->c-bool force)
3445                   (bytevector->pointer bv-min)
3446                   (bytevector->pointer bv-max)
3447                   (bytevector->pointer bv-mean)
3448                   (bytevector->pointer bv-std-dev))))
3449     (if (= result CE_NONE)
3450       (values (bytevector-ieee-double-ref bv-min 0 (native-endianness))
3451               (bytevector-ieee-double-ref bv-max 0 (native-endianness))
3452               (bytevector-ieee-double-ref bv-mean 0 (native-endianness))
3453               (bytevector-ieee-double-ref bv-std-dev 0 (native-endianness)))
3454       (error "failed to compute raster statistics"))))
3455
3456 (export get-raster-statistics)
3457
3458 ;;------------------------------------------------------------------------------
3459
3460 (define-gdal-foreign %gdal-compute-raster-statistics
3461   int "GDALComputeRasterStatistics" (list '* int '* '* '* '* '* '*) 20)
3462
3463 (define* (compute-raster-statistics h-band approx-ok
3464                                  #:key (progress-callback '())
3465                                  (progress-data %null-pointer))
3466   "Compute image statistics as multiple values of minimum, maximum, mean and
3467 standard deviation, respectively. Once computed, the statistics will generally
3468 be \"set\" back on the raster band using \"set-render-statistics\".
3469
3470 Parameters:
3471     h-band: a handle representing GDALRasterBandH.
3472     approx-ok: a boolean value. If #t statistics may be computed based on
3473 overviews or a subset of all tiles.
3474     progress-callback (optional): a function to call to report progress.
3475     progress-data (optional): application data to pass to the progress
3476 function."
3477   (let* ((bv-min (make-bytevector (sizeof double)))
3478          (bv-max (make-bytevector (sizeof double)))
3479          (bv-mean (make-bytevector (sizeof double)))
3480          (bv-std-dev (make-bytevector (sizeof double)))
3481          (result (%gdal-compute-raster-statistics
3482                   h-band
3483                   (boolean->c-bool approx-ok)
3484                   (bytevector->pointer bv-min)
3485                   (bytevector->pointer bv-max)
3486                   (bytevector->pointer bv-mean)
3487                   (bytevector->pointer bv-std-dev)
3488                   (gdal-progress-func progress-callback)
3489                   progress-data)))
3490     (if (= result CE_NONE)
3491       (values (bytevector-ieee-double-ref bv-min 0 (native-endianness))
3492               (bytevector-ieee-double-ref bv-max 0 (native-endianness))
3493               (bytevector-ieee-double-ref bv-mean 0 (native-endianness))
3494               (bytevector-ieee-double-ref bv-std-dev 0 (native-endianness)))
3495       (error "failed to compute raster statistics"))))
3496
3497 (export compute-raster-statistics)
3498
3499 ;;------------------------------------------------------------------------------
3500
3501 (define-gdal-foreign %gdal-set-raster-statistics
3502   int "GDALSetRasterStatistics" (list '* double double double double) 20)
3503
3504 (define (set-raster-statistics h-band minimum maximum mean std-dev)
3505   "Set image statistics on band.
3506
3507 Parameters:
3508     h-band: a handle representing GDALRasterBandH.
3509     minimum: minimum pixel value.
3510     maximum: maximum pixel value.
3511     mean: mean (average) of all pixel values.
3512     std-dev: standard deviation of all pixel values."
3513   (let ((result (%gdal-set-raster-statistics
3514                  h-band
3515                  minimum
3516                  maximum
3517                  mean
3518                  std-dev)))
3519     (unless (= result CE_NONE)
3520       (error "failed to set raster statistics"))))
3521
3522 (export set-raster-statistics)
3523
3524 ;;------------------------------------------------------------------------------
3525
3526 (define-gdal-foreign %gdal-raster-band-as-md-array
3527   '* "GDALRasterBandAsMDArray" (list '*) 31)
3528
3529 (define (raster-band-as-md-array h-band)
3530     "Return a view of this raster band as a 2D multidimensional GDALMDArray.
3531 The returned pointer must be released with \"md-array-release\".
3532
3533 Parameters:
3534     h-band: a handle representing GDALRasterBandH."
3535     (%gdal-raster-band-as-md-array h-band))
3536
3537 (export raster-band-as-md-array)
3538
3539 ;;------------------------------------------------------------------------------
3540
3541 (define-gdal-foreign %gdal-get-raster-unit-type
3542   '* "GDALGetRasterUnitType" (list '*) 20)
3543
3544 (define (get-raster-unit-type h-band)
3545     "Return raster unit type as a string.
3546
3547 Parameters:
3548     h-band: a handle representing GDALRasterBandH."
3549     (pointer->string (%gdal-get-raster-unit-type h-band)))
3550
3551 (export get-raster-unit-type)
3552
3553 ;;------------------------------------------------------------------------------
3554
3555 (define-gdal-foreign %gdal-set-raster-unit-type
3556   int "GDALSetRasterUnitType" (list '* '*) 18)
3557
3558 (define (set-raster-unit-type h-band new-value)
3559     "Set unit type.
3560
3561 Parameters:
3562     h-band: a handle representing GDALRasterBandH.
3563     new-value: the new unit type value."
3564     (unless (= CE_NONE (%gdal-set-raster-unit-type
3565                         h-band
3566                         (string->pointer new-value)))
3567       (error "failed to set raster unit type")))
3568
3569 (export set-raster-unit-type)
3570
3571 ;;------------------------------------------------------------------------------
3572
3573 (define-gdal-foreign %gdal-get-raster-offset
3574   double "GDALGetRasterOffset" (list '* '*) 20)
3575
3576 (define (get-raster-offset h-band)
3577   "Fetch the raster value offset.
3578
3579   Parameters:
3580       h-band: a handle representing GDALRasterBandH."
3581   (let* ((bv-success (make-bytevector (sizeof int)))
3582          (offset (%gdal-get-raster-offset
3583                   h-band
3584                   (bytevector->pointer bv-success))))
3585     (if (c-bool->boolean (bytevector-sint-ref bv-success
3586                                               0
3587                                               (native-endianness)
3588                                               (sizeof int)))
3589       offset
3590       (error "failed to get raster offset"))))
3591
3592 (export get-raster-offset)
3593
3594 ;;------------------------------------------------------------------------------
3595
3596 (define-gdal-foreign %gdal-set-raster-offset
3597   int "GDALSetRasterOffset" (list '* double) 20)
3598
3599 (define (set-raster-offset h-band new-offset)
3600   "Set scaling offset.
3601
3602 Parameters:
3603     h-band: a handle representing GDALRasterBandH.
3604     new-offset: the new offset."
3605   (unless (= CE_NONE (%gdal-set-raster-offset
3606                       h-band
3607                       new-offset))
3608     (error "failed to set raster offset")))
3609
3610 (export set-raster-offset)
3611
3612 ;;------------------------------------------------------------------------------
3613
3614 (define-gdal-foreign %gdal-get-raster-scale
3615   double "GDALGetRasterScale" (list '* '*) 20)
3616
3617 (define (get-raster-scale h-band)
3618   "Fetch the raster value scale.
3619
3620   Parameters:
3621       h-band: a handle representing GDALRasterBandH."
3622   (let* ((bv-success (make-bytevector (sizeof int)))
3623          (scale (%gdal-get-raster-scale
3624                  h-band
3625                  (bytevector->pointer bv-success))))
3626     (if (c-bool->boolean (bytevector-sint-ref bv-success
3627                                               0
3628                                               (native-endianness)
3629                                               (sizeof int)))
3630       scale
3631       (error "failed to get raster scale"))))
3632
3633 (export get-raster-scale)
3634
3635 ;;------------------------------------------------------------------------------
3636
3637 (define-gdal-foreign %gdal-set-raster-scale
3638   int "GDALSetRasterScale" (list '* double) 20)
3639
3640 (define (set-raster-scale h-band new-scale)
3641   "Set scaling ratio.
3642
3643 Parameters:
3644     h-band: a handle representing GDALRasterBandH.
3645     new-scale: the new scale."
3646   (unless (= CE_NONE (%gdal-set-raster-scale
3647                       h-band
3648                       new-scale))
3649     (error "failed to set raster scale")))
3650
3651 (export set-raster-scale)
3652
3653 ;;------------------------------------------------------------------------------
3654
3655 (define-gdal-foreign %gdal-compute-raster-min-max
3656   void "GDALComputeRasterMinMax" (list '* int '*) 20)
3657
3658 (define (compute-raster-min-max h-band approx-ok)
3659   "Compute the min/max values for a band and return as values with minimum
3660 and maximum, respectively.
3661
3662 Parameters:
3663     h-band: a handle representing GDALRasterBandH.
3664     approx-ok: #t if an approximate (faster) answer is OK, otherwise #f."
3665   (let ((bv-minmax (make-bytevector (* 2 (sizeof double)))))
3666     (%gdal-compute-raster-min-max h-band
3667                                   (boolean->c-bool approx-ok)
3668                                   (bytevector->pointer bv-minmax))
3669     (values
3670      (bytevector-ieee-double-ref bv-minmax
3671                                  0
3672                                  (native-endianness))
3673      (bytevector-ieee-double-ref bv-minmax
3674                                  (sizeof double)
3675                                  (native-endianness)))))
3676
3677 (export compute-raster-min-max)
3678
3679 ;;------------------------------------------------------------------------------
3680
3681 (define-gdal-foreign %gdal-flush-raster-cache
3682   int "GDALFlushRasterCache" (list '*) 20)
3683
3684 (define (flush-raster-cache h-band)
3685   "Flush raster data cache.
3686
3687 Parameters:
3688     h-band: a handle representing GDALRasterBandH."
3689   (unless (= CE_NONE (%gdal-flush-raster-cache h-band))
3690     (error "failed to flush raster cache")))
3691
3692 (export flush-raster-cache)
3693
3694 ;;------------------------------------------------------------------------------
3695
3696 (define-gdal-foreign %gdal-get-raster-histogram-ex
3697   int "GDALGetRasterHistogramEx" (list '* double double int '*
3698                                        int int '* '*) 20)
3699
3700 (define* (get-raster-histogram h-band minimum maximum n-buckets
3701                                histogram include-out-of-range approx-ok
3702                                #:key (progress-callback '())
3703                                (progress-data %null-pointer))
3704   "Compute raster histogram.
3705
3706 Parameters:
3707     h-band: a handle representing GDALRasterBandH.
3708     minimum: the lower bound of the histogram.
3709     maximum: the upper bound of the histogram.
3710     n-buckets: the number of buckets in histogram.
3711     histogram: int64 buffer of bytevector into which the histogram totals are
3712 placed.
3713     include-out-of-range: if #t values below the histogram range will mapped
3714 into histogram[0], and values above will be mapped into histogram[n-buckets-1]
3715 otherwise out of range values are discarded.
3716     approx-ok: #t if an approximate, or incomplete histogram OK.
3717     progress-callback (optional): a function to call to report progress.
3718     progress-data (optional): application data to pass to the progress
3719 function."
3720   (let ((result (%gdal-get-raster-histogram-ex h-band
3721                                                minimum
3722                                                maximum
3723                                                n-buckets
3724                                                (bytevector->pointer histogram)
3725                                                (boolean->c-bool
3726                                                 include-out-of-range)
3727                                                (boolean->c-bool approx-ok)
3728                                                (gdal-progress-func
3729                                                 progress-callback)
3730                                                progress-data)))
3731     (when (= result CE_FAILURE)
3732       (error "failed to get raster histogram"))))
3733
3734 (export get-raster-histogram)
3735
3736 ;;------------------------------------------------------------------------------
3737
3738 (define-gdal-foreign %gdal-get-default-histogram-ex
3739   int "GDALGetDefaultHistogramEx" (list '* '* '* '* '* int '* '*) 20)
3740
3741 (define* (get-default-histogram h-band is-force
3742                                 #:key (progress-callback '())
3743                                 (progress-data %null-pointer))
3744   "Fetch default raster histogram. Return values of the lower bound of the
3745 histogram, the upper bound of the histogram, number of buckets and a int
3746 bytevector of histogram, respectively.
3747
3748 Parameters:
3749     h-band: a handle representing GDALRasterBandH.
3750     is-force: #t to force the computation. If #f and no default histogram is
3751 available, the method will return CE_WARNING.
3752     progress-callback (optional): a function to call to report progress.
3753     progress-data (optional): application data to pass to the progress
3754 function."
3755   (let* ((bv-min (make-bytevector (sizeof double)))
3756          (bv-max (make-bytevector (sizeof double)))
3757          (bv-n-buckets (make-bytevector (sizeof int)))
3758          (bv-histogram (make-bytevector (sizeof '*)))
3759          (result (%gdal-get-default-histogram-ex
3760                   h-band
3761                   (bytevector->pointer bv-min)
3762                   (bytevector->pointer bv-max)
3763                   (bytevector->pointer bv-n-buckets)
3764                   (bytevector->pointer bv-histogram)
3765                   (boolean->c-bool
3766                    is-force)
3767                   (gdal-progress-func
3768                    progress-callback)
3769                   progress-data)))
3770     (if (= result CE_FAILURE)
3771       (error "failed to get default histogram")
3772       (let ((minimum (bytevector-ieee-double-ref
3773                       bv-min 0
3774                       (native-endianness)))
3775             (maximum (bytevector-ieee-double-ref
3776                       bv-max 0
3777                       (native-endianness)))
3778             (n-buckets (bytevector-sint-ref bv-n-buckets
3779                                             0
3780                                             (native-endianness)
3781                                             (sizeof int))))
3782         (values minimum
3783                 maximum
3784                 n-buckets
3785                 (pointer->list (dereference-pointer
3786                                 (bytevector->pointer bv-histogram))
3787                                n-buckets
3788                                int64))))))
3789
3790 (export get-default-histogram)
3791
3792 ;;------------------------------------------------------------------------------
3793
3794 (define-gdal-foreign %gdal-set-default-histogram-ex
3795   int "GDALSetDefaultHistogramEx" (list '* double double int '*) 20)
3796
3797 (define (set-default-histogram h-band minimum maximum n-buckets histogram)
3798   "Set default histogram.
3799
3800 Parameters:
3801     h-band: a handle representing GDALRasterBandH.
3802     minimum: the lower bound of the histogram.
3803     maximum: the upper bound of the histogram.
3804     n-buckets: the number of buckets in histogram.
3805     histogram: the int64 bytevector of the histogram."
3806   (let ((result (%gdal-set-default-histogram-ex
3807                  h-band
3808                  minimum
3809                  maximum
3810                  n-buckets
3811                  (bytevector->pointer histogram))))
3812     (unless (= result CE_NONE)
3813       (error "failed to set default histogram"))))
3814
3815 (export set-default-histogram)
3816
3817 ;;------------------------------------------------------------------------------
3818
3819 (define-gdal-foreign %gdal-get-random-raster-sample
3820   int "GDALGetRandomRasterSample" (list '* int '*) 20)
3821
3822 (define (get-random-raster-sample h-band count)
3823   "Return a list of random pixels as floating point numbers on the band.
3824
3825 Parameters:
3826     h-band: a handle representing GDALRasterBandH.
3827     count: number of samples."
3828   (let* ((bv-buffer (make-bytevector (* count (sizeof float))))
3829          (real-count (%gdal-get-random-raster-sample
3830                       h-band
3831                       count
3832                       (bytevector->pointer bv-buffer))))
3833     (pointer->list (bytevector->pointer bv-buffer) real-count float)))
3834
3835 (export get-random-raster-sample)
3836
3837 ;;------------------------------------------------------------------------------
3838
3839 (define-gdal-foreign %gdal-get-raster-sample-overview-ex
3840   '* "GDALGetRasterSampleOverviewEx" (list '* int64) 20)
3841
3842 (define (get-raster-sample-overview h-band desired-samples)
3843   "Fetch best sampling overview.
3844
3845 Parameters:
3846     h-band: a handle representing GDALRasterBandH.
3847     desired-samples: the returned band will have at least this many pixels."
3848   (%gdal-get-raster-sample-overview-ex h-band desired-samples))
3849
3850 (export get-raster-sample-overview)
3851
3852 ;;------------------------------------------------------------------------------
3853
3854 (define-gdal-foreign %gdal-fill-raster
3855   int "GDALFillRaster" (list '* double double) 20)
3856
3857 (define* (fill-raster h-band real #:key (imaginary 0))
3858   "Fill this band with a constant value.
3859
3860 Parameters:
3861     h-band: a handle representing GDALRasterBandH.
3862     real: real component of fill value.
3863     imaginary (optional): imaginary component of fill value, defaults to zero."
3864   (let ((result (%gdal-fill-raster h-band real imaginary)))
3865     (unless (= result CE_NONE)
3866       (error "failed to fill raster"))))
3867
3868 (export fill-raster)
3869
3870 ;;------------------------------------------------------------------------------
3871
3872 (define-gdal-foreign %gdal-compute-band-stats
3873   int "GDALComputeBandStats" (list '* int '* '* '* '*) 20)
3874
3875 (define* (compute-band-stats h-band n-sample-step
3876                              #:key (progress-callback '())
3877                              (progress-data %null-pointer))
3878   "Compute image statistics as multiple values of mean and standard deviation,
3879 respectively.
3880
3881 Parameters:
3882     h-band: a handle representing GDALRasterBandH.
3883     n-sample-step: a number of sample steps.
3884     progress-callback (optional): a function to call to report progress.
3885     progress-data (optional): application data to pass to the progress
3886 function."
3887   (let* ((bv-mean (make-bytevector (sizeof double)))
3888          (bv-std-dev (make-bytevector (sizeof double)))
3889          (result (%gdal-compute-band-stats
3890                   h-band
3891                   n-sample-step
3892                   (bytevector->pointer bv-mean)
3893                   (bytevector->pointer bv-std-dev)
3894                   (gdal-progress-func progress-callback)
3895                   progress-data)))
3896     (if (= result CE_NONE)
3897       (values (bytevector-ieee-double-ref bv-mean 0 (native-endianness))
3898               (bytevector-ieee-double-ref bv-std-dev 0 (native-endianness)))
3899       (error "failed to compute band statistics"))))
3900
3901 (export compute-band-stats)
3902
3903 ;;------------------------------------------------------------------------------
3904
3905 (define-gdal-foreign %gdal-get-default-rat
3906   '* "GDALGetDefaultRAT" (list '*) 20)
3907
3908 (define (get-default-rat h-band)
3909   "Fetch default Raster Attribute Table. Returns #f if not set.
3910
3911 Parameters:
3912     h-band: a handle representing GDALRasterBandH."
3913   (let ((ptr (%gdal-get-default-rat h-band)))
3914     (if (null-pointer? ptr)
3915              #f
3916         ptr)))
3917
3918 (export get-default-rat)
3919
3920 ;;------------------------------------------------------------------------------
3921
3922 (define-gdal-foreign %gdal-set-default-rat
3923   int "GDALSetDefaultRAT" (list '* '*) 20)
3924
3925 (define (set-default-rat h-band h-rat)
3926   "Set default Raster Attribute Table.
3927
3928 Parameters:
3929     h-band: a handle representing GDALRasterBandH.
3930     h-rat: a handle for raster attribute table to set."
3931   (let ((result (%gdal-set-default-rat h-band h-rat)))
3932     (unless (= result CE_NONE)
3933       (error "failed to set raster attribute table"))))
3934
3935 (export set-default-rat)
3936
3937 ;;------------------------------------------------------------------------------
3938
3939 (define-gdal-foreign %gdal-create-raster-attribute-table
3940   '* "GDALCreateRasterAttributeTable" '() 20)
3941
3942 (define (make-raster-attribute-table)
3943   "Construct empty table."
3944   (%gdal-create-raster-attribute-table))
3945
3946 (export make-raster-attribute-table)
3947
3948 ;;------------------------------------------------------------------------------
3949
3950 (define-gdal-foreign %gdal-destroy-raster-attribute-table
3951   void "GDALDestroyRasterAttributeTable" (list '*) 20)
3952
3953 (define (destroy-raster-attribute-table h-rat)
3954   "Destroys a RAT."
3955   (%gdal-destroy-raster-attribute-table h-rat))
3956
3957 (export destroy-raster-attribute-table)
3958
3959 ;;------------------------------------------------------------------------------
3960
3961 (define-gdal-foreign %gdal-rat-get-column-count
3962   int "GDALRATGetColumnCount" (list '*) 20)
3963
3964 (define (rat-get-column-count h-rat)
3965   "Fetch table column count.
3966
3967 Parameters:
3968   h-rat: handle representing raster attribute table."
3969   (%gdal-rat-get-column-count h-rat))
3970
3971 (export rat-get-column-count)
3972
3973 ;;------------------------------------------------------------------------------
3974
3975 (define-gdal-foreign %gdal-rat-get-name-of-col
3976   '* "GDALRATGetNameOfCol" (list '* int) 20)
3977
3978 (define (get-rat-name-of-column h-rat i-col)
3979     "Fetch name of indicated column.
3980
3981 Parameters:
3982     h-rat: handle representing raster attribute table.
3983     i-col: column index."
3984   (let ((ptr (%gdal-rat-get-name-of-col h-rat i-col)))
3985     (if (null-pointer? ptr)
3986              (error "failed to get name of column")
3987         (pointer->string ptr))))
3988
3989 (export get-rat-name-of-column)
3990
3991 ;;------------------------------------------------------------------------------
3992
3993 (define-gdal-foreign %gdal-rat-get-usage-of-col
3994   int "GDALRATGetUsageOfCol" (list '* int) 20)
3995
3996 (define (rat-get-usage-of-column h-rat i-col)
3997   "Fetch column usage value. See GFU_* enums for possible values.
3998
3999 Parameters:
4000   h-rat: handle representing raster attribute table.
4001   i-col: column index."
4002   (%gdal-rat-get-usage-of-col h-rat i-col))
4003
4004 (export rat-get-usage-of-column)
4005
4006 ;;------------------------------------------------------------------------------
4007
4008 (define-gdal-foreign %gdal-rat-get-type-of-col
4009   int "GDALRATGetTypeOfCol" (list '* int) 20)
4010
4011 (define (rat-get-type-of-column h-rat i-col)
4012   "Fetch column type. See GFT_* enums for possible values.
4013
4014 Parameters:
4015   h-rat: handle representing raster attribute table.
4016   i-col: column index."
4017   (%gdal-rat-get-usage-of-col h-rat i-col))
4018
4019 (export rat-get-type-of-column)
4020
4021 ;;------------------------------------------------------------------------------
4022
4023 (define-gdal-foreign %gdal-rat-get-col-of-usage
4024   int "GDALRATGetColOfUsage" (list '* int) 20)
4025
4026 (define (rat-get-col-of-usage h-rat usage)
4027   "Fetch column index for given usage.
4028
4029 Parameters:
4030   h-rat: handle representing raster attribute table.
4031   usage: field usage. see GFU_* enums for possible values"
4032   (%gdal-rat-get-col-of-usage h-rat usage))
4033
4034 (export rat-get-col-of-usage)
4035
4036 ;;------------------------------------------------------------------------------
4037
4038 (define-gdal-foreign %gdal-rat-get-row-count
4039   int "GDALRATGetRowCount" (list '*) 20)
4040
4041 (define (rat-get-row-count h-rat)
4042   "Fetch table row count.
4043
4044 Parameters:
4045   h-rat: handle representing raster attribute table."
4046   (%gdal-rat-get-row-count h-rat))
4047
4048 (export rat-get-row-count)
4049
4050 ;;------------------------------------------------------------------------------
4051
4052 (define-gdal-foreign %gdal-rat-get-value-as-string
4053   '* "GDALRATGetValueAsString" (list '* int int) 20)
4054
4055 (define (rat-get-value-as-string h-rat i-row i-field)
4056     "Fetch field value as a string.
4057
4058 Parameters:
4059     h-rat: handle representing raster attribute table.
4060     i-row: row index.
4061     i-field: column index"
4062   (let ((ptr (%gdal-rat-get-value-as-string h-rat i-row i-field)))
4063     (if (null-pointer? ptr)
4064              (error "failed to get value as string")
4065         (pointer->string ptr))))
4066
4067 (export rat-get-value-as-string)
4068
4069 ;;------------------------------------------------------------------------------
4070
4071 (define-gdal-foreign %gdal-rat-get-value-as-int
4072   int "GDALRATGetValueAsInt" (list '* int int) 20)
4073
4074 (define (rat-get-value-as-int h-rat i-row i-field)
4075     "Fetch field value as an integer.
4076
4077 Parameters:
4078     h-rat: handle representing raster attribute table.
4079     i-row: row index.
4080     i-field: column index."
4081   (%gdal-rat-get-value-as-int h-rat i-row i-field))
4082
4083 (export rat-get-value-as-int)
4084
4085 ;;------------------------------------------------------------------------------
4086
4087 (define-gdal-foreign %gdal-rat-get-value-as-double
4088   double "GDALRATGetValueAsDouble" (list '* int int) 20)
4089
4090 (define (rat-get-value-as-double h-rat i-row i-field)
4091     "Fetch field value as a double.
4092
4093 Parameters:
4094     h-rat: handle representing raster attribute table.
4095     i-row: row index.
4096     i-field: column index."
4097   (%gdal-rat-get-value-as-double h-rat i-row i-field))
4098
4099 (export rat-get-value-as-double)
4100
4101 ;;------------------------------------------------------------------------------
4102
4103 (define-gdal-foreign %gdal-rat-set-value-as-string
4104   void "GDALRATSetValueAsString" (list '* int int '*) 20)
4105
4106 (define (rat-set-value-as-string h-rat i-row i-field value)
4107     "Set field value from string.
4108
4109 Parameters:
4110     h-rat: handle representing raster attribute table.
4111     i-row: row index.
4112     i-field: column index.
4113     value: string value to set."
4114   (%gdal-rat-set-value-as-string h-rat i-row i-field (string->pointer value)))
4115
4116 (export rat-set-value-as-string)
4117
4118 ;;------------------------------------------------------------------------------
4119
4120 (define-gdal-foreign %gdal-rat-set-value-as-int
4121   void "GDALRATSetValueAsInt" (list '* int int int) 20)
4122
4123 (define (rat-set-value-as-int h-rat i-row i-field value)
4124     "Set field value from integer.
4125
4126 Parameters:
4127     h-rat: handle representing raster attribute table.
4128     i-row: row index.
4129     i-field: column index.
4130     value: integer value to set."
4131   (%gdal-rat-set-value-as-int h-rat i-row i-field value))
4132
4133 (export rat-set-value-as-int)
4134
4135 ;;------------------------------------------------------------------------------
4136
4137 (define-gdal-foreign %gdal-rat-set-value-as-double
4138   void "GDALRATSetValueAsDouble" (list '* int int double) 20)
4139
4140 (define (rat-set-value-as-double h-rat i-row i-field value)
4141     "Set field value from double.
4142
4143 Parameters:
4144     h-rat: handle representing raster attribute table.
4145     i-row: row index.
4146     i-field: column index.
4147     value: double value to set."
4148   (%gdal-rat-set-value-as-double h-rat i-row i-field value))
4149
4150 (export rat-set-value-as-double)
4151
4152 ;;------------------------------------------------------------------------------
4153
4154 (define-gdal-foreign %gdal-rat-changes-are-written-to-file
4155   int "GDALRATChangesAreWrittenToFile" (list '*) 20)
4156
4157 (define (rat-changes-are-written-to-file h-rat)
4158     "Determine whether changes made to this RAT are reflected directly
4159 in the dataset.
4160
4161 Parameters:
4162     h-rat: handle representing raster attribute table."
4163   (c-bool->boolean (%gdal-rat-changes-are-written-to-file h-rat)))
4164
4165 (export rat-changes-are-written-to-file)
4166
4167 ;;------------------------------------------------------------------------------
4168
4169 (define-gdal-foreign %gdal-rat-values-io-as-double
4170   int "GDALRATValuesIOAsDouble" (list '* int int int int '*) 20)
4171
4172 (define (rat-read-values-as-double h-rat i-field i-start-row i-length)
4173   "Read a block of doubles from the Attribute Table.
4174
4175 Parameters:
4176     h-rat: handle representing raster attribute table.
4177     i-field: column index.
4178     i-start-row: row index to start reading (zero based).
4179     i-length: number of rows to read."
4180   (let* ((bv (make-bytevector (* i-length (sizeof double))))
4181          (result (%gdal-rat-values-io-as-double h-rat GF_READ
4182                                                 i-field i-start-row
4183                                                 i-length
4184                                                 (bytevector->pointer bv))))
4185     (if (= result CE_FAILURE)
4186       (error "failed to read values as doubles")
4187       (pointer->list (bytevector->pointer bv) i-length double))))
4188
4189 (export rat-read-values-as-double)
4190
4191 (define (rat-write-values-as-double h-rat i-field i-start-row lst)
4192   "Write a block of doubles to the Attribute Table.
4193
4194 Parameters:
4195     h-rat: handle representing raster attribute table.
4196     i-field: column index.
4197     i-start-row: row index to start writing (zero based).
4198     lst: list of doubles to write."
4199   (let ((result (%gdal-rat-values-io-as-double
4200                   h-rat GF_WRITE
4201                   i-field i-start-row
4202                   (length lst)
4203                   (list->pointer lst double))))
4204     (if (= result CE_FAILURE)
4205       (error "failed to write values as doubles"))))
4206
4207 (export rat-write-values-as-double)
4208
4209 ;;------------------------------------------------------------------------------
4210
4211 (define-gdal-foreign %gdal-rat-values-io-as-integer
4212   int "GDALRATValuesIOAsInteger" (list '* int int int int '*) 20)
4213
4214 (define (rat-read-values-as-integer h-rat i-field i-start-row i-length)
4215   "Read a block of integers from the Attribute Table.
4216
4217 Parameters:
4218     h-rat: handle representing raster attribute table.
4219     i-field: column index.
4220     i-start-row: row index to start reading (zero based).
4221     i-length: number of rows to read."
4222   (let* ((bv (make-bytevector (* i-length (sizeof int))))
4223          (result (%gdal-rat-values-io-as-integer h-rat GF_READ
4224                                                  i-field i-start-row
4225                                                  i-length
4226                                                  (bytevector->pointer bv))))
4227     (if (= result CE_FAILURE)
4228       (error "failed to read values as integers")
4229       (bytevector->sint-list bv (native-endianness) i-length))))
4230
4231 (export rat-read-values-as-integer)
4232
4233 (define (rat-write-values-as-integer h-rat i-field i-start-row lst)
4234   "Write a block of integers to the Attribute Table.
4235
4236 Parameters:
4237     h-rat: handle representing raster attribute table.
4238     i-field: column index.
4239     i-start-row: row index to start writing (zero based).
4240     lst: list of integers to write."
4241   (let ((result (%gdal-rat-values-io-as-integer
4242                   h-rat GF_WRITE
4243                   i-field i-start-row
4244                   (length lst)
4245                   (sint-list->bytevector lst (native-endianness)
4246                                          (length lst)))))
4247     (if (= result CE_FAILURE)
4248       (error "failed to write values as integers"))))
4249
4250 (export rat-write-values-as-integer)
4251
4252 ;;------------------------------------------------------------------------------
4253
4254 (define-gdal-foreign %gdal-rat-values-io-as-string
4255   int "GDALRATValuesIOAsString" (list '* int int int int '*) 20)
4256
4257 (define (rat-read-values-as-string h-rat i-field i-start-row i-length)
4258   "Read a block of strings from the Attribute Table.
4259
4260 Parameters:
4261     h-rat: handle representing raster attribute table.
4262     i-field: column index.
4263     i-start-row: row index to start reading (zero based).
4264     i-length: number of rows to read."
4265   (let* ((bv (make-bytevector (* i-length (sizeof '*))))
4266          (result (%gdal-rat-values-io-as-string h-rat GF_READ
4267                                                 i-field i-start-row
4268                                                 i-length
4269                                                 (bytevector->pointer bv))))
4270     (if (= result CE_FAILURE)
4271       (error "failed to read values as strings")
4272       (pointerpointer->string-list (bytevector->pointer bv)))))
4273
4274 (export rat-read-values-as-string)
4275
4276 (define (rat-write-values-as-string h-rat i-field i-start-row lst)
4277   "Write a block of strings to the Attribute Table.
4278
4279 Parameters:
4280     h-rat: handle representing raster attribute table.
4281     i-field: column index.
4282     i-start-row: row index to start writing (zero based).
4283     lst: list of strings to write."
4284   (let ((result (%gdal-rat-values-io-as-string
4285                   h-rat GF_WRITE
4286                   i-field i-start-row
4287                   (length lst)
4288                   (string-list->pointerpointer lst))))
4289     (if (= result CE_FAILURE)
4290       (error "failed to write values as strings"))))
4291
4292 (export rat-write-values-as-string)
4293
4294 ;;------------------------------------------------------------------------------
4295
4296 (define-gdal-foreign %gdal-rat-set-row-count
4297   void "GDALRATSetRowCount" (list '* int) 20)
4298
4299 (define (rat-set-row-count h-rat new-count)
4300     "Set row count.
4301
4302 Parameters:
4303     h-rat: handle representing raster attribute table.
4304     new-count: the new number of rows."
4305   (%gdal-rat-set-row-count h-rat new-count))
4306
4307 (export rat-set-row-count)
4308
4309 ;;------------------------------------------------------------------------------
4310
4311 (define-gdal-foreign %gdal-rat-create-column
4312   int "GDALRATCreateColumn" (list '* '* int int) 20)
4313
4314 (define (rat-create-column h-rat field-name field-type field-usage)
4315   "Create new column. If the table already has rows, all row values for the
4316 new column will be initialized to the default value (\"\", or zero). The new
4317 column is always created as the last column, column index will be
4318 \"(- (get-column-count) 1)\" after rat-create-column has completed successfully.
4319
4320 Parameters:
4321     h-rat: handle representing raster attribute table.
4322     field-name: the name of the field to create.
4323     field-type: the field type. see GFT_* enums for possible values.
4324     field-usage: the field usage. see GFU_* enums for possible values."
4325   (let ((result (%gdal-rat-create-column h-rat (string->pointer field-name)
4326                                          field-type
4327                                          field-usage)))
4328     (if (= result CE_FAILURE)
4329       (error "failed to create column"))))
4330
4331 (export rat-create-column)
4332
4333 ;;------------------------------------------------------------------------------
4334
4335 (define-gdal-foreign %gdal-rat-set-linear-binning
4336   int "GDALRATSetLinearBinning" (list '* double double) 20)
4337
4338 (define (rat-set-linear-binning h-rat row-min bin-size)
4339   "Set linear binning information.
4340
4341 For RATs with equal sized categories (in pixel value space) that are evenly
4342 spaced, this method may be used to associate the linear binning information
4343 with the table.
4344
4345 Parameters:
4346     h-rat: handle representing raster attribute table.
4347     row-min: the lower bound (pixel value) of the first category.
4348     bin-size:the width of each category (in pixel value units)."
4349   (let ((result (%gdal-rat-set-linear-binning h-rat row-min bin-size)))
4350     (if (= result CE_FAILURE)
4351       (error "failed to set linear binning information"))))
4352
4353 (export rat-set-linear-binning)
4354
4355 ;;------------------------------------------------------------------------------
4356
4357 (define-gdal-foreign %gdal-rat-get-linear-binning
4358   int "GDALRATGetLinearBinning" (list '* '* '*) 20)
4359
4360 (define (rat-get-linear-binning h-rat)
4361   "Get linear binning information. Returns values of (#t, row-min, bin-size)
4362 if linear binning information exists or (values #f 0.0 0.0) if there is none.
4363 row-min is the lower bound (pixel value) of the first category, and bin-size
4364 is the width of each category (in pixel value units).
4365
4366 Parameters:
4367     h-rat: handle representing raster attribute table."
4368   (let* ((row-min ((make-bytevector (sizeof double))))
4369          (bin-size ((make-bytevector (sizeof double))))
4370          (result (%gdal-rat-get-linear-binning h-rat
4371                                                (bytevector->pointer row-min)
4372                                                (bytevector->pointer bin-size))))
4373     (if (c-bool->boolean result)
4374       (values #t
4375               (bytevector-ieee-double-ref row-min 0 (native-endianness))
4376               (bytevector-ieee-double-ref bin-size 0 (native-endianness)))
4377       (values #f 0.0 0.0))))
4378
4379 (export rat-get-linear-binning)
4380
4381 ;;------------------------------------------------------------------------------
4382
4383 (define-gdal-foreign %gdal-rat-set-table-type
4384   int "GDALRATSetTableType" (list '* int) 24)
4385
4386 (define (rat-set-table-type h-rat table-type)
4387   "Set whether the RAT is thematic or athematic (continuous).
4388
4389 Parameters:
4390     h-rat: handle for raster attribute table to set.
4391     table-type: table type to set."
4392   (let ((result (%gdal-rat-set-table-type h-rat table-type)))
4393     (unless (= result CE_NONE)
4394       (error "failed to set table type"))))
4395
4396 (export rat-set-table-type)
4397
4398 ;;------------------------------------------------------------------------------
4399
4400 (define-gdal-foreign %gdal-rat-get-table-type
4401   int "GDALRATGetTableType" (list '*) 24)
4402
4403 (define (rat-get-table-type h-rat)
4404     "Indicates whether the RAT is thematic or athematic (continuous).
4405
4406 Parameters:
4407     h-rat: handle representing raster attribute table."
4408   (%gdal-rat-get-table-type h-rat))
4409
4410 (export rat-get-table-type)
4411
4412 ;;------------------------------------------------------------------------------
4413
4414 (define-gdal-foreign %gdal-rat-initialize-from-color-table
4415   int "GDALRATInitializeFromColorTable" (list '* '*) 20)
4416
4417 (define (make-rat-from-color-table h-rat h-table)
4418   "Initialize from color table.
4419
4420 This method will setup a whole raster attribute table based on the contents of
4421 the passed color table. The Value (GFU_MIN_MAX), Red (GFU_RED),
4422 Green (GFU_GREEN), Blue (GFU_BLUE), and Alpha (GFU_ALPHA) fields are created,
4423 and a row is set for each entry in the color table.
4424
4425 The raster attribute table must be empty before calling
4426 make-rat-from-color-table
4427
4428 The Value fields are set based on the implicit assumption with color tables
4429 that entry 0 applies to pixel value 0, 1 to 1, etc.
4430
4431 Parameters:
4432     h-rat: handle for raster attribute table to set.
4433     h-table: color table to copy from."
4434   (let ((result (%gdal-rat-initialize-from-color-table h-rat h-table)))
4435     (unless (= result CE_NONE)
4436       (error "failed to make rat from color table"))))
4437
4438 (export make-rat-from-color-table)
4439
4440 ;;------------------------------------------------------------------------------
4441
4442 (define-gdal-foreign %gdal-rat-translate-to-color-table
4443   '* "GDALRATTranslateToColorTable" (list '* int) 20)
4444
4445 (define (rat-translate-to-color-table h-rat entry-count)
4446   "Translate to a color table.
4447
4448 Parameters:
4449     h-rat: handle representing raster attribute table.
4450     entry-count: the number of entries to produce (0 to entry-count - 1)."
4451   (let ((result (%gdal-rat-translate-to-color-table h-rat entry-count)))
4452     (if (null-pointer? result)
4453       (error "failed to translate to color table")
4454       result)))
4455
4456 (export rat-translate-to-color-table)
4457
4458 ;;------------------------------------------------------------------------------
4459
4460 (define-gdal-foreign %gdal-rat-clone
4461   '* "GDALRATClone" (list '*) 20)
4462
4463 (define (clone-rat h-rat)
4464     "Copy Raster Attribute Table.
4465
4466 Parameters:
4467     h-rat: handle representing raster attribute table."
4468   (%gdal-rat-clone h-rat))
4469
4470 (export clone-rat)
4471
4472 ;;------------------------------------------------------------------------------
4473
4474 (define-gdal-foreign %gdal-rat-get-row-of-value
4475   int "GDALRATGetRowOfValue" (list '* double) 20)
4476
4477 (define (rat-get-row-of-value h-rat value)
4478     "Get row for pixel value.
4479
4480 Given a raw pixel value, the raster attribute table is scanned to determine
4481 which row in the table applies to the pixel value. The row index is returned.
4482
4483 Parameters:
4484     h-rat: handle representing raster attribute table.
4485     value: the pixel value."
4486   (%gdal-rat-get-row-of-value h-rat value))
4487
4488 (export rat-get-row-of-value)
4489
4490 ;;------------------------------------------------------------------------------
4491
4492 (define-gdal-foreign %gdal-rat-remove-statistics
4493   void "GDALRATRemoveStatistics" (list '*) 24)
4494
4495 (define (rat-remove-statistics h-rat)
4496     "Remove statistics from the RAT.
4497
4498 Parameters:
4499     h-rat: handle representing raster attribute table."
4500   (%gdal-rat-remove-statistics h-rat))
4501
4502 (export rat-remove-statistics)
4503
4504 ;;------------------------------------------------------------------------------
4505
4506 (define-gdal-foreign %gdal-set-cache-max
4507   void "GDALSetCacheMax" (list int) 20)
4508
4509 (define (set-cache-max new-size-in-bytes)
4510     "Set maximum cache memory.
4511
4512 This function sets the maximum amount of memory that GDAL is permitted to use
4513 for GDALRasterBlock caching. The unit of the value is bytes.
4514
4515 The maximum value is 2GB, due to the use of a signed 32 bit integer.
4516
4517 Parameters:
4518     new-size-in-bytes: the maximum number of bytes for caching."
4519   (%gdal-set-cache-max new-size-in-bytes))
4520
4521 (export set-cache-max)
4522
4523 ;;------------------------------------------------------------------------------
4524
4525 (define-gdal-foreign %gdal-set-cache-max-64
4526   void "GDALSetCacheMax64" (list int64) 18)
4527
4528 (define (set-cache-max-64 new-size-in-bytes)
4529     "Set maximum cache memory.
4530
4531 This function sets the maximum amount of memory that GDAL is permitted to use
4532 for GDALRasterBlock caching. The unit of the value is bytes.
4533
4534 Note: On 32 bit platforms, the maximum amount of memory that can be addressed
4535 by a process might be 2 GB or 3 GB, depending on the operating system
4536 capabilities. This function will not make any attempt to check the consistency
4537 of the passed value with the effective capabilities of the OS.
4538
4539 Parameters:
4540     new-size-in-bytes: the maximum number of bytes for caching."
4541   (%gdal-set-cache-max-64 new-size-in-bytes))
4542
4543 (export set-cache-max-64)
4544
4545 ;;------------------------------------------------------------------------------
4546
4547 (define-gdal-foreign %gdal-get-cache-max
4548   int "GDALGetCacheMax" '() 20)
4549
4550 (define (get-cache-max)
4551     "Get maximum cache memory.
4552
4553 Gets the maximum amount of memory available to the GDALRasterBlock caching
4554 system for caching GDAL read/write imagery.
4555
4556 The first type this function is called, it will read the GDAL_CACHEMAX
4557 configuration option to initialize the maximum cache memory. Starting with
4558 GDAL 2.1, the value can be expressed as x% of the usable physical RAM
4559 (which may potentially be used by other processes). Otherwise it is expected to
4560 be a value in MB.
4561
4562 This function cannot return a value higher than 2 GB. Use get-cache-max-64 to
4563 get a non-truncated value."
4564   (%gdal-get-cache-max))
4565
4566 (export get-cache-max)
4567
4568 ;;------------------------------------------------------------------------------
4569
4570 (define-gdal-foreign %gdal-get-cache-max-64
4571   int64 "GDALGetCacheMax64" '() 18)
4572
4573 (define (get-cache-max-64)
4574     "Get maximum cache memory.
4575
4576 Gets the maximum amount of memory available to the GDALRasterBlock caching
4577 system for caching GDAL read/write imagery.
4578
4579 The first type this function is called, it will read the GDAL_CACHEMAX
4580 configuration option to initialize the maximum cache memory. Starting with
4581 GDAL 2.1, the value can be expressed as x% of the usable physical RAM
4582 (which may potentially be used by other processes). Otherwise it is expected
4583 to be a value in MB."
4584   (%gdal-get-cache-max-64))
4585
4586 (export get-cache-max-64)
4587
4588 ;;------------------------------------------------------------------------------
4589
4590 (define-gdal-foreign %gdal-get-cache-used
4591   int "GDALGetCacheUsed" '() 20)
4592
4593 (define (get-cache-used)
4594     "Get cache memory used."
4595   (%gdal-get-cache-used))
4596
4597 (export get-cache-used)
4598
4599 ;;------------------------------------------------------------------------------
4600
4601 (define-gdal-foreign %gdal-get-cache-used-64
4602   int64 "GDALGetCacheUsed64" '() 18)
4603
4604 (define (get-cache-used-64)
4605     "Get cache memory used."
4606   (%gdal-get-cache-used-64))
4607
4608 (export get-cache-used-64)
4609
4610 ;;------------------------------------------------------------------------------
4611
4612 (define-gdal-foreign %gdal-flush-cache-block
4613   int "GDALFlushCacheBlock" '() 20)
4614
4615 (define (flush-cache-block)
4616   "Try to flush one cached raster block.
4617
4618 This function will search the first unlocked raster block and will flush it to
4619 release the associated memory.
4620
4621 Returns #t if one block was flushed, #f if there are no cached blocks or if
4622 they are currently locked."
4623   (c-bool->boolean (%gdal-flush-cache-block)))
4624
4625 (export flush-cache-block)
4626
4627 ;;------------------------------------------------------------------------------
4628
4629 (define-gdal-foreign %gdal-create-multi-multidimensional
4630   '* "GDALCreateMultiDimensional" (list '* '* '* '*) 31)
4631
4632 (define* (make-multidimensional-dataset h-driver file-name #:key
4633                                         (root-group-options '())
4634                                         (options '()))
4635   "Create a new multidimensional dataset with this driver.
4636
4637 Only drivers that advertise the GDAL_DCAP_MULTIDIM_RASTER capability and
4638 implement the pfnCreateMultiDimensional method might return a non nullptr
4639 GDALDataset.
4640
4641 Parameters:
4642     file-name: the name of the dataset to create.
4643     root-group-options (optional): driver specific options regarding the
4644 creation of the root group.
4645     options (optional): driver specific options regarding the creation of the
4646 dataset."
4647   (let ((ptr (%gdal-create-multi-multidimensional
4648               h-driver
4649               (string->pointer file-name)
4650               (string-list->pointerpointer root-group-options)
4651               (string-list->pointerpointer options))))
4652     (if (null-pointer? ptr)
4653              (error "failed to create multidimensional dataset")
4654         ptr)))
4655
4656 (export make-multidimensional-dataset)
4657
4658 ;;------------------------------------------------------------------------------
4659
4660 (define-gdal-foreign %gdal-group-get-vector-layer-names
4661   '* "GDALGroupGetVectorLayerNames" (list '* '*) 34)
4662
4663 (define* (group-get-vector-layer-names h-group #:key
4664                                        (options '()))
4665   "Return the list of layer names contained in this group.
4666
4667 Parameters:
4668     h-group: handle of the group.
4669     options (optional): driver specific options determining how layers should
4670 be retrieved."
4671   (let ((ptr (%gdal-group-get-vector-layer-names
4672               h-group
4673               (string-list->pointerpointer options))))
4674     (pointerpointer->string-list ptr)))
4675
4676 (export group-get-vector-layer-names)
4677
4678 ;;------------------------------------------------------------------------------