o
    ó<âgf  ã                   @   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ƒ  |¡ 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ÚfieldÚ	load_func©Ú	__class__© úU/var/www/html/venv/lib/python3.10/site-packages/django/contrib/gis/db/models/proxy.pyr      s   
zSpatialProxy.__init__c                    sŠ   |du r| S z	|j | jj }W n ty   tƒ  ||¡}Y nw t|| jƒr*|}|S |du s2|dkr6d}|S |  |¡}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ÚclsÚ	geo_valueÚgeo_objr   r   r   r      s    ÿù
þzSpatialProxy.__get__c                 C   s’   | j j}|dkr|du st|tt| jfƒrn*t|| jƒr'|jdu r&| j j|_n|du s2t|ttfƒr3n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.
        ÚRASTERNz6Cannot set %s SpatialProxy (%s) with value of type: %s)r
   Ú	geom_typer   ÚstrÚdictr   ÚsridÚ
memoryviewÚ	TypeErrorr   Ú__name__Útyper   r   )r   r   ÚvalueÚgtyper   r   r   Ú__set__2   s"   	

€ÿÿzSpatialProxy.__set__)N)r"   Ú
__module__Ú__qualname__r   r   r&   Ú__classcell__r   r   r   r   r      s    	r   N)Ú__doc__Údjango.db.models.query_utilsr   r   r   r   r   r   Ú<module>   s    