NetTopologySuite / NetTopologySuite.IO.Esri

BSD 3-Clause "New" or "Revised" License
31 stars 16 forks source link

Create Shapefile From WKT failed #14

Closed KubaSzostak closed 1 year ago

KubaSzostak commented 1 year ago

Based on @anthea-wu question on Stack overflow:

I want to create a shapefile from wkt by using NetTopologySuite.IO.Esri, but get this error like this:

Unhandled exception. System.InvalidCastException: Unable to cast object of type 'NetTopologySuite.Geometries.Polygon' to type 'NetTopologySuite.Geo
metries.MultiPolygon'.
   at NetTopologySuite.IO.Esri.Shapefiles.Writers.ShapefileWriter`1.Write(IFeature feature)
   at NetTopologySuite.IO.Esri.Shapefiles.Writers.ShapefileWriter.Write(IEnumerable`1 features)
   at NetTopologySuite.IO.Esri.Shapefile.WriteAllFeatures(IEnumerable`1 features, String shpPath, Encoding encoding, String projection) 

Here is my code:


using System.Text;
using NetTopologySuite.Features;
using NetTopologySuite.IO;
using NetTopologySuite.IO.Esri;

var features = new List(); var wktReader = new WKTReader();

// I tried wkt with EPSG:4326 and EPSG:3826, but no one work. var geometry = wktReader.Read($"POLYGON((229884.458362927 2698919.1790506,229878.266318657 2698913.05244554,229872.637726088 2698914.0245398,229870.132848978 2698912.76299454,229868.585854503 2698911.60799823,229862.417875893 2698913.56799102,229859.466886255 2698913.2179918,229854.168905161 2698909.4280045,229832.918531065 2698900.75121021,229826.266555421 2698903.87487001,229815.242942057 2698911.30138916,229814.158096991 2698912.0322406,229802.736136414 2698917.5934203,229787.107090625 2698922.43900198,229770.41744862 2698926.49898628,229758.597689508 2698931.31876844,229754.622568952 2698933.63004125,229756.82934315 2698935.98290305,229758.154520219 2698937.39368305,229761.467578897 2698937.10864875,229772.427440716 2698935.38875568,229785.926793536 2698934.84915899,229801.796838123 2698933.66916467,229811.707603543 2698932.67086896,229818.576179578 2698931.9792722,229834.383317175 2698930.1646309,229840.615502711 2698929.44918301,229859.255237777 2698926.54949485,229875.612191821 2698922.46243583,229879.34506797 2698921.52971422,229884.458362927 2698919.1790506))");

// Also tried this but not work too. // var geometry = wktReader.Read(wkt) as Polygon;

var attributes = new AttributesTable { { "Date", new DateTime(2022, 1, 1) }, { "Content", $"I am No. 1" } };

var feature = new Feature(geometry, attributes); features.Add(feature);

Shapefile.WriteAllFeatures(features, @"C:\Users\user\Downloads\test.shp");

KubaSzostak commented 1 year ago

This library keep naming convention consistent with ESRI Shapefile Technical Description. For example there is ShapefilePolyLineWriter even though there is no such a think like PolyLine in the NetTopologySuite library. Similarly ShapefilePolygonWriter writes polygons that according to the Shapefile specification may contain multiple outer rings. That means Shapefile's Polygon can be fully handled only by geometry named in NetTopologySuite library as MultiPolygon.

KubaSzostak commented 1 year ago

I have some updates in progress. Let's wait until I finish it.