Skip to content

Instantly share code, notes, and snippets.

@billpratt
Created September 15, 2015 20:20
Show Gist options
  • Save billpratt/6bf4ab8e4435dc39db92 to your computer and use it in GitHub Desktop.
Save billpratt/6bf4ab8e4435dc39db92 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.IO;
using GeoAPI.Geometries;
using NetTopologySuite.Features;
using NetTopologySuite.Geometries;
using ProjNet.CoordinateSystems;
using NetTopologySuite.IO;
using System.Diagnostics;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
// Wkt in 4326 SRID (WGS84)
var wkt =
"POLYGON ((-86.7605020509258 41.5101338613656, -86.7604972038273 41.5100611525915, -86.7604971708084 41.5100606308085, -86.7604611720717 41.5094596307695, -86.7604611426546 41.5094591103497, -86.7604291439208 41.5088571103154, -86.760429130715 41.508856853856, -86.7603991319814 41.5082548538241, -86.7603991259966 41.5082547317887, -86.7603701303631 41.5076537960468, -86.7603401446338 41.5070530565908, -86.7603071566895 41.5064532528163, -86.7603071500912 41.506453131098, -86.7602814240795 41.5059715533315, -86.7605549835241 41.5059607024218, -86.7605808466407 41.5064448078787, -86.760613844555 41.5070447469854, -86.7606138651484 41.5070451395365, -86.7606438664126 41.5076461395046, -86.7606438727239 41.5076462680791, -86.7606728710439 41.5082472070294, -86.7607028628788 41.5088490177453, -86.7607348434949 41.5094506292495, -86.7607708135428 41.5100511081057, -86.760776407335 41.5101350123382, -86.7605020509258 41.5101338613656))";
var geomFactory = new GeometryFactory(new PrecisionModel(), 4326);
var wktReader = new WKTReader(geomFactory);
var geometry = wktReader.Read(wkt);
Debug.WriteLine($"Geometry Type: {geometry.GeometryType}");
Debug.WriteLine($"Shapefile Type: {Shapefile.GetShapeType(geometry)}");
IPolygon geomPolygon = geometry as IPolygon;
Debug.WriteLine($"IPolygon Geometry Type: {geomPolygon.GeometryType}");
Debug.WriteLine($"IPolygon Sheel CoordindateSequence Ordinates: {geomPolygon.Shell.CoordinateSequence.Ordinates}");
var attributesTable = new AttributesTable();
attributesTable.AddAttribute("Foo", "Bar");
var features = new List<IFeature>
{
new Feature(geometry, attributesTable)
};
// Create the directory where we will save the shapefile
var shapeFilePath = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
if (!Directory.Exists(shapeFilePath))
Directory.CreateDirectory(shapeFilePath);
var name = "test";
// Construct the shapefile name. Don't add the .shp extension or the ShapefileDataWriter will
// produce an unwanted shapefile
var shapeFileName = Path.Combine(shapeFilePath, name);
var shapeFilePrjName = Path.Combine(shapeFilePath, $"{name}.prj");
// Create the shapefile
var outGeomFactory = GeometryFactory.Default;
var writer = new ShapefileDataWriter(shapeFileName, outGeomFactory);
var outDbaseHeader = ShapefileDataWriter.GetHeader(features[0], features.Count);
writer.Header = outDbaseHeader;
writer.Write(features);
// Create the projection file
using (var streamWriter = new StreamWriter(shapeFilePrjName))
{
streamWriter.Write(GeographicCoordinateSystem.WGS84.WKT);
}
var shapeFileReader = new ShapefileDataReader(shapeFileName, GeometryFactory.Default);
var read = shapeFileReader.Read();
var geom = shapeFileReader.Geometry;
}
}
}
@MerlinDuChaos
Copy link

Thank you so much Bill !!! I was trying to do the exact same thing and couldn't find any resource on the web...
Not for future readers who could have the same problem I had, at least 1 entry in AttributesTable is required, it will not produce a correct file is the collection is empty.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment