Turfjs / turf-intersect

find the intersection of features
30 stars 9 forks source link

Error thrown when calling `turfIntersect` on two features #11

Closed NickHeiner closed 8 years ago

NickHeiner commented 9 years ago

Here is my code:

var turfIntersect = require('turf-intersect'),

    feature1 = {
        "type": "Feature",
        "geometry": {
        "type": "Polygon",
        "coordinates": [
            [
            [
                -0.002507080691666959,
                -0.0011250000000000001
            ],
            [
                -0.0023009735543523544,
                -0.001
            ],
            [
                -0.0023750000000000004,
                -0.001
            ],
            [
                -0.0023820806916669593,
                -0.0011250000000000001
            ],
            [
                -0.002507080691666959,
                -0.0011250000000000001
            ]
            ]
        ]
        },
        "properties": {
        "id": "Ekhec9w1i2",
        "generationDebugging_polyAreaKm": 0.1936254535770603,
        "generationDebugging_reasonStopped": "area below minimum block size"
        }
    },

    feature2 = {
        "type": "Feature",
        "geometry": {
        "type": "MultiPolygon",
        "coordinates": [
            [
            [
                [
                0.0009087550118278532,
                -0.0013568242308416504
                ],
                [
                0.0008458222552659575,
                -0.0012938914744331181
                ],
                [
                0.0008458222552659575,
                -0.0012309587180245858
                ],
                [
                0.0007828894987040619,
                -0.0011680259616160535
                ],
                [
                0.0007828894987040619,
                -0.0011050932052075212
                ],
                [
                0.0007828894987040619,
                -0.001042160448798989
                ],
                [
                0.0007828894987040619,
                -0.0009792276923904566
                ],
                [
                0.0008458222552659575,
                -0.0009162949359819244
                ],
                [
                0.0008458222552659575,
                -0.000853362179573392
                ],
                [
                0.0009087550118278532,
                -0.0007904294231648598
                ],
                [
                0.0009716877683897488,
                -0.000853362179573392
                ],
                [
                0.0009716877683897488,
                -0.0007904294231648598
                ],
                [
                0.0010346205249516444,
                -0.000853362179573392
                ],
                [
                0.0010346205249516444,
                -0.0007904294231648598
                ],
                [
                0.0010346205249516444,
                -0.0007274966667563275
                ],
                [
                0.00109755328151354,
                -0.0007274966667563275
                ],
                [
                0.00109755328151354,
                -0.0007904294231648598
                ],
                [
                0.0011604860380754355,
                -0.000853362179573392
                ],
                [
                0.0011604860380754355,
                -0.0009162949359819244
                ],
                [
                0.001223418794637331,
                -0.0009792276923904566
                ],
                [
                0.001223418794637331,
                -0.001042160448798989
                ],
                [
                0.001223418794637331,
                -0.0011050932052075212
                ],
                [
                0.001223418794637331,
                -0.0011680259616160535
                ],
                [
                0.0011604860380754355,
                -0.0012309587180245858
                ],
                [
                0.00109755328151354,
                -0.0012938914744331181
                ],
                [
                0.00109755328151354,
                -0.0013568242308416504
                ],
                [
                0.0010346205249516444,
                -0.0013568242308416504
                ],
                [
                0.0009716877683897488,
                -0.0013568242308416504
                ],
                [
                0.0009087550118278532,
                -0.0013568242308416504
                ]
            ],
            [
                [
                0.0010346205249516444,
                -0.000853362179573392
                ],
                [
                0.0010346205249516444,
                -0.0009162949359819244
                ],
                [
                0.00109755328151354,
                -0.000853362179573392
                ],
                [
                0.0010346205249516444,
                -0.000853362179573392
                ]
            ],
            [
                [
                0.0010346205249516444,
                -0.0009162949359819244
                ],
                [
                0.0009716877683897488,
                -0.0009162949359819244
                ],
                [
                0.0009716877683897488,
                -0.0009792276923904566
                ],
                [
                0.0010346205249516444,
                -0.0009162949359819244
                ]
            ]
            ],
            [
            [
                [
                0.0009716877683897488,
                -0.0007274966667563275
                ],
                [
                0.0009716877683897488,
                -0.0007904294231648598
                ],
                [
                0.0009087550118278532,
                -0.0007904294231648598
                ],
                [
                0.0009087550118278532,
                -0.0007274966667563275
                ],
                [
                0.0009087550118278532,
                -0.0006645639103477952
                ],
                [
                0.0009087550118278532,
                -0.0006016311539392629
                ],
                [
                0.0009716877683897488,
                -0.0006016311539392629
                ],
                [
                0.0009087550118278532,
                -0.0005386983975307306
                ],
                [
                0.0009716877683897488,
                -0.00047576564112219825
                ],
                [
                0.0009716877683897488,
                -0.00041283288471366596
                ],
                [
                0.0010346205249516444,
                -0.00034990012830513366
                ],
                [
                0.00109755328151354,
                -0.00028696737189660136
                ],
                [
                0.0011604860380754355,
                -0.00034990012830513366
                ],
                [
                0.0011604860380754355,
                -0.00041283288471366596
                ],
                [
                0.00109755328151354,
                -0.00047576564112219825
                ],
                [
                0.00109755328151354,
                -0.0005386983975307306
                ],
                [
                0.00109755328151354,
                -0.0006016311539392629
                ],
                [
                0.0010346205249516444,
                -0.0006645639103477952
                ],
                [
                0.0009716877683897488,
                -0.0006645639103477952
                ],
                [
                0.0009716877683897488,
                -0.0007274966667563275
                ]
            ]
            ]
        ]
        },
        "properties": {}
    };

