.netshapefilenettopologysuite

Create Shapefile From WKT failed Using NetTopologySuite.IO.Esri


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) 

I try to open the debug mode and tracking code, I found that in Shapefile.cs line 185, there are trying to get the parameter's geometry type like this:

var shapeType = features.FindNonEmptyGeometry().GetShapeType();

And in my situation, it always return PolygonZM 😭 I try to search like NetTopologySuite create shp as keyword but no answer for NetTopologySuite.IO.Esri; there only the way create shp using NetTopologySuite.IO.Shapefile, but I saw in it's github page said it would never be updated.

Could anyone give me a point to solve this question? Thanks!!
( I'm not a native English speaker, please bear with me for any grammatical errors, thanks! )

Here is my code:

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

var features = new List<Feature>();
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");

Solution

  • I finally fixed this issue by separating polygon writer from original polygon writer, who use MultiPolygon at begin and cause this error I guessed. Now, I have MultiPolygonWriter ( original PolygonWriter ) and PolygonWriter ( new ), the newer one will only create the type Polygon in Geometry, and other polygon types will be processed by the original writer.

    If anyone need the change I made, here is the repo: https://github.com/anthea-wu/NetTopologySuite.IO.Esri Hopes it can be helpful for you 😃