Ticket #14318: geos_validreason.2.diff

File geos_validreason.2.diff, 4.6 KB (added by jbronn, 14 years ago)
  • django/contrib/gis/geos/geometry.py

    diff -r 6d3bd7133374 django/contrib/gis/geos/geometry.py
    a b  
    275275        "This property tests the validity of this Geometry."
    276276        return capi.geos_isvalid(self.ptr)
    277277
     278    @property
     279    def invalid_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/__init__.py

    diff -r 6d3bd7133374 django/contrib/gis/geos/prototypes/__init__.py
    a b  
    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 *
    2222
    2323# Predicates
    2424from django.contrib.gis.geos.prototypes.predicates import geos_hasz, geos_isempty, \
  • django/contrib/gis/geos/prototypes/misc.py

    diff -r 6d3bd7133374 django/contrib/gis/geos/prototypes/misc.py
    a b  
    33 ones that return the area, distance, and length.
    44"""
    55from ctypes import c_int, c_double, POINTER
    6 from django.contrib.gis.geos.libgeos import GEOM_PTR
    7 from django.contrib.gis.geos.prototypes.errcheck import check_dbl
     6from django.contrib.gis.geos.libgeos import GEOM_PTR, GEOS_PREPARE
     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
     11__all__ = ['geos_area', 'geos_distance', 'geos_length']
     12
    1013### ctypes generator function ###
    1114def dbl_from_geom(func, num_geom=1):
    1215    """
     
    2629geos_area = dbl_from_geom(GEOSFunc('GEOSArea'))
    2730geos_distance = dbl_from_geom(GEOSFunc('GEOSDistance'), num_geom=2)
    2831geos_length = dbl_from_geom(GEOSFunc('GEOSLength'))
     32
     33# Validity reason; only in GEOS 3.1+
     34if GEOS_PREPARE:
     35    geos_isvalidreason = GEOSFunc('GEOSisValidReason')
     36    geos_isvalidreason.argtypes = [GEOM_PTR]
     37    geos_isvalidreason.restype = geos_char_p
     38    geos_isvalidreason.errcheck = check_string
     39    __all__.append('geos_isvalidreason')
  • django/contrib/gis/geos/tests/test_geos.py

    diff -r 6d3bd7133374 django/contrib/gis/geos/tests/test_geos.py
    a b  
    11import ctypes, random, unittest, sys
    22from django.contrib.gis.geos import *
    33from django.contrib.gis.geos.base import gdal, numpy, GEOSBase
     4from django.contrib.gis.geos.libgeos import GEOS_PREPARE
    45from django.contrib.gis.geometry.test_data import TestDataMixin
    56
    67class GEOSTest(unittest.TestCase, TestDataMixin):
     
    917918        for geom, merged in zip(ref_geoms, ref_merged):
    918919            self.assertEqual(merged, geom.merged)
    919920
     921    def test27_valid_reason(self):
     922        "Testing IsValidReason support"
     923        # Skipping tests if GEOS < v3.1.
     924        if not GEOS_PREPARE: return
     925
     926        g = GEOSGeometry("POINT(0 0)")
     927        self.assert_(g.valid)
     928        self.assert_(isinstance(g.invalid_reason, basestring))
     929        self.assertEqual(g.invalid_reason, "Valid Geometry")
     930
     931        print "\nBEGIN - expecting GEOS_NOTICE; safe to ignore.\n"
     932
     933        g = GEOSGeometry("LINESTRING(0 0, 0 0)")
     934
     935        self.assert_(not g.valid)
     936        self.assert_(isinstance(g.invalid_reason, basestring))
     937        self.assertEqual(g.invalid_reason, "Too few points in geometry component[0 0]")
     938
     939        print "\nEND - expecting GEOS_NOTICE; safe to ignore.\n"
     940
    920941def suite():
    921942    s = unittest.TestSuite()
    922943    s.addTest(unittest.makeSuite(GEOSTest))
  • docs/ref/contrib/gis/geos.txt

    diff -r 6d3bd7133374 docs/ref/contrib/gis/geos.txt
    a b  
    219219
    220220Returns a boolean indicating whether the geometry is valid.
    221221
     222.. attribute:: GEOSGeometry.invalid_reason
     223
     224.. versionadded:: 1.3
     225
     226 Returns 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