ThinkGeo.com    |     Documentation    |     Premium Support

‘Attempted to divide by zero’ error from GetFeaturesNearestTo()

Dear ThinkGeo team,

Our customer has extra-large scale shapefiles (attached) and got ‘Attempted to divide by zero’ error when click on map-control. The error comes from GetFeaturesNearestTo() method. Below are details of the error information.

Please help us to fix this problem.

Thank you very much!!!

Mengbo

/////////////// Error Details /////////////////////

We use your Map suite Desktop with version 9.0.0.854 (today version).
Open an extra-large scale shape file (attached). Click on the map-control and get a point shape (X = 35671570693374.945, Y = 20838915659409.129). Then pass this shape to ThinkGeo.MapSuite.Core.FeatureSource.GetFeaturesNearestTo(BaseShape targetShape, …). Got exception as below.

************** Exception Text **************
System.DivideByZeroException: Attempted to divide by zero.
at GisSharpBlog.NetTopologySuite.Algorithm.CGAlgorithms.IsCCW(ICoordinate[] ring)
at GisSharpBlog.NetTopologySuite.Operation.Buffer.OffsetCurveSetBuilder.AddPolygonRing(ICoordinate[] coord, Double offsetDistance, Positions side, Locations cwLeftLoc, Locations cwRightLoc)
at GisSharpBlog.NetTopologySuite.Operation.Buffer.OffsetCurveSetBuilder.AddPolygon(IPolygon p)
at GisSharpBlog.NetTopologySuite.Operation.Buffer.OffsetCurveSetBuilder.Add(IGeometry g)
at GisSharpBlog.NetTopologySuite.Operation.Buffer.BufferBuilder.Buffer(IGeometry g, Double distance)
at GisSharpBlog.NetTopologySuite.Operation.Buffer.BufferOp.BufferOriginalPrecision()
at GisSharpBlog.NetTopologySuite.Operation.Buffer.BufferOp.ComputeGeometry()
at GisSharpBlog.NetTopologySuite.Operation.Buffer.BufferOp.Buffer(IGeometry g, Double distance, Int32 quadrantSegments, BufferStyle endCapStyle)
at ThinkGeo.MapSuite.Core.BaseShape.eT4=(Double distance, Int32 quadrantSegments, BufferCapType bufferCapType)
at ThinkGeo.MapSuite.Core.BaseShape.BufferCore(Double distance, Int32 quadrantSegments, BufferCapType bufferCapType, GeographyUnit shapeUnit, DistanceUnit distanceUnit)
at ThinkGeo.MapSuite.Core.BaseShape.Buffer(Double distance, GeographyUnit shapeUnit, DistanceUnit distanceUnit)
at ThinkGeo.MapSuite.Core.FeatureSource.GetFeaturesWithinDistanceOfCore(BaseShape targetShape, GeographyUnit unitOfData, DistanceUnit distanceUnit, Double distance, IEnumerable1 returningColumnNames) at ThinkGeo.MapSuite.Core.FeatureSource.GetFeaturesWithinDistanceOf(BaseShape targetShape, GeographyUnit unitOfData, DistanceUnit distanceUnit, Double distance, IEnumerable1 returningColumnNames)
at ThinkGeo.MapSuite.Core.FeatureSource.GetFeaturesNearestTo(BaseShape targetShape, GeographyUnit unitOfData, Int32 maxItemsToFind, IEnumerable`1 returningColumnNames, Double searchRadius, DistanceUnit unitOfSearchRadius)

shapefiles.zip (1.2 MB)

Hi Mengbo,

We created one sample with you shape file to test this issue.

  • This shape file can be display correctly on Map;
  • We can call GetFeautresNeatestTo() method without exception, but due to the huge scale, there is no features returns. You can consider use bounding box to get nearest features. Please refer the attachment sample to details.

Please refer the test sample. if there is still issue, please feel free to let us know that.

Thanks
Mark

Hi Mark,

Thank you very much for your time of taking care of my problem. You sample code is great and works well on my PC.

Our map application needs to limit a search radius. We want GetFeaturesNearestTo() method only returns features within the search radius (e.g., 10 meter). And if beyond of search radius and outside of the target rectangle, return nothing.

So I changed your sample code to limit a very small rectangle as below:

RectangleShape target = new RectangleShape(e.WorldX - 0.01, e.WorldY + 0.01, e.WorldX + 0.01, e.WorldY - 0.01);
var feature = shapeFileFeatureLayer.FeatureSource.GetFeaturesNearestTo(target, GeographyUnit.Meter, 1, ReturningColumnsType.NoColumns);

But when I click anywhere on the map-control, it always returns a feature without limit of search distances, even outside of the rectangle.

I changed your code to call other two GetFeaturesNearestTo() methods that limit search radius as below:

double radius = 0.01 * winformsMap1.CurrentExtent.Width; // radius is 108083195882.21535 at runtime.
PointShape target = new PointShape(e.WorldX, e.WorldY);

// method-1
var feature = shapeFileFeatureLayer.FeatureSource.GetFeaturesWithinDistanceOf(target, GeographyUnit.Meter, DistanceUnit.Meter, radius, ReturningColumnsType.NoColumns);

// method-2
var feature = shapeFileFeatureLayer.FeatureSource.GetFeaturesNearestTo(target, GeographyUnit.Meter, 1, new string[] { }, radius, DistanceUnit.Meter);

But both methods return ‘Attempted to divide by zero’ error.

Mark, do you have other ideas how to get near features within a search-radius (e.g., within 10 meter)?

Thank you again!

Mengbo

Hi Mengbo,

Please modify your code just like our sample, don’t use the point but use a rectangle shape around the point can solved this issue.

Please see the modified code:

        double radius = 0.01 * winformsMap1.CurrentExtent.Width; // radius is 108083195882.21535 at runtime.
        PointShape target = new PointShape(e.WorldX, e.WorldY);

        var feature = shapeFileFeatureLayer.FeatureSource.GetFeaturesNearestTo(new RectangleShape(target.X - 1, target.Y + 1, target.X + 1, target.Y - 1), GeographyUnit.Meter, 1, new string[] { }, radius, DistanceUnit.Meter);

Wish that’s helpful.

Regards,

Ethan

Hi Ethan,

Yes, your code works greatly. It solves the problem. Thank you so much for your help. Also thanks Mark’s help.

ThinkGeo team is a great team. I have excellent experiences from your tech supports. I will recommend your Web product for our coming Web projects.

Have you both a great day!

Mengbo

Hi Mengbo,

I am glad to hear that’s helpful, and thanks to your appreciate.

Any question please let us know.

Regards,

Ethan