feat: add WKB to EWKB conversion with SRID handling and integrate into MSSQL transformation
This commit is contained in:
@@ -1,5 +1,9 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
)
|
||||||
|
|
||||||
func mssqlUuidToBigEndian(mssqlUuid []byte) []byte {
|
func mssqlUuidToBigEndian(mssqlUuid []byte) []byte {
|
||||||
if len(mssqlUuid) != 16 {
|
if len(mssqlUuid) != 16 {
|
||||||
return mssqlUuid
|
return mssqlUuid
|
||||||
@@ -12,3 +16,37 @@ func mssqlUuidToBigEndian(mssqlUuid []byte) []byte {
|
|||||||
|
|
||||||
return pgUuid
|
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
|
||||||
|
}
|
||||||
|
|||||||
@@ -79,6 +79,10 @@ func transformRowsMssql(columns []ColumnType, in <-chan []UnknownRowValues, out
|
|||||||
if b, ok := value.([]byte); ok {
|
if b, ok := value.([]byte); ok {
|
||||||
rowValues[i] = mssqlUuidToBigEndian(b)
|
rowValues[i] = mssqlUuidToBigEndian(b)
|
||||||
}
|
}
|
||||||
|
} else if col.SystemType() == "geometry" || col.SystemType() == "geography" {
|
||||||
|
if b, ok := value.([]byte); ok {
|
||||||
|
rowValues[i] = wkbToEwkbWithSrid(b, 4326)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -90,7 +94,7 @@ func transformRowsMssql(columns []ColumnType, in <-chan []UnknownRowValues, out
|
|||||||
func logSampleRow(job MigrationJob, columns []ColumnType, rowValues UnknownRowValues, tag string) {
|
func logSampleRow(job MigrationJob, columns []ColumnType, rowValues UnknownRowValues, tag string) {
|
||||||
log.Infof("[%s.%s] Sample row: (%s)", job.Schema, job.Table, tag)
|
log.Infof("[%s.%s] Sample row: (%s)", job.Schema, job.Table, tag)
|
||||||
for i, col := range columns {
|
for i, col := range columns {
|
||||||
log.Infof("%s: %v", col.Name(), rowValues[i])
|
log.Infof("%s (%T): %v", col.Name(), rowValues[i], rowValues[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
59
scripts/wkb-to-ewkb/main.go
Normal file
59
scripts/wkb-to-ewkb/main.go
Normal file
@@ -0,0 +1,59 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/binary"
|
||||||
|
"fmt"
|
||||||
|
)
|
||||||
|
|
||||||
|
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 main() {
|
||||||
|
shape := []byte{
|
||||||
|
1, 3, 0, 0, 0, 1, 0, 0, 0, 5, 0, 0, 0, 217, 61, 121, 88, 168, 57, 83, 192,
|
||||||
|
60, 78, 209, 145, 92, 222, 39, 192, 232, 106, 43, 246, 151, 57, 83, 192,
|
||||||
|
60, 78, 209, 145, 92, 222, 39, 192, 232, 106, 43, 246, 151, 57, 83, 192,
|
||||||
|
174, 182, 98, 127, 217, 221, 39, 192, 217, 61, 121, 88, 168, 57, 83, 192,
|
||||||
|
174, 182, 98, 127, 217, 221, 39, 192, 217, 61, 121, 88, 168, 57, 83, 192,
|
||||||
|
60, 78, 209, 145, 92, 222, 39, 192,
|
||||||
|
}
|
||||||
|
|
||||||
|
srid := 4326
|
||||||
|
result := wkbToEwkbWithSrid(shape, srid)
|
||||||
|
|
||||||
|
fmt.Printf("WKB Original (len): %d\n", len(shape))
|
||||||
|
fmt.Printf("EWKB Result (len): %d\n", len(result))
|
||||||
|
fmt.Printf("Primeros bytes (original): %v\n", shape[:10])
|
||||||
|
fmt.Printf("Primeros bytes (resultado): %v\n", result[:10])
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user