-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathshx.go
69 lines (60 loc) · 1.4 KB
/
shx.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
package shapefile
import (
"archive/zip"
"encoding/binary"
"fmt"
"io"
)
// An SHX is a .shx file.
type SHX struct {
SHxHeader
Records []SHXRecord
}
// An SHXRecord is a record in a SHX.
type SHXRecord struct {
Offset int
ContentLength int
}
// ReadSHX reads a SHX from an io.Reader.
func ReadSHX(r io.Reader, size int64) (*SHX, error) {
header, err := readSHxHeader(r, size)
if err != nil {
return nil, err
}
data := make([]byte, size-headerSize)
if err := readFull(r, data); err != nil {
return nil, err
}
n := int((size - headerSize) / 8)
records := make([]SHXRecord, 0, n)
for i := range n {
record := ParseSHXRecord(data[8*i : 8*i+8])
records = append(records, record)
}
return &SHX{
SHxHeader: *header,
Records: records,
}, nil
}
// ReadSHXZipFile reads a SHX from a *zip.File.
func ReadSHXZipFile(zipFile *zip.File) (*SHX, error) {
readCloser, err := zipFile.Open()
if err != nil {
return nil, err
}
defer readCloser.Close()
shx, err := ReadSHX(readCloser, int64(zipFile.UncompressedSize64))
if err != nil {
return nil, fmt.Errorf("%s: %w", zipFile.Name, err)
}
return shx, nil
}
// ParseSHXRecord parses a SHXRecord from data.
func ParseSHXRecord(data []byte) SHXRecord {
offset := 2 * int(binary.BigEndian.Uint32(data[:4]))
contentLength := 2 * int(binary.BigEndian.Uint32(data[4:]))
return SHXRecord{
Offset: offset,
ContentLength: contentLength,
}
}