forked from lukeroth/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ogr.go
1836 lines (1521 loc) · 49.8 KB
/
ogr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package gdal
/*
#include "go_gdal.h"
#include "gdal_version.h"
#cgo linux pkg-config: gdal
#cgo darwin pkg-config: gdal
#cgo windows LDFLAGS: -Lc:/gdal/release-1600-x64/lib -lgdal_i
#cgo windows CFLAGS: -IC:/gdal/release-1600-x64/include
*/
import "C"
import (
"reflect"
"time"
"unsafe"
)
func init() {
C.OGRRegisterAll()
}
/* -------------------------------------------------------------------- */
/* Significant constants. */
/* -------------------------------------------------------------------- */
// List of well known binary geometry types
type GeometryType uint32
const (
GT_Unknown = GeometryType(C.wkbUnknown)
GT_Point = GeometryType(C.wkbPoint)
GT_LineString = GeometryType(C.wkbLineString)
GT_Polygon = GeometryType(C.wkbPolygon)
GT_MultiPoint = GeometryType(C.wkbMultiPoint)
GT_MultiLineString = GeometryType(C.wkbMultiLineString)
GT_MultiPolygon = GeometryType(C.wkbMultiPolygon)
GT_GeometryCollection = GeometryType(C.wkbGeometryCollection)
GT_None = GeometryType(C.wkbNone)
GT_LinearRing = GeometryType(C.wkbLinearRing)
GT_Point25D = GeometryType(C.wkbPoint25D)
GT_LineString25D = GeometryType(C.wkbLineString25D)
GT_Polygon25D = GeometryType(C.wkbPolygon25D)
GT_MultiPoint25D = GeometryType(C.wkbMultiPoint25D)
GT_MultiLineString25D = GeometryType(C.wkbMultiLineString25D)
GT_MultiPolygon25D = GeometryType(C.wkbMultiPolygon25D)
GT_GeometryCollection25D = GeometryType(C.wkbGeometryCollection25D)
)
/* -------------------------------------------------------------------- */
/* Envelope functions */
/* -------------------------------------------------------------------- */
type Envelope struct {
cval C.OGREnvelope
}
func (env Envelope) MinX() float64 {
return float64(env.cval.MinX)
}
func (env Envelope) MaxX() float64 {
return float64(env.cval.MaxX)
}
func (env Envelope) MinY() float64 {
return float64(env.cval.MinY)
}
func (env Envelope) MaxY() float64 {
return float64(env.cval.MaxY)
}
func (env *Envelope) SetMinX(val float64) {
env.cval.MinX = C.double(val)
}
func (env *Envelope) SetMaxX(val float64) {
env.cval.MaxX = C.double(val)
}
func (env *Envelope) SetMinY(val float64) {
env.cval.MinY = C.double(val)
}
func (env *Envelope) SetMaxY(val float64) {
env.cval.MaxY = C.double(val)
}
func (env Envelope) IsInit() bool {
return env.cval.MinX != 0 || env.cval.MinY != 0 || env.cval.MaxX != 0 || env.cval.MaxY != 0
}
func min(a, b C.double) C.double {
if a < b {
return a
}
return b
}
func max(a, b C.double) C.double {
if a > b {
return a
}
return b
}
// Return the union of this envelope with another one
func (env Envelope) Union(other Envelope) {
if env.IsInit() {
env.cval.MinX = min(env.cval.MinX, other.cval.MinX)
env.cval.MinY = min(env.cval.MinY, other.cval.MinY)
env.cval.MaxX = max(env.cval.MaxX, other.cval.MaxX)
env.cval.MaxY = max(env.cval.MaxY, other.cval.MaxY)
} else {
env.cval.MinX = other.cval.MinX
env.cval.MinY = other.cval.MinY
env.cval.MaxX = other.cval.MaxX
env.cval.MaxY = other.cval.MaxY
}
}
// Return the intersection of this envelope with another
func (env Envelope) Intersect(other Envelope) {
if env.Intersects(other) {
if env.IsInit() {
env.cval.MinX = max(env.cval.MinX, other.cval.MinX)
env.cval.MinY = max(env.cval.MinY, other.cval.MinY)
env.cval.MaxX = min(env.cval.MaxX, other.cval.MaxX)
env.cval.MaxY = min(env.cval.MaxY, other.cval.MaxY)
} else {
env.cval.MinX = other.cval.MinX
env.cval.MinY = other.cval.MinY
env.cval.MaxX = other.cval.MaxX
env.cval.MaxY = other.cval.MaxY
}
} else {
env.cval.MinX = 0
env.cval.MinY = 0
env.cval.MaxX = 0
env.cval.MaxY = 0
}
}
// Test if one envelope intersects another
func (env Envelope) Intersects(other Envelope) bool {
return env.cval.MinX <= other.cval.MaxX &&
env.cval.MaxX >= other.cval.MinX &&
env.cval.MinY <= other.cval.MaxY &&
env.cval.MaxY >= other.cval.MinY
}
// Test if one envelope completely contains another
func (env Envelope) Contains(other Envelope) bool {
return env.cval.MinX <= other.cval.MinX &&
env.cval.MaxX >= other.cval.MaxX &&
env.cval.MinY <= other.cval.MinY &&
env.cval.MaxY >= other.cval.MaxY
}
/* -------------------------------------------------------------------- */
/* Misc functions */
/* -------------------------------------------------------------------- */
// Clean up all OGR related resources
func CleanupOGR() {
C.OGRCleanupAll()
}
// Convert a go bool to a C int
func BoolToCInt(in bool) (out C.int) {
if in {
out = 1
} else {
out = 0
}
return
}
/* -------------------------------------------------------------------- */
/* Geometry functions */
/* -------------------------------------------------------------------- */
type Geometry struct {
cval C.OGRGeometryH
}
//Create a geometry object from its well known binary representation
func CreateFromWKB(wkb []uint8, srs SpatialReference, bytes int) (Geometry, error) {
cString := (*C.uchar)(unsafe.Pointer(&wkb[0]))
var newGeom Geometry
return newGeom, C.OGR_G_CreateFromWkb(
cString, srs.cval, &newGeom.cval, C.int(bytes),
).Err()
}
//Create a geometry object from its well known text representation
func CreateFromWKT(wkt string, srs SpatialReference) (Geometry, error) {
cString := C.CString(wkt)
defer C.free(unsafe.Pointer(cString))
var newGeom Geometry
return newGeom, C.OGR_G_CreateFromWkt(
&cString, srs.cval, &newGeom.cval,
).Err()
}
//Create a geometry object from its GeoJSON representation
func CreateFromJson(_json string) Geometry {
cString := C.CString(_json)
defer C.free(unsafe.Pointer(cString))
var newGeom Geometry
newGeom.cval = C.OGR_G_CreateGeometryFromJson(cString)
return newGeom
}
// Destroy geometry object
func (geometry Geometry) Destroy() {
C.OGR_G_DestroyGeometry(geometry.cval)
}
// Create an empty geometry of the desired type
func Create(geomType GeometryType) Geometry {
geom := C.OGR_G_CreateGeometry(C.OGRwkbGeometryType(geomType))
return Geometry{geom}
}
// Stroke arc to linestring
func ApproximateArcAngles(
x, y, z,
primaryRadius,
secondaryRadius,
rotation,
startAngle,
endAngle,
stepSizeDegrees float64,
) Geometry {
geom := C.OGR_G_ApproximateArcAngles(
C.double(x),
C.double(y),
C.double(z),
C.double(primaryRadius),
C.double(secondaryRadius),
C.double(rotation),
C.double(startAngle),
C.double(endAngle),
C.double(stepSizeDegrees))
return Geometry{geom}
}
// Convert to polygon
func (geom Geometry) ForceToPolygon() Geometry {
newGeom := C.OGR_G_ForceToPolygon(geom.cval)
return Geometry{newGeom}
}
// Convert to multipolygon
func (geom Geometry) ForceToMultiPolygon() Geometry {
newGeom := C.OGR_G_ForceToMultiPolygon(geom.cval)
return Geometry{newGeom}
}
// Convert to multipoint
func (geom Geometry) ForceToMultiPoint() Geometry {
newGeom := C.OGR_G_ForceToMultiPoint(geom.cval)
return Geometry{newGeom}
}
// Convert to multilinestring
func (geom Geometry) ForceToMultiLineString() Geometry {
newGeom := C.OGR_G_ForceToMultiLineString(geom.cval)
return Geometry{newGeom}
}
// Get the dimension of this geometry
func (geom Geometry) Dimension() int {
dim := C.OGR_G_GetDimension(geom.cval)
return int(dim)
}
// Get the dimension of the coordinates in this geometry
func (geom Geometry) CoordinateDimension() int {
dim := C.OGR_G_GetCoordinateDimension(geom.cval)
return int(dim)
}
// Set the dimension of the coordinates in this geometry
func (geom Geometry) SetCoordinateDimension(dim int) {
C.OGR_G_SetCoordinateDimension(geom.cval, C.int(dim))
}
// Create a copy of this geometry
func (geom Geometry) Clone() Geometry {
newGeom := C.OGR_G_Clone(geom.cval)
return Geometry{newGeom}
}
// Compute and return the bounding envelope for this geometry
func (geom Geometry) Envelope() Envelope {
var env Envelope
C.OGR_G_GetEnvelope(geom.cval, &env.cval)
return env
}
// Unimplemented: GetEnvelope3D
// Assign a geometry from well known binary data
func (geom Geometry) FromWKB(wkb []uint8, bytes int) error {
cString := (*C.uchar)(unsafe.Pointer(&wkb[0]))
return C.OGR_G_ImportFromWkb(geom.cval, cString, C.int(bytes)).Err()
}
// Convert a geometry to well known binary data
func (geom Geometry) ToWKB() ([]uint8, error) {
b := make([]uint8, geom.WKBSize())
cString := (*C.uchar)(unsafe.Pointer(&b[0]))
err := C.OGR_G_ExportToWkb(geom.cval, C.OGRwkbByteOrder(C.wkbNDR), cString).Err()
return b, err
}
// Returns size of related binary representation
func (geom Geometry) WKBSize() int {
size := C.OGR_G_WkbSize(geom.cval)
return int(size)
}
// Assign geometry object from its well known text representation
func (geom Geometry) FromWKT(wkt string) error {
cString := C.CString(wkt)
defer C.free(unsafe.Pointer(cString))
return C.OGR_G_ImportFromWkt(geom.cval, &cString).Err()
}
// Fetch geometry as WKT
func (geom Geometry) ToWKT() (string, error) {
var p *C.char
err := C.OGR_G_ExportToWkt(geom.cval, &p).Err()
wkt := C.GoString(p)
return wkt, err
}
// Fetch geometry type
func (geom Geometry) Type() GeometryType {
gt := C.OGR_G_GetGeometryType(geom.cval)
return GeometryType(gt)
}
// Fetch geometry name
func (geom Geometry) Name() string {
name := C.OGR_G_GetGeometryName(geom.cval)
return C.GoString(name)
}
// Unimplemented: DumpReadable
// Convert geometry to strictly 2D
func (geom Geometry) FlattenTo2D() {
C.OGR_G_FlattenTo2D(geom.cval)
}
// Force rings to be closed
func (geom Geometry) CloseRings() {
C.OGR_G_CloseRings(geom.cval)
}
// Create a geometry from its GML representation
func CreateFromGML(gml string) Geometry {
cString := C.CString(gml)
defer C.free(unsafe.Pointer(cString))
geom := C.OGR_G_CreateFromGML(cString)
return Geometry{geom}
}
// Convert a geometry to GML format
func (geom Geometry) ToGML() string {
val := C.OGR_G_ExportToGML(geom.cval)
return C.GoString(val)
}
// Convert a geometry to GML format with options
func (geom Geometry) ToGML_Ex(options []string) string {
length := len(options)
opts := make([]*C.char, length+1)
for i := 0; i < length; i++ {
opts[i] = C.CString(options[i])
defer C.free(unsafe.Pointer(opts[i]))
}
opts[length] = (*C.char)(unsafe.Pointer(nil))
val := C.OGR_G_ExportToGMLEx(geom.cval, (**C.char)(unsafe.Pointer(&opts[0])))
return C.GoString(val)
}
// Convert a geometry to KML format
func (geom Geometry) ToKML() string {
val := C.OGR_G_ExportToKML(geom.cval, nil)
return C.GoString(val)
}
// Convert a geometry to JSON format
func (geom Geometry) ToJSON() string {
val := C.OGR_G_ExportToJson(geom.cval)
return C.GoString(val)
}
// Convert a geometry to JSON format with options
func (geom Geometry) ToJSON_ex(options []string) string {
length := len(options)
opts := make([]*C.char, length+1)
for i := 0; i < length; i++ {
opts[i] = C.CString(options[i])
defer C.free(unsafe.Pointer(opts[i]))
}
opts[length] = (*C.char)(unsafe.Pointer(nil))
val := C.OGR_G_ExportToJsonEx(geom.cval, (**C.char)(unsafe.Pointer(&opts[0])))
return C.GoString(val)
}
// Fetch the spatial reference associated with this geometry
func (geom Geometry) SpatialReference() SpatialReference {
spatialRef := C.OGR_G_GetSpatialReference(geom.cval)
return SpatialReference{spatialRef}
}
// Assign a spatial reference to this geometry
func (geom Geometry) SetSpatialReference(spatialRef SpatialReference) {
C.OGR_G_AssignSpatialReference(geom.cval, spatialRef.cval)
}
// Apply coordinate transformation to geometry
func (geom Geometry) Transform(ct CoordinateTransform) error {
return C.OGR_G_Transform(geom.cval, ct.cval).Err()
}
// Transform geometry to new spatial reference system
func (geom Geometry) TransformTo(sr SpatialReference) error {
return C.OGR_G_TransformTo(geom.cval, sr.cval).Err()
}
// Simplify the geometry
func (geom Geometry) Simplify(tolerance float64) Geometry {
newGeom := C.OGR_G_Simplify(geom.cval, C.double(tolerance))
return Geometry{newGeom}
}
// Simplify the geometry while preserving topology
func (geom Geometry) SimplifyPreservingTopology(tolerance float64) Geometry {
newGeom := C.OGR_G_SimplifyPreserveTopology(geom.cval, C.double(tolerance))
return Geometry{newGeom}
}
// Modify the geometry such that it has no line segment longer than the given distance
func (geom Geometry) Segmentize(distance float64) {
C.OGR_G_Segmentize(geom.cval, C.double(distance))
}
// Return true if these features intersect
func (geom Geometry) Intersects(other Geometry) bool {
val := C.OGR_G_Intersects(geom.cval, other.cval)
return val != 0
}
// Return true if these features are equal
func (geom Geometry) Equals(other Geometry) bool {
val := C.OGR_G_Equals(geom.cval, other.cval)
return val != 0
}
// Return true if the features are disjoint
func (geom Geometry) Disjoint(other Geometry) bool {
val := C.OGR_G_Disjoint(geom.cval, other.cval)
return val != 0
}
// Return true if this feature touches the other
func (geom Geometry) Touches(other Geometry) bool {
val := C.OGR_G_Touches(geom.cval, other.cval)
return val != 0
}
// Return true if this feature crosses the other
func (geom Geometry) Crosses(other Geometry) bool {
val := C.OGR_G_Crosses(geom.cval, other.cval)
return val != 0
}
// Return true if this geometry is within the other
func (geom Geometry) Within(other Geometry) bool {
val := C.OGR_G_Within(geom.cval, other.cval)
return val != 0
}
// Return true if this geometry contains the other
func (geom Geometry) Contains(other Geometry) bool {
val := C.OGR_G_Contains(geom.cval, other.cval)
return val != 0
}
// Return true if this geometry overlaps the other
func (geom Geometry) Overlaps(other Geometry) bool {
val := C.OGR_G_Overlaps(geom.cval, other.cval)
return val != 0
}
// Compute boundary for the geometry
func (geom Geometry) Boundary() Geometry {
newGeom := C.OGR_G_Boundary(geom.cval)
return Geometry{newGeom}
}
// Compute convex hull for the geometry
func (geom Geometry) ConvexHull() Geometry {
newGeom := C.OGR_G_ConvexHull(geom.cval)
return Geometry{newGeom}
}
// Compute buffer of the geometry
func (geom Geometry) Buffer(distance float64, segments int) Geometry {
newGeom := C.OGR_G_Buffer(geom.cval, C.double(distance), C.int(segments))
return Geometry{newGeom}
}
// Compute intersection of this geometry with the other
func (geom Geometry) Intersection(other Geometry) Geometry {
newGeom := C.OGR_G_Intersection(geom.cval, other.cval)
return Geometry{newGeom}
}
// Compute union of this geometry with the other
func (geom Geometry) Union(other Geometry) Geometry {
newGeom := C.OGR_G_Union(geom.cval, other.cval)
return Geometry{newGeom}
}
// Unimplemented: UnionCascaded
// Unimplemented: PointOn Surface (until 2.0)
// Return a point guaranteed to lie on the surface
// func (geom Geometry) PointOnSurface() Geometry {
// newGeom := C.OGR_G_PointOnSurface(geom.cval)
// return Geometry{newGeom}
// }
// Compute difference between this geometry and the other
func (geom Geometry) Difference(other Geometry) Geometry {
newGeom := C.OGR_G_Difference(geom.cval, other.cval)
return Geometry{newGeom}
}
// Compute symmetric difference between this geometry and the other
func (geom Geometry) SymmetricDifference(other Geometry) Geometry {
newGeom := C.OGR_G_SymDifference(geom.cval, other.cval)
return Geometry{newGeom}
}
// Compute distance between thie geometry and the other
func (geom Geometry) Distance(other Geometry) float64 {
dist := C.OGR_G_Distance(geom.cval, other.cval)
return float64(dist)
}
// Compute length of geometry
func (geom Geometry) Length() float64 {
length := C.OGR_G_Length(geom.cval)
return float64(length)
}
// Compute area of geometry
func (geom Geometry) Area() float64 {
area := C.OGR_G_Area(geom.cval)
return float64(area)
}
// Compute centroid of geometry
func (geom Geometry) Centroid() Geometry {
var centroid Geometry
C.OGR_G_Centroid(geom.cval, centroid.cval)
return centroid
}
// Clear the geometry to its uninitialized state
func (geom Geometry) Empty() {
C.OGR_G_Empty(geom.cval)
}
// Test if the geometry is empty
func (geom Geometry) IsEmpty() bool {
val := C.OGR_G_IsEmpty(geom.cval)
return val != 0
}
// Test if the geometry is valid
func (geom Geometry) IsValid() bool {
val := C.OGR_G_IsValid(geom.cval)
return val != 0
}
// Test if the geometry is simple
func (geom Geometry) IsSimple() bool {
val := C.OGR_G_IsSimple(geom.cval)
return val != 0
}
// Test if the geometry is a ring
func (geom Geometry) IsRing() bool {
val := C.OGR_G_IsRing(geom.cval)
return val != 0
}
// Polygonize a set of sparse edges
func (geom Geometry) Polygonize() Geometry {
newGeom := C.OGR_G_Polygonize(geom.cval)
return Geometry{newGeom}
}
// Fetch number of points in the geometry
func (geom Geometry) PointCount() int {
count := C.OGR_G_GetPointCount(geom.cval)
return int(count)
}
// Unimplemented: Points
// Fetch the X coordinate of a point in the geometry
func (geom Geometry) X(index int) float64 {
x := C.OGR_G_GetX(geom.cval, C.int(index))
return float64(x)
}
// Fetch the Y coordinate of a point in the geometry
func (geom Geometry) Y(index int) float64 {
y := C.OGR_G_GetY(geom.cval, C.int(index))
return float64(y)
}
// Fetch the Z coordinate of a point in the geometry
func (geom Geometry) Z(index int) float64 {
z := C.OGR_G_GetZ(geom.cval, C.int(index))
return float64(z)
}
// Fetch the coordinates of a point in the geometry
func (geom Geometry) Point(index int) (x, y, z float64) {
C.OGR_G_GetPoint(
geom.cval,
C.int(index),
(*C.double)(&x),
(*C.double)(&y),
(*C.double)(&z))
return
}
// Set the coordinates of a point in the geometry
func (geom Geometry) SetPoint(index int, x, y, z float64) {
C.OGR_G_SetPoint(
geom.cval,
C.int(index),
C.double(x),
C.double(y),
C.double(z))
}
// Set the coordinates of a point in the geometry, ignoring the 3rd dimension
func (geom Geometry) SetPoint2D(index int, x, y float64) {
C.OGR_G_SetPoint_2D(geom.cval, C.int(index), C.double(x), C.double(y))
}
// Add a new point to the geometry (line string or polygon only)
func (geom Geometry) AddPoint(x, y, z float64) {
C.OGR_G_AddPoint(geom.cval, C.double(x), C.double(y), C.double(z))
}
// Add a new point to the geometry (line string or polygon only), ignoring the 3rd dimension
func (geom Geometry) AddPoint2D(x, y float64) {
C.OGR_G_AddPoint_2D(geom.cval, C.double(x), C.double(y))
}
// Fetch the number of elements in the geometry, or number of geometries in the container
func (geom Geometry) GeometryCount() int {
count := C.OGR_G_GetGeometryCount(geom.cval)
return int(count)
}
// Fetch geometry from a geometry container
func (geom Geometry) Geometry(index int) Geometry {
newGeom := C.OGR_G_GetGeometryRef(geom.cval, C.int(index))
return Geometry{newGeom}
}
// Add a geometry to a geometry container
func (geom Geometry) AddGeometry(other Geometry) error {
return C.OGR_G_AddGeometry(geom.cval, other.cval).Err()
}
// Add a geometry to a geometry container and assign ownership to that container
func (geom Geometry) AddGeometryDirectly(other Geometry) error {
return C.OGR_G_AddGeometryDirectly(geom.cval, other.cval).Err()
}
// Remove a geometry from the geometry container
func (geom Geometry) RemoveGeometry(index int, delete bool) error {
return C.OGR_G_RemoveGeometry(geom.cval, C.int(index), BoolToCInt(delete)).Err()
}
// Build a polygon / ring from a set of lines
func (geom Geometry) BuildPolygonFromEdges(autoClose bool, tolerance float64) (Geometry, error) {
var cErr C.OGRErr
newGeom := C.OGRBuildPolygonFromEdges(
geom.cval,
0,
BoolToCInt(autoClose),
C.double(tolerance),
&cErr,
)
return Geometry{newGeom}, cErr.Err()
}
/* -------------------------------------------------------------------- */
/* Field definition functions */
/* -------------------------------------------------------------------- */
// List of well known binary geometry types
type FieldType int
const (
FT_Integer = FieldType(C.OFTInteger)
FT_IntegerList = FieldType(C.OFTIntegerList)
FT_Real = FieldType(C.OFTReal)
FT_RealList = FieldType(C.OFTRealList)
FT_String = FieldType(C.OFTString)
FT_StringList = FieldType(C.OFTStringList)
FT_Binary = FieldType(C.OFTBinary)
FT_Date = FieldType(C.OFTDate)
FT_Time = FieldType(C.OFTTime)
FT_DateTime = FieldType(C.OFTDateTime)
)
type Justification int
const (
J_Undefined = Justification(C.OJUndefined)
J_Left = Justification(C.OJLeft)
J_Right = Justification(C.OJRight)
)
type FieldDefinition struct {
cval C.OGRFieldDefnH
}
type Field struct {
cval *C.OGRField
}
// Create a new field definition
func CreateFieldDefinition(name string, fieldType FieldType) FieldDefinition {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
fieldDef := C.OGR_Fld_Create(cName, C.OGRFieldType(fieldType))
return FieldDefinition{fieldDef}
}
// Destroy the field definition
func (fd FieldDefinition) Destroy() {
C.OGR_Fld_Destroy(fd.cval)
}
// Fetch the name of the field
func (fd FieldDefinition) Name() string {
name := C.OGR_Fld_GetNameRef(fd.cval)
return C.GoString(name)
}
// Set the name of the field
func (fd FieldDefinition) SetName(name string) {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
C.OGR_Fld_SetName(fd.cval, cName)
}
// Fetch the type of this field
func (fd FieldDefinition) Type() FieldType {
fType := C.OGR_Fld_GetType(fd.cval)
return FieldType(fType)
}
// Set the type of this field
func (fd FieldDefinition) SetType(fType FieldType) {
C.OGR_Fld_SetType(fd.cval, C.OGRFieldType(fType))
}
// Fetch the justification for this field
func (fd FieldDefinition) Justification() Justification {
justify := C.OGR_Fld_GetJustify(fd.cval)
return Justification(justify)
}
// Set the justification for this field
func (fd FieldDefinition) SetJustification(justify Justification) {
C.OGR_Fld_SetJustify(fd.cval, C.OGRJustification(justify))
}
// Fetch the formatting width for this field
func (fd FieldDefinition) Width() int {
width := C.OGR_Fld_GetWidth(fd.cval)
return int(width)
}
// Set the formatting width for this field
func (fd FieldDefinition) SetWidth(width int) {
C.OGR_Fld_SetWidth(fd.cval, C.int(width))
}
// Fetch the precision for this field
func (fd FieldDefinition) Precision() int {
precision := C.OGR_Fld_GetPrecision(fd.cval)
return int(precision)
}
// Set the precision for this field
func (fd FieldDefinition) SetPrecision(precision int) {
C.OGR_Fld_SetPrecision(fd.cval, C.int(precision))
}
// Set defining parameters of field in a single call
func (fd FieldDefinition) Set(
name string,
fType FieldType,
width, precision int,
justify Justification,
) {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
C.OGR_Fld_Set(
fd.cval,
cName,
C.OGRFieldType(fType),
C.int(width),
C.int(precision),
C.OGRJustification(justify),
)
}
// Fetch whether this field should be ignored when fetching features
func (fd FieldDefinition) IsIgnored() bool {
ignore := C.OGR_Fld_IsIgnored(fd.cval)
return ignore != 0
}
// Set whether this field should be ignored when fetching features
func (fd FieldDefinition) SetIgnored(ignore bool) {
C.OGR_Fld_SetIgnored(fd.cval, BoolToCInt(ignore))
}
// Fetch human readable name for the field type
func (ft FieldType) Name() string {
name := C.OGR_GetFieldTypeName(C.OGRFieldType(ft))
return C.GoString(name)
}
/* -------------------------------------------------------------------- */
/* Feature definition functions */
/* -------------------------------------------------------------------- */
type FeatureDefinition struct {
cval C.OGRFeatureDefnH
}
// Create a new feature definition object
func CreateFeatureDefinition(name string) FeatureDefinition {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
fd := C.OGR_FD_Create(cName)
return FeatureDefinition{fd}
}
// Destroy a feature definition object
func (fd FeatureDefinition) Destroy() {
C.OGR_FD_Destroy(fd.cval)
}
// Drop a reference, and delete object if no references remain
func (fd FeatureDefinition) Release() {
C.OGR_FD_Release(fd.cval)
}
// Fetch the name of this feature definition
func (fd FeatureDefinition) Name() string {
name := C.OGR_FD_GetName(fd.cval)
return C.GoString(name)
}
// Fetch the number of fields in the feature definition
func (fd FeatureDefinition) FieldCount() int {
count := C.OGR_FD_GetFieldCount(fd.cval)
return int(count)
}
// Fetch the definition of the indicated field
func (fd FeatureDefinition) FieldDefinition(index int) FieldDefinition {
fieldDefn := C.OGR_FD_GetFieldDefn(fd.cval, C.int(index))
return FieldDefinition{fieldDefn}
}
// Fetch the index of the named field
func (fd FeatureDefinition) FieldIndex(name string) int {
cName := C.CString(name)
defer C.free(unsafe.Pointer(cName))
index := C.OGR_FD_GetFieldIndex(fd.cval, cName)
return int(index)
}
// Add a new field definition to this feature definition
func (fd FeatureDefinition) AddFieldDefinition(fieldDefn FieldDefinition) {
C.OGR_FD_AddFieldDefn(fd.cval, fieldDefn.cval)
}
// Delete a field definition from this feature definition
func (fd FeatureDefinition) DeleteFieldDefinition(index int) error {
return C.OGR_FD_DeleteFieldDefn(fd.cval, C.int(index)).Err()
}
// Fetch the geometry base type of this feature definition
func (fd FeatureDefinition) GeometryType() GeometryType {
gt := C.OGR_FD_GetGeomType(fd.cval)
return GeometryType(gt)
}
// Set the geometry base type for this feature definition
func (fd FeatureDefinition) SetGeometryType(geomType GeometryType) {
C.OGR_FD_SetGeomType(fd.cval, C.OGRwkbGeometryType(geomType))
}
// Fetch if the geometry can be ignored when fetching features
func (fd FeatureDefinition) IsGeometryIgnored() bool {
isIgnored := C.OGR_FD_IsGeometryIgnored(fd.cval)
return isIgnored != 0
}
// Set whether the geometry can be ignored when fetching features
func (fd FeatureDefinition) SetGeometryIgnored(val bool) {
C.OGR_FD_SetGeometryIgnored(fd.cval, BoolToCInt(val))
}
// Fetch if the style can be ignored when fetching features
func (fd FeatureDefinition) IsStyleIgnored() bool {
isIgnored := C.OGR_FD_IsStyleIgnored(fd.cval)
return isIgnored != 0
}
// Set whether the style can be ignored when fetching features
func (fd FeatureDefinition) SetStyleIgnored(val bool) {
C.OGR_FD_SetStyleIgnored(fd.cval, BoolToCInt(val))
}
// Increment the reference count by one
func (fd FeatureDefinition) Reference() int {
count := C.OGR_FD_Reference(fd.cval)
return int(count)
}
// Decrement the reference count by one
func (fd FeatureDefinition) Dereference() int {
count := C.OGR_FD_Dereference(fd.cval)
return int(count)
}
// Fetch the current reference count
func (fd FeatureDefinition) ReferenceCount() int {
count := C.OGR_FD_GetReferenceCount(fd.cval)
return int(count)
}
/* -------------------------------------------------------------------- */
/* Feature functions */
/* -------------------------------------------------------------------- */
type Feature struct {
cval C.OGRFeatureH
}
// Create a feature from this feature definition
func (fd FeatureDefinition) Create() Feature {
feature := C.OGR_F_Create(fd.cval)
return Feature{feature}
}
// Destroy this feature
func (feature Feature) Destroy() {
C.OGR_F_Destroy(feature.cval)
}
// Fetch feature definition
func (feature Feature) Definition() FeatureDefinition {
fd := C.OGR_F_GetDefnRef(feature.cval)
return FeatureDefinition{fd}
}
// Set feature geometry
func (feature Feature) SetGeometry(geom Geometry) error {