3
©ôÁg2  ã               @   s$   d Z ddlmZ G d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).
é    )ÚDeferredAttributec                   s4   e Zd Zd‡ fdd„	Zd	‡ fdd„	Zdd„ Z‡  ZS )
ÚSpatialProxyNc                s    || _ |p|| _tƒ j|ƒ dS )zy
        Initialize on the given Geometry or Raster class (not an instance)
        and the corresponding field.
        N)Ú_klassÚ
_load_funcÚsuperÚ__init__)ÚselfÚklassÚfieldZ	load_func)Ú	__class__© ú`/var/www/tester-filtro-web/env/lib/python3.6/site-packages/django/contrib/gis/db/models/proxy.pyr      s    
zSpatialProxy.__init__c                sˆ   |dkr| S y|j | jj }W n" tk
r@   tƒ j||ƒ}Y nX t|| jƒrT|}n0|dksd|dkrjd}n| j|ƒ}t	|| jj|ƒ |S )zü
        Retrieve 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
   ÚattnameÚKeyErrorr   Ú__get__Ú
isinstancer   r   Úsetattr)r   ÚinstanceÚclsZ	geo_valueZgeo_obj)r   r   r   r      s    
zSpatialProxy.__get__c             C   s’   | j j}|dkr,|dks€t|tt| jfƒr,nTt|| jƒrN|jdkr€| j j|_n2|dks€t|ttfƒrfntd|j	j
|t|ƒf ƒ‚||j| j j< |S )zñ
        Retrieve the proxied geometry or raster with the corresponding class
        specified during initialization.

        To set geometries, use values of None, HEXEWKB, or WKT.
        To set rasters, use JSON or dict values.
        ZRASTERNz6Cannot set %s SpatialProxy (%s) with value of type: %s)r
   Z	geom_typer   ÚstrÚdictr   ZsridÚ
memoryviewÚ	TypeErrorr   Ú__name__Útyper   r   )r   r   ÚvalueZgtyper   r   r   Ú__set__1   s    	"
zSpatialProxy.__set__)N)N)r   Ú
__module__Ú__qualname__r   r   r   Ú__classcell__r   r   )r   r   r      s   	r   N)Ú__doc__Zdjango.db.models.query_utilsr   r   r   r   r   r   Ú<module>   s   