Converting between Esri Geometry and WKT using ArcObjects?

Using the IWkb interface does a nice job at converting between an IGeometry and WKB. From a WKB you can use the Microsoft.SqlServer.Types library to convert a WKB to SqlGeometry then back to WKT.

IWkb wkb = geometry as (IWkb); //(Where geometry is an instance of IGeometry)
byte[] wkb_bytes = new byte[wkb.WkbSize];
int byte_count = wkb.WkbSize;
wkb.ExportToWkb(ref byte_count, out wkb_bytes[0]);

At this point you have the WKB stored in wkb_bytes. If you want to go the next step to SqlGeometry then to WKT:

SqlGeometry sqlGeom = SqlGeometry.STGeomFromWKB(new SqlBytes(wkb_bytes), srid);
string wkt = sqlGeom.ToString();

In the past, I've used Sharpmap's converter, but I had to get to WKB first. I don't know if it is the best option now.

SharpMap.Geometries.IGeometry sharpGeom = SharpMap.Converters.WellKnownBinary.GeometryFromWKB.Parse(geombytes);
wkt = SharpMap.Converters.WellKnownText.GeometryToWKT.Write(sharpGeom);

SharpMap on Github

I also don't know of where the current SharpMap repository is, but I did find a reference of the class here:

At the time, I think I was using SharpMap from Codeplex.

I forgot about ZigGIS. You might be able to build on that code--this link is the older ziggis. It's the aoPolygonToWkt,aoPointToWkt,aoPolylineToWkt methods that I'm thinking would work: https://code.google.com/archive/p/ziggis/downloads


You might try referencing the Microsoft.SqlServer.Types assembly (which I believe is included with the free Sql Server Express edition), then use STGeomFromWKB static method to create a microsoft geometry, which could then be converted into WKT using STAsText.

Also note that while Microsoft catches a lot of flak about being proprietary, they do offer source code to a lot of useful functions in their SqlServer Spatial Tools at codeplex. So if the SqlServerTypes is too much external dependency for you you might be able to find source code that does this.