is there is a way to direct convert dbgeometry or sqlgeometry to shape file?

1.5k views Asked by At

I have a table that contains a SqlGeometry data field I want to export that table to shapefile on asp.net application. Is there is a way to convert the table directly to a shapefile?

If not, is there is an free SDK or tool for writing the shape file?

Note that the table contains strings, int and Boolean fields.

1

There are 1 answers

0
XavierFischer On

You can give NetTopologySuite a try, they have a ShapeFileWriter class that will do the job with some little effort.

EDIT : Here is some sample code. I wrote it using NetTopologySuite source files. But there are NuGet packages.

namespace ConsoleApplication1
{
using GeoAPI.Geometries;
using NetTopologySuite.Features;
using NetTopologySuite.Geometries;
using NetTopologySuite.IO;

class Program
{
    static void Main(string[] args)
    {
        FeatureCollection features = new FeatureCollection();

        // Polygon 1
        List<Coordinate> coords = new List<Coordinate>();
        coords.Add(new Coordinate(0,0));
        coords.Add(new Coordinate(0,10));
        coords.Add(new Coordinate(10,10));
        coords.Add(new Coordinate(10,0));
       coords.Add(new Coordinate(0,0));
        LinearRing ring = new LinearRing(coords.ToArray());
        Polygon poly = new Polygon(ring);

        // Polygon1 attributes
        AttributesTable attr = new AttributesTable();
        attr.AddAttribute("Column1", "HELLO");
        attr.AddAttribute("Column2", "WORLD !");

        Feature featureForPolygon = new Feature(poly, attr);

        features.Add(featureForPolygon);

        ShapefileDataWriter writer = new ShapefileDataWriter(@"%userprofile%\documents\outFile");
        writer.Header = new DbaseFileHeader();
        writer.Header.AddColumn("Column1", 'C', 10, 0);
        writer.Header.AddColumn("Column2", 'C', 10, 0);
        writer.Write(features.Features);
    }
}
}

It's not straightforward as you have to manually convert each SqlGeometry. (Maybe NTS MsSqlSpatial would help...)

Give it a try and let me know.