62 lines
1.2 KiB
Go
62 lines
1.2 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/binary"
|
|
"time"
|
|
)
|
|
|
|
func mssqlUuidToBigEndian(mssqlUuid []byte) []byte {
|
|
if len(mssqlUuid) != 16 {
|
|
return mssqlUuid
|
|
}
|
|
pgUuid := make([]byte, 16)
|
|
pgUuid[0], pgUuid[1], pgUuid[2], pgUuid[3] = mssqlUuid[3], mssqlUuid[2], mssqlUuid[1], mssqlUuid[0]
|
|
pgUuid[4], pgUuid[5] = mssqlUuid[5], mssqlUuid[4]
|
|
pgUuid[6], pgUuid[7] = mssqlUuid[7], mssqlUuid[6]
|
|
copy(pgUuid[8:], mssqlUuid[8:])
|
|
|
|
return pgUuid
|
|
}
|
|
|
|
const sridFlag = 0x20000000
|
|
|
|
func wkbToEwkbWithSrid(geometry []byte, srid int) []byte {
|
|
if len(geometry) < 5 {
|
|
return geometry
|
|
}
|
|
|
|
var byteOrder binary.ByteOrder
|
|
if geometry[0] == 0 {
|
|
byteOrder = binary.BigEndian
|
|
} else {
|
|
byteOrder = binary.LittleEndian
|
|
}
|
|
|
|
wkbType := byteOrder.Uint32(geometry[1:5])
|
|
if wkbType&sridFlag != 0 {
|
|
return geometry
|
|
}
|
|
|
|
ewkbType := wkbType | sridFlag
|
|
|
|
result := make([]byte, len(geometry)+4)
|
|
|
|
result[0] = geometry[0]
|
|
|
|
byteOrder.PutUint32(result[1:5], ewkbType)
|
|
|
|
byteOrder.PutUint32(result[5:9], uint32(srid))
|
|
|
|
copy(result[9:], geometry[5:])
|
|
|
|
return result
|
|
}
|
|
|
|
func ensureUTC(t time.Time) time.Time {
|
|
if t.Location() == time.UTC {
|
|
return t
|
|
}
|
|
|
|
return time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), t.Nanosecond(), time.UTC)
|
|
}
|