Django

Code

Changeset 5474

Show
Ignore:
Timestamp:
06/14/07 22:49:25 (1 year ago)
Author:
jbronn
Message:

gis: GEOSGeometry updates, improved initialization & tests, simplified some routines.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/gis/django/contrib/gis/geos/GEOSGeometry.py

    r5395 r5474  
    3333     c_char_p, c_double, c_float, c_int, c_uint, c_size_t 
    3434import os, sys 
     35from types import StringType, IntType, FloatType 
    3536 
    3637""" 
     
    121122 
    122123    #### Python 'magic' routines #### 
    123     def __init__(self, input, geom_type='wkt'): 
     124    def __init__(self, input, input_type='wkt'): 
    124125        "Takes an input and the type of the input for initialization." 
    125126 
    126         if geom_type == 'wkt': 
    127             # If the geometry is in WKT form 
    128             g = lgeos.GEOSGeomFromWKT(c_char_p(input)) 
    129         elif geom_type == 'hex': 
    130             # If the geometry is in HEX form. 
    131             sz = c_size_t(len(input)) 
    132             buf = create_string_buffer(input) 
    133             g = lgeos.GEOSGeomFromHEX_buf(buf, sz) 
    134         elif geom_type == 'geos': 
     127        if isinstance(input, StringType): 
     128            if input_type == 'wkt': 
     129                # If the geometry is in WKT form 
     130                g = lgeos.GEOSGeomFromWKT(c_char_p(input)) 
     131            elif input_type == 'hex': 
     132                # If the geometry is in HEX form. 
     133                sz = c_size_t(len(input)) 
     134                buf = create_string_buffer(input) 
     135                g = lgeos.GEOSGeomFromHEX_buf(buf, sz) 
     136            else: 
     137                raise GEOSException, 'GEOS input geometry type "%s" not supported!' % input_type 
     138        elif isinstance(input, IntType): 
    135139            # When the input is a C pointer (Python integer) 
    136140            g = input 
    137141        else: 
    138142            # Invalid geometry type. 
    139             raise GEOSException, 'Improper geometry input type!' 
    140  
    141         # If the geometry pointer is NULL (0), then raise an exception. 
     143            raise GEOSException, 'Improper geometry input type: %s' % str(type(input)) 
     144 
     145        # If the geometry pointer is NULL (0), raise an exception. 
    142146        if not g: 
    143             raise GEOSException, 'Invalid %s given!' % geom_type.upper() 
     147            raise GEOSException, 'Invalid %s given!' % input_type.upper() 
    144148        else: 
    145149            self._g = g 
    146150 
    147151        # Setting the class type (e.g. 'Point', 'Polygon', etc.) 
    148         self.__class__ = GEO_CLASSES[self.geom_type] 
     152        self.__class__ = GEOS_CLASSES[self.geom_type] 
    149153 
    150154    def __del__(self): 
     
    184188        if n == -1: raise GEOSException, 'Error getting number of coordinates!' 
    185189        else: return n 
     190 
     191    @property 
     192    def num_points(self): 
     193        "Returns the number points, or coordinates, in the geometry." 
     194        return self.num_coords 
    186195 
    187196    @property 
     
    328337        (Text from PostGIS documentation at ch. 6.1.3) 
    329338        """ 
    330         if type(width) != type(0.0): 
     339        if not isinstance(width, FloatType): 
    331340            raise TypeError, 'width parameter must be a float' 
    332         if type(quadsegs) != type(0): 
     341        if not isinstance(quadsegs, IntType): 
    333342            raise TypeError, 'quadsegs parameter must be an integer' 
    334343        b = lgeos.GEOSBuffer(self._g, c_double(width), c_int(quadsegs)) 
    335         return GEOSGeometry(b, 'geos'
     344        return GEOSGeometry(b
    336345 
    337346    @property 
    338347    def envelope(self): 
    339348        "Return the geometries bounding box." 
    340         e = lgeos.GEOSEnvelope(self._g) 
    341         return GEOSGeometry(e, 'geos') 
     349        return GEOSGeometry(lgeos.GEOSEnvelope(self._g)) 
    342350 
    343351    @property 
     
    346354        of highest dimension (since the lower-dimension geometries contribute zero 
    347355        "weight" to the centroid).""" 
    348         g = lgeos.GEOSGetCentroid(self._g) 
    349         return GEOSGeometry(g, 'geos') 
     356        return GEOSGeometry(lgeos.GEOSGetCentroid(self._g)) 
    350357 
    351358    @property 
    352359    def boundary(self): 
    353360        "Returns the boundary as a newly allocated Geometry object." 
    354         g = lgeos.GEOSBoundary(self._g) 
    355         return GEOSGeometry(g, 'geos') 
     361        return GEOSGeometry(lgeos.GEOSBoundary(self._g)) 
    356362 
    357363    @property 
    358364    def convex_hull(self): 
    359365        "Returns the smallest convex Polygon that contains all the points in the Geometry." 
    360         g = lgeos.GEOSConvexHull(self._g) 
    361         return GEOSGeometry(g, 'geos') 
     366        return GEOSGeometry(lgeos.GEOSConvexHull(self._g)) 
    362367 
    363368    @property 
    364369    def point_on_surface(self): 
    365370        "Computes an interior point of this Geometry." 
    366         g = lgeos.GEOSPointOnSurface(self._g) 
    367         return GEOSGeometry(g, 'geos') 
     371        return GEOSGeometry(lgeos.GEOSPointOnSurface(self._g)) 
    368372 
    369373    def relate(self, other): 
     
    374378        """Returns a Geometry representing the points making up this Geometry 
    375379        that do not make up other.""" 
    376         d = lgeos.GEOSDifference(self._g, other._g) 
    377         return GEOSGeometry(d, 'geos') 
     380        return GEOSGeometry(lgeos.GEOSDifference(self._g, other._g)) 
    378381 
    379382    def sym_difference(self, other): 
    380383        """Returns a set combining the points in this Geometry not in other, 
    381384        and the points in other not in this Geometry.""" 
    382         d = lgeos.GEOSSymDifference(self._g, other._g) 
    383         return GEOSGeometry(d, 'geos') 
     385        return GEOSGeometry(lgeos.GEOSSymDifference(self._g, other._g)) 
    384386 
    385387    def intersection(self, other): 
    386388        "Returns a Geometry representing the points shared by this Geometry and other." 
    387         i = lgeos.GEOSIntersection(self._g, other._g) 
    388         return GEOSGeometry(i, 'geos') 
     389        return GEOSGeometry(lgeos.GEOSIntersection(self._g, other._g)) 
    389390 
    390391    def union(self, other): 
    391392        "Returns a Geometry representing all the points in this Geometry and other." 
    392         u = lgeos.GEOSUnion(self._g, other._g) 
    393         return GEOSGeometry(u, 'geos') 
     393        return GEOSGeometry(lgeos.GEOSUnion(self._g, other._g)) 
    394394 
    395395    #### Other Routines #### 
     
    641641        "Returns the number of rings in this Polygon." 
    642642        return self.num_interior_rings + 1 
    643                          
    644643 
    645644    def get_interior_ring(self, ring_i): 
     
    699698        "For indexing on the multiple geometries." 
    700699        self._checkindex(index) 
    701         item = lgeos.GEOSGeom_clone(lgeos.GEOSGetGeometryN(self._g, c_int(index))) 
    702         return GEOSGeometry(item, 'geos'
     700        # Cloning the GEOS Geometry first, before returning it. 
     701        return GEOSGeometry(lgeos.GEOSGeom_clone(lgeos.GEOSGetGeometryN(self._g, c_int(index)))
    703702 
    704703    def __len__(self): 
     
    712711 
    713712# Class mapping dictionary 
    714 GEO_CLASSES = {'Point' : Point, 
    715                'Polygon' : Polygon, 
    716                'LineString' : LineString, 
    717                'LinearRing' : LinearRing, 
    718                'GeometryCollection' : GeometryCollection, 
    719                'MultiPoint' : MultiPoint, 
    720                'MultiLineString' : MultiLineString, 
    721                'MultiPolygon' : MultiPolygon, 
    722                
     713GEOS_CLASSES = {'Point' : Point, 
     714                'Polygon' : Polygon, 
     715                'LineString' : LineString, 
     716                'LinearRing' : LinearRing, 
     717                'GeometryCollection' : GeometryCollection, 
     718                'MultiPoint' : MultiPoint, 
     719                'MultiLineString' : MultiLineString, 
     720                'MultiPolygon' : MultiPolygon, 
     721               
  • django/branches/gis/django/contrib/gis/tests/test_geos.py

    r5395 r5474  
    8484            self.assertEqual(p.n_i, poly.num_interior_rings) 
    8585            self.assertEqual(p.n_i + 1, len(poly)) # Testing __len__ 
     86            self.assertEqual(p.n_p, poly.num_points) 
     87 
     88            # Area & Centroid 
     89            self.assertAlmostEqual(p.area, poly.area, 9) 
     90            self.assertAlmostEqual(p.centroid[0], poly.centroid.tuple[0], 9) 
     91            self.assertAlmostEqual(p.centroid[1], poly.centroid.tuple[1], 9) 
    8692 
    8793            # Testing the geometry equivalence 
     
    145151            self.assertAlmostEqual(l.centroid[0], ml.centroid.x, 9) 
    146152            self.assertAlmostEqual(l.centroid[1], ml.centroid.y, 9) 
    147  
    148153 
    149154            self.assertEqual(True, ml == GEOSGeometry(l.wkt)) 
  • django/branches/gis/django/contrib/gis/tests/test_with_swig.py

    r5008 r5474  
    44from geometries import swig_geoms as geos_geoms 
    55 
    6 class GeosTest(unittest.TestCase): 
     6class GeosTestWithSwig(unittest.TestCase): 
    77 
    88    def test001_hex_to_wkt(self): 
     
    2121        "Testing the centroid property." 
    2222        for g in geos_geoms: 
    23             wkt1 = (centroid(g.wkt, geom_type='wkt')).wkt 
     23            wkt1 = centroid(g.wkt, geom_type='wkt') 
    2424            wkt2 = geomToWKT((geomFromWKT(g.wkt)).getCentroid()) 
    2525            self.assertEqual(wkt1, wkt2) 
     
    6161def suite(): 
    6262    s = unittest.TestSuite() 
    63     s.addTest(unittest.makeSuite(GeosTest)) 
     63    s.addTest(unittest.makeSuite(GeosTestWithSwig)) 
    6464    return s 
    6565