1 (define-module (gdal ogr)
2 #:use-module (system foreign)
3 #:use-module (rnrs bytevectors)
5 #:use-module (srfi srfi-9)
6 #:use-module (srfi srfi-1)
7 #:use-module (srfi srfi-4 gnu)
8 #:use-module (gdal config)
9 #:use-module (gdal internal))
11 ;;------------------------------------------------------------------------------
15 ;;------------------------------------------------------------------------------
17 ;;; OGRFieldType enums
18 (define-public OFT_INTEGER 0)
19 (define-public OFT_INTEGER_LIST 1)
20 (define-public OFT_REAL 2)
21 (define-public OFT_REAL_LIST 3)
22 (define-public OFT_STRING 4)
23 (define-public OFT_STRING_LIST 5)
24 (define-public OFT_WIDE_STRING 6)
25 (define-public OFT_WIDE_STRING_LIST 7)
26 (define-public OFT_BINARY 8)
27 (define-public OFT_DATE 9)
28 (define-public OFT_TIME 10)
29 (define-public OFT_DATE_TIME 11)
30 (define-public OFT_INTEGER64 12)
31 (define-public OFT_INTEGER64_LIST 13)
33 ;;------------------------------------------------------------------------------
37 ;;------------------------------------------------------------------------------
41 (define-record-type <gdal-datetime>
42 (make-gdal-datetime year month day hour minute second tz)
44 (year gdal-datetime-year set-gdal-datetime-year!)
45 (month gdal-datetime-month set-gdal-datetime-month!)
46 (day gdal-datetime-day set-gdal-datetime-day!)
47 (hour gdal-datetime-hour set-gdal-datetime-hour!)
48 (minute gdal-datetime-minute set-gdal-datetime-minute!)
49 (second gdal-datetime-second set-gdal-datetime-second!)
50 (tz gdal-datetime-tz set-gdal-datetime-tz!))
52 (export make-gdal-datetime
55 set-gdal-datetime-year!
57 set-gdal-datetime-month!
59 set-gdal-datetime-day!
61 set-gdal-datetime-hour!
63 set-gdal-datetime-minute!
65 set-gdal-datetime-second!
67 set-gdal-datetime-tz!)
69 ;;------------------------------------------------------------------------------
71 ;;; OGR Function Bindings
73 ;;------------------------------------------------------------------------------
75 (define-gdal-foreign %ogr-l-reset-reading
76 void "OGR_L_ResetReading" (list '*) 20)
78 (define (reset-layer-reading h-layer)
79 "Reset feature reading to start on the first feature.
81 This affects get-next-feature.
84 h-layer: handle to the layer on which features are read."
85 (%ogr-l-reset-reading h-layer))
87 (export reset-layer-reading)
89 ;;------------------------------------------------------------------------------
91 (define-gdal-foreign %ogr-l-get-next-feature
92 '* "OGR_L_GetNextFeature" (list '*) 20)
94 (define (get-next-feature h-layer)
95 "Fetch the next available feature from this layer.
97 The returned feature becomes the responsibility of the caller to delete with
98 \"destroy-feature\". It is critical that all features associated with an
99 OGRLayer (more specifically an OGRFeatureDefn) be deleted before that
100 layer/datasource is deleted.
102 This function implements sequential access to the features of a layer. The
103 \"reset-layer-reading\" function can be used to start at the beginning again.
105 Returns a handle to a feature, or NULL if no more features are available.
108 h-layer: handle to the layer on which features are read."
109 (%ogr-l-get-next-feature h-layer))
111 (export get-next-feature)
113 ;;------------------------------------------------------------------------------
115 (define-gdal-foreign %ogr-l-get-layer-defn
116 '* "OGR_L_GetLayerDefn" (list '*) 20)
118 (define (get-feature-definition-of-layer h-layer)
119 "Fetch the schema information for this layer.
121 The returned handle to the OGRFeatureDefn is owned by the OGRLayer, and should
122 not be modified or freed by the application. It encapsulates the attribute
123 schema of the features of the layer.
126 h-layer: handle to the layer on which features are read."
127 (%ogr-l-get-layer-defn h-layer))
129 (export get-feature-definition-of-layer)
131 ;;------------------------------------------------------------------------------
133 (define-gdal-foreign %ogr-fd-get-field-count
134 int "OGR_FD_GetFieldCount" (list '*) 20)
136 (define (get-field-count-of-feature-definition h-defn)
137 "Fetch number of fields on the passed feature definition.
140 h-defn: handle to the feature definition to get the fields count from."
141 (%ogr-fd-get-field-count h-defn))
143 (export get-field-count-of-feature-definition)
145 ;;------------------------------------------------------------------------------
147 (define-gdal-foreign %ogr-fd-get-field-defn
148 '* "OGR_FD_GetFieldDefn" (list '* int) 20)
150 (define (get-field-definition-of-feature-definition h-defn i-field)
151 "Fetch field definition of the passed feature definition.
154 h-defn: handle to the feature definition to get the field definition from.
155 i-field: the field to fetch, between 0 and
156 (- (get-field-count-of-feature-definition) 1)."
157 (%ogr-fd-get-field-defn h-defn i-field))
159 (export get-field-definition-of-feature-definition)
161 ;;------------------------------------------------------------------------------
163 (define-gdal-foreign %ogr-fld-get-type
164 int "OGR_Fld_GetType" (list '*) 20)
166 (define (get-type-of-field h-defn)
167 "Fetch type of this field. See OFT_* enums for possible return values.
170 h-defn: handle to the field definition to get type from."
171 (%ogr-fld-get-type h-defn))
173 (export get-type-of-field)
175 ;;------------------------------------------------------------------------------
177 (define-gdal-foreign %ogr-f-get-field-as-integer
178 int "OGR_F_GetFieldAsInteger" (list '* int) 20)
180 (define (get-field-as-integer h-feat i-field)
181 "Fetch field value as integer.
183 OFTString features will be translated using atoi(). OFTReal fields will be cast
184 to integer. Other field types, or errors will result in a return value of zero.
187 h-feat: handle to the feature that owned the field.
188 i-field: the field to fetch, from 0 to GetFieldCount()-1"
189 (%ogr-f-get-field-as-integer h-feat i-field))
191 (export get-field-as-integer)
193 ;;------------------------------------------------------------------------------
195 (define-gdal-foreign %ogr-f-get-field-as-integer64
196 int64 "OGR_F_GetFieldAsInteger64" (list '* int) 20)
198 (define (get-field-as-integer64 h-feat i-field)
199 "Fetch field value as integer 64 bit.
201 OFTInteger are promoted to 64 bit. OFTReal fields will be cast to integer.
202 Other field types, or errors will result in a return value of zero.
205 h-feat: handle to the feature that owned the field.
206 i-field: the field to fetch, from 0 to GetFieldCount()-1"
207 (%ogr-f-get-field-as-integer64 h-feat i-field))
209 (export get-field-as-integer64)
211 ;;------------------------------------------------------------------------------
213 (define-gdal-foreign %ogr-f-get-field-as-double
214 double "OGR_F_GetFieldAsDouble" (list '* int) 20)
216 (define (get-field-as-double h-feat i-field)
217 "Fetch field value as a double.
219 OFTString features will be translated using CPLAtof(). OFTInteger fields will
220 be cast to double. Other field types, or errors will result in a return value
224 h-feat: handle to the feature that owned the field.
225 i-field: the field to fetch, from 0 to GetFieldCount()-1"
226 (%ogr-f-get-field-as-double h-feat i-field))
228 (export get-field-as-double)
230 ;;------------------------------------------------------------------------------
232 (define-gdal-foreign %ogr-f-get-field-as-string
233 '* "OGR_F_GetFieldAsString" (list '* int) 20)
235 (define (get-field-as-string h-feat i-field)
236 "Fetch field value as a string.
238 OFTReal and OFTInteger fields will be translated to string using sprintf(),
239 but not necessarily using the established formatting rules. Other field types,
240 or errors will result in a return value of zero.
243 h-feat: handle to the feature that owned the field.
244 i-field: the field to fetch, from 0 to GetFieldCount()-1"
245 (pointer->string (%ogr-f-get-field-as-string h-feat i-field)))
247 (export get-field-as-string)
249 ;;------------------------------------------------------------------------------
251 (define-gdal-foreign %ogr-f-get-field-as-integer-list
252 '* "OGR_F_GetFieldAsIntegerList" (list '* int '*) 20)
254 (define (get-field-as-integer-list h-feat i-field)
255 "Fetch field value as a list of integers.
257 Currently this function only works for OFTIntegerList fields.
260 h-feat: handle to the feature that owned the field.
261 i-field: the field to fetch, from 0 to GetFieldCount()-1"
262 (let* ((bv-count (make-bytevector (sizeof int)))
263 (bv-result (%ogr-f-get-field-as-integer-list h-feat
267 (pointer->list bv-result
268 (bytevector-s32-native-ref bv-count 0)
271 (export get-field-as-integer-list)
273 ;;------------------------------------------------------------------------------
275 (define-gdal-foreign %ogr-f-get-field-as-integer64-list
276 '* "OGR_F_GetFieldAsInteger64List" (list '* int '*) 20)
278 (define (get-field-as-integer64-list h-feat i-field)
279 "Fetch field value as a list of 64 bit integers.
281 Currently this function only works for OFTInteger64List fields.
284 h-feat: handle to the feature that owned the field.
285 i-field: the field to fetch, from 0 to GetFieldCount()-1"
286 (let* ((bv-count (make-bytevector (sizeof int)))
287 (bv-result (%ogr-f-get-field-as-integer64-list h-feat
291 (pointer->list bv-result
292 (bytevector-s32-native-ref bv-count 0)
295 (export get-field-as-integer64-list)
297 ;;------------------------------------------------------------------------------
299 (define-gdal-foreign %ogr-f-get-field-as-double-list
300 '* "OGR_F_GetFieldAsDoubleList" (list '* int '*) 20)
302 (define (get-field-as-double-list h-feat i-field)
303 "Fetch field value as a list of doubles.
305 Currently this function only works for OFTRealList fields.
308 h-feat: handle to the feature that owned the field.
309 i-field: the field to fetch, from 0 to GetFieldCount()-1"
310 (let* ((bv-count (make-bytevector (sizeof int)))
311 (bv-result (%ogr-f-get-field-as-double-list h-feat
315 (pointer->list bv-result
316 (bytevector-s32-native-ref bv-count 0)
319 (export get-field-as-double-list)
321 ;;------------------------------------------------------------------------------
323 (define-gdal-foreign %ogr-f-get-field-as-binary
324 '* "OGR_F_GetFieldAsBinary" (list '* int '*) 20)
326 (define (get-field-as-binary h-feat i-field)
327 "Fetch field value as binary.
329 This method only works for OFTBinary and OFTString fields.
332 h-feat: handle to the feature that owned the field.
333 i-field: the field to fetch, from 0 to GetFieldCount()-1"
334 (let* ((bv-count (make-bytevector (sizeof int)))
335 (bv-result (%ogr-f-get-field-as-binary h-feat
339 (pointer->bytevector bv-result
340 (bytevector-s32-native-ref bv-count 0))))
342 (export get-field-as-binary)
344 ;;------------------------------------------------------------------------------
346 (define-gdal-foreign %ogr-f-get-field-as-string-list
347 '* "OGR_F_GetFieldAsStringList" (list '* int) 20)
349 (define (get-field-as-string-list h-feat i-field)
350 "Fetch field value as a list of strings.
352 Currently this method only works for OFTStringList fields.
355 h-feat: handle to the feature that owned the field.
356 i-field: the field to fetch, from 0 to GetFieldCount()-1"
357 (pointerpointer->string-list (%ogr-f-get-field-as-string-list h-feat
360 (export get-field-as-string-list)
362 ;;------------------------------------------------------------------------------
364 (define-gdal-foreign %ogr-f-get-field-as-datetime
365 int "OGR_F_GetFieldAsDateTime" (list '* int '* '* '* '* '* '* '*) 20)
367 (define (get-field-as-datetime h-feat i-field)
368 "Fetch field value as date and time.
370 Currently this method only works for OFTDate, OFTTime and OFTDateTime fields.
371 Use get-field-as-datetime-ex for second with millisecond accuracy.
374 h-feat: handle to the feature that owned the field.
375 i-field: the field to fetch, from 0 to GetFieldCount()-1"
376 (let* ((bv-year (make-bytevector (sizeof int)))
377 (bv-month (make-bytevector (sizeof int)))
378 (bv-day (make-bytevector (sizeof int)))
379 (bv-hour (make-bytevector (sizeof int)))
380 (bv-minute (make-bytevector (sizeof int)))
381 (bv-second (make-bytevector (sizeof int)))
382 (bv-tz (make-bytevector (sizeof int)))
383 (result (%ogr-f-get-field-as-datetime h-feat
385 (bytevector->pointer bv-year)
386 (bytevector->pointer bv-month)
387 (bytevector->pointer bv-day)
388 (bytevector->pointer bv-hour)
389 (bytevector->pointer bv-minute)
390 (bytevector->pointer bv-second)
391 (bytevector->pointer bv-tz))))
392 (if (c-bool->boolean result)
393 (make-gdal-datetime (bytevector-s32-native-ref bv-year 0)
394 (bytevector-s32-native-ref bv-month 0)
395 (bytevector-s32-native-ref bv-day 0)
396 (bytevector-s32-native-ref bv-hour 0)
397 (bytevector-s32-native-ref bv-minute 0)
398 (bytevector-s32-native-ref bv-second 0)
399 (bytevector-s32-native-ref bv-tz 0))
400 (error "failed to get datetime"))))
402 (export get-field-as-datetime)
404 ;;------------------------------------------------------------------------------
406 (define-gdal-foreign %ogr-f-get-field-as-datetime-ex
407 int "OGR_F_GetFieldAsDateTimeEx" (list '* int '* '* '* '* '* '* '*) 20)
409 (define (get-field-as-datetime-ex h-feat i-field)
410 "Fetch field value as date and time.
412 Currently this method only works for OFTDate, OFTTime and OFTDateTime fields.
415 h-feat: handle to the feature that owned the field.
416 i-field: the field to fetch, from 0 to GetFieldCount()-1"
417 (let* ((bv-year (make-bytevector (sizeof int)))
418 (bv-month (make-bytevector (sizeof int)))
419 (bv-day (make-bytevector (sizeof int)))
420 (bv-hour (make-bytevector (sizeof int)))
421 (bv-minute (make-bytevector (sizeof int)))
422 (bv-second (make-bytevector (sizeof int)))
423 (bv-tz (make-bytevector (sizeof int)))
424 (result (%ogr-f-get-field-as-datetime-ex
427 (bytevector->pointer bv-year)
428 (bytevector->pointer bv-month)
429 (bytevector->pointer bv-day)
430 (bytevector->pointer bv-hour)
431 (bytevector->pointer bv-minute)
432 (bytevector->pointer bv-second)
433 (bytevector->pointer bv-tz))))
434 (if (c-bool->boolean result)
435 (make-gdal-datetime (bytevector-s32-native-ref bv-year 0)
436 (bytevector-s32-native-ref bv-month 0)
437 (bytevector-s32-native-ref bv-day 0)
438 (bytevector-s32-native-ref bv-hour 0)
439 (bytevector-s32-native-ref bv-minute 0)
440 (bytevector-s32-native-ref bv-second 0)
441 (bytevector-s32-native-ref bv-tz 0))
442 (error "failed to get datetime"))))
444 (export get-field-as-datetime-ex)
446 ;;------------------------------------------------------------------------------