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.

6 Answers 6

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();
2
  • 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 9, 2012 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." Jul 30, 2012 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

4
  • AFAIK codeplex still has the most current sharpmap repository. sharpmap.codeplex.com Dec 21, 2010 at 17:28
  • Thank you. I will most likely end up implementing the conversion on my own, but code from existing OS projects might provide some valuable pointers.
    – Petr Krebs
    Dec 21, 2010 at 17:53
  • @JayCummins The ZigGIS link is dead
    – Barbarossa
    Sep 27, 2018 at 13:42
  • @Barbarossa, i know--i'm trying to find it. Although I suspect it is retired. Sep 27, 2018 at 15:15
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.

1
  • Thanks, I would rather not drag any MsSql bits into my implementation, but some of the source might be interesting.
    – Petr Krebs
    Dec 21, 2010 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).

1
  • Thank you, NTS and GeoTools looks good, I might reuse some bits in my implementation.
    – Petr Krebs
    Dec 21, 2010 at 17:54
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.

1
  • Thank you, look useful. I actually ended up writing my own WKT parser. Cannot open source it, though.
    – Petr Krebs
    Feb 28, 2013 at 15:13
-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

3
  • 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, 2010 at 16:23
  • @Mapperz: looked there too, but works only for WKT representations of coordinate systems and units, not geometries
    – Petr Krebs
    Dec 21, 2010 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.