NetTopologySuite / NetTopologySuite.IO.Esri

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

Invalid Polygon Issue #25

Closed Em3a-c closed 1 year ago

Em3a-c commented 1 year ago

Hi all,

I've been using your library recently, it's really nice and easy to use!

I have noticed however, that there are a few shapefiles that are unable to be read in, and thought best to raise this as a potential issue.

An exception is thrown when this line is executed in my project: foreach (var feature in Shapefile.ReadAllFeatures(filePath)) { ... }

The exception thrown: NetTopologySuite.IO.Esri.ShapefileException : {"Invalid polygon geometry.":null}

StackTrace: at NetTopologySuite.IO.Esri.Shp.Readers.ShpPolygonReader.BuildMultiPolygon(ShpMultiPartBuilder parts) at NetTopologySuite.IO.Esri.Shp.Readers.ShpPolygonReader.ReadGeometry(Stream stream, MultiPolygon& geometry) at NetTopologySuite.IO.Esri.Shp.Readers.ShpReader1.ReadCore(Int32& skippedCount) at NetTopologySuite.IO.Esri.Shapefiles.Readers.ShapefileReader1.Read(Boolean& deleted) at NetTopologySuite.IO.Esri.Shapefiles.Readers.ShapefileReader1.Read(Boolean& deleted, Feature& feature) at NetTopologySuite.IO.Esri.Shapefiles.Readers.ShapefileReader.FeatureEnumerator.MoveNext() at System.Linq.Buffer1..ctor(IEnumerable1 source) at System.Linq.Enumerable.ToArray[TSource](IEnumerable1 source) at NetTopologySuite.IO.Esri.Shapefile.ReadAllFeatures(String shpPath, ShapefileReaderOptions options)

I've attached a couple of the shapefile examples, that cause the same exception to be thrown just below, Example Shapefiles.zip FYI: Shapefiles are from here: https://hub.arcgis.com/documents/NSTAUTHORITY::-nsta-offshore-zipped-shapefiles-wgs84/about

Thanks in advance for your help, Emma

KubaSzostak commented 1 year ago

You got an error because the shapefiles contain invalid geometries (confirmed by GDAL) and you are using default GeometryBuilderMode (Strict). You can ignore invalid geometries by explicitly setting ShapefileReaderOptions:

var opts = new ShapefileReaderOptions()
{
    GeometryBuilderMode = GeometryBuilderMode.IgnoreInvalidShapes
};
var features = Shapefile.ReadAllFeatures(shpPath, opts);
KubaSzostak commented 1 year ago

Thanks to your feedback a bug was found and fixed. Thank you @Em3a-c !

Em3a-c commented 1 year ago

Thanks alot for your help @KubaSzostak