1 回答
The Microsoft.SqlServer.Types
API does offer functionality opposite to SqlGeometryBuilder
, in the form of the SqlGeometry.Populate(IGeometrySink110)
method.
This method accepts an object implementing the IGeometrySink110
interface, whose definition very closely mirrors that of SqlGeometryBuilder
. The method will "replay" the method calls that were used on a SqlGeometryBuilder
in order to construct the SqlGeometry
.
For example, given the SqlGeometry geometry
as shown in the question above, as well as the following IGeometrySink110
implementation:
class ConsoleGeometrySink : IGeometrySink110
{
public void SetSrid(int srid)
{
Console.WriteLine($"SetSrid(srid: {srid})");
}
public void BeginGeometry(OpenGisGeometryType type)
{
Console.WriteLine($"BeginGeometry(type: {type})");
}
public void BeginFigure(double x, double y, double? z, double? m)
{
Console.WriteLine($"BeginFigure(x: {x}, y: {y}, z: {z}, m: {m})");
}
public void AddCircularArc(double x1, double y1, double? z1, double? m1,
double x2, double y2, double? z2, double? m2)
{
Console.WriteLine($"AddCircularArc(x1: {x1}, y1: {y1}, z1: {z1}, m1: {m1}, " +
$"x2: {x2}, y2: {y2}, z2: {z2}, m2: {m2})");
}
public void AddLine(double x, double y, double? z, double? m)
{
Console.WriteLine($"AddLine(x: {x}, y: {y}, z: {z}, m: {m})");
}
public void EndFigure()
{
Console.WriteLine($"EndFigure()");
}
public void EndGeometry()
{
WriteLine($"EndGeometry()");
}
}
Calling geometry.Populate(new ConsoleGeometrySink())
would output the following:
SetSrid(srid: …)
BeginGeometry(type: Polygon)
BeginFigure(x: 0, y: 0, z: , m: )
AddLine(…)
…
EndFigure()
EndGeometry()
which mirrors exactly the original steps performed to build the SqlGeometry
.