Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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.

Thanks.

share|improve this question
add comment

6 Answers 6

up vote 4 down vote accepted

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

I also don't know of where the current SharpMap repository is, but I did find a reference of the class here: http://www.google.com/codesearch/p?hl=en#FDsea6rqN6I/trunk/Thirdparty/SharpMap/SharpMap/Converters/WellKnownText/GeometryToWKT.cs&q=converter%20package:http://fdotoolbox%5C.googlecode%5C.com&sa=N&cd=6&ct=rc

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: http://www.google.com/codesearch/p?hl=en#YxxleBCXM18/ZigGis/helpers.cs&q=WKT%20package:http://ziggis%5C.googlecode%5C.com&sa=N&cd=1&ct=rc&l=138

share|improve this answer
    
AFAIK codeplex still has the most current sharpmap repository. sharpmap.codeplex.com –  Kirk Kuykendall Dec 21 '10 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 '10 at 17:53
add comment

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();
share|improve this answer
    
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 '12 at 17:10
1  
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
add comment

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.

share|improve this answer
    
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
add comment

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).

share|improve this answer
    
Thank you, NTS and GeoTools looks good, I might reuse some bits in my implementation. –  Petr Krebs Dec 21 '10 at 17:54
add comment

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.

share|improve this answer
    
Thank you, look useful. I actually ended up writing my own WKT parser. Cannot open source it, though. –  Petr Krebs Feb 28 '13 at 15:13
add comment

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

share|improve this answer
    
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
    
    
@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
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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