'geodjango creating GEOS multi geometries fails after python upgrade (M1)

I have an @property on a model that gets a bounding box for all the geometries associated with a dataset. This code has worked fine for a couple of years. Now, on a new M1 mac laptop, I upgraded Python (3.7.4 to 3.9.7) and the configuration of GDAL and GEOS was difficult. But as I understand, django.contrib.gis includes its own versions of those libraries. Relevent code snippets:

from django.contrib.gis.geos import GeometryCollection, MultiPoint, Point
from places.models import PlaceGeom
from datasets.models import Dataset

class Dataset(models.Model):

    fields …

    @property
    def bounds(self):
        dsgeoms=PlaceGeom.objects.values_list(‘geom’,flat=True).filter(place__dataset=self.label)

        print(tuple(dsgeoms[:2]))
        # (<Point object at 0x12ee39988>, <Point object at 0x12ee39a08>)

        gc = GeometryCollection(tuple(dsgeoms[:2]))

        return json.loads(gc.envelope.geojson) if pg_geoms.count() > 0 else None

This crashes when creating the GeometryCollection with no real clue as to why, in PyCharm: “process finished with exit code 138 (interrupted by signal 10: SIGBUS)”

in django shell: “67692 bus error ./manage.py shell”

in browser: simply quits runserver

So I simply tried the examples from the Geodjango docs at https://docs.djangoproject.com/en/2.2/ref/contrib/gis/geos/, and though the Point and LineString creation worked, GeometryCollection and MultiPoint did not, with the shell error "68483 segmentation fault ./manage.py shell"

I'm stumped, but before I try building the bbox with Shapely and multiple transformations, thought I'd ask for help



Solution 1:[1]

The answer and a patch to fix this were provided by @bpartridge on GitHub, here: https://github.com/libgeos/geos/issues/528#issuecomment-997327327

TLDR; -- Django 2.2 was lax in its call to GEOS, which earlier GEOS versions 'forgave' but later ones do not.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 kgeo