Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions MEOS.NET.Tests/ExtendedWkbTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using MEOS.NET.Types;

namespace MEOS.NET.Tests
{
/// <summary>
/// The extended WKB of a geometry is a buffer whose length MEOS states
/// through a pointer, so the array a caller gets is exactly as long as the
/// data — which the hex form of the same value states independently.
/// </summary>
[TestClass]
public class ExtendedWkbTests : MeosTest
{
private const string Line = "SRID=4326;LINESTRING(1 1, 2 2, 3 4)";

[TestMethod]
public void TheBufferIsAsLongAsTheHexFormSays()
{
Geo line = Geometry.In(Line, -1)!;

byte[]? ewkb = line.AsEWKB("ndr");
string hex = line.AsHexewkb("ndr");

Assert.IsNotNull(ewkb);
Assert.AreEqual(hex.Length, ewkb!.Length * 2);
Assert.AreEqual(hex.ToUpperInvariant(), Convert.ToHexString(ewkb));
}

[TestMethod]
public void AGeometryRoundTripsThroughItsOwnEWKB()
{
Geo line = Geometry.In(Line, -1)!;

Geo? read = Geo.FromEWKB(line.AsEWKB("ndr")!, 4326);

Assert.IsNotNull(read);
Assert.AreEqual(line.ToString(), read!.ToString());
}
}
}
21 changes: 21 additions & 0 deletions MEOS.NET/Types/Geo.g.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,27 @@ internal Geo(IntPtr ptr) : base(ptr) { }
public override string ToString()
=> this.Out();

public byte[]? AsEWKB(string endian)
{
IntPtr _size = Marshal.AllocHGlobal(sizeof(long));
try
{
IntPtr _bytes = Meos.GeoAsEwkb(this.Ptr, endian, _size);
if (_bytes == IntPtr.Zero)
{
return null;
}

byte[] _wkb = new byte[Marshal.ReadInt64(_size)];
Marshal.Copy(_bytes, _wkb, 0, _wkb.Length);
return _wkb;
}
finally
{
Marshal.FreeHGlobal(_size);
}
}

public string AsEWKT(int precision)
=> Meos.GeoAsEwkt(this.Ptr, precision);

Expand Down
Loading