mrJean1 / PyGeodesy

Pure Python geodesy tools
https://mrjean1.github.io/PyGeodesy/
297 stars 58 forks source link

Problem with boundsOf function #62

Open christophelebrun opened 3 years ago

christophelebrun commented 3 years ago

Hello,

There seems to be a problem with the boundsOf function. I guess it occurs when one of the pole is included in the polygon.

Here is a snippet to figure out the problem:


from pygeodesy.sphericalNvector import LatLon
from pygeodesy.points import boundsOf

square = [(-87, 169), (-87, 170), (-86, 170), (-86, 169)]
square_latlon = [LatLon(*vertex) for vertex in square]

poly = [(-53.123167265109586, -142.31549347964588),
 (-56.18098637168117, -150.59345713564096),
 (-61.21449183902621, -174.32775284700298),
 (-60.924537261111254, 150.33105852563114),
 (-59.41255092548878, 116.90184661596409),
 (-55.96850597283981, 73.62965786693006),
 (-58.22943736394686, 66.03709129230528),
 (-61.44699148107358, 57.81181456237063),
 (-66.53009631577717, 33.6500584396396),
 (-78.524168110103, 4.735160439589678),
 (-55.007664818434854, -127.16484351174928),
 (-54.689657785955546, -127.41783959200605)]
poly_latlon = [LatLon(*vertex) for vertex in poly]

square_latlon[0].isenclosedBy(poly_latlon) # returns True

boundsOf(poly_latlon).overlap(boundsOf(square_latlon)) # returns None
mrJean1 commented 3 years ago

Polar polygons need special handling. If those can occur use function ispolar to check first.

from pygeodesy import ispolar

print(ispolar(poly_latlon))  # returns True

Polygons wrapping around the earth longitudinally are troublesome as well, as in poly_latlon[2].lon -174.32.. to poly_latlon[3].lon 150.33..

The boundsOf and the .overlap results above are correct. The issue is the .isenclosedBy result. Try

square_latlon[0].isenclosedBy(poly_latlon) and not ispolar(poly_latlon)

or

from pygeodesy import isenclosedBy
isenclosedBy(square_latlon[0], poly_latlon, wrap=True)

The latter can be used with any LatLon class, even with pygeodesy.LatLon_, which is intended for use with x-y functions like clipSH, isenclosedBy, ispolar, etc.