The distance from an empty collection should be +inf.
The distance from an empty collection returns zero.
Run the following script:
from shapely.geometry import Point, LineString
empty_thing = LineString()
assert empty_thing.is_empty
print(empty_thing.distance(Point(1, 1))) # Returns zero, should probably be +inf
Windows 10 64 bit
Python 3.6
Shapely 1.6b4 installed from PyPI using pip
This behavior is inherited from GEOS. If either geometry is empty the distance method returns 0. I think this is the relevant line of code:
Interesting. Given that they explicitly covered this case, is this working as desgined? Why should this be the case?
Similar to GEOS, JTS returns 0.0, so perhaps this behavior was drafted a long time ago.
PostGIS returns null
for this scenario, since there are not any geometries to measure between. I'd suggest Shapely similarly return None
if any of the inputs are empty.
@mwtoews I'd like to see an exception raised instead of getting None: the intent is more clear.
Thanks for bringing this up, @jebob!
SQL Server spatial types return NULL, similar to PostGIS. Esri's ArcPy generates an error. It seems we have 0, NULL, or error depending on whom you ask. I can see some merit behind arguments for any of them, so clear documentation is probably the most important part of addressing this issue. After all, without a consensus among geospatial products, it is a matter of expectations and not correctness.
I've changed my mind and am inclined to leave it as zero. Here's an analogy in Python:
>>> "" in "foo"
True
The empty string is in every non-empty string. Would you be willing to accept that the empty geometry is in every non-empty geometry and thus be at distance 0?
I wouldn't accept that the empty geometry is in every non-empty geometry, as the operation isn't analogous.
Two identical strings are in each other, but two identical shapes may have different offsets and therefore aren't "in" each other.
Empty geometries should not be presumed to occupy every space, because then the intersection of an empty geometry and X geometry returns X.
Empty geometries must be further than any shape. dist(foo, X) <= dist(foo, union(X+Y)) by definition. If X is the empty set, then dist(foo, X) must return infinity for the identity to be valid for all foo, Y.
@jebob thanks for being patient with me. I see the logic in +inf now.
Still, I think I'd prefer to treat it as undefined and raise an exception (in a feature release) for usability reasons.
Raising an exception seems a good compromise candidate as it is an unambiguous result, therefore preventing confusion from those who rely on GEOS/JTS returning zero.
Most helpful comment
Similar to GEOS, JTS returns 0.0, so perhaps this behavior was drafted a long time ago.
PostGIS returns
null
for this scenario, since there are not any geometries to measure between. I'd suggest Shapely similarly returnNone
if any of the inputs are empty.