How to export WKT from Shapefile to C #?

I have a Shapefile that contains several thousand polygons.

I need to read from this file in C # and list the WKT formatted strings.

I looked at DotSpatial and the "CatFood" ESRI Shapefile Reader . I can get or load the shapefile just fine, but I cannot figure out how to export it as WKT then.

In DotSpatial, the only examples I could find use WktWriterthat accepts Geometry. I could not figure out how to get Geometryfrom Shape.

Is there a library that is more suitable for this?

Update

Thanks to mdm20's answer, I was able to write the following:

using (var fs = FeatureSet.Open(path))
{
    var writer = new WktWriter();
    var numRows = fs.NumRows();
    for (int i = 0; i < numRows; i++)
    {
        var shape = fs.GetShape(i, true);                    
        var geometry = shape.ToGeometry();
        var wkt = writer.Write((Geometry) geometry);
        Debug.WriteLine(wkt);
    }
}

, , , , fs.ShapeIndices fs.GetShape(). Shape, a ShapeRange, .

  • fs.IndexMode = true? ? , .
  • fs.GetShape() getAttributes. , , , , true false. , . ?
  • , WKT , ? - ? - dotSpatial ?
  • , , - . .prj. dotSpatial , - - ?

!

+5
3

DotSpatial Shape ToGeometry.

/// <summary>
/// Converts this shape into a Geometry using the default factory.
/// </summary>
/// <returns>The geometry version of this shape.</returns>
public IGeometry ToGeometry()
{
    return ToGeometry(Geometry.DefaultFactory);
}

Edit

dotspatial , .

1-2: . , , .

3: WKT - . , , , . .. dotspatial

4: prj , . , , , , . , Bing Maps Google Earth . dotspatial projectionions .

. , .

+4

:

private void button1_Click(object sender, EventArgs e)
    {            
        String result = "";

        OpenFileDialog openfile = new OpenFileDialog();
        openfile.Filter = "Shapefile (*.shp)|*.shp|All files (*.*)|*.*";
        openfile.ShowDialog();
        String filePath = openfile.FileName.Replace(".shp", "").Replace(@"\", @"\\");
        String[] a = filePath.Split('\\');

        String shpName = a[a.Length-1];

        try
        {

            SQLiteConnection.CreateFile(openfile.FileName.Replace(".shp", "")+".sqlite");

            System.Data.SQLite.SQLiteConnection connection = new SQLiteConnection(@"Data Source=" + openfile.FileName.Replace(".shp", "") + ".sqlite");



            connection.Open();
            object returnvalue = new SQLiteCommand("SELECT load_extension('libspatialite-2.dll')", connection).ExecuteScalar();

            System.Data.SQLite.SQLiteCommand commande = new SQLiteCommand(connection);
            commande.CommandText = "CREATE virtual TABLE "+shpName+"VT USING VirtualShape('" + filePath + "', 'CP1252', 4326);";

            commande.ExecuteScalar();

            commande.CommandText = "CREATE TABLE geom AS SELECT * FROM " + shpName + "VT;";
            commande.ExecuteScalar();

            commande.CommandText = "drop table " + shpName + "VT";
            commande.ExecuteScalar();


            commande.CommandText = "ALTER TABLE geom ADD COLUMN WKT TEXT;";
            commande.ExecuteScalar();

            commande.CommandText = " UPDATE  geom set WKT= ST_AsText(Geometry);";
            commande.ExecuteScalar();


           // the test commande

            commande.CommandText = "SELECT WKT FROM geom;";

            result = (string)commande.ExecuteScalar();





        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);

        }
        MessageBox.Show(result);


    }
+1

First open the shapefile, and then get its basic geometry elements ........

        IFeatureSet fb = FeatureSet.Open("F:\\Test_value\\test.shp");
        List<string> str = new List<string>();
        foreach (IFeature ff in fb.Features)
        {
            Geometry geometry = ff.BasicGeometry as Geometry;
            WktWriter wktWriter = new WktWriter();
            str.Add(wktWriter.Write(geometry));          
        }
+1
source

All Articles