【问题标题】:Convert between SqlGeometry and DbGeometry在 SqlGeometry 和 DbGeometry 之间转换
【发布时间】:2013-03-18 20:09:05
【问题描述】:

有没有一种简单的方法可以在 SqlGeometry 和 DbGeometry 之间进行转换? 我正在使用一个流行的 sql 空间助手库,其中的所有函数都需要 SqlGeometry。但是,当我针对 ESRI ArcSDE 要素类使用实体框架时,Shape 字段将作为 DbGeometry 返回。 我无法使用该 DbGeometry 类型调用任何我想调用的方法(例如 LocateAlongGeom)。 也许有一种方法可以将其序列化为二进制或文本,然后将其作为 SqlGeometry 读回?

【问题讨论】:

    标签: c# geospatial sqlgeometry


    【解决方案1】:
    //Convert from SqlGeometry to DbGeometry 
    SqlGeometry sqlGeo = ...
    DbGeometry dbGeo = DbGeometry.FromBinary(sqlGeo.STAsBinary().Buffer);
    
    //Convert from DBGeometry to SqlGeometry
     SqlGeometry sqlGeo2 = SqlGeometry.STGeomFromWKB(new SqlBytes(dbGeo.AsBinary()), 0);
    

    【讨论】:

    • 不应该将DbGeometry 转换为SqlGeometry...SqlGeometry sqlGeo2 = SqlGeometry.STGeomFromWKB(new SqlBytes(dbGeo.AsBinary()), dbGeo.CoordinateSystemId); 考虑不在SRID-0 中的几何形状吗?
    • 是的,我假设您在大多数情况下都希望传递 SRID。对于大多数 GPS 坐标,您将使用 4326 表示 WGS84。 spatialreference.org/ref/epsg/4326
    • 我相信您也可以使用dbGeo.CoordinateSystemId代替上面的零来动态检测它。
    【解决方案2】:

    管理多种空间类型的一种简单方法是通过扩展方法,如下所示:(使用来自@BizarroDavid 的代码示例的略微修改版本)

    public static class GeometryExtensions
    {
        public static DbGeometry ToDbGeometry(this SqlGeometry sqlGeometry)
        {
            return DbGeometry.FromBinary(sqlGeometry.STAsBinary().Buffer);
        }
    
        public static SqlGeometry ToSqlGeometry(this DbGeometry dbGeometry)
        {
            return SqlGeometry.STGeomFromWKB(new SqlBytes(dbGeometry.AsBinary()), dbGeometry.CoordinateSystemId);
        }
    }
    

    一旦你实现了它们,你就可以像这样使用它们......

    DbGeometry anyDbGeometry;
    SqlGeometry anySqlGeometry;
    
    //Convert to DbGeometry
    anyDbGeometry = anySqlGeometry.ToDbGeometry();
    
    //Convert to SqlGeometry
    anySqlGeometry = anyDbGeometry.ToSqlGeometry();
    

    【讨论】:

      【解决方案3】:

      Entity Framework 不支持 CURVES (CIRCLE) -> 改为线:

      return DbGeometry.FromBinary(sqlGeometry.**STCurveToLine()**.STAsBinary().Buffer);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-12
        • 2015-06-28
        • 2017-06-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多