Turfjs / turf

A modular geospatial engine written in JavaScript and TypeScript

Home Page:https://turfjs.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

turf-polygonize throws when otherwise valid geojson produces no valid polygons

stevage opened this issue · comments

  • version 6.5
  • can't share the data
  • no snippet.

Confirming this issue

Error: Each LinearRing of a Polygon must have 4 or more Positions.
    polygon index.js:227
    toPolygon EdgeRing.js:137
    getEnvelope EdgeRing.js:148
    findEdgeRingContaining EdgeRing.js:162
    findEdgeRingContaining EdgeRing.js:161
    polygonize index.js:41
    polygonize index.js:40

I haven't deeply investigated but I wouldn't be surprised if the incoming data is a bit defective.

The biggest problem is that an exception is thrown, which means no polygons at all are returned.

Hey, thanks for raising. Is it possible to provide a minimal sample data set that reproduces the issue?

Not exactly minimal but:

{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"LineString","coordinates":[[145.34046292304993,-37.85737162269892],[145.34046560525894,-37.85740338839779],[145.33954292535782,-37.8585109438722],[145.3394651412964,-37.858570238915846],[145.3398996591568,-37.858773535846204],[145.34051924943924,-37.8591864810098],[145.34064531326294,-37.859396129207425],[145.34054070711136,-37.86022412787682],[145.34113079309464,-37.8609843537584],[145.34264892339706,-37.86132105296641],[145.3430673480034,-37.86152645990395],[145.3432497382164,-37.861761512501204],[145.3434669971466,-37.862309965646226],[145.34492880105972,-37.86064341777292],[145.34520775079727,-37.85977518857622],[145.3454625606537,-37.85967142391486],[145.3454491496086,-37.85947448242115],[145.34577906131744,-37.859182245686526],[145.3467607498169,-37.85948930599275],[145.3476271033287,-37.859396129207425],[145.34757614135742,-37.85854694444009],[145.34745812416077,-37.85853423835931],[145.34591853618622,-37.85762363020013],[145.34569591283798,-37.857589746888756],[145.34539818763733,-37.85737162269892]]},"properties":{}},{"type":"Feature","geometry":{"type":"MultiLineString","coordinates":[[[145.33796310424805,-37.85673842357878],[145.33806771039963,-37.856768071819495],[145.3390011191368,-37.856935372382885],[145.34044951200485,-37.857145026983225],[145.34046560525894,-37.85740338839779],[145.34026712179184,-37.85764268955594]],[[145.3459507226944,-37.85764268955594],[145.34591853618622,-37.85762363020013],[145.34569591283798,-37.857589746888756],[145.34517288208008,-37.85720644084414],[145.34492880105972,-37.8571852636565],[145.34438967704773,-37.856878193752024],[145.34369498491287,-37.85677654274322],[145.34307539463043,-37.856533003297734],[145.34307807683945,-37.85629581628177],[145.34351527690887,-37.85576426046543],[145.34124612808228,-37.8537629502023],[145.33796310424805,-37.85319960862569]],[[145.33796310424805,-37.85296876440541],[145.33848613500595,-37.852343998899535],[145.33796310424805,-37.85204961602951]]]},"properties":{}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[145.34567445516586,-37.85757492293513],[145.34517288208008,-37.85720749970336],[145.34493014216423,-37.8571852636565],[145.34438967704773,-37.856878193752024],[145.34369632601738,-37.85677548387782],[145.34354209899902,-37.856715128524144]]},"properties":{}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[145.34046292304993,-37.85737162269892],[145.34046560525894,-37.85740338839779],[145.33954292535782,-37.8585109438722],[145.3394651412964,-37.858570238915846],[145.3398996591568,-37.858773535846204],[145.34051924943924,-37.8591864810098],[145.34064531326294,-37.859396129207425],[145.34054070711136,-37.86022412787682],[145.34113079309464,-37.8609843537584],[145.34264892339706,-37.86132105296641],[145.3430673480034,-37.86152645990395],[145.3432497382164,-37.861761512501204],[145.3434669971466,-37.862309965646226],[145.34492880105972,-37.86064341777292],[145.34520775079727,-37.85977518857622],[145.3454625606537,-37.85967142391486],[145.3454491496086,-37.85947448242115],[145.34577906131744,-37.859182245686526],[145.3467607498169,-37.85948930599275],[145.3476271033287,-37.859396129207425],[145.34757614135742,-37.85854694444009],[145.34745812416077,-37.85853423835931],[145.34591853618622,-37.85762363020013],[145.34569591283798,-37.857589746888756],[145.34539818763733,-37.85737162269892]]},"properties":{}},{"type":"Feature","geometry":{"type":"MultiLineString","coordinates":[[[145.33796310424805,-37.85673842357878],[145.33806771039963,-37.856768071819495],[145.3390011191368,-37.856935372382885],[145.34044951200485,-37.857145026983225],[145.34046560525894,-37.85740338839779],[145.34026712179184,-37.85764268955594]],[[145.3459507226944,-37.85764268955594],[145.34591853618622,-37.85762363020013],[145.34569591283798,-37.857589746888756],[145.34517288208008,-37.85720644084414],[145.34492880105972,-37.8571852636565],[145.34438967704773,-37.856878193752024],[145.34369498491287,-37.85677654274322],[145.34307539463043,-37.856533003297734],[145.34307807683945,-37.85629581628177],[145.34351527690887,-37.85576426046543],[145.34124612808228,-37.8537629502023],[145.33796310424805,-37.85319960862569]],[[145.33796310424805,-37.85296876440541],[145.33848613500595,-37.852343998899535],[145.33796310424805,-37.85204961602951]]]},"properties":{}},{"type":"Feature","geometry":{"type":"LineString","coordinates":[[145.34567445516586,-37.85757492293513],[145.34517288208008,-37.85720749970336],[145.34493014216423,-37.8571852636565],[145.34438967704773,-37.856878193752024],[145.34369632601738,-37.85677548387782],[145.34354209899902,-37.856715128524144]]},"properties":{}}]}

No doubt this particular example doesn't actually contain any usable polygons. But in that case it should just return an empty FeatureCollection instead of throwing an error.

I suspect it has to do with this condition stated on what I think is the original library.

Polygonizes a set of lines that represents edges in a planar graph. Edges must be correctly noded, i.e., they must only meet at their endpoints.

For my uses I really want to be able to throw a messy set of linestrings at it and get some polygons back. I generally can't meet the required guarantee.

it should just return an empty FeatureCollection instead of throwing an error

Thanks @stevage. Have updated the title of the issue based on the above. Will see if we can update the implementation to return an empty FeatureCollection if it's fed nothing useful.