proxy.py :  » Web-Frameworks » Django » django » contrib » gis » db » models » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Web Frameworks » Django 
Django » django » contrib » gis » db » models » proxy.py
"""
The GeometryProxy object, allows for lazy-geometries.  The proxy uses
Python descriptors for instantiating and setting Geometry objects
corresponding to geographic model fields.

Thanks to Robert Coup for providing this functionality (see #4322).
"""

class GeometryProxy(object):
    def __init__(self, klass, field):
        """
        Proxy initializes on the given Geometry class (not an instance) and
        the GeometryField.
        """
        self._field = field
        self._klass = klass

    def __get__(self, obj, type=None):
        """
        This accessor retrieves the geometry, initializing it using the geometry
        class specified during initialization and the HEXEWKB value of the field.
        Currently, only GEOS or OGR geometries are supported.
        """
        if obj is None:
            # Accessed on a class, not an instance
            return self

        # Getting the value of the field.
        geom_value = obj.__dict__[self._field.attname]

        if isinstance(geom_value, self._klass):
            geom = geom_value
        elif (geom_value is None) or (geom_value==''):
            geom = None
        else:
            # Otherwise, a Geometry object is built using the field's contents,
            # and the model's corresponding attribute is set.
            geom = self._klass(geom_value)
            setattr(obj, self._field.attname, geom)
        return geom

    def __set__(self, obj, value):
        """
        This accessor sets the proxied geometry with the geometry class
        specified during initialization.  Values of None, HEXEWKB, or WKT may
        be used to set the geometry as well.
        """
        # The OGC Geometry type of the field.
        gtype = self._field.geom_type

        # The geometry type must match that of the field -- unless the
        # general GeometryField is used.
        if isinstance(value, self._klass) and (str(value.geom_type).upper() == gtype or gtype == 'GEOMETRY'):
            # Assigning the SRID to the geometry.
            if value.srid is None: value.srid = self._field.srid
        elif value is None or isinstance(value, (basestring, buffer)):
            # Set with None, WKT, HEX, or WKB
            pass
        else:
            raise TypeError('cannot set %s GeometryProxy with value of type: %s' % (obj.__class__.__name__, type(value)))

        # Setting the objects dictionary with the value, and returning.
        obj.__dict__[self._field.attname] = value
        return value
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.