Shapely Polygon creating empty polygon

Israel Brewster ijbrewster at alaska.edu
Wed Jan 5 12:48:14 EST 2022


Found it! Apparently, it’s an import order issue. This works:

>>> from shapely.geometry import Polygon
>>> from osgeo import osr
>>> bounds = [-164.29635821669632, 54.64251856269729, -163.7631779798799, 54.845450778742546]
>>> print(Polygon.from_bounds(*bounds))
POLYGON ((-164.2963582166963 54.64251856269729, -164.2963582166963 54.84545077874255, -163.7631779798799 54.84545077874255, -163.7631779798799 54.64251856269729, -164.2963582166963 54.64251856269729))

But this doesn’t:

>>> from osgeo import osr
>>> from shapely.geometry import Polygon
>>> bounds = [-164.29635821669632, 54.64251856269729, -163.7631779798799, 54.845450778742546]
>>> print(Polygon.from_bounds(*bounds))
POLYGON EMPTY

…So apparently I have to make sure to import shapely *before* I import anything from osgeo. Why? I have no idea...
---
Israel Brewster
Software Engineer
Alaska Volcano Observatory 
Geophysical Institute - UAF 
2156 Koyukuk Drive 
Fairbanks AK 99775-7320
Work: 907-474-5172
cell:  907-328-9145

> On Jan 4, 2022, at 1:57 PM, Israel Brewster <ijbrewster at alaska.edu> wrote:
> 
> I’m running into an issue with shapely that is baffling me. Perhaps someone here can help out?
> 
> When running shapely directly from a python 3.8 interpreter, it works as expected:
> 
> >>> import shapely
> >>> shapely.__version__
> '1.8.0'
> >>> from shapely.geometry import Polygon
> >>> bounds = [-164.29635821669632, 54.64251856269729, -163.7631779798799, 54.845450778742546]
> >>> print(Polygon.from_bounds(*bounds))
> POLYGON ((-164.2963582166963 54.64251856269729, -164.2963582166963 54.84545077874255, -163.7631779798799 54.84545077874255, -163.7631779798799 54.64251856269729, -164.2963582166963 54.64251856269729))
> 
> However, if I put this exact same code into my Flask app (currently running under the Flask development environment) as part of handling a request, I get an empty polygon:
> 
> >>>    import shapely
> >>>    print(shapely.__version__)
> >>>    from shapely.geometry import Polygon
> >>>    print(Polygon.from_bounds(*bounds))
> 
> Output:
> 
> 1.8.0
> POLYGON EMPTY
> 
> In fact, *any* attempt to create a polygon gives the same result:
> >>> test = Polygon(((1, 1), (2, 1), (2, 2)))
> >>> print(test)
> POLYGON EMPTY
> 
> What am I missing here? Why doesn’t it work as part of a Flask request call?
> ---
> Israel Brewster
> Software Engineer
> Alaska Volcano Observatory 
> Geophysical Institute - UAF 
> 2156 Koyukuk Drive 
> Fairbanks AK 99775-7320
> Work: 907-474-5172
> cell:  907-328-9145
> 



More information about the Python-list mailing list