Search code examples
c#geospatialsqlgeometry

Convert between SqlGeometry and DbGeometry


Is there a simple way to convert between SqlGeometry and DbGeometry? I'm using a popular sql spatial helper library and all of the functions in there expect SqlGeometry. But when i use Entity Framework against an ESRI ArcSDE feature class the Shape field is returned as a DbGeometry. I can't call any of the methods I would like to (such as LocateAlongGeom) with that DbGeometry type. Maybe there is a way to serialize it as binary or text then read it back in as a SqlGeometry?


Solution

  • //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);