forked from lukeroth/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayer.go
232 lines (191 loc) · 6.54 KB
/
layer.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
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 "unsafe"
type Layer struct {
cval C.OGRLayerH
}
// Return the layer name
func (layer *Layer) Name() string {
name := C.OGR_L_GetName(layer.cval)
return C.GoString(name)
}
// Return the layer geometry type
func (layer *Layer) GeomType() GeometryType {
gt := C.OGR_L_GetGeomType(layer.cval)
return GeometryType(gt)
}
// Return the current spatial filter for this layer
func (layer *Layer) SpatialFilter() *Geometry {
geom := C.OGR_L_GetSpatialFilter(layer.cval)
if geom == nil {
return nil
}
return &Geometry{geom}
}
// Set a new spatial filter for this layer
func (layer *Layer) SetSpatialFilter(filter *Geometry) {
if filter == nil {
C.OGR_L_SetSpatialFilter(layer.cval, nil)
} else {
C.OGR_L_SetSpatialFilter(layer.cval, filter.cval)
}
}
// Set a new rectangular spatial filter for this layer
func (layer *Layer) SetSpatialFilterRect(minX, minY, maxX, maxY float64) {
C.OGR_L_SetSpatialFilterRect(
layer.cval,
C.double(minX), C.double(minY), C.double(maxX), C.double(maxY),
)
}
// Set a new attribute query filter
func (layer *Layer) SetAttributeFilter(filter string) error {
cFilter := C.CString(filter)
defer C.free(unsafe.Pointer(cFilter))
return C.OGR_L_SetAttributeFilter(layer.cval, cFilter).Err()
}
// Reset reading to start on the first feature
func (layer *Layer) ResetReading() {
C.OGR_L_ResetReading(layer.cval)
}
// Fetch the next available feature from this layer
func (layer *Layer) NextFeature() *Feature {
feature := C.OGR_L_GetNextFeature(layer.cval)
if feature == nil {
return nil
}
return &Feature{feature}
}
// Move read cursor to the provided index
func (layer *Layer) SetNextByIndex(index int) error {
return C.OGR_L_SetNextByIndex(layer.cval, C.GIntBig(index)).Err()
}
// Fetch a feature by its index
func (layer *Layer) Feature(index int) Feature {
feature := C.OGR_L_GetFeature(layer.cval, C.GIntBig(index))
return Feature{feature}
}
// Rewrite the provided feature
func (layer *Layer) SetFeature(feature *Feature) error {
return C.OGR_L_SetFeature(layer.cval, feature.cval).Err()
}
// Create and write a new feature within a layer
func (layer *Layer) CreateFeature(feature *Feature) error {
return C.OGR_L_CreateFeature(layer.cval, feature.cval).Err()
}
// Delete indicated feature from layer
func (layer *Layer) DeleteFeature(index int) error {
return C.OGR_L_DeleteFeature(layer.cval, C.GIntBig(index)).Err()
}
// Fetch the schema information for this layer
func (layer *Layer) Definition() FeatureDefinition {
defn := C.OGR_L_GetLayerDefn(layer.cval)
return FeatureDefinition{defn}
}
// Fetch the spatial reference system for this layer
func (layer *Layer) SpatialRef() SpatialReference {
sr := C.OGR_L_GetSpatialRef(layer.cval)
return SpatialReference{sr}
}
// Fetch the feature count for this layer
func (layer *Layer) FeatureCount(force bool) (count int, ok bool) {
count = int(C.OGR_L_GetFeatureCount(layer.cval, BoolToCInt(force)))
return count, count != -1
}
// Fetch the extent of this layer
func (layer *Layer) Extent(force bool) (env Envelope, err error) {
err = C.OGR_L_GetExtent(layer.cval, &env.cval, BoolToCInt(force)).Err()
return
}
// Test if this layer supports the named capability
func (layer *Layer) TestCapability(capability string) bool {
cString := C.CString(capability)
defer C.free(unsafe.Pointer(cString))
val := C.OGR_L_TestCapability(layer.cval, cString)
return val != 0
}
// Create a new field on a layer
func (layer *Layer) CreateField(fd FieldDefinition, approxOK bool) error {
return C.OGR_L_CreateField(layer.cval, fd.cval, BoolToCInt(approxOK)).Err()
}
// Delete a field from the layer
func (layer *Layer) DeleteField(index int) error {
return C.OGR_L_DeleteField(layer.cval, C.int(index)).Err()
}
// Reorder all the fields of a layer
func (layer *Layer) ReorderFields(layerMap []int) error {
return C.OGR_L_ReorderFields(layer.cval, (*C.int)(unsafe.Pointer(&layerMap[0]))).Err()
}
// Reorder an existing field of a layer
func (layer *Layer) ReorderField(oldIndex, newIndex int) error {
return C.OGR_L_ReorderField(layer.cval, C.int(oldIndex), C.int(newIndex)).Err()
}
// Alter the definition of an existing field of a layer
func (layer *Layer) AlterFieldDefn(index int, newDefn FieldDefinition, flags int) error {
return C.OGR_L_AlterFieldDefn(layer.cval, C.int(index), newDefn.cval, C.int(flags)).Err()
}
// Begin a transation on data sources which support it
func (layer *Layer) StartTransaction() error {
return C.OGR_L_StartTransaction(layer.cval).Err()
}
// Commit a transaction on data sources which support it
func (layer *Layer) CommitTransaction() error {
return C.OGR_L_CommitTransaction(layer.cval).Err()
}
// Roll back the current transaction on data sources which support it
func (layer *Layer) RollbackTransaction() error {
return C.OGR_L_RollbackTransaction(layer.cval).Err()
}
// Flush pending changes to the layer
func (layer *Layer) Sync() error {
return C.OGR_L_SyncToDisk(layer.cval).Err()
}
// Fetch the name of the FID column
func (layer *Layer) FIDColumn() string {
name := C.OGR_L_GetFIDColumn(layer.cval)
return C.GoString(name)
}
// Fetch the name of the geometry column
func (layer *Layer) GeometryColumn() string {
name := C.OGR_L_GetGeometryColumn(layer.cval)
return C.GoString(name)
}
// Set which fields can be ignored when retrieving features from the layer
func (layer *Layer) SetIgnoredFields(names []string) error {
length := len(names)
cNames := make([]*C.char, length+1)
for i := 0; i < length; i++ {
cNames[i] = C.CString(names[i])
defer C.free(unsafe.Pointer(cNames[i]))
}
cNames[length] = (*C.char)(unsafe.Pointer(nil))
return C.OGR_L_SetIgnoredFields(layer.cval, (**C.char)(unsafe.Pointer(&cNames[0]))).Err()
}
// Return the intersection of two layers
// Unimplemented: Intersection
// Will be new in 2.0
// Return the union of two layers
// Unimplemented: Union
// Will be new in 2.0
// Return the symmetric difference of two layers
// Unimplemented: SymDifference
// Will be new in 2.0
// Identify features in this layer with ones from the provided layer
// Unimplemented: Identity
// Will be new in 2.0
// Update this layer with features from the provided layer
// Unimplemented: Update
// Will be new in 2.0
// Clip off areas that are not covered by the provided layer
// Unimplemented: Clip
// Will be new in 2.0
// Remove areas that are covered by the provided layer
// Unimplemented: Erase
// Will be new in 2.0