2

我最近在 Microsoft SQL 中发现了空间列。我现在已经成功地使用正确的 EPSG (STRID) 存储了 LINESTRING sqlgeometry。使用 AutoCAD Map 检查。

数据现在是包含 XY 坐标组的 sqlgeometry LINESTRING。

使用 C#(或 sql 语句),如何获取坐标点的纬度和经度值?

4

1 回答 1

0

您需要的是重新投影到球坐标上。SQL Server 不提供此类功能。您需要在 C# 中使用一些管道来获取几何图形和项目坐标。

如果你有你的转换功能,使用 GeometrySink 会很快并且会有所帮助。

internal class SqlGeometryProjectionSink : IGeometrySink110
{

    IGeometrySink110 _sink;
    int _outSrid;
    Func<double, double, double[]> _coordTransform;

    public SqlGeometryProjectionSink(IGeometrySink110 p_Sink, int outSrid, Func<double, double, double[]> coordTransform)
    {
        _sink = p_Sink;
        _outSrid = outSrid;
        _coordTransform = coordTransform;
        if (_coordTransform == null)
        {
            _coordTransform = new Func<double, double, double[]>((x, y) => new double[] { x, y });
        }
    }

    void IGeometrySink.AddLine(double x, double y, double? z, double? m)
    {
        double[] proj = _coordTransform(x, y);
        _sink.AddLine(proj[0], proj[1], z, m);
    }

    void IGeometrySink.BeginFigure(double x, double y, double? z, double? m)
    {
        double[] proj = _coordTransform(x, y);
        _sink.BeginFigure(proj[0], proj[1], z, m);
    }

    void IGeometrySink.BeginGeometry(OpenGisGeometryType type)
    {
        _sink.BeginGeometry(type);
    }

    void IGeometrySink.EndFigure()
    {
        _sink.EndFigure();
    }

    void IGeometrySink.EndGeometry()
    {
        _sink.EndGeometry();
    }

    void IGeometrySink.SetSrid(int srid)
    {
        _sink.SetSrid(_outSrid);
    }


    public static SqlGeometry ReprojectGeometry(SqlGeometry geom, int srid, Func<double, double, double[]> coordTransform)
    {
        if (geom != null)
        {
            SqlGeometryBuilder builder = new SqlGeometryBuilder();
            SqlGeometryProjectionSink sink = new SqlGeometryProjectionSink(builder, srid, coordTransform);
            geom.Populate(sink);

            return builder.ConstructedGeometry;
        }
        return null;
    }


    void IGeometrySink110.AddCircularArc(double x1, double y1, double? z1, double? m1, double x2, double y2, double? z2, double? m2)
    {
        throw new NotImplementedException();
    }
}

如果您不知道公式,您可以使用例如提供转换功能的 DotSpatial。(见这里的实现示例:https ://github.com/xfischer/SqlServerSpatial.Toolkit/blob/4a60154f206af430b27de730afd0340db19f9191/SqlServerSpatial.Toolkit/Viewers/GDI/SqlGeometryReprojection.cs )

于 2018-03-30T15:36:12.140 回答