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 ;;------------------------------------------------------------------------------
448 (define-gdal-foreign %ogr-f-set-field-integer
449 void "OGR_F_SetFieldInteger" (list '* int int) 20)
451 (define (set-field-integer h-feat i-field n-value)
452 "Set field to integer value.
454 OFTInteger, OFTInteger64 and OFTReal fields will be set directly. OFTString
455 fields will be assigned a string representation of the value, but not
456 necessarily taking into account formatting constraints on this field. Other
457 field types may be unaffected.
460 h-feat: handle to the feature that owned the field.
461 i-field: the field to fetch, from 0 to GetFieldCount()-1.
462 n-value: the value to assign"
463 (%ogr-f-set-field-integer h-feat i-field n-value))
465 (export set-field-integer)
467 ;;------------------------------------------------------------------------------
469 (define-gdal-foreign %ogr-f-set-field-integer64
470 void "OGR_F_SetFieldInteger64" (list '* int int64) 20)
472 (define (set-field-integer64 h-feat i-field n-value)
473 "Set field to 64 bit integer value.
475 OFTInteger, OFTInteger64 and OFTReal fields will be set directly. OFTString
476 fields will be assigned a string representation of the value, but not
477 necessarily taking into account formatting constraints on this field. Other
478 field types may be unaffected.
481 h-feat: handle to the feature that owned the field.
482 i-field: the field to fetch, from 0 to GetFieldCount()-1.
483 n-value: the value to assign"
484 (%ogr-f-set-field-integer64 h-feat i-field n-value))
486 (export set-field-integer64)
488 ;;------------------------------------------------------------------------------
490 (define-gdal-foreign %ogr-f-set-field-integer64-list
491 void "OGR_F_SetFieldInteger64List" (list '* int int '*) 20)
493 (define (set-field-integer64-list h-feat i-field values)
494 "Set field to list of 64 bit integers value.
496 This function currently on has an effect of OFTIntegerList, OFTInteger64List
497 and OFTRealList fields.
500 h-feat: handle to the feature that owned the field.
501 i-field: the field to fetch, from 0 to GetFieldCount()-1.
502 values: the values to assign"
503 (%ogr-f-set-field-integer64-list h-feat i-field (length values)
504 (list->pointer values int64)))
506 (export set-field-integer64-list)
508 ;;------------------------------------------------------------------------------
510 (define-gdal-foreign %ogr-f-set-field-integer-list
511 void "OGR_F_SetFieldIntegerList" (list '* int int '*) 20)
513 (define (set-field-integer-list h-feat i-field values)
514 "Set field to list of integers value.
516 This function currently on has an effect of OFTIntegerList, OFTInteger64List
517 and OFTRealList fields.
520 h-feat: handle to the feature that owned the field.
521 i-field: the field to fetch, from 0 to GetFieldCount()-1.
522 values: the values to assign"
523 (%ogr-f-set-field-integer-list h-feat i-field (length values)
524 (list->pointer values int)))
526 (export set-field-integer-list)
528 ;;------------------------------------------------------------------------------
530 (define-gdal-foreign %ogr-f-set-field-null
531 void "OGR_F_SetFieldNull" (list '* int) 22)
533 (define (set-field-null h-feat i-field)
534 "Clear a field, marking it as null.
537 h-feat: handle to the feature that owned the field.
538 i-field: the field to fetch, from 0 to GetFieldCount()-1"
539 (%ogr-f-set-field-null h-feat i-field))
541 (export set-field-null)
543 ;;------------------------------------------------------------------------------
545 (define-gdal-foreign %ogr-f-set-field-raw
546 void "OGR_F_SetFieldRaw" (list '* int '*) 20)
548 (define (set-field-raw h-feat i-field value)
551 The passed value OGRField must be of exactly the same type as the target field,
552 or an application crash may occur. The passed value is copied, and will not be
553 affected. It remains the responsibility of the caller.
556 h-feat: handle to the feature that owned the field.
557 i-field: the field to fetch, from 0 to GetFieldCount()-1.
558 value: handle on the value to assign"
559 (%ogr-f-set-field-raw h-feat i-field value))
561 (export set-field-raw)
563 ;;------------------------------------------------------------------------------
565 (define-gdal-foreign %ogr-f-set-field-string
566 void "OGR_F_SetFieldString" (list '* int '*) 20)
568 (define (set-field-string h-feat i-field value)
569 "Set field to string value.
571 OFTInteger fields will be set based on an atoi() conversion of the string.
572 OFTInteger64 fields will be set based on an CPLAtoGIntBig() conversion of the
573 string. OFTReal fields will be set based on an CPLAtof() conversion of the
574 string. Other field types may be unaffected.
577 h-feat: handle to the feature that owned the field.
578 i-field: the field to fetch, from 0 to GetFieldCount()-1.
579 value: string to assign"
580 (%ogr-f-set-field-string h-feat i-field (string->pointer value)))
582 (export set-field-string)
584 ;;------------------------------------------------------------------------------
586 (define-gdal-foreign %ogr-f-set-field-string-list
587 void "OGR_F_SetFieldStringList" (list '* int '*) 20)
589 (define (set-field-string-list h-feat i-field lst)
590 "Set field to list of strings value.
592 This function currently on has an effect of OFTStringList fields.
595 h-feat: handle to the feature that owned the field.
596 i-field: the field to fetch, from 0 to GetFieldCount()-1.
597 lst: string list to assign"
598 (%ogr-f-set-field-string-list h-feat i-field (length lst)
599 (string-list->pointerpointer lst)))
601 (export set-field-string-list)
603 ;;------------------------------------------------------------------------------
605 (define-gdal-foreign %ogr-f-set-field-date-time
606 void "OGR_F_SetFieldDateTime" (list '* int int int int int int int int) 20)
608 (define (set-field-date-time h-feat i-field year month day hour
609 minute second tz-flag)
610 "Set field to datetime.
612 This method currently only has an effect for OFTDate, OFTTime and OFTDateTime
616 h-feat: handle to the feature that owned the field.
617 i-field: the field to fetch, from 0 to GetFieldCount()-1.
618 year: including century.
624 tz-flag: 0=unknown, 1=localtime, 100=GMT, see data model for details"
625 (%ogr-f-set-field-date-time h-feat i-field year month day
626 hour minute second tz-flag))
628 (export set-field-date-time)
630 ;;------------------------------------------------------------------------------
632 (define-gdal-foreign %ogr-f-set-field-date-time-ex
633 void "OGR_F_SetFieldDateTimeEx" (list '* int int int int int
636 (define (set-field-date-time-ex h-feat i-field year month day hour
637 minute second tz-flag)
638 "Set field to datetime.
640 This method currently only has an effect for OFTDate, OFTTime and OFTDateTime
644 h-feat: handle to the feature that owned the field.
645 i-field: the field to fetch, from 0 to GetFieldCount()-1.
646 year: including century.
651 second: (0-59, with millisecond accuracy).
652 tz-flag: 0=unknown, 1=localtime, 100=GMT, see data model for details"
653 (%ogr-f-set-field-date-time-ex h-feat i-field year month day
654 hour minute second tz-flag))
656 (export set-field-date-time-ex)
658 ;;------------------------------------------------------------------------------