forked from lukeroth/gdal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayer_test.go
131 lines (121 loc) · 3.06 KB
/
layer_test.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
package gdal
import "testing"
func getLayer(t *testing.T) *Layer {
fname := "test/poly.shp"
ds, err := OpenEx(fname, VectorDrivers|ReadOnly, nil, nil, nil)
if err != nil {
t.Fatal(err)
}
lyr, err := ds.Layer(0)
if err != nil {
t.Fatal(err)
}
return &lyr
}
func TestLayerName(t *testing.T) {
lyr := getLayer(t)
if lyr.Name() != "poly" {
t.Errorf("invalid layer name: %s", lyr.Name())
}
}
func TestLayerGeomType(t *testing.T) {
lyr := getLayer(t)
if lyr.GeomType() != GT_Polygon {
t.Errorf("invalid geometry type: %+v", lyr.GeomType())
}
}
func TestFeatureCount(t *testing.T) {
lyr := getLayer(t)
if n, ok := lyr.FeatureCount(true); !ok || n != 10 {
t.Errorf("invalid feature count: %d", n)
}
}
func TestNextFeature(t *testing.T) {
lyr := getLayer(t)
n, _ := lyr.FeatureCount(true)
i := 0
for feat := lyr.NextFeature(); feat != nil; feat = lyr.NextFeature() {
i++
}
if i != n {
t.Errorf("did not get %d features, only %d", n, i)
}
}
func TestSpatialFilters(t *testing.T) {
// Extent: (478315.531250, 4762880.500000) - (481645.312500, 4765610.500000)
lyr := getLayer(t)
if lyr.SpatialFilter() != nil {
t.Errorf("spatial filter not nil")
}
// Create a spatial filter that has no features
geom, err := CreateFromWKT("POLYGON((0 0, 1 0, 1 1, 0 1, 0 0))", lyr.SpatialRef())
if err != nil {
t.Fatal("could not create filter geometry")
}
lyr.SetSpatialFilter(&geom)
lyr.ResetReading()
if lyr.SpatialFilter() == nil {
t.Error("spatial filter not set")
}
if n, ok := lyr.FeatureCount(true); ok != true || n != 0 {
t.Error("spatial filter not set")
}
lyr.SetSpatialFilter(nil)
lyr.ResetReading()
if lyr.SpatialFilter() != nil {
t.Error("spatial filter set")
}
if n, ok := lyr.FeatureCount(true); ok != true || n != 10 {
t.Error("spatial filter set")
}
lyr.SetSpatialFilterRect(478903, 4764757, 478904, 4764756)
lyr.ResetReading()
if lyr.SpatialFilter() == nil {
t.Error("spatial filter not set")
}
n, ok := lyr.FeatureCount(true)
if ok != true {
t.Error("failed to get feature count")
}
if n != 1 {
t.Error("spatial filter does not include expected feature")
}
feat := lyr.NextFeature()
if feat == nil || feat.FieldAsInteger(feat.FieldIndex("EAS_ID")) != 173 {
t.Error("failed to get feature with proper fid")
}
}
func TestAttributeFilter(t *testing.T) {
lyr := getLayer(t)
lyr.SetAttributeFilter("EAS_ID=173")
lyr.ResetReading()
if n, ok := lyr.FeatureCount(true); !ok || n != 1 {
t.Error("failed to set attribute filter")
}
}
func TestResetReading(t *testing.T) {
lyr := getLayer(t)
lyr.NextFeature()
lyr.ResetReading()
n, _ := lyr.FeatureCount(true)
i := 0
for feat := lyr.NextFeature(); feat != nil; feat = lyr.NextFeature() {
i++
}
if i != n {
t.Error("failed to reset reading")
}
}
func TestIgnoreFields(t *testing.T) {
t.Skip("find a format that supports ignoring fields")
lyr := getLayer(t)
err := lyr.SetIgnoredFields([]string{"EAS_ID"})
if err != nil {
t.Fatal(err)
}
feat := lyr.NextFeature()
if feat.FieldIndex("EAS_ID") != -1 {
t.Error("ignored fields not ignored")
t.Log(feat.FieldIndex("EAS_ID"))
}
}