Develop unit reading WKB binary from MSSQL

I want to convert a Well known binary string WKB into a geometrical object in Delphi. I read the content from an MSSQL DB with this command

geomString := FieldbyName('Geom').AsString;

As a next step I must convert this information into a Polygon. All my data are just simple polygons. I have written this small unit, I did not find any starting point somewhere else.

interface

uses
  System.SysUtils, System.Classes, System.Math, System.Types;

type
  WKBByteOrder = (wkbXDR, // Big Endian
    wkbNDR // Little Endian
    );

  /// <summary>
  /// Well-known text (WKT) is a text markup language for representing vector
  /// geometry objects. A binary equivalent, known as well-known binary (WKB)
  /// </summary>
  /// <remarks>
  /// <para>
  /// see also:
  /// </para>
  /// <para>
  /// https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry#Well-known_binary
  /// https://libgeos.org/specifications/wkb/
  /// </para>
  /// </remarks>
  WKBGeometryType = (wkbPoint = 1, wkbLineString = 2, wkbPolygon = 3,
    wkbTriangle = 17, wkbMultiPoint = 4, wkbMultiLineString = 5,
    wkbMultiPolygon = 6, wkbGeometryCollection = 7, wkbPolyhedralSurface = 15,
    wkbTIN = 16, wkbPointZ = 1001, wkbLineStringZ = 1002, wkbPolygonZ = 1003,
    wkbTriangleZ = 1017, wkbMultiPointZ = 1004, wkbMultiLineStringZ = 1005,
    wkbMultiPolygonZ = 1006, wkbGeometryCollectionZ = 1007,
    wkbPolyhedralSurfaceZ = 1015, wkbTINZ = 1016, wkbPointM = 2001,
    wkbLineStringM = 2002, wkbPolygonM = 2003, wkbTriangleM = 2017,
    wkbMultiPointM = 2004, wkbMultiLineStringM = 2005, wkbMultiPolygonM = 2006,
    wkbGeometryCollectionM = 2007, wkbPolyhedralSurfaceM = 2015, wkbTINM = 2016,
    wkbPointZM = 3001, wkbLineStringZM = 3002, wkbPolygonZM = 3003,
    wkbTriangleZM = 3017, wkbMultiPointZM = 3004, wkbMultiLineStringZM = 3005,
    wkbMultiPolygonZM = 3006, wkbGeometryCollectionZM = 3007,
    wkbPolyhedralSurfaceZM = 3015, wkbTinZM = 3016);

type
  TWKBReadError = class(Exception);

type
  TPolygon = TArray<TPointF>;

function WellknownBinary2Geom(const s: string): TPolygon;

implementation

procedure ReverseBytes(var Bytes: TBytes);
var
  i: Integer;
  Temp: Byte;
begin
  for i := 0 to (Length(Bytes) div 2) - 1 do
  begin
    Temp := Bytes[i];
    Bytes[i] := Bytes[Length(Bytes) - 1 - i];
    Bytes[Length(Bytes) - 1 - i] := Temp;
  end;
end;

function ReadDouble(var Buffer: TBytes; var Offset: Integer;
  IsBigEndian: Boolean): Double;
var
  Bytes: TBytes;
begin
  if Offset + SizeOf(Double) > Length(Buffer) then
    raise TWKBReadError.Create('Buffer overflow while reading double');
  SetLength(Bytes, SizeOf(Double));
  Move(Buffer[Offset], Bytes[0], SizeOf(Double));
  Inc(Offset, SizeOf(Double));

  if IsBigEndian then
    ReverseBytes(Bytes);

  Move(Bytes[0], Result, SizeOf(Double));
end;

function ReadUInt32(var Buffer: TBytes; var Offset: Integer;
  IsBigEndian: Boolean): UInt32;
var
  Bytes: TBytes;
begin
  if Offset + SizeOf(UInt32) > Length(Buffer) then
    raise TWKBReadError.Create('Buffer overflow while reading UInt32');
  SetLength(Bytes, SizeOf(UInt32));
  Move(Buffer[Offset], Bytes[0], SizeOf(UInt32));
  Inc(Offset, SizeOf(UInt32));

  if IsBigEndian then
    ReverseBytes(Bytes);

  Move(Bytes[0], Result, SizeOf(UInt32));
end;

function WellknownBinary2Geom(const s: string): TPolygon;
var
  Buffer: TBytes;
  Offset: Integer;
  ByteOrder: Byte;
  WKBType: UInt32;
  NumRings: UInt32;
  NumPoints: UInt32;
  Points: TPolygon;
  IsBigEndian: Boolean;
begin
  // Convert input string to byte array
  Buffer := TEncoding.UTF8.GetBytes(s);
  Offset := 0;

  // Read byte order
  ByteOrder := Buffer[Offset];
  Inc(Offset);

  IsBigEndian := ByteOrder = 0;

  // Read WKB type
  WKBType := ReadUInt32(Buffer, Offset, IsBigEndian);

  if WKBType <> Ord(wkbPolygon) then
    raise TWKBReadError.Create('Unsupported WKB type');

  // Read number of rings
  NumRings := ReadUInt32(Buffer, Offset, IsBigEndian);

  if NumRings <> 1 then
    raise TWKBReadError.Create('Unsupported number of rings');

  // Read number of points in the exterior ring
  NumPoints := ReadUInt32(Buffer, Offset, IsBigEndian);

  SetLength(Points, NumPoints);

  // Read points
  for var i := 0 to NumPoints - 1 do
  begin
    Points[i].X := ReadDouble(Buffer, Offset, IsBigEndian);
    Points[i].Y := ReadDouble(Buffer, Offset, IsBigEndian);
  end;

  Result := Points;
end;

end.

Debugging my data I already fail getting the expected WKBGeometryType and pretty useless values for NumRings, NumPoints which makes it pretty hard to continue.
The data on the SQL server are correct, I can verify with this function in Delphi :

function SpatialIntegrityAsText(FServername, FDatabasename, FTableName,
  FFieldName: string; ID: Integer): String;
var
  FQuery: TQuery;
  FConnection: TTConnection;
  SQLSTR: string;

begin
  FConnection := TConnection.create(nil);
  FQuery := TQuery.create(nil);
  try
    FConnection.LoginPrompt := False;
    FQuery.Connection := FConnection;

    DBConnect(FServername, FDatabasename, FConnection);

    SQLSTR := 'SELECT ' + FFieldName + '.STAsText() FROM ' + FTableName +
      ' where REcordIndex=' + IntToStr(ID);

    // SQLSTR := 'SELECT ' + FFieldName + '.ToString() FROM ' + FTableName +
    // ' where REcordIndex=' + IntToStr(ID);

    FQuery.SQL.add(SQLSTR);

    FQuery.Open;

    Result := FQuery.Fields[0].asString + '<END>';

  finally
    FQuery.Free;
    FConnection.Free;
  end;

end;

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật