Ticket #14318: geos_validreason.1.diff

File geos_validreason.1.diff, 4.3 KB (added by Robert Coup, 14 years ago)

Patch against r13683 (without the random test_sqlite change this time)

  • django/contrib/gis/geos/geometry.py

     
    275275        "This property tests the validity of this Geometry."
    276276        return capi.geos_isvalid(self.ptr)
    277277
     278    @property
     279    def valid_reason(self):
     280        """
     281        Returns a string containing the reason for any invalidity.
     282        """
     283        if not GEOS_PREPARE:
     284            raise GEOSException('Upgrade GEOS to 3.1 to get validity reason.')               
     285        return capi.geos_isvalidreason(self.ptr)
     286
    278287    #### Binary predicates. ####
    279288    def contains(self, other):
    280289        "Returns true if other.within(this) returns true."
  • django/contrib/gis/geos/prototypes/misc.py

     
    44"""
    55from ctypes import c_int, c_double, POINTER
    66from django.contrib.gis.geos.libgeos import GEOM_PTR
    7 from django.contrib.gis.geos.prototypes.errcheck import check_dbl
     7from django.contrib.gis.geos.prototypes.errcheck import check_dbl, check_string
     8from django.contrib.gis.geos.prototypes.geom import geos_char_p
    89from django.contrib.gis.geos.prototypes.threadsafe import GEOSFunc
    910
    1011### ctypes generator function ###
     
    2627geos_area = dbl_from_geom(GEOSFunc('GEOSArea'))
    2728geos_distance = dbl_from_geom(GEOSFunc('GEOSDistance'), num_geom=2)
    2829geos_length = dbl_from_geom(GEOSFunc('GEOSLength'))
     30
     31# Validity reason
     32geos_isvalidreason = GEOSFunc('GEOSisValidReason')
     33geos_isvalidreason.argtypes = [GEOM_PTR]
     34geos_isvalidreason.restype = geos_char_p
     35geos_isvalidreason.errcheck = check_string
  • django/contrib/gis/geos/prototypes/__init__.py

     
    1818    to_hex, to_wkb, to_wkt
    1919
    2020# Miscellaneous routines.
    21 from django.contrib.gis.geos.prototypes.misc import geos_area, geos_distance, geos_length
     21from django.contrib.gis.geos.prototypes.misc import geos_area, geos_distance, geos_length, geos_isvalidreason
    2222
    2323# Predicates
    2424from django.contrib.gis.geos.prototypes.predicates import geos_hasz, geos_isempty, \
  • django/contrib/gis/geos/tests/test_geos.py

     
    22from django.contrib.gis.geos import *
    33from django.contrib.gis.geos.base import gdal, numpy, GEOSBase
    44from django.contrib.gis.tests.geometries import *
     5from django.contrib.gis.geos.libgeos import GEOS_PREPARE
    56
    67class GEOSTest(unittest.TestCase):
    78
     
    919920        for geom, merged in zip(ref_geoms, ref_merged):
    920921            self.assertEqual(merged, geom.merged)
    921922
     923    def test27_valid_reason(self):
     924        "Testing IsValidReason support"
     925        if not GEOS_PREPARE:
     926            print >>sys.stderr, "Skipping tests (GEOS < v3.1)"
     927            return
     928           
     929        g = GEOSGeometry("POINT(0 0)")
     930        self.assert_(g.valid)
     931        self.assert_(isinstance(g.valid_reason, basestring))
     932        self.assertEqual(g.valid_reason, "Valid Geometry")
     933       
     934        g = GEOSGeometry("LINESTRING(0 0, 0 0)")
     935        self.assert_(not g.valid)
     936        self.assert_(isinstance(g.valid_reason, basestring))
     937        self.assertEqual(g.valid_reason, "Too few points in geometry component[0 0]")
     938
    922939def suite():
    923940    s = unittest.TestSuite()
    924941    s.addTest(unittest.makeSuite(GEOSTest))
  • docs/ref/contrib/gis/geos.txt

     
    219219
    220220Returns a boolean indicating whether the geometry is valid.
    221221
     222.. attribute:: GEOSGeometry.valid_reason
     223
     224.. versionadded:: 1.3
     225
     226Returns a string describing the reason why a geometry is invalid.
     227
    222228.. attribute:: GEOSGeometry.srid
    223229
    224230Property that may be used to retrieve or set the SRID associated with the
Back to Top