Replies: 4 comments
-
Interesting question @Pitouli. Many mathematical examples discussing sets say a union is a combination of two sets. On the other hand (and more in line with your thinking I believe) it seems mapping tools (notably ESRI) describe it as the geometric union of the input features. Even intersect just says "geometric intersection of the input features". Which sounds like passing in 0 or 1 features guarantees you an empty result. Taking those examples into account Turf's "two feature minimum" does seem overly protective, especially now we've switched from Follow up question - what would you say is the most intuitive return type for an empty result? Is it null? Or a Feature with an empty coordinates array (a situation mentioned in the spec). It seems cleaner to avoid null altogether, provided the process in question worked as intended. Anyone passing shapes into functions like these should understand an empty result set is a distinct possibility. What's your take on that?
If possible would like to avoid adding workarounds like this, unless we have a major limitation this was placing on someone. What if we were to make a goal to tidy this up when we do another major version bump? Prepared to make breaking changes (across major revisions) if it makes Turf more consistent and predictable long term. |
Beta Was this translation helpful? Give feedback.
-
Hello, thank you for your fast answer! :) Personnally, my main usecase for And my main usecase for But I do not know if those kind of usecases can be generalized. But reading your answer, it gives me an idea. If "Feature with an empty coordinates array" are valid, then I can probably solve my problem easily by providing an Empty Feature in case my "surface of interest" is null. I will look in turfs doc if there is a "out of the box" emptyFeature generator, and if it works well with union and intersect. And if not, then I will see if I can make a PR for it. |
Beta Was this translation helpful? Give feedback.
-
Sounds good @Pitouli. Give that a try.
If you just pass @turf/helpers polygon() an empty coordinates array it should create a polygon feature with a null geometry. |
Beta Was this translation helpful? Give feedback.
-
In lieu of any further info, will mark this as resolved. |
Beta Was this translation helpful? Give feedback.
-
Hello,
When you try to union less than 2 geometries, the function throw an error with a specific message, so it is not a bug.
https://github.com/Turfjs/turf/blob/master/packages/turf-union/index.ts
But should not we allow the union of a FeatureCollection having 0 or 1 geometries, by returning 'null' or the provided geometry?
I do not make a Pull Request for now because I suppose it is something to be discussed, and in the mean time I will implement a check to tackle this scenario in my code; but I am afraid it means reimplementing several lines that are already in the method.
I feel like most of the time the dev wants to "aggregate" in order to have the biggest area, so if there is only 1 geom he will prefer to have it back rather than having an error thrown.
On the other hand, I can imagine that for the intersect() function, the question is less "obvious": if you intersect 0 geom, you return null; but if you intersect only 1, should you return the geom untouched, or null?
Perhaps the best solution could be to keep the current behavior (so no breaking change), and add an option for both method where the user can specify the behavior in case there is 0 or 1 geom?
For example:
Beta Was this translation helpful? Give feedback.
All reactions