bilaldursun1 / nettopologysuite

Automatically exported from code.google.com/p/nettopologysuite
0 stars 0 forks source link

Unioning a set of disjoint polygons fails with NullReferenceException #123

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I have a set of disjoint polygons that I'm trying to union via:

new UnaryUnionOp(rootNode.AllPolygons).Union()

However, on certain sets this fails with the following stacktrace:

System.NullReferenceException was unhandled
  Message=Object reference not set to an instance of an object.
  Source=NetTopologySuite
  StackTrace:
       at NetTopologySuite.Operation.Union.CascadedPolygonUnion.RestrictToPolygons(IGeometry g) in D:\Users\krysmasn\Documents\HSI\MapConverter\Spatialite.Net\NTS\NetTopologySuite\Operation\Union\CascadedPolygonUnion.cs:line 369
       at NetTopologySuite.Operation.Union.CascadedPolygonUnion.UnionActual(IGeometry g0, IGeometry g1) in D:\Users\krysmasn\Documents\HSI\MapConverter\Spatialite.Net\NTS\NetTopologySuite\Operation\Union\CascadedPolygonUnion.cs:line 347
       at NetTopologySuite.Operation.Union.CascadedPolygonUnion.UnionUsingEnvelopeIntersection(IGeometry g0, IGeometry g1, Envelope common) in D:\Users\krysmasn\Documents\HSI\MapConverter\Spatialite.Net\NTS\NetTopologySuite\Operation\Union\CascadedPolygonUnion.cs:line 310
       at NetTopologySuite.Operation.Union.CascadedPolygonUnion.UnionOptimized(IGeometry g0, IGeometry g1) in D:\Users\krysmasn\Documents\HSI\MapConverter\Spatialite.Net\NTS\NetTopologySuite\Operation\Union\CascadedPolygonUnion.cs:line 286
       at NetTopologySuite.Operation.Union.CascadedPolygonUnion.UnionSafe(IGeometry g0, IGeometry g1) in D:\Users\krysmasn\Documents\HSI\MapConverter\Spatialite.Net\NTS\NetTopologySuite\Operation\Union\CascadedPolygonUnion.cs:line 270
       at NetTopologySuite.Operation.Union.CascadedPolygonUnion.BinaryUnion(IList`1 geoms, Int32 start, Int32 end) in D:\Users\krysmasn\Documents\HSI\MapConverter\Spatialite.Net\NTS\NetTopologySuite\Operation\Union\CascadedPolygonUnion.cs:line 167
       at NetTopologySuite.Operation.Union.CascadedPolygonUnion.Worker.Execute() in D:\Users\krysmasn\Documents\HSI\MapConverter\Spatialite.Net\NTS\NetTopologySuite\Operation\Union\CascadedPolygonUnion.cs:line 208
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()

Basically what's happening is that in 
"CascadedPolygonUnion.UnionUsingEnvelopeIntersection()" the local vars "g0Int" 
and "g1Int" both become empty geometry collections (in some cases) and thus 
UnionActual() fails.

Here are the input parameters:

g0: MULTIPOLYGON (((-2.775 -37.382, -2.7694818956884695 -37.302294048833446, 
-4.381 -37.19, -4.379 -37.16, -2.7674053419183364 -37.272299383264858, -2.766 
-37.252, -2.703 -37.257, -2.712 -37.386, -2.775 -37.382)), ((-0.558 -16.355, 
-0.556624473051351 -16.33528411373603, -2.168 -16.223, -2.165 -16.193, 
-0.55452706181921063 -16.305221219408683, -0.549 -16.226, -0.485 -16.23, -0.494 
-16.36, -0.558 -16.355)))

g1: MULTIPOLYGON (((-4.218 -16.08, -4.216 -16.05, -2.924 -16.14, -2.926 -16.17, 
-4.218 -16.08)), ((-5.291 -18.097, -5.243 -17.415, -5.239 -17.352, 
-5.15929328747628 -17.357518157020873, -5.071 -16.091, -5.041 -16.093, 
-5.1292306097055169 -17.359599419328081, -5.109 -17.361, -5.114 -17.424, -5.161 
-18.106, -5.291 -18.097)))

common: Env[-4,381 : -2,924, -18,106 : -16,193]

I'm using the latest trunk version (rev 867).

What steps will reproduce the problem?
1.
2.
3.

What is the expected output? What do you see instead?

What version of the product are you using? On what operating system?

Please provide any additional information below.

Original issue reported on code.google.com by goo...@mail.manski.net on 16 Aug 2012 at 8:05

GoogleCodeExporter commented 9 years ago
Not sure whether this is 100% correct but this fixes the problem (for me):

https://bitbucket.org/mayastudios_3rdparty/nts-src/changeset/9958462c46cf632d14d
62d99b5cd7c553c8e773f

Original comment by goo...@mail.manski.net on 21 Aug 2012 at 12:26

GoogleCodeExporter commented 9 years ago
can you help me replicate the bug? This test looks ok (doesn't generate errors) 
with current trunk)

Original comment by diegogu...@gmail.com on 22 Aug 2012 at 8:07

Attachments:

GoogleCodeExporter commented 9 years ago
Hi Diego!

I am experiencing (exactly?) the same problem, "g0_Int" and "g1Int" both 
becomes empty as the OP describes.

But I'm not sure I like the OP's solution. My polygon sets are bigger, but I 
have the data at hand (Shape) for testing if necessary.

I have tried to union each polygon in my data set with each of the others and 
that didn't cause any problems. So the problem seems to be in a derived set of 
my polygons.

I'm using version 1.12.0, revision I don't know.

Regards, Casper

Original comment by emailtoc...@gmail.com on 28 Sep 2012 at 11:23

GoogleCodeExporter commented 9 years ago
@caspar: Could you setup a testcase and provide your ShapeFile?

Original comment by felix.ob...@netcologne.de on 28 Sep 2012 at 11:33

GoogleCodeExporter commented 9 years ago
I can confirm that it is a problem if both g0int and g0int become empty 
GeometryCollections. I can even imagine that this could happen, but 
unfortunatly I cannot reproduce this situation with the geometries provided 
above by OP.

Anyhow, the result of g0.Union(g1) is null in this case, and therefore we have 
no factory at hand to create an empty polygon.

I'd suggest:
- remove static keywords from both UnionActual and RestrictToPolygons functions
- use _geomFactory for creation of result geometry, instead of g.Factory

Un

Original comment by felix.ob...@netcologne.de on 28 Sep 2012 at 9:53

GoogleCodeExporter commented 9 years ago
Fixed as of r916

Original comment by felix.ob...@netcologne.de on 1 Oct 2012 at 12:19