NetTopologySuite / NetTopologySuite.IO.Esri

BSD 3-Clause "New" or "Revised" License
35 stars 18 forks source link

Unable to write single POLYGONs to SHP file #54

Closed tjmay1981 closed 2 months ago

tjmay1981 commented 2 months ago

Attempting to write a single polygon to a shapefile results in the error "Unable to cast object of type 'NetTopologySuite.Geometries.Polygon' to type 'NetTopologySuite.Geometries.MultiPolygon'.". If the WKT supplied is a MULTIPOLYGON then the code works fine.

var wktReader = new WKTReader(); Geometry g = wktReader.Read("POLYGON ((262300 366500, 262300 366600, 262400 366600, 262400 366500, 262300 366500))"); var fields = new List<DbfField>(); var field = fields.AddCharacterField("NAME"); field.StringValue = "test"; fields.Add(field); using (var shpWriter = Shapefile.OpenWrite("test.shp", new ShapefileWriterOptions(ShapeType.Polygon, fields.ToArray()))) { shpWriter.Geometry = g; shpWriter.Write(); }

The only way I can rectify this is to add the line if (g.GeometryType == "Polygon") g = new MultiPolygon(new[] { (Polygon)g }); to convert single polygons into multipolygons.

KubaSzostak commented 2 months ago

Thank you for bringing this to our attention! According to the specification, Shapefile polygons are always stored as MultiPolygons, despite the shape type being named 'Polygon'. For the time being, please use MultiPolygons or MultiLineStrings as a workaround. We will address this issue as soon as we have an available time slot.