7

Anyone know of a free OpenSource .NET library i could use to convert KML to WKT?

I know i can use openlayers and there are many JavaScript possibilities, but i need to do this on the back-end.

1

3 Answers 3

6

How about using GDAL's OGR .NET bindings?

http://bjarte.com/post/gdal-in-csharp

and its KML driver through libkml http://www.gdal.org/ogr/drv_libkml.html

Alternatively, you could use libkml directly.

3
  • I don't see any KML readers in the GDAL gone wild project. Am i missing something?
    – CaptDragon
    Commented Oct 31, 2011 at 19:10
  • Ugh, you are right. The libkml drivers came this year and those are from last year. Can you check if the OSGeo4W binaries have the libkml dll in it? Commented Oct 31, 2011 at 20:10
  • Nothing from what i can see.
    – CaptDragon
    Commented Oct 31, 2011 at 20:35
3

Your other option is to use the NetTopologySuite which is a port from the Java version, it supports WKT Reading/Writing.

We've then taken the Kml xsd, using the provided ms tools converted it to classes which the xml serialiser understands and then walked the tree converting the nodes we cared about into NTS IGeometry that the rest of our system used.

6
  • I'll try this and let you know.
    – CaptDragon
    Commented Oct 31, 2011 at 20:35
  • I've created the classes from xsd now...Do you know which dlls int he NTS have the WKT read/write capabilities?
    – CaptDragon
    Commented Nov 1, 2011 at 13:43
  • I use GisSharpBlog.NetTopologySuite.IO WKTWriter/WKTReader Commented Nov 2, 2011 at 5:43
  • How did you go with it? Commented Nov 4, 2011 at 2:29
  • Actually, I'm using something very unconventional, but it works like a charm and it's very easy, and it's what i need. I'll post an update soon. Basically i'm having my PostGIS database do it for me.
    – CaptDragon
    Commented Nov 4, 2011 at 16:39
1

Since i'm using post GIS, I ended up having it do it for me:

If i need two KML feature's converted, i'll send it a select like the following:

SELECT  ST_AsText(ST_GeomFromKML('<Polygon >
  <outerBoundaryIs>
    <LinearRing>
      <coordinates>
                    120.801170655354,14.632489189195,0
                    120.782956111463,14.5427894930054,0
                    120.76474480255,14.4530886861418,0
                    120.925219414103,14.4220920136416,0
                    120.943496765891,14.5117984961193,0
                    120.961777812394,14.6015038368517,0
                    120.801170655354,14.632489189195,0
                  </coordinates>
    </LinearRing>
  </outerBoundaryIs>
</Polygon>')), ST_AsText(ST_GeomFromKML('<Polygon >
  <outerBoundaryIs>
    <LinearRing>
      <coordinates>
                    121.087312420981,14.6013782055702,0
                    121.071223131928,14.5142178677531,0
                    121.244070778376,14.4838207663532,0
                    121.260233278214,14.571006143522,0
                    121.087312420981,14.6013782055702,0
                  </coordinates>
    </LinearRing>
  </outerBoundaryIs>
</Polygon>'))

Result:

"POLYGON((120.801170655354 14.632489189195,120.782956111463 14.5427894930054,120.76474480255 14.4530886861418,120.925219414103 14.4220920136416,120.943496765891 14.5117984961193,120.961777812394 14.6015038368517,120.801170655354 14.632489189195))";"POLYGON((121.087312420981 14.6013782055702,121.071223131928 14.5142178677531,121.244070778376 14.4838207663532,121.260233278214 14.571006143522,121.087312420981 14.6013782055702))"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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