Django

Code

Changeset 5805

Show
Ignore:
Timestamp:
08/05/07 20:13:49 (1 year ago)
Author:
jbronn
Message:

gis: geos: added support for KML; added coords property to Point.

Files:

Legend:

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

    r5795 r5805  
    205205    def getquoted(self): 
    206206        "Returns a properly quoted string for use in PostgresSQL/PostGIS." 
    207         # GeometryFromText() is ST_GeometryFromText() in PostGIS >= 1.2.2 
    208         return "%sGeometryFromText('%s', %s)" % (GEOM_FUNC_PREFIX, self.wkt, self.srid or -1) 
     207        # GeomFromText() is ST_GeomFromText() in PostGIS >= 1.2.2 to correspond 
     208        #  to SQL/MM ISO standard. 
     209        return "%sGeomFromText('%s', %s)" % (GEOM_FUNC_PREFIX, self.wkt, self.srid or -1) 
    209210     
    210211    #### Coordinate Sequence Routines #### 
     
    385386        h = lgeos.GEOSGeomToHEX_buf(self._ptr(), byref(sz)) 
    386387        return string_at(h, sz.value) 
     388 
     389    @property 
     390    def kml(self): 
     391        "Returns the KML representation of this Geometry." 
     392        gtype = self.geom_type 
     393        return '<%s>%s</%s>' % (gtype, self.coord_seq.kml, gtype) 
    387394 
    388395    #### Topology Routines #### 
  • django/branches/gis/django/contrib/gis/geos/collections.py

    r5786 r5805  
    8686        # Creating a new geometry collection from the list, and 
    8787        #  re-assigning the pointers. 
    88         new_collection = self.__class__(*new_geoms
     88        new_collection = self.__class__(*new_geoms, **{'srid':self.srid}
    8989        self._reassign(new_collection) 
    9090         
     
    115115            self._geoms[i] = GEOSPointer(lgeos.GEOSGetGeometryN(self._ptr(), c_int(i))) 
    116116 
     117    @property 
     118    def kml(self): 
     119        "Returns the KML for this Geometry Collection." 
     120        kml = '<MultiGeometry>' 
     121        for g in self: kml += g.kml 
     122        return kml + '</MultiGeometry>' 
     123 
    117124# MultiPoint, MultiLineString, and MultiPolygon class definitions. 
    118125class MultiPoint(GeometryCollection):  
  • django/branches/gis/django/contrib/gis/geos/coordseq.py

    r5742 r5805  
    163163 
    164164    @property 
     165    def kml(self): 
     166        "Returns the KML representation for the coordinates." 
     167        # Getting the substitution string depending on whether the coordinates have 
     168        #  a Z dimension. 
     169        if self.hasz: substr = '%s,%s,%s ' 
     170        else: substr = '%s,%s,0 ' 
     171        kml = '<coordinates>' 
     172        for i in xrange(len(self)): 
     173            kml += substr % self[i] 
     174        return kml.strip() + '</coordinates>' 
     175 
     176    @property 
    165177    def tuple(self): 
    166178        "Returns a tuple version of this coordinate sequence." 
  • django/branches/gis/django/contrib/gis/geos/geometries.py

    r5786 r5805  
    110110 
    111111    ### Tuple setting and retrieval routines. ### 
    112     def get_tuple(self): 
     112    def get_coords(self): 
    113113        "Returns a tuple of the point." 
    114114        return self._cs.tuple 
    115115 
    116     def set_tuple(self, tup): 
     116    def set_coords(self, tup): 
    117117        "Sets the coordinates of the point with the given tuple." 
    118118        self._cs[0] = tup 
    119119     
    120     # The tuple property 
    121     tuple = property(get_tuple, set_tuple) 
     120    # The tuple and coords properties 
     121    tuple = property(get_coords, set_coords) 
     122    coords = property(get_coords, set_coords) 
    122123 
    123124class LineString(GEOSGeometry): 
     
    333334 
    334335        # Constructing the new Polygon with the ring parameters, and reassigning the internals. 
    335         new_poly = Polygon(*new_rings
     336        new_poly = Polygon(*new_rings, **{'srid':self.srid}
    336337        self._reassign(new_poly) 
    337338 
     
    402403        return tuple(self.__getitem__(i).tuple for i in xrange(self.__len__())) 
    403404 
     405    @property 
     406    def kml(self): 
     407        "Returns the KML representation of this Polygon." 
     408        inner_kml = '' 
     409        if self.num_interior_rings > 0:  
     410            for i in xrange(self.num_interior_rings): 
     411                inner_kml += "<innerBoundaryIs>%s</innerBoundaryIs>" % self[i+1].kml 
     412        return "<Polygon><outerBoundaryIs>%s</outerBoundaryIs>%s</Polygon>" % (self[0].kml, inner_kml) 
  • django/branches/gis/django/contrib/gis/tests/geometries.py

    r5786 r5805  
    3535 
    3636# WKT Output 
    37 wkt_out = (TestGeom('POINT(110 130)', ewkt='POINT (110.0000000000000000 130.0000000000000000)'), 
    38            TestGeom('LINESTRING(40 40, 50 130, 130 130)', ewkt='LINESTRING (40.0000000000000000 40.0000000000000000, 50.0000000000000000 130.0000000000000000, 130.0000000000000000 130.0000000000000000)'), 
    39            TestGeom('POLYGON((150 150, 410 150, 280 20, 20 20, 150 150),(170 120, 330 120, 260 50, 100 50, 170 120))', ewkt='POLYGON ((150.0000000000000000 150.0000000000000000, 410.0000000000000000 150.0000000000000000, 280.0000000000000000 20.0000000000000000, 20.0000000000000000 20.0000000000000000, 150.0000000000000000 150.0000000000000000), (170.0000000000000000 120.0000000000000000, 330.0000000000000000 120.0000000000000000, 260.0000000000000000 50.0000000000000000, 100.0000000000000000 50.0000000000000000, 170.0000000000000000 120.0000000000000000))'), 
    40            TestGeom('MULTIPOINT(10 80, 110 170, 110 120)', ewkt='MULTIPOINT (10.0000000000000000 80.0000000000000000, 110.0000000000000000 170.0000000000000000, 110.0000000000000000 120.0000000000000000)'), 
    41            TestGeom('MULTILINESTRING((110 100, 40 30, 180 30),(170 30, 110 90, 50 30))', ewkt='MULTILINESTRING ((110.0000000000000000 100.0000000000000000, 40.0000000000000000 30.0000000000000000, 180.0000000000000000 30.0000000000000000), (170.0000000000000000 30.0000000000000000, 110.0000000000000000 90.0000000000000000, 50.0000000000000000 30.0000000000000000))'), 
    42            TestGeom('MULTIPOLYGON(((110 110, 70 200, 150 200, 110 110),(110 110, 100 180, 120 180, 110 110)),((110 110, 150 20, 70 20, 110 110),(110 110, 120 40, 100 40, 110 110)))', ewkt='MULTIPOLYGON (((110.0000000000000000 110.0000000000000000, 70.0000000000000000 200.0000000000000000, 150.0000000000000000 200.0000000000000000, 110.0000000000000000 110.0000000000000000), (110.0000000000000000 110.0000000000000000, 100.0000000000000000 180.0000000000000000, 120.0000000000000000 180.0000000000000000, 110.0000000000000000 110.0000000000000000)), ((110.0000000000000000 110.0000000000000000, 150.0000000000000000 20.0000000000000000, 70.0000000000000000 20.0000000000000000, 110.0000000000000000 110.0000000000000000), (110.0000000000000000 110.0000000000000000, 120.0000000000000000 40.0000000000000000, 100.0000000000000000 40.0000000000000000, 110.0000000000000000 110.0000000000000000)))'), 
    43            TestGeom('GEOMETRYCOLLECTION(POINT(110 260), LINESTRING(110 0, 110 60))', ewkt='GEOMETRYCOLLECTION (POINT (110.0000000000000000 260.0000000000000000), LINESTRING (110.0000000000000000 0.0000000000000000, 110.0000000000000000 60.0000000000000000))'), 
     37wkt_out = (TestGeom('POINT(110 130)', ewkt='POINT (110.0000000000000000 130.0000000000000000)', kml='<Point><coordinates>110.0,130.0,0</coordinates></Point>'), 
     38           TestGeom('LINESTRING(40 40, 50 130, 130 130)', ewkt='LINESTRING (40.0000000000000000 40.0000000000000000, 50.0000000000000000 130.0000000000000000, 130.0000000000000000 130.0000000000000000)', kml='<LineString><coordinates>40.0,40.0,0 50.0,130.0,0 130.0,130.0,0</coordinates></LineString>'), 
     39           TestGeom('POLYGON((150 150, 410 150, 280 20, 20 20, 150 150),(170 120, 330 120, 260 50, 100 50, 170 120))', ewkt='POLYGON ((150.0000000000000000 150.0000000000000000, 410.0000000000000000 150.0000000000000000, 280.0000000000000000 20.0000000000000000, 20.0000000000000000 20.0000000000000000, 150.0000000000000000 150.0000000000000000), (170.0000000000000000 120.0000000000000000, 330.0000000000000000 120.0000000000000000, 260.0000000000000000 50.0000000000000000, 100.0000000000000000 50.0000000000000000, 170.0000000000000000 120.0000000000000000))', kml='<Polygon><outerBoundaryIs><LinearRing><coordinates>150.0,150.0,0 410.0,150.0,0 280.0,20.0,0 20.0,20.0,0 150.0,150.0,0</coordinates></LinearRing></outerBoundaryIs><innerBoundaryIs><LinearRing><coordinates>170.0,120.0,0 330.0,120.0,0 260.0,50.0,0 100.0,50.0,0 170.0,120.0,0</coordinates></LinearRing></innerBoundaryIs></Polygon>'), 
     40           TestGeom('MULTIPOINT(10 80, 110 170, 110 120)', ewkt='MULTIPOINT (10.0000000000000000 80.0000000000000000, 110.0000000000000000 170.0000000000000000, 110.0000000000000000 120.0000000000000000)', kml='<MultiGeometry><Point><coordinates>10.0,80.0,0</coordinates></Point><Point><coordinates>110.0,170.0,0</coordinates></Point><Point><coordinates>110.0,120.0,0</coordinates></Point></MultiGeometry>'), 
     41           TestGeom('MULTILINESTRING((110 100, 40 30, 180 30),(170 30, 110 90, 50 30))', ewkt='MULTILINESTRING ((110.0000000000000000 100.0000000000000000, 40.0000000000000000 30.0000000000000000, 180.0000000000000000 30.0000000000000000), (170.0000000000000000 30.0000000000000000, 110.0000000000000000 90.0000000000000000, 50.0000000000000000 30.0000000000000000))', kml='<MultiGeometry><LineString><coordinates>110.0,100.0,0 40.0,30.0,0 180.0,30.0,0</coordinates></LineString><LineString><coordinates>170.0,30.0,0 110.0,90.0,0 50.0,30.0,0</coordinates></LineString></MultiGeometry>'), 
     42           TestGeom('MULTIPOLYGON(((110 110, 70 200, 150 200, 110 110),(110 110, 100 180, 120 180, 110 110)),((110 110, 150 20, 70 20, 110 110),(110 110, 120 40, 100 40, 110 110)))', ewkt='MULTIPOLYGON (((110.0000000000000000 110.0000000000000000, 70.0000000000000000 200.0000000000000000, 150.0000000000000000 200.0000000000000000, 110.0000000000000000 110.0000000000000000), (110.0000000000000000 110.0000000000000000, 100.0000000000000000 180.0000000000000000, 120.0000000000000000 180.0000000000000000, 110.0000000000000000 110.0000000000000000)), ((110.0000000000000000 110.0000000000000000, 150.0000000000000000 20.0000000000000000, 70.0000000000000000 20.0000000000000000, 110.0000000000000000 110.0000000000000000), (110.0000000000000000 110.0000000000000000, 120.0000000000000000 40.0000000000000000, 100.0000000000000000 40.0000000000000000, 110.0000000000000000 110.0000000000000000)))', kml='<MultiGeometry><Polygon><outerBoundaryIs><LinearRing><coordinates>110.0,110.0,0 70.0,200.0,0 150.0,200.0,0 110.0,110.0,0</coordinates></LinearRing></outerBoundaryIs><innerBoundaryIs><LinearRing><coordinates>110.0,110.0,0 100.0,180.0,0 120.0,180.0,0 110.0,110.0,0</coordinates></LinearRing></innerBoundaryIs></Polygon><Polygon><outerBoundaryIs><LinearRing><coordinates>110.0,110.0,0 150.0,20.0,0 70.0,20.0,0 110.0,110.0,0</coordinates></LinearRing></outerBoundaryIs><innerBoundaryIs><LinearRing><coordinates>110.0,110.0,0 120.0,40.0,0 100.0,40.0,0 110.0,110.0,0</coordinates></LinearRing></innerBoundaryIs></Polygon></MultiGeometry>'), 
     43           TestGeom('GEOMETRYCOLLECTION(POINT(110 260), LINESTRING(110 0, 110 60))', ewkt='GEOMETRYCOLLECTION (POINT (110.0000000000000000 260.0000000000000000), LINESTRING (110.0000000000000000 0.0000000000000000, 110.0000000000000000 60.0000000000000000))', kml='<MultiGeometry><Point><coordinates>110.0,260.0,0</coordinates></Point><LineString><coordinates>110.0,0.0,0 110.0,60.0,0</coordinates></LineString></MultiGeometry>'), 
    4444           ) 
    4545 
  • django/branches/gis/django/contrib/gis/tests/test_geos.py

    r5786 r5805  
    2323            self.assertEqual(g.hex, geom.hex) 
    2424 
    25     def test01c_errors(self): 
     25    def test01c_kml(self): 
     26        "Testing KML output." 
     27        for tg in wkt_out: 
     28            geom = fromstr(tg.wkt) 
     29            kml = getattr(tg, 'kml', False) 
     30            if kml: self.assertEqual(kml, geom.kml) 
     31 
     32    def test01d_errors(self): 
    2633        "Testing the Error handlers." 
    2734        print "\nBEGIN - expecting GEOS_ERROR; safe to ignore.\n" 
     
    5663                self.assertEqual(p.z, pnt.tuple[2], 9) 
    5764                tup_args = (p.x, p.y, p.z) 
    58                 set_tup = (2.71, 3.14, 5.23) 
     65                set_tup1 = (2.71, 3.14, 5.23) 
     66                set_tup2 = (5.23, 2.71, 3.14) 
    5967            else: 
    6068                self.assertEqual(False, pnt.hasz) 
    6169                self.assertEqual(None, pnt.z) 
    6270                tup_args = (p.x, p.y) 
    63                 set_tup = (2.71, 3.14) 
     71                set_tup1 = (2.71, 3.14) 
     72                set_tup2 = (3.14, 2.71) 
    6473 
    6574            # Centroid operation on point should be point itself 
     
    7887            self.assertEqual(2.71, pnt.x) 
    7988 
    80             # Setting via the tuple property 
    81             pnt.tuple = set_tup 
    82             self.assertEqual(set_tup, pnt.tuple) 
    83  
     89            # Setting via the tuple/coords property 
     90            pnt.tuple = set_tup1 
     91            self.assertEqual(set_tup1, pnt.tuple) 
     92            pnt.coords = set_tup2 
     93            self.assertEqual(set_tup2, pnt.coords) 
     94             
    8495            prev = pnt # setting the previous geometry 
    8596 
     
    308319        for p in pts: 
    309320            self.assertRaises(GEOSException, str, p) # tests p's geometry pointer 
    310             self.assertRaises(GEOSException, p.get_tuple) # tests p's coordseq pointer 
     321            self.assertRaises(GEOSException, p.get_coords) # tests p's coordseq pointer 
    311322 
    312323        # Now doing this with a GeometryCollection