console.log('intersect', turfIntersect(feature1, feature2));

When I run this, I get:

λ node sample.js 

/Users/nick.heiner/public-repos/iberville/node_modules/turf-intersect/node_modules/jsts/lib/jsts.js:1436
rlayOp.overlayOp(this.geom[0],this.geom[1],opCode);}catch(ex){throw savedExcep
                                                                    ^
Error
    at Object.<anonymous> (/Users/nick.heiner/public-repos/iberville/node_modules/turf-intersect/node_modules/jsts/lib/jsts.js:21:414)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
    at Module.load (module.js:355:32)
    at Function.Module._load (module.js:310:12)
    at Module.require (module.js:365:17)
    at require (module.js:384:17)
    at Object.<anonymous> (/Users/nick.heiner/public-repos/iberville/node_modules/turf-intersect/node_modules/jsts/index.js:2:12)
    at Module._compile (module.js:460:26)
    at Object.Module._extensions..js (module.js:478:10)
λ node --version
v0.12.7

λ npm ls --depth 0 turf-intersect
iberville@0.0.1 /Users/nick.heiner/public-repos/iberville
└── turf-intersect@1.4.2 

Is this a bug, or am I doing something wrong? If I am doing something wrong, it would be nice if the error were more descriptive.

jamesbursa commented 8 years ago

I've reproduced this issue with another test case:

var turf = require('turf');

var feature = {
    "type": "Feature",
    "geometry": {
        "type": "Polygon",
        "coordinates": [
            [[-77.04709500074387, 38.89175098284184],
            [-77.04718619585037, 38.891700879145844],
            [-77.04723984003067, 38.89156100614076],
            [-77.04739272594452, 38.89153595423019],
            [-77.04737395048141, 38.891494201026234],
            [-77.04719960689545, 38.891515077631254],
            [-77.04711645841599, 38.89141278220791],
            [-77.04717546701431, 38.89136685360256],
            [-77.04733908176422, 38.891421132860245],
            [-77.04733371734619, 38.891337626292795],
            [-77.0474624633789, 38.89131674963559],
            [-77.0474624633789, 38.89076560366621],
            [-77.04648613929749, 38.89076560366621],
            [-77.04644590616226, 38.89081362034119],
            [-77.04651027917862, 38.890928442693195],
            [-77.04644054174423, 38.89103908951131],
            [-77.04647809267044, 38.89132718796495],
            [-77.04660683870316, 38.891408606881384],
            [-77.0468321442604, 38.89137311659593],
            [-77.04681068658829, 38.89143992182437],
            [-77.04672753810883, 38.89143157117428],
            [-77.04672485589981, 38.89148376272141],
            [-77.04679995775223, 38.89150881465039],
            [-77.04675167798996, 38.89161946056447],
            [-77.0469206571579, 38.89169252852642],
            [-77.04696893692017, 38.89166956431788],
            [-77.04703867435455, 38.89174054457476],
            [-77.04709500074387, 38.89175098284184]],
            [[-77.0414462685585, 38.89186789132842],
            [-77.04195320606232, 38.891813612412165],
            [-77.04202562570572, 38.8917843852862],
            [-77.04212754964828, 38.89181778771487],
            [-77.0422026515007, 38.891765596413194],
            [-77.04234480857849, 38.89179273589488],
            [-77.0424011349678, 38.891640337132685],
            [-77.0424896478653, 38.89159232101656],
            [-77.04290807247162, 38.89156518145833],
            [-77.04322189092636, 38.891642424789154],
            [-77.0432299375534, 38.89171131741875],
            [-77.04331308603287, 38.89174680753521],
            [-77.04357326030731, 38.89178647293846],
            [-77.0437154173851, 38.89173845692116],
            [-77.04378515481949, 38.89178647293846],
            [-77.04397022724152, 38.891773947024035],
            [-77.04400509595871, 38.891684177906],
            [-77.04410433769226, 38.89164660010195],
            [-77.04491972923279, 38.891527603591356],
            [-77.04506993293762, 38.89143157117428],
            [-77.04493314027786, 38.891001511366],
            [-77.04496264457703, 38.89077186671261],
            [-77.04496800899506, 38.89076560366621],
            [-77.04070597887039, 38.89076560366621],
            [-77.04071938991547, 38.890796918892704],
            [-77.04070329666138, 38.891085018328596],
            [-77.04077035188675, 38.891101719709326],
            [-77.04068720340729, 38.89114764848611],
            [-77.04070061445236, 38.89121445392658],
            [-77.04077303409576, 38.89123741828226],
            [-77.04071670770645, 38.89137311659593],
            [-77.04081594944, 38.891500464008374],
            [-77.04091787338257, 38.891496288686994],
            [-77.04105734825134, 38.89164660010195],
            [-77.04126119613647, 38.891732193959996],
            [-77.0413202047348, 38.891811524760726],
            [-77.04138189554214, 38.89179482354686],
            [-77.04136312007904, 38.89183866422482],
            [-77.0414462685585, 38.89186789132842]]
        ]},
};
var tilePoly = {
    "type": "Feature",
    "geometry": {
        "type": "Polygon",
        "coordinates": [
            [[-77.04574584960938, 38.892101707724294],
            [-77.04437255859375, 38.892101707724294],
            [-77.04437255859375, 38.89103282648847],
            [-77.04574584960938, 38.89103282648847],
            [-77.04574584960938, 38.892101707724294]]
        ]},
};

var result = turf.intersect(feature, tilePoly);
console.log("result: ", result);

Output with node is as in the original report.

In a browser, I get the following error message:

TopologyError: no outgoing dirEdge found [ (-77.04574584960938, 38.892101707724294) ]

morganherlocker commented 8 years ago

@jamesbursa in your first polygon, the start and end coordinates do not match. That makes it an invalid polygon.

jamesbursa commented 8 years ago

The start and end coordinates do match in each of the 2 rings - isn't that all that is needed?

morganherlocker commented 8 years ago

Ah, I see the issue now. Your polygon has a hole that is outside the outer ring, making it invalid.

screenshot 2016-03-23 09 58 31
morganherlocker commented 8 years ago

The original test case had a similar issue. The first polygon contained two rings that touched, which is invalid.

screenshot 2016-03-23 10 05 00
morganherlocker commented 8 years ago

Thanks for reporting this. Polygon validity can be a tricky issue to nail down. I'm closing this issue, but if you still run into snags after fixing your polygons, please reopen.