î
ªÍ Xö  ã               @   s@   d  Z  d d l m Z d d l m Z Gd d „  d e ƒ Z d S)a  
The SpatialProxy object allows for lazy-geometries and lazy-rasters. The proxy
uses Python descriptors for instantiating and setting Geometry or Raster
objects corresponding to geographic model fields.

Thanks to Robert Coup for providing this functionality (see #4322).
é    )ÚDeferredAttribute)Úsixc                   sC   e  Z d  Z ‡  f d d †  Z d ‡  f d d † Z d d „  Z ‡  S)ÚSpatialProxyc                s2   | |  _  | |  _ t t |  ƒ j | j | ƒ d S)z€
        Proxy initializes on the given Geometry or Raster class (not an instance)
        and the corresponding field.
        N)Ú_fieldÚ_klassÚsuperr   Ú__init__Úattname)ÚselfÚklassÚfield)Ú	__class__© úP/home/ubuntu/projects/ifolica/build/django/django/contrib/gis/db/models/proxy.pyr      s    		zSpatialProxy.__init__Nc                s¼   | d k r |  Sy | j  |  j j } Wn- t k
 rV t t |  ƒ j | | ƒ } Yn Xt | |  j ƒ rr | } nF | d k sŠ | d k r“ d } n% |  j | ƒ } t	 | |  j j | ƒ | S)a  
        This accessor retrieves the geometry or raster, initializing it using
        the corresponding class specified during initialization and the value
        of the field. Currently, GEOS or OGR geometries as well as GDALRasters
        are supported.
        NÚ )
Ú__dict__r   r	   ÚKeyErrorr   r   Ú__get__Ú
isinstancer   Úsetattr)r
   ÚinstanceÚclsZ	geo_valueZgeo_obj)r   r   r   r      s     		zSpatialProxy.__get__c             C   s
  |  j  j } | d k rF | d k só t | t j t |  j f ƒ rF n­ t | |  j ƒ r£ t | j ƒ j ƒ  | k s | d k r£ | j	 d k ró |  j  j	 | _	 qó nP | d k só t | t j t j
 f ƒ rÎ n% t d | j j | t | ƒ f ƒ ‚ | | j |  j  j <| S)a  
        This accessor sets the proxied geometry or raster with the
        corresponding class specified during initialization.

        To set geometries, values of None, HEXEWKB, or WKT may be used.
        To set rasters, JSON or dict values may be used.
        ZRASTERNZGEOMETRYz6Cannot set %s SpatialProxy (%s) with value of type: %s)r   Z	geom_typer   r   Ústring_typesÚdictr   ÚstrÚupperZsridÚ
memoryviewÚ	TypeErrorr   Ú__name__Útyper   r	   )r
   r   ÚvalueZgtyper   r   r   Ú__set__2   s    	79(zSpatialProxy.__set__)r   Ú
__module__Ú__qualname__r   r   r!   r   r   )r   r   r      s   	r   N)Ú__doc__Zdjango.db.models.query_utilsr   Zdjango.utilsr   r   r   r   r   r   Ú<module>   s   