|
| 1 | +package ewkb |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "encoding/binary" |
| 6 | + "encoding/hex" |
| 7 | + "errors" |
| 8 | + "io" |
| 9 | + |
| 10 | + "github.com/paulmach/orb" |
| 11 | + "github.com/paulmach/orb/encoding/internal/wkbcommon" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + // ErrUnsupportedDataType is returned by Scan methods when asked to scan |
| 16 | + // non []byte data from the database. This should never happen |
| 17 | + // if the driver is acting appropriately. |
| 18 | + ErrUnsupportedDataType = errors.New("wkb: scan value must be []byte") |
| 19 | + |
| 20 | + // ErrNotEWKB is returned when unmarshalling EWKB and the data is not valid. |
| 21 | + ErrNotEWKB = errors.New("wkb: invalid data") |
| 22 | + |
| 23 | + // ErrIncorrectGeometry is returned when unmarshalling EWKB data into the wrong type. |
| 24 | + // For example, unmarshaling linestring data into a point. |
| 25 | + ErrIncorrectGeometry = errors.New("wkb: incorrect geometry") |
| 26 | + |
| 27 | + // ErrUnsupportedGeometry is returned when geometry type is not supported by this lib. |
| 28 | + ErrUnsupportedGeometry = errors.New("wkb: unsupported geometry") |
| 29 | +) |
| 30 | + |
| 31 | +var commonErrorMap = map[error]error{ |
| 32 | + wkbcommon.ErrUnsupportedDataType: ErrUnsupportedDataType, |
| 33 | + wkbcommon.ErrNotWKB: ErrNotEWKB, |
| 34 | + wkbcommon.ErrNotWKBHeader: ErrNotEWKB, |
| 35 | + wkbcommon.ErrIncorrectGeometry: ErrIncorrectGeometry, |
| 36 | + wkbcommon.ErrUnsupportedGeometry: ErrUnsupportedGeometry, |
| 37 | +} |
| 38 | + |
| 39 | +func mapCommonError(err error) error { |
| 40 | + e, ok := commonErrorMap[err] |
| 41 | + if ok { |
| 42 | + return e |
| 43 | + } |
| 44 | + |
| 45 | + return err |
| 46 | +} |
| 47 | + |
| 48 | +// DefaultByteOrder is the order used for marshalling or encoding is none is specified. |
| 49 | +var DefaultByteOrder binary.ByteOrder = binary.LittleEndian |
| 50 | + |
| 51 | +// DefaultSRID is set to 4326, a common SRID, which represents spatial data using |
| 52 | +// longitude and latitude coordinates on the Earth's surface as defined in the WGS84 standard, |
| 53 | +// which is also used for the Global Positioning System (GPS). |
| 54 | +// This will be used by the encoder if non is specified. |
| 55 | +var DefaultSRID int = 4326 |
| 56 | + |
| 57 | +// An Encoder will encode a geometry as EWKB to the writer given at creation time. |
| 58 | +type Encoder struct { |
| 59 | + srid int |
| 60 | + e *wkbcommon.Encoder |
| 61 | +} |
| 62 | + |
| 63 | +// MustMarshal will encode the geometry and panic on error. |
| 64 | +// Currently there is no reason to error during geometry marshalling. |
| 65 | +func MustMarshal(geom orb.Geometry, srid int, byteOrder ...binary.ByteOrder) []byte { |
| 66 | + d, err := Marshal(geom, srid, byteOrder...) |
| 67 | + if err != nil { |
| 68 | + panic(err) |
| 69 | + } |
| 70 | + |
| 71 | + return d |
| 72 | +} |
| 73 | + |
| 74 | +// Marshal encodes the geometry with the given byte order. |
| 75 | +// An SRID of 0 will not be included in the encoding and the result will be a wkb encoding of the geometry. |
| 76 | +func Marshal(geom orb.Geometry, srid int, byteOrder ...binary.ByteOrder) ([]byte, error) { |
| 77 | + buf := bytes.NewBuffer(make([]byte, 0, wkbcommon.GeomLength(geom, srid != 0))) |
| 78 | + |
| 79 | + e := NewEncoder(buf) |
| 80 | + e.SetSRID(srid) |
| 81 | + |
| 82 | + if len(byteOrder) > 0 { |
| 83 | + e.SetByteOrder(byteOrder[0]) |
| 84 | + } |
| 85 | + |
| 86 | + err := e.Encode(geom) |
| 87 | + if err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + |
| 91 | + if buf.Len() == 0 { |
| 92 | + return nil, nil |
| 93 | + } |
| 94 | + |
| 95 | + return buf.Bytes(), nil |
| 96 | +} |
| 97 | + |
| 98 | +// MarshalToHex will encode the geometry into a hex string representation of the binary ewkb. |
| 99 | +func MarshalToHex(geom orb.Geometry, srid int, byteOrder ...binary.ByteOrder) (string, error) { |
| 100 | + data, err := Marshal(geom, srid, byteOrder...) |
| 101 | + if err != nil { |
| 102 | + return "", err |
| 103 | + } |
| 104 | + |
| 105 | + return hex.EncodeToString(data), nil |
| 106 | +} |
| 107 | + |
| 108 | +// MustMarshalToHex will encode the geometry and panic on error. |
| 109 | +// Currently there is no reason to error during geometry marshalling. |
| 110 | +func MustMarshalToHex(geom orb.Geometry, srid int, byteOrder ...binary.ByteOrder) string { |
| 111 | + d, err := MarshalToHex(geom, srid, byteOrder...) |
| 112 | + if err != nil { |
| 113 | + panic(err) |
| 114 | + } |
| 115 | + |
| 116 | + return d |
| 117 | +} |
| 118 | + |
| 119 | +// NewEncoder creates a new Encoder for the given writer. |
| 120 | +func NewEncoder(w io.Writer) *Encoder { |
| 121 | + e := wkbcommon.NewEncoder(w) |
| 122 | + e.SetByteOrder(DefaultByteOrder) |
| 123 | + return &Encoder{e: e, srid: DefaultSRID} |
| 124 | +} |
| 125 | + |
| 126 | +// SetByteOrder will override the default byte order set when |
| 127 | +// the encoder was created. |
| 128 | +func (e *Encoder) SetByteOrder(bo binary.ByteOrder) *Encoder { |
| 129 | + e.e.SetByteOrder(bo) |
| 130 | + return e |
| 131 | +} |
| 132 | + |
| 133 | +// SetSRID will override the default srid. |
| 134 | +func (e *Encoder) SetSRID(srid int) *Encoder { |
| 135 | + e.srid = srid |
| 136 | + return e |
| 137 | +} |
| 138 | + |
| 139 | +// Encode will write the geometry encoded as EWKB to the given writer. |
| 140 | +func (e *Encoder) Encode(geom orb.Geometry, srid ...int) error { |
| 141 | + s := e.srid |
| 142 | + if len(srid) > 0 { |
| 143 | + s = srid[0] |
| 144 | + } |
| 145 | + |
| 146 | + return e.e.Encode(geom, s) |
| 147 | +} |
| 148 | + |
| 149 | +// Decoder can decoder WKB geometry off of the stream. |
| 150 | +type Decoder struct { |
| 151 | + d *wkbcommon.Decoder |
| 152 | +} |
| 153 | + |
| 154 | +// Unmarshal will decode the type into a Geometry. |
| 155 | +func Unmarshal(data []byte) (orb.Geometry, int, error) { |
| 156 | + g, srid, err := wkbcommon.Unmarshal(data) |
| 157 | + if err != nil { |
| 158 | + return nil, 0, mapCommonError(err) |
| 159 | + } |
| 160 | + |
| 161 | + return g, srid, nil |
| 162 | +} |
| 163 | + |
| 164 | +// NewDecoder will create a new EWKB decoder. |
| 165 | +func NewDecoder(r io.Reader) *Decoder { |
| 166 | + return &Decoder{ |
| 167 | + d: wkbcommon.NewDecoder(r), |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +// Decode will decode the next geometry off of the stream. |
| 172 | +func (d *Decoder) Decode() (orb.Geometry, int, error) { |
| 173 | + g, srid, err := d.d.Decode() |
| 174 | + if err != nil { |
| 175 | + return nil, 0, mapCommonError(err) |
| 176 | + } |
| 177 | + |
| 178 | + return g, srid, nil |
| 179 | +} |
0 commit comments