Read fields, values and feature points from a .shp example with SharpMap

Topics: Data Access
Jan 29 at 2:10 AM
Hello,
I was wondering if somebody could provide a SharpMap C# example on how .shp files' fields, values and feature points can be read from it, and printed to a console?
I checked both tutorials and "how to" pages, but could not find such example.

I would be very grateful for any kind help on this.

Thank you.
Coordinator
Jan 29 at 2:01 PM

public static void Example()
{
    using (var p = new SharpMap.Data.Providers.ShapeFile(@"c:\tmp\shapefile.shp"))
    {
        // Number of features:
        System.Console.WriteLine("Number of features: {0}", p.GetFeatureCount());

        // Get Feature 25
        var fdr = p.GetFeature((uint) 25);
        WriteDataRow(fdr);

        // Get extent
        var extent = p.GetExtents();
        System.Console.WriteLine("Extent of dataset: {0}", extent);

        // Grab all features for shapefiles extent
        var fds = new SharpMap.Data.FeatureDataSet();
        p.ExecuteIntersectionQuery(extent, fds);
        foreach (var table in fds.Tables)
        {
            var headerWritten = false;
            foreach (SharpMap.Data.FeatureDataRow row in table.Rows)
            {
                WriteDataRowCsv(row, ref headerWritten);
            }
        }
    }
}

private static void WriteDataRowCsv(SharpMap.Data.FeatureDataRow row, ref bool headerWritten)
{
    if (!headerWritten)
    {
        System.Console.Write("{0}", row.Table.Columns[0].ColumnName);
        var i = 0;
        foreach (System.Data.DataColumn dc in row.Table.Columns) {
            i++; if (i == 1) continue;
            System.Console.Write(";{0}", dc.ColumnName);
        }
        System.Console.WriteLine();
        headerWritten = true;
    }

    var items = row.ItemArray;
    System.Console.Write("{0}", items[0] ?? "");
    for(var i = 1; i < items.Length; i++) 
        System.Console.Write(";{0}", items[i] ?? "");
    System.Console.WriteLine();
}

private static void WriteDataRow(SharpMap.Data.FeatureDataRow row)
{
    foreach (System.Data.DataColumn dc in row.Table.Columns)
    {
        System.Console.WriteLine("{0}: {1}", dc.ColumnName, row[dc] ?? "");
    }
            
}

Jan 29 at 3:41 PM
Thank you FObermaier!!

In which way should I edit the upper code so that the transformation of the .shp geometry is added? Transformation to some EPSG projection for example?