Skip to content

Commit

Permalink
fix: Force simple geometrycollection on sites
Browse files Browse the repository at this point in the history
  • Loading branch information
LePetitTim committed Jan 5, 2023
1 parent f734f47 commit d091254
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
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

0 comments on commit d091254

Please sign in to comment.