-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #1059: Support for reading/writing spatial WKB
- Loading branch information
Showing
15 changed files
with
3,153 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 19 additions & 1 deletion
20
src/Microsoft.Spatial/PublicAPI/net8.0/PublicAPI.Unshipped.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,19 @@ | ||
| ||
abstract Microsoft.Spatial.SpatialImplementation.CreateWellKnownBinaryFormatter(Microsoft.Spatial.WellKnownBinaryWriterSettings settings) -> Microsoft.Spatial.WellKnownBinaryFormatter | ||
Microsoft.Spatial.ByteOrder | ||
Microsoft.Spatial.ByteOrder.BigEndian = 0 -> Microsoft.Spatial.ByteOrder | ||
Microsoft.Spatial.ByteOrder.LittleEndian = 1 -> Microsoft.Spatial.ByteOrder | ||
Microsoft.Spatial.WellKnownBinaryFormatter | ||
Microsoft.Spatial.WellKnownBinaryFormatter.WellKnownBinaryFormatter(Microsoft.Spatial.SpatialImplementation creator) -> void | ||
Microsoft.Spatial.WellKnownBinaryWriterSettings | ||
Microsoft.Spatial.WellKnownBinaryWriterSettings.HandleM.get -> bool | ||
Microsoft.Spatial.WellKnownBinaryWriterSettings.HandleM.set -> void | ||
Microsoft.Spatial.WellKnownBinaryWriterSettings.HandleSRID.get -> bool | ||
Microsoft.Spatial.WellKnownBinaryWriterSettings.HandleSRID.set -> void | ||
Microsoft.Spatial.WellKnownBinaryWriterSettings.HandleZ.get -> bool | ||
Microsoft.Spatial.WellKnownBinaryWriterSettings.HandleZ.set -> void | ||
Microsoft.Spatial.WellKnownBinaryWriterSettings.IsoWKB.get -> bool | ||
Microsoft.Spatial.WellKnownBinaryWriterSettings.IsoWKB.set -> void | ||
Microsoft.Spatial.WellKnownBinaryWriterSettings.Order.get -> Microsoft.Spatial.ByteOrder | ||
Microsoft.Spatial.WellKnownBinaryWriterSettings.Order.set -> void | ||
Microsoft.Spatial.WellKnownBinaryWriterSettings.WellKnownBinaryWriterSettings() -> void | ||
static Microsoft.Spatial.WellKnownBinaryFormatter.Create(Microsoft.Spatial.WellKnownBinaryWriterSettings settings) -> Microsoft.Spatial.WellKnownBinaryFormatter |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
src/Microsoft.Spatial/WellKnown/BinaryFormatterExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
//--------------------------------------------------------------------- | ||
// <copyright file="BinaryFormatterExtensions.cs" company="Microsoft"> | ||
// Copyright (C) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. | ||
// </copyright> | ||
//--------------------------------------------------------------------- | ||
|
||
namespace Microsoft.Spatial | ||
{ | ||
using System; | ||
using System.Buffers.Binary; | ||
using System.IO; | ||
|
||
internal static class BinaryFormatterExtensions | ||
{ | ||
/// <summary> | ||
/// Writes the double value based on the byte order setting. | ||
/// </summary> | ||
/// <param name="writer">The binary writer.</param> | ||
/// <param name="value">The double value.</param> | ||
/// <param name="order">The byte order.</param> | ||
public static void Write(this BinaryWriter writer, double value, ByteOrder order) | ||
{ | ||
Span<byte> buffer = stackalloc byte[8]; | ||
if (order == ByteOrder.LittleEndian) | ||
{ | ||
BinaryPrimitives.WriteDoubleLittleEndian(buffer, value); | ||
} | ||
else | ||
{ | ||
BinaryPrimitives.WriteDoubleBigEndian(buffer, value); | ||
} | ||
|
||
writer.Write(buffer); | ||
} | ||
|
||
/// <summary> | ||
/// Writes the uint value based on the byte order setting. | ||
/// </summary> | ||
/// <param name="writer">The binary writer.</param> | ||
/// <param name="value">The uint value.</param> | ||
/// <param name="order">The byte order.</param> | ||
public static void Write(this BinaryWriter writer, uint value, ByteOrder order) | ||
{ | ||
Span<byte> buffer = stackalloc byte[4]; | ||
if (order == ByteOrder.LittleEndian) | ||
{ | ||
BinaryPrimitives.WriteUInt32LittleEndian(buffer, value); | ||
} | ||
else | ||
{ | ||
BinaryPrimitives.WriteUInt32BigEndian(buffer, value); | ||
} | ||
|
||
writer.Write(buffer); | ||
} | ||
|
||
/// <summary> | ||
/// Writes the int value based on the byte order setting. | ||
/// </summary> | ||
/// <param name="writer">The binary writer.</param> | ||
/// <param name="value">The int value.</param> | ||
/// <param name="order">The byte order.</param> | ||
public static void Write(this BinaryWriter writer, int value, ByteOrder order) | ||
{ | ||
Span<byte> buffer = stackalloc byte[4]; | ||
if (order == ByteOrder.LittleEndian) | ||
{ | ||
BinaryPrimitives.WriteInt32LittleEndian(buffer, value); | ||
} | ||
else | ||
{ | ||
BinaryPrimitives.WriteInt32BigEndian(buffer, value); | ||
} | ||
|
||
writer.Write(buffer); | ||
} | ||
|
||
/// <summary> | ||
/// Reads the uint value from reader based on the byte order setting. | ||
/// </summary> | ||
/// <param name="reader">The binary reader.</param> | ||
/// <param name="order">The byte order.</param> | ||
/// <returns>The uint value read from the reader.</returns> | ||
public static uint ReadUInt32(this BinaryReader reader, ByteOrder order) | ||
{ | ||
Span<byte> buffer = stackalloc byte[4]; | ||
int num = reader.Read(buffer); | ||
if (num != 4) | ||
{ | ||
throw new FormatException(Error.Format(SRResources.WellKnownBinary_ByteLengthNotEnough, "UInt32", 4, num)); | ||
} | ||
|
||
if (order == ByteOrder.LittleEndian) | ||
{ | ||
return BinaryPrimitives.ReadUInt32LittleEndian(buffer); | ||
} | ||
|
||
return BinaryPrimitives.ReadUInt32BigEndian(buffer); | ||
} | ||
|
||
/// <summary> | ||
/// Reads the int value from reader based on the byte order setting. | ||
/// </summary> | ||
/// <param name="reader">The binary reader.</param> | ||
/// <param name="order">The byte order.</param> | ||
/// <returns>The int value read from the reader.</returns> | ||
public static int ReadInt32(this BinaryReader reader, ByteOrder order) | ||
{ | ||
Span<byte> buffer = stackalloc byte[4]; | ||
int num = reader.Read(buffer); | ||
if (num != 4) | ||
{ | ||
throw new FormatException(Error.Format(SRResources.WellKnownBinary_ByteLengthNotEnough, "Int32", 4, num)); | ||
} | ||
|
||
if (order == ByteOrder.LittleEndian) | ||
{ | ||
return BinaryPrimitives.ReadInt32LittleEndian(buffer); | ||
} | ||
|
||
return BinaryPrimitives.ReadInt32BigEndian(buffer); | ||
} | ||
|
||
/// <summary> | ||
/// Reads the double value from reader based on the byte order setting. | ||
/// </summary> | ||
/// <param name="reader">The binary reader.</param> | ||
/// <param name="order">The byte order.</param> | ||
/// <returns>The double value read from the reader.</returns> | ||
public static double ReadDouble(this BinaryReader reader, ByteOrder order) | ||
{ | ||
Span<byte> buffer = stackalloc byte[8]; | ||
int num = reader.Read(buffer); | ||
if (num != 8) | ||
{ | ||
throw new FormatException(Error.Format(SRResources.WellKnownBinary_ByteLengthNotEnough, "Double", 8, num)); | ||
} | ||
|
||
if (order == ByteOrder.LittleEndian) | ||
{ | ||
return BinaryPrimitives.ReadDoubleLittleEndian(buffer); | ||
} | ||
|
||
return BinaryPrimitives.ReadDoubleBigEndian(buffer); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
//--------------------------------------------------------------------- | ||
// <copyright file="ByteOrder.cs" company="Microsoft"> | ||
// Copyright (C) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. | ||
// </copyright> | ||
//--------------------------------------------------------------------- | ||
|
||
namespace Microsoft.Spatial | ||
{ | ||
/// <summary> | ||
/// Byte order | ||
/// </summary> | ||
public enum ByteOrder | ||
{ | ||
/// <summary> | ||
/// Big Endian | ||
/// </summary> | ||
BigEndian = 0x00, | ||
|
||
/// <summary> | ||
/// Little Endian | ||
/// </summary> | ||
LittleEndian = 0x01, | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
src/Microsoft.Spatial/WellKnown/WellKnownBinaryFormatter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
//--------------------------------------------------------------------- | ||
// <copyright file="WellKnownBinaryFormatter.cs" company="Microsoft"> | ||
// Copyright (C) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. | ||
// </copyright> | ||
//--------------------------------------------------------------------- | ||
|
||
namespace Microsoft.Spatial | ||
{ | ||
using System.IO; | ||
|
||
/// <summary> | ||
/// The object to move spatial types to and from the WellKnownBinary format | ||
/// </summary> | ||
public abstract class WellKnownBinaryFormatter : SpatialFormatter<Stream, Stream> | ||
{ | ||
/// <summary> | ||
/// Initializes a new instance of the <see cref="WellKnownBinaryFormatter"/> class. | ||
/// </summary> | ||
/// <param name="creator">The implementation that created this instance.</param> | ||
protected WellKnownBinaryFormatter(SpatialImplementation creator) | ||
: base(creator) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Creates the implementation of the formatter. | ||
/// </summary> | ||
/// <returns>Returns the created <see cref="WellKnownBinaryFormatter"> implementation.</returns> | ||
public static WellKnownBinaryFormatter Create(WellKnownBinaryWriterSettings settings) | ||
=> SpatialImplementation.CurrentImplementation.CreateWellKnownBinaryFormatter(settings); | ||
} | ||
} |
Oops, something went wrong.