使用C#从sqlgeometry linestring中获取纬度和经度值

时间:2015-11-12 13:36:55

标签: latitude-longitude sqlgeometry

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

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

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

1 个答案:

答案 0 :(得分: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