NetTopologySuite/NetTopologySuite.IO.Esri

Invalid Polygon Issue

Closed this issue · 3 comments

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

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);

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

Thanks alot for your help @KubaSzostak