rowanwins/polygon-splitter

Out of memory error

stevage opened this issue · 3 comments

First - awesome that this exists, thanks :)

const poly = {
  "type": "Feature",
  "properties": {},
  "geometry": {
    "type": "Polygon",
    "coordinates": [
      [
        [
          145.32697677612305,
          -37.86612784876244
        ],
        [
          145.32704651355743,
          -37.866045267693124
        ],
        [
          145.32697677612305,
          -37.86612784876244
        ],
        [
          145.33059507608414,
          -37.86179539389486
        ],
        [
          145.32697677612305,
          -37.86612784876244
        ]
      ]
    ]
  }
}

const linestring = {
  "type": "Feature",
  "properties": {},
  "geometry": {
    "type": "MultiLineString",
    "coordinates": [
      [
        [
          145.35186767578125,
          -37.86401247373356
        ],
        [
          145.35186767578125,
          -37.877021386076336
        ],
        [
          145.33538818359375,
          -37.877021386076336
        ],
        [
          145.33538818359375,
          -37.86401247373356
        ],
        [
          145.35186767578125,
          -37.86401247373356
        ]
      ],
      [
        [
          145.34088134765625,
          -37.86401247373356
        ],
        [
          145.34088134765625,
          -37.877021386076336
        ],
        [
          145.32440185546875,
          -37.877021386076336
        ],
        [
          145.32440185546875,
          -37.86401247373356
        ],
        [
          145.34088134765625,
          -37.86401247373356
        ]
      ],
      [
        [
          145.35186767578125,
          -37.8553385894982
        ],
        [
          145.35186767578125,
          -37.86834903305901
        ],
        [
          145.33538818359375,
          -37.86834903305901
        ],
        [
          145.33538818359375,
          -37.8553385894982
        ],
        [
          145.35186767578125,
          -37.8553385894982
        ]
      ],
      [
        [
          145.34088134765625,
          -37.8553385894982
        ],
        [
          145.34088134765625,
          -37.86834903305901
        ],
        [
          145.32440185546875,
          -37.86834903305901
        ],
        [
          145.32440185546875,
          -37.8553385894982
        ],
        [
          145.34088134765625,
          -37.8553385894982
        ]
      ],
      [
        [
          145.33538818359375,
          -37.85967565921001
        ],
        [
          145.33538818359375,
          -37.846663684549135
        ],
        [
          145.35186767578125,
          -37.846663684549135
        ],
        [
          145.35186767578125,
          -37.85967565921001
        ],
        [
          145.33538818359375,
          -37.85967565921001
        ]
      ],
      [
        [
          145.32440185546875,
          -37.85967565921001
        ],
        [
          145.32440185546875,
          -37.846663684549135
        ],
        [
          145.34088134765625,
          -37.846663684549135
        ],
        [
          145.34088134765625,
          -37.85967565921001
        ],
        [
          145.32440185546875,
          -37.85967565921001
        ]
      ],
      [
        [
          145.33538818359375,
          -37.85100126460796
        ],
        [
          145.33538818359375,
          -37.83798775896512
        ],
        [
          145.35186767578125,
          -37.83798775896512
        ],
        [
          145.35186767578125,
          -37.85100126460796
        ],
        [
          145.33538818359375,
          -37.85100126460796
        ]
      ],
      [
        [
          145.32440185546875,
          -37.85100126460796
        ],
        [
          145.32440185546875,
          -37.83798775896512
        ],
        [
          145.34088134765625,
          -37.83798775896512
        ],
        [
          145.34088134765625,
          -37.85100126460796
        ],
        [
          145.32440185546875,
          -37.85100126460796
        ]
      ],
      [
        [
          145.35186767578125,
          -37.86401247373356
        ],
        [
          145.35186767578125,
          -37.877021386076336
        ],
        [
          145.33538818359375,
          -37.877021386076336
        ],
        [
          145.33538818359375,
          -37.86401247373356
        ],
        [
          145.35186767578125,
          -37.86401247373356
        ]
      ],
      [
        [
          145.34088134765625,
          -37.86401247373356
        ],
        [
          145.34088134765625,
          -37.877021386076336
        ],
        [
          145.32440185546875,
          -37.877021386076336
        ],
        [
          145.32440185546875,
          -37.86401247373356
        ],
        [
          145.34088134765625,
          -37.86401247373356
        ]
      ],
      [
        [
          145.35186767578125,
          -37.8553385894982
        ],
        [
          145.35186767578125,
          -37.86834903305901
        ],
        [
          145.33538818359375,
          -37.86834903305901
        ],
        [
          145.33538818359375,
          -37.8553385894982
        ],
        [
          145.35186767578125,
          -37.8553385894982
        ]
      ],
      [
        [
          145.34088134765625,
          -37.8553385894982
        ],
        [
          145.34088134765625,
          -37.86834903305901
        ],
        [
          145.32440185546875,
          -37.86834903305901
        ],
        [
          145.32440185546875,
          -37.8553385894982
        ],
        [
          145.34088134765625,
          -37.8553385894982
        ]
      ],
      [
        [
          145.33538818359375,
          -37.85967565921001
        ],
        [
          145.33538818359375,
          -37.846663684549135
        ],
        [
          145.35186767578125,
          -37.846663684549135
        ],
        [
          145.35186767578125,
          -37.85967565921001
        ],
        [
          145.33538818359375,
          -37.85967565921001
        ]
      ],
      [
        [
          145.32440185546875,
          -37.85967565921001
        ],
        [
          145.32440185546875,
          -37.846663684549135
        ],
        [
          145.34088134765625,
          -37.846663684549135
        ],
        [
          145.34088134765625,
          -37.85967565921001
        ],
        [
          145.32440185546875,
          -37.85967565921001
        ]
      ],
      [
        [
          145.33538818359375,
          -37.85100126460796
        ],
        [
          145.33538818359375,
          -37.83798775896512
        ],
        [
          145.35186767578125,
          -37.83798775896512
        ],
        [
          145.35186767578125,
          -37.85100126460796
        ],
        [
          145.33538818359375,
          -37.85100126460796
        ]
      ],
      [
        [
          145.32440185546875,
          -37.85100126460796
        ],
        [
          145.32440185546875,
          -37.83798775896512
        ],
        [
          145.34088134765625,
          -37.83798775896512
        ],
        [
          145.34088134765625,
          -37.85100126460796
        ],
        [
          145.32440185546875,
          -37.85100126460796
        ]
      ]
    ]
  }
}

polygonSplitter(poly, linestring);

throws "out of memory"

Thanks for the test case @stevage - the basic problem is that the algorithm is spinning itself into an unending loop trying to rewire the polygon, I'll take a look and see if I can spot the problematic bit.

We'll you've definitely provided some curly geometries!

It appears that your polygon is essentially a line with no area to it. On top of that your input linestrings have segments overlapping. So 4 intersection points are being recorded that all occur exactly the same position, and the theoretical line segments between them will have 0 length 😂

geojson-editor 2023-08-31 08-36-21

Yeah I know. It's an output from turf.polygonize. it's hard to prevent stuff like that happening when you're processing third party data.