17

It seems like there is no means of converting an ArcObjects geometry to the Well-Known Text representation (and vice versa) in ArcGIS ArcObjects API. The only thing I was able to find is conversion to WKB (the IWkb interface).

Is there a way to perform conversion between geometry objects and WKT or do I have to implement it myself? I primarily aim for .NET implementation without much external dependencies.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Petr Krebs
  • 10,291
  • 1
  • 22
  • 33

6 Answers6

12

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();
Jay Cummins
  • 14,642
  • 7
  • 66
  • 141
SagebrushGIS
  • 589
  • 6
  • 15
  • 1
    Your approach seems to work fine for me, but I'm wondering about my choice of srid here. I used this: int srid = feature.Shape.SpatialReference.FactoryCode; Do you agree with that implementation? – elrobis Jul 09 '12 at 17:10
  • 2
    As long as you are using one of Esri's pre-defined coordinate systems this will work. The documentation notes the following "If you create a custom projected coordinate system, the factory code is zero." – SagebrushGIS Jul 30 '12 at 20:37
7

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

Jay Cummins
  • 14,642
  • 7
  • 66
  • 141
5

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.

Kirk Kuykendall
  • 25,787
  • 8
  • 65
  • 153
  • Thanks, I would rather not drag any MsSql bits into my implementation, but some of the source might be interesting. – Petr Krebs Dec 21 '10 at 17:56
3

NTS can "speak" WKT, and has readers and writers for both WKB and WKT. So if you have WKB, NTS can read that and write to WKT.

However, you could also create NTS objects by using the attributes of an ArcObjects geometry and then output as WKT.

For one of our ArcObjects projects we created utility methods for NTS to move geometry around (mostly to take advantage of the alternate projection methods NTS offers).

mwalker
  • 5,752
  • 25
  • 32
3

The DNRGPS open source project (licensing here) has extension methods for converting between IGeometry and WKT, although you'd have to adapt them to not use IObjectFactory if you want to use them outside an ArcGIS application.

The license looks pretty permissive so I put my standalone version (does not use IObjectFactory) of it up on GitHub if anyone was interested:

Update: I've made a few improvements to my version to support applying spatial references for the output geometry objects and using a integer coordinate friendly default spatial reference (Plate Carree) if none is specified.

blah238
  • 35,793
  • 7
  • 94
  • 195
-2

Check out GeometryBridge, it might be able to help you. http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#//00010000039n000000

patrick
  • 2,730
  • 26
  • 50
  • It does not provide any WKT-related methods... You might be confusing it with WKS, which is a safe representation of a geometry (useful for thread safety and such). – Petr Krebs Dec 21 '10 at 16:23
  • http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/IGeometryServer_Interface/002m000001ww000000/ – Mapperz Dec 21 '10 at 16:48
  • @Mapperz: looked there too, but works only for WKT representations of coordinate systems and units, not geometries – Petr Krebs Dec 21 '10 at 16:49