"Vertex components can't be NaN." Exception

Topics: SharpMap v2.0
Jul 17, 2008 at 8:25 PM

I am getting a "Vertex components can't be NaN." exception thrown inside getVertexInternal (BufferedCoordinate2DFactory.cs) My call stack is below

I have been unable to locate the problem. Does anyone know what the problem may be??


     ManagedBufferedCoordinate2D.dll!NetTopologySuite.Coordinates.BufferedCoordinate2DFactory.getVertexInternal(double x = NaN, double y = NaN, double w = 1.0) Line 621    C#
     ManagedBufferedCoordinate2D.dll!NetTopologySuite.Coordinates.BufferedCoordinate2DFactory.getVertexInternal(double x = NaN, double y = NaN) Line 614 + 0x24 bytes    C#
     ManagedBufferedCoordinate2D.dll!NetTopologySuite.Coordinates.BufferedCoordinate2DFactory.Create(double x = NaN, double y = NaN) Line 78 + 0x1d bytes    C#
     ManagedBufferedCoordinate2D.dll!NetTopologySuite.Coordinates.BufferedCoordinate2DFactory.GeoAPI.Coordinates.ICoordinateFactory.Create(double x = NaN, double y = NaN) Line 192 + 0x1d bytes    C#
     SharpMap.dll!SharpMap.Presentation.Presenters.MapPresenter2D.getGeoCenter() Line 1307 + 0x87 bytes    C#
     SharpMap.dll!SharpMap.Presentation.Presenters.MapPresenter2D.handleViewSizeChanged(object sender = {SharpMap.Presentation.WinForms.MapViewControl}, System.EventArgs e = {System.EventArgs}) Line 1049 + 0x1d bytes    C#
>    SharpMap.Presentation.WinForms.dll!SharpMap.Presentation.WinForms.MapViewControl.OnSizeChanged(System.EventArgs args = {System.EventArgs}) Line 838 + 0xd bytes    C#

Jul 17, 2008 at 8:53 PM
Edited Jul 17, 2008 at 8:55 PM
Hi dcb, this one is slightly counter-intuitive, try adding a layer to the map before assigning the map to the view. (In case you are wondering the error is due to the map extents having NaN as the ordinates of each vertex) hth jd
Jul 19, 2008 at 7:33 PM
This has me confused. I looked at the source code and samples to see how to use the layers and all use the VectorLayer (and its dependencies) which does not seem to be part of the current build from svn. Also the comments in the file headers give examples of using FeatureLayer which is abstract (see ShapeFileProvider.cs and below). Could you give me an example of how to create and add a shapefile layer for v2 code and explain about VectorLayer.

    /// FeatureLayer myLayer = new FeatureLayer("My layer");
    /// myLayer.DataSource = new ShapeFile(@"C:\data\MyShapeData.shp");


JohnDiss wrote:
Hi dcb, this one is slightly counter-intuitive, try adding a layer to the map before assigning the map to the view. (In case you are wondering the error is due to the map extents having NaN as the ordinates of each vertex) hth jd

Jul 21, 2008 at 1:58 PM
Edited Jul 21, 2008 at 2:11 PM
Hi Derek, that code looks like 0.9/1.1 code did it come from the win forms demo(winforms demo hasn't been refreshed for v2 yet)? You can have a look at the (currently) top patch http://www.codeplex.com/SharpMap/SourceControl/PatchList.aspx which is also a little out of date (from memory you have to change any references of VectorStyle to GeometryStyle) but will hopefully shed some light on the matter.. hth jd
May 8, 2009 at 4:24 PM

Hi, I'm also getting the "Vertex Components Can't be NaN" exception while converting a shapefile from NAD83 to WGS84. It converts the first 376 records with no problems then bombs on row 377 in  {NetTopologySuite.Coordinates.BufferedCoordinate getVertexInternal(Double, Double, Double, Double)}

It appears that one of the geometries in my shapefile is bad. I'd like to just skip over it and move on to the next one but my IFeatureDataReader loses its place after the exception is thrown. The shapefiles I work with are going to have a small percentage of bad geometries from time to time, and I just want to to bypass them.

Any ideas?

<font size="2">



IFeatureDataReader ifdr = sf.GetReader();

        while (linenumber <= sf.FeatureCount)      <--I put this in here instead of while (ifdr.Read()) in an effort to get past the bad record
                        SqlGeometryBuilder gb = new SqlGeometryBuilder();

                        geom = ifdr.Geometry;
                        SqlBytes b = new SqlBytes(geom.AsBinary());
                        SqlGeometry g = SqlGeometry.STGeomFromWKB(b, 4326);
                        cmd.Parameters["@MBA"].Value = "4835500000";
                        cmd.Parameters["@SeqNo"].Value = seq;
                        cmd.Parameters["@Area"].Value = ifdr.GetString(ifdr.GetOrdinal("Shape_area"));
                        cmd.Parameters["@Perimeter"].Value = ifdr.GetString(ifdr.GetOrdinal("Shape_len"));
                        cmd.Parameters["@ParcelNo"].Value = ifdr.GetString(ifdr.GetOrdinal("TAXID"));
                        cmd.Parameters["@TaxYear"].Value = 2008;
                        cmd.Parameters["@dtCreate"].Value = System.DateTime.Now;
                        sp.Value = g;

                    catch (Exception e) { prcl.ToString(); errorcnt++; }
            catch (Exception e) { linenumber.ToString(); }    <--I'm catching the "Vertex Components Can't be NaN exception" here


May 8, 2009 at 4:55 PM

Hi Brad, Shapefile Oids are sequential UInt values so you could try to enumerate between 1 (the first index in a shapefile) and FeatureCount + 1 calling shpfile.GetGeometryByOid(oid).

This will break if the actual file is truncated or the data does not lay at the expected stream position hth jd

May 8, 2009 at 5:33 PM
Edited May 8, 2009 at 7:03 PM

Right. I've tried two different Windows apps for converting Shapefiles, the Shape2SQL program that everyone is using and another one called FransonCoordinateTransformation. Shape2SQL will load this particular shapefile to SQL Server but it can't convert the spatial reference system. The Franson tool converts all the shapes but the conversion is not correct. So, I'm back to trying this on my own.

I'll give this a try. My fear is that since I'm not building a file-based index when I open the ShapeFile(they are too big), reading byOID is going to be dreadfully slow. I wish there was a way to position a reader at a point in the stream. I'll give your suggestion a try. We will see!

Thx John

May 8, 2009 at 8:03 PM

It was slow, but it worked! Thank you John