Django

Code

Changeset 5760

Show
Ignore:
Timestamp:
07/25/07 20:55:37 (1 year ago)
Author:
jbronn
Message:

gis: geos: added srid keyword and added psycopg2 adaptor routines.

Files:

Legend:

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

    r5749 r5760  
    1313import re 
    1414from warnings import warn 
    15 from django.contrib.gis.geos.libgeos import lgeos, GEOSPointer, HAS_NUMPY 
     15from django.contrib.gis.geos.libgeos import lgeos, GEOSPointer, HAS_NUMPY, ISQLQuote 
    1616from django.contrib.gis.geos.error import GEOSException, GEOSGeometryIndexError 
    1717from django.contrib.gis.geos.coordseq import GEOSCoordSeq, create_cs 
     
    2525     
    2626    #### Python 'magic' routines #### 
    27     def __init__(self, geo_input, input_type=False, parent=False): 
     27    def __init__(self, geo_input, input_type=False, parent=None, srid=None): 
    2828        """The constructor for GEOS geometry objects.  May take the following 
    2929        strings as inputs, WKT ("wkt"), HEXEWKB ("hex", PostGIS-specific canonical form). 
     
    7373            self._parent = GEOSPointer(0) 
    7474         
     75        # Setting the SRID, if given. 
     76        if srid and isinstance(srid, int): self.srid = srid 
     77 
    7578        # Setting the class type (e.g., 'Point', 'Polygon', etc.) 
    7679        self.__class__ = GEOS_CLASSES[self.geom_type] 
     
    127130        "Return the symmetric difference of this Geometry and the other." 
    128131        return self.sym_difference(other) 
     132 
     133    #### Psycopg2 database adaptor routines #### 
     134    def __conform__(self, proto): 
     135        # Does the given protocol conform to what Psycopg2 expects? 
     136        if proto == ISQLQuote:  
     137            return self 
     138        else: 
     139            raise GEOSException, 'Error implementing psycopg2 protocol.  Is psycopg2 installed?' 
     140 
     141    def getquoted(self): 
     142        "Returns a properly quoted string for use in PostgresSQL/PostGIS." 
     143        return "GeometryFromText('%s', %s)" % (self.wkt, self.srid or -1) 
    129144     
    130145    #### Coordinate Sequence Routines #### 
  • django/branches/gis/django/contrib/gis/geos/collections.py

    r5742 r5760  
    2929    _typeid = 7 
    3030 
    31     def __init__(self, *args): 
     31    def __init__(self, *args, **kwargs): 
    3232        self._ptr = GEOSPointer(0) # Initially NULL 
    3333        self._geoms = {} 
     
    6262 
    6363        # Calling the parent class, using the pointer returned from GEOS createCollection() 
    64         super(GeometryCollection, self).__init__(lgeos.GEOSGeom_createCollection(c_int(self._typeid), byref(geoms), c_uint(ngeom))
     64        super(GeometryCollection, self).__init__(lgeos.GEOSGeom_createCollection(c_int(self._typeid), byref(geoms), c_uint(ngeom)), **kwargs
    6565 
    6666    def __del__(self): 
     
    8686        # Checking the index and returning the corresponding GEOS geometry. 
    8787        self._checkindex(index) 
    88         return GEOSGeometry(self._geoms[index], parent=self._ptr
     88        return GEOSGeometry(self._geoms[index], parent=self._ptr, srid=self.srid
    8989 
    9090    def __iter__(self): 
  • django/branches/gis/django/contrib/gis/geos/geometries.py

    r5742 r5760  
    1515class Point(GEOSGeometry): 
    1616 
    17     def __init__(self, x, y=None, z=None): 
     17    def __init__(self, x, y=None, z=None, srid=None): 
    1818        """The Point object may be initialized with either a tuple, or individual 
    1919        parameters.  For example: 
     
    5757 
    5858        # Initializing from the geometry, and getting a Python object 
    59         super(Point, self).__init__(lgeos.GEOSGeom_createPoint(cs)
     59        super(Point, self).__init__(lgeos.GEOSGeom_createPoint(cs), srid=srid
    6060 
    6161    def __len__(self): 
     
    176176        else: 
    177177            func = lgeos.GEOSGeom_createLineString 
     178 
     179        # If SRID was passed in with the keyword arguments 
     180        srid = kwargs.get('srid', None) 
    178181        
    179182        # Calling the base geometry initialization with the returned pointer from the function. 
    180         super(LineString, self).__init__(func(cs._ptr.coordseq())
     183        super(LineString, self).__init__(func(cs._ptr.coordseq()), srid=srid
    181184 
    182185    def __getitem__(self, index): 
     
    236239# LinearRings are LineStrings used within Polygons. 
    237240class LinearRing(LineString): 
    238     def __init__(self, *args): 
     241    def __init__(self, *args, **kwargs): 
    239242        "Overriding the initialization function to set the ring keyword." 
    240         kwargs = {'ring' : True} 
     243        kwargs['ring'] = True # Setting the ring keyword argument to True 
    241244        super(LinearRing, self).__init__(*args, **kwargs) 
    242245 
    243246class Polygon(GEOSGeometry): 
    244247 
    245     def __init__(self, *args): 
     248    def __init__(self, *args, **kwargs): 
    246249        """Initializes on an exterior ring and a sequence of holes (both instances of LinearRings. 
    247250        All LinearRing instances used for creation will become owned by this Polygon. 
     
    281284 
    282285        # Calling with the GEOS createPolygon factory. 
    283         super(Polygon, self).__init__(lgeos.GEOSGeom_createPolygon(shell, byref(holes), c_uint(nholes))
     286        super(Polygon, self).__init__(lgeos.GEOSGeom_createPolygon(shell, byref(holes), c_uint(nholes)), **kwargs
    284287 
    285288    def __del__(self): 
     
    341344        # Returning the ring from the internal ring dictionary (have to 
    342345        #   add one to the index) 
    343         return GEOSGeometry(self._rings[ring_i+1], parent=self._ptr
     346        return GEOSGeometry(self._rings[ring_i+1], parent=self._ptr, srid=self.srid
    344347                                                         
    345348    #### Polygon Properties #### 
     
    357360    def get_ext_ring(self): 
    358361        "Gets the exterior ring of the Polygon." 
    359         return GEOSGeometry(self._rings[0], parent=self._ptr
     362        return GEOSGeometry(self._rings[0], parent=self._ptr, srid=self.srid
    360363 
    361364    def set_ext_ring(self): 
  • django/branches/gis/django/contrib/gis/geos/__init__.py

    r5742 r5760  
    3535from error import GEOSException, GEOSGeometryIndexError 
    3636 
    37 def fromstr(wkt_or_hex): 
     37def fromstr(wkt_or_hex, **kwargs): 
    3838    "Given a string value (wkt or hex), returns a GEOSGeometry object." 
    39     return GEOSGeometry(wkt_or_hex
     39    return GEOSGeometry(wkt_or_hex, **kwargs
    4040 
    4141def hex_to_wkt(hex): 
  • django/branches/gis/django/contrib/gis/geos/libgeos.py

    r5742 r5760  
    33  as the notice and error handler function callbacks (get called 
    44  when an error occurs in GEOS). 
     5 
     6  This module also houses GEOS Pointer utilities, including the 
     7  GEOSPointer class, get_pointer_arr(), GEOM_PTR, and init_from_geom(). 
    58""" 
    69 
     
    1518except ImportError: 
    1619    HAS_NUMPY = False 
     20 
     21# Psycopg2 supported? 
     22try: 
     23    from psycopg2.extensions import ISQLQuote 
     24except ImportError: 
     25    ISQLQuote = None 
    1726 
    1827# Setting the appropriate name for the GEOS-C library, depending on which 
     
    119128        else: return False 
    120129     
    121     ### Coordinate Sequence properties ### 
     130    ### Coordinate Sequence routines and properties ### 
    122131    def coordseq(self): 
    123132        "If the coordinate sequence pointer is NULL (0), an exception will be raised."