Search code examples
polygongdalgeodjangogeos

GEOS C error always when creating valid geometry or polygon


I am trying to create a Polygon with django.contrib.gis.geos.Polygon. I know that the points are valid but I get the error:

django.contrib.gis.geos.error.GEOSException: Error encountered checking Geometry returned from GEOS C function "GEOSGeom_createPolygon_r"

If I instead use django.contrib.gis.geos.geometry.GEOSGeometry it works just fine.

Ex:

from django.contrib.gis.geos import Polygon
from django.contrib.gis.geos.geometry import GEOSGeometry

poly = GEOSGeometry('POLYGON((4 4, 4 4, 4 4, 4 4))')
poly.coords()
>>> (((4.0, 4.0), (4.0, 4.0), (4.0, 4.0), (4.0, 4.0)),)

poly2 = Polygon(((4, 4), (4, 4), (4, 4), (4, 4)))
>>> GEOSException: Error encountered checking Geometry returned from GEOS C function "GEOSGeom_createPolygon_r".

Operating System & other info:

  • MacOS Monterey, M1 chip
  • gdal installed via homebrew (3.5.1)
  • Python 3.8.12, Django 3.2
  • also have geos and proj installed previously as dependencies via homebrew
  • Have set:
GDAL_LIBRARY_PATH="/opt/homebrew/lib/libgdal.dylib"`
GEOS_LIBRARY_PATH="/opt/homebrew/lib/libgeos_c.dylib"

Exact same code is working on an older Mac setup w/ the same environment just fine.

Have also now tried Python 3.9 and reinstalling all related brew packages. I also get the same error with other geometry types (Point & LineString work fine, but MultiPoint & MultiLineString fail).


Solution

  • Turns out the solution here would be to upgrade to Django 4.0.1 or higher after they fixed the bug: https://github.com/django/django/blob/4f284115a9181990f713d5167b25628fa171a5e4/docs/releases/4.0.1.txt#L67-L70