Ticket #30649: make_valid_patch.diff

File make_valid_patch.diff, 3.2 KB (added by milosu, 5 years ago)
  • django/contrib/gis/db/backends/base/operations.py

    a/a a/b class BaseSpatialOperations(object):  
    133133
    134134    def spatial_ref_sys(self):
    135135        raise NotImplementedError('subclasses of BaseSpatialOperations must a provide spatial_ref_sys() method')
     136
     137    def make_valid(self, geom):
     138        return geom.buffer(0)
  • django/contrib/gis/db/backends/postgis/operations.py

    a/a a/b class PostGISOperations(DatabaseOperations, BaseSpatialOperations):  
    99)
    1010from django.contrib.gis.db.backends.utils import SpatialOperator
    1111from django.contrib.gis.gdal import GDALRaster
     12from django.contrib.gis.geometry.backend import Geometry
    1213from django.contrib.gis.measure import Distance
    1314from django.core.exceptions import ImproperlyConfigured
    1415from django.db.backends.postgresql.operations import DatabaseOperations
    class PostGISOperations(DatabaseOperations, BaseSpatialOperations):  
    591591
    592592    def deconstruct_raster(self, value):
    593593        return to_pgraster(value)
     594
     595    def make_valid(self, geom):
     596        cursor = self.connection._cursor()
     597        try:
     598            try:
     599                cursor.execute('SELECT ST_MakeValid(%s)', (self.Adapter(geom),))
     600                row = cursor.fetchone()
     601            except:
     602                # Responsibility of callers to perform error handling.
     603                raise
     604        finally:
     605            # Close out the connection.  See #9437.
     606            self.connection.close()
     607        return Geometry(row[0])
  • a/b/tests/gis_tests/test_make_valid.py

     
     1"""
     2MakeValid enables to correct invalid geometry
     3"""
     4
     5import unittest
     6
     7from django.db import connection
     8from django.db.utils import DatabaseError
     9from django.contrib.gis.geos import fromstr
     10
     11class MakeValidTest(unittest.TestCase):
     12    "Testing the Make Valid operator"
     13
     14    def testEquality(self):
     15        geom = fromstr('POINT(0 0)')
     16        geom2 = connection.ops.make_valid(geom)
     17        self.assertEqual(geom, geom2)
     18
     19    def testEmpty(self):
     20        geom = fromstr('POLYGON EMPTY')
     21        self.assertTrue(geom.valid)
     22        geom2 = connection.ops.make_valid(geom)
     23        self.assertEqual(geom2.wkt, "POLYGON EMPTY")
     24
     25    def testMakeValid(self):
     26
     27        OVERLAPPING_POLYGONS = 'MULTIPOLYGON(((-817919.2992374257 -993731.1603221426,-817903.1039358412 -993927.4507220673,-817513.0408654083 -993857.9949068283,-817564.0353255265 -993647.1541963345,-817919.2992374257 -993731.1603221426)),((-818062.2435801325 -993576.9766019919,-818040.2372730081 -993795.6885085825,-817671.0262602307 -993741.5098793216,-817720.6315932958 -993521.6543376467,-818062.2435801325 -993576.9766019919)),((-818213.5106938615 -993436.8959622316,-818182.2592243042 -993635.488124189,-817817.200519477 -993608.3730649975,-817854.7430425821 -993390.3676253688,-818213.5106938615 -993436.8959622316)))'
     28
     29        geom = fromstr(OVERLAPPING_POLYGONS)
     30        self.assertFalse(geom.valid)
     31
     32        geom2 = connection.ops.make_valid(geom)
     33        self.assertTrue(geom2.valid)
     34
     35        self.assertEqual(geom2.num_points, 23)
     36        self.assertEqual(len(geom2), 3)
Back to Top