3
š(ƒh&  ã               @   sl   d dl mZ d dlmZ d dlmZmZ d dlmZ	 erHd dlm
Z
mZ G dd„ de	ƒZG dd	„ d	eƒZd
S )é    )Úunicode_literals)ÚHAS_GDAL)ÚSerializationErrorÚSerializerDoesNotExist)Ú
Serializer)ÚCoordTransformÚSpatialReferencec                   sP   e Zd ZdZ‡ fdd„Zdd„ Zdd„ Z‡ fdd	„Zd
d„ Z‡ fdd„Z	‡  Z
S )r   z<
    Convert a queryset to GeoJSON, http://geojson.org/
    c                sf   t t| ƒjƒ  | jjdd ƒ| _| jjddƒ| _| jd k	rb| jd k	rb| j| jkrbt| jƒ| jg | _d S )NÚgeometry_fieldÚsridiæ  )	Úsuperr   Ú_init_optionsZjson_kwargsÚpopr	   r
   Úselected_fieldsÚlist)Úself)Ú	__class__© úJ/tmp/pip-install-q3hcpn_q/Django/django/contrib/gis/serializers/geojson.pyr      s    zSerializer._init_optionsc             C   s$   | j ƒ  i | _| jjd| j ƒ d S )Nzg{"type": "FeatureCollection", "crs": {"type": "name", "properties": {"name": "EPSG:%d"}}, "features": [)r   Ú_ctsÚstreamÚwriter
   )r   r   r   r   Ústart_serialization   s
    zSerializer.start_serializationc             C   s   | j jdƒ d S )Nz]})r   r   )r   r   r   r   Úend_serialization    s    zSerializer.end_serializationc                sJ   t t| ƒj|ƒ d | _| jd krFx$|jjD ]}t|dƒr*|j| _P q*W d S )NZ	geom_type)	r   r   Ústart_objectÚ	_geometryr	   Ú_metaÚfieldsÚhasattrÚname)r   ÚobjÚfield)r   r   r   r   #   s    

zSerializer.start_objectc             C   sÌ   d| j dœ}| jd ks d| jkrBd|d krB|jjj|ƒ|d d< | jrÀ| jj| jkr®tshtd| j ƒ‚| jj| j	kr˜t
| jƒ}t| jj|ƒ| j	| jj< | jj| j	| jj ƒ t| jjƒ|d< nd |d< |S )NZFeature)ÚtypeÚ
propertiesÚpkr"   zAUnable to convert geometry to SRID %s when GDAL is not installed.Zgeometry)Ú_currentr   r   r#   Zvalue_to_stringr   r
   r   r   r   r   r   ÚsrsZ	transformÚevalZgeojson)r   r   Údatar%   r   r   r   Úget_dump_object-   s"    

zSerializer.get_dump_objectc                s0   |j | jkr|j|ƒ| _ntt| ƒj||ƒ d S )N)r   r	   Zvalue_from_objectr   r   r   Úhandle_field)r   r   r    )r   r   r   r)   E   s    zSerializer.handle_field)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r(   r)   Ú__classcell__r   r   )r   r   r      s   
r   c               @   s   e Zd Zdd„ ZdS )ÚDeserializerc             O   s   t dƒ‚d S )Nz*geojson is a serialization-only serializer)r   )r   ÚargsÚkwargsr   r   r   Ú__init__M   s    zDeserializer.__init__N)r*   r+   r,   r2   r   r   r   r   r/   L   s   r/   N)Ú
__future__r   Zdjango.contrib.gis.gdalr   Zdjango.core.serializers.baser   r   Zdjango.core.serializers.jsonr   ZJSONSerializerr   r   Úobjectr/   r   r   r   r   Ú<module>   s   ?