-
Notifications
You must be signed in to change notification settings - Fork 1
/
wkb.go
312 lines (254 loc) · 10.8 KB
/
wkb.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
package wkb
import (
"io"
"errors"
"encoding/binary"
)
type Header struct {
Version uint16
Bands uint16
ScaleX float64
ScaleY float64
IpX float64
IpY float64
SkewX float64
SkewY float64
Srid int32
Width uint16
Height uint16
}
type RasterBand struct {
NoData int
IsOffline bool
HasNoDataValue bool
IsNoDataValue bool
Data [][]int
}
type Raster struct {
Version uint16
ScaleX float64
ScaleY float64
IpX float64
IpY float64
SkewX float64
SkewY float64
Srid int32
Width uint16
Height uint16
Bands []RasterBand
}
func readIntOfType(reader io.Reader, endiannes binary.ByteOrder, valueType int) (int, error) {
switch valueType {
case 0, 1, 2, 4:
var value uint8
err := binary.Read(reader, endiannes, &value)
if err != nil {
return 0, err
}
return int(value), nil
case 3:
var value int8
err := binary.Read(reader, endiannes, &value)
if err != nil {
return 0, err
}
return int(value), nil
case 5:
var value int16
err := binary.Read(reader, endiannes, &value)
if err != nil {
return 0, err
}
return int(value), nil
case 6:
var value uint16
err := binary.Read(reader, endiannes, &value)
if err != nil {
return 0, err
}
return int(value), nil
case 7:
var value int32
err := binary.Read(reader, endiannes, &value)
if err != nil {
return 0, err
}
return int(value), nil
case 8:
var value uint32
err := binary.Read(reader, endiannes, &value)
if err != nil {
return 0, err
}
return int(value), nil
case 9:
var value float32
err := binary.Read(reader, endiannes, &value)
if err != nil {
return 0, err
}
return int(value), nil
case 10:
var value float64
err := binary.Read(reader, endiannes, &value)
if err != nil {
return 0, err
}
return int(value), nil
}
return 0, errors.New("Unknown value type")
}
func ReadWKBRaster(wkb io.Reader) (Raster, error) {
raster := Raster{}
// Determine the endiannes of the raster
//
// +---------------+-------------+------------------------------+
// | endiannes | byte | 1:ndr/little endian |
// | | | 0:xdr/big endian |
// +---------------+-------------+------------------------------+
var endiannesValue uint8
err := binary.Read(wkb, binary.LittleEndian, &endiannesValue)
if err != nil {
return raster, err
}
var endiannes binary.ByteOrder
if endiannesValue == 0 {
endiannes = binary.BigEndian
} else if endiannesValue == 1 {
endiannes = binary.LittleEndian
}
// Read the raster header data.
//
// +---------------+-------------+------------------------------+
// | version | uint16 | format version (0 for this |
// | | | structure) |
// +---------------+-------------+------------------------------+
// | nBands | uint16 | Number of bands |
// +---------------+-------------+------------------------------+
// | scaleX | float64 | pixel width |
// | | | in geographical units |
// +---------------+-------------+------------------------------+
// | scaleY | float64 | pixel height |
// | | | in geographical units |
// +---------------+-------------+------------------------------+
// | ipX | float64 | X ordinate of upper-left |
// | | | pixel's upper-left corner |
// | | | in geographical units |
// +---------------+-------------+------------------------------+
// | ipY | float64 | Y ordinate of upper-left |
// | | | pixel's upper-left corner |
// | | | in geographical units |
// +---------------+-------------+------------------------------+
// | skewX | float64 | rotation about Y-axis |
// +---------------+-------------+------------------------------+
// | skewY | float64 | rotation about X-axis |
// +---------------+-------------+------------------------------+
// | srid | int32 | Spatial reference id |
// +---------------+-------------+------------------------------+
// | width | uint16 | number of pixel columns |
// +---------------+-------------+------------------------------+
// | height | uint16 | number of pixel rows |
// +---------------+-------------+------------------------------+
var header Header
err = binary.Read(wkb, endiannes, &header)
if err != nil {
return raster, err
}
raster.Version = header.Version
raster.ScaleX = header.ScaleX
raster.ScaleY = header.ScaleY
raster.IpX = header.IpX
raster.IpY = header.IpY
raster.SkewX = header.SkewX
raster.SkewY = header.SkewY
raster.Srid = header.Srid
raster.Width = header.Width
raster.Height = header.Height
for i := 0; i < int(header.Bands); i++ {
band := RasterBand{}
// Read band header data
//
// +---------------+--------------+-----------------------------------+
// | isOffline | 1bit | If true, data is to be found |
// | | | on the filesystem, trought the |
// | | | path specified in RASTERDATA |
// +---------------+--------------+-----------------------------------+
// | hasNodataValue| 1bit | If true, stored nodata value is |
// | | | a true nodata value. Otherwise |
// | | | the value stored as a nodata |
// | | | value should be ignored. |
// +---------------+--------------+-----------------------------------+
// | isNodataValue | 1bit | If true, all the values of the |
// | | | band are expected to be nodata |
// | | | values. This is a dirty flag. |
// | | | To set the flag to its real value |
// | | | the function st_bandisnodata must |
// | | | must be called for the band with |
// | | | 'TRUE' as last argument. |
// +---------------+--------------+-----------------------------------+
// | reserved | 1bit | unused in this version |
// +---------------+--------------+-----------------------------------+
// | pixtype | 4bits | 0: 1-bit boolean |
// | | | 1: 2-bit unsigned integer |
// | | | 2: 4-bit unsigned integer |
// | | | 3: 8-bit signed integer |
// | | | 4: 8-bit unsigned integer |
// | | | 5: 16-bit signed integer |
// | | | 6: 16-bit unsigned signed integer |
// | | | 7: 32-bit signed integer |
// | | | 8: 32-bit unsigned signed integer |
// | | | 9: 32-bit float |
// | | | 10: 64-bit float |
// +---------------+--------------+-----------------------------------+
//
// Requires reading a single byte, and splitting the bits into the
// header attributes
var bandheader uint8
err := binary.Read(wkb, endiannes, &bandheader)
if err != nil {
return raster, err
}
band.IsOffline = (int(bandheader) & 128) != 0
band.HasNoDataValue = (int(bandheader) & 64) != 0
band.IsNoDataValue = (int(bandheader) & 32) != 0
// Read the pixel type
pixType := (int(bandheader) & 15) - 1
// +---------------+--------------+-----------------------------------+
// | nodata | 1 to 8 bytes | Nodata value |
// | | depending on | |
// | | pixtype [1] | |
// +---------------+--------------+-----------------------------------+
// Read the nodata value
noData, err := readIntOfType(wkb, endiannes, pixType)
if err != nil {
return raster, err
}
band.NoData = noData
// Read the pixel values: width * height * size
//
// +---------------+--------------+-----------------------------------+
// | pix[w*h] | 1 to 8 bytes | Pixels values, row after row, |
// | | depending on | so pix[0] is upper-left, pix[w-1] |
// | | pixtype [1] | is upper-right. |
// | | | |
// | | | As for endiannes, it is specified |
// | | | at the start of WKB, and implicit |
// | | | up to 8bits (bit-order is most |
// | | | significant first) |
// | | | |
// +---------------+--------------+-----------------------------------+
for i := 0; i < int(header.Height); i++ {
row := []int{}
for i := 0; i < int(header.Width); i++ {
value, err := readIntOfType(wkb, endiannes, pixType)
if err != nil {
return raster, err
}
row = append(row, value)
}
band.Data = append(band.Data, row)
}
raster.Bands = append(raster.Bands, band)
}
return raster, nil
}