Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Force simple geometrycollection on sites #3399

Merged
merged 8 commits into from
Jan 9, 2023
2 changes: 2 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ In preparation for HD Views developments (PR #3298)
**Bug fixes**

- Recreate cache folders if missing. (#3384)
- Modify site's geometry before saving to avoid edition and export of shapefiles (#3399)


2.94.0 (2022-12-12)
-----------------------
Expand Down
11 changes: 11 additions & 0 deletions geotrek/outdoor/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.contrib.gis.db import models
from django.contrib.gis.geos import GEOSGeometry, GeometryCollection
from django.contrib.gis.measure import D
from django.contrib.postgres.indexes import GistIndex
from django.core.validators import MinValueValidator
from django.db.models import Q
from django.db import connection
from django.utils.html import escape
from django.utils.translation import gettext_lazy as _
from mptt.models import MPTTModel, TreeForeignKey
Expand Down Expand Up @@ -299,6 +301,15 @@ def site_interventions(self):
qs |= Q(target_id__in=topologies) & ~Q(target_type__in=not_topology_content_types)
return Intervention.objects.existing().filter(qs).distinct('pk')

def save(self, *args, **kwargs):
with connection.cursor() as c:
c.callproc('ST_Dump', [self.geom.wkt])
geometries = []
for ids, geometry in c.fetchall():
geometries.append(GEOSGeometry(geometry, srid=settings.SRID))
self.geom = GeometryCollection(geometries, srid=settings.SRID)
return super().save(*args, **kwargs)


Path.add_property('sites', lambda self: intersecting(Site, self), _("Sites"))
Topology.add_property('sites', lambda self: intersecting(Site, self), _("Sites"))
Expand Down
12 changes: 11 additions & 1 deletion geotrek/outdoor/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.contrib.gis.geos import Polygon
from django.contrib.gis.geos.collections import GeometryCollection
from django.contrib.gis.geos.point import Point
from django.contrib.gis.geos.point import Point, GEOSGeometry
from django.test import TestCase, override_settings

from geotrek.common.tests.factories import OrganismFactory
Expand All @@ -25,6 +25,16 @@ def test_published_children_by_lang(self):
SiteFactory(name='child3', parent=parent, published_fr=True)
self.assertQuerysetEqual(parent.published_children, ['<Site: child2>', '<Site: child3>'])

def test_validate_collection_geometrycollection(self):
site_simple = SiteFactory.create(name='site', description='LUL', geom='GEOMETRYCOLLECTION(POINT(0 0), POLYGON((1 1, 2 2, 1 2, 1 1))))')
self.assertEqual(site_simple.geom.wkt,
GEOSGeometry('GEOMETRYCOLLECTION(POINT(0 0), POLYGON((1 1, 2 2, 1 2, 1 1)))').wkt
)
site_complex_geom = SiteFactory.create(name='site', geom='GEOMETRYCOLLECTION(MULTIPOINT(0 0, 1 1), POLYGON((1 1, 2 2, 1 2, 1 1))))')
self.assertEqual(site_complex_geom.geom.wkt,
GEOSGeometry('GEOMETRYCOLLECTION(POINT(0 0), POINT(1 1), POLYGON((1 1, 2 2, 1 2, 1 1)))').wkt
)


class SiteSuperTest(TestCase):
@classmethod
Expand Down