
^Q\              #   @   s  d  d l  Z  d  d l Z y d  d l m Z Wn e k
 r@ Yn Xd  d l m Z d  d l m Z d  d l	 m
 Z
 d  d l m Z d  d l m Z y d  d l m Z WnW e e f k
 r y d  d l m Z Wn( e e f k
 r d	 d l m Z Yn XYn Xy d  d
 l m Z Wn( e e f k
 r=d	 d
 l m Z Yn Xd	 d l m Z d	 d l m Z d	 d l m Z Gd d   d e  Z Gd d   d e e  Z Gd d   d e  Z d S)    N)Intersection)ListView)method_decorator)	gzip_page)SuspiciousOperation)ImproperlyConfigured)Polygon   )
PointField)HttpJSONResponse)
Serializer)GEOJSON_DEFAULT_SRIDc               @   s^   e  Z d  Z d Z e Z g  Z d Z d Z e	 Z
 d Z d Z d Z d Z d Z d d   Z d S)GeoJSONResponseMixinz@
    A mixin that can be used to render a GeoJSON response.
    NZgeomFc             K   s   t    } |  j |   } |  j   } t d |  j d |  j d |  j d |  j d |  j d |  j	 d |  j
 d |  j d	 |  j  	} | j | d
 | d d | | S)zV
        Returns a JSON response, transforming 'context' to make the payload.
        
properties	precisionsimplifysridgeometry_fieldforce2dbbox	bbox_autouse_natural_keysstreamZensure_asciiF)GeoJSONSerializerresponse_classget_querysetdictr   r   r   r   r   r   r   r   r   Z	serialize)selfcontextZresponse_kwargsZ
serializerresponseZquerysetoptions r!   D/var/www/dbchiro/venv/lib/python3.4/site-packages/djgeojson/views.pyrender_to_response9   s    								z'GeoJSONResponseMixin.render_to_response)__name__
__module____qualname____doc__r   r   r   r   r   r   r   r   r   r   r   r   r#   r!   r!   r!   r"   r   !   s   r   c                   s4   e  Z d  Z d Z e e    f d d    Z   S)GeoJSONLayerViewz5
    A generic view to serve a model as a layer.
    c                s   t  t |   j | |   S)N)superr(   dispatch)r   argskwargs)	__class__r!   r"   r*   S   s    zGeoJSONLayerView.dispatch)r$   r%   r&   r'   r   r   r*   r!   r!   )r-   r"   r(   O   s   r(   c                   sX   e  Z d  Z d Z d Z d Z d Z d Z d d   Z d d   Z	   f d	 d
   Z
   S)TiledGeoJSONLayerView   i  TNc             C   s{   |  j  d k s t d   d | } | | d d } t j t j t j d d | |   } t j |  } | | f S)a2  
        This returns the NW-corner of the square. Use the function
        with xtile+1 and/or ytile+1 to get the other corners.
        With xtile+0.5 & ytile+0.5 it will return the center of the tile.
        http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames#Tile_numbers_to_lon..2Flat._2
        i  z(Custom tile projection not supported yetg       @g     v@g     f@r	      )	tile_sridAssertionErrormathatansinhpidegrees)r   ZxtileZytileZzoomnZlon_degZlat_radZlat_degr!   r!   r"   
tile_coord`   s    
+z TiledGeoJSONLayerView.tile_coordc             C   s   y3 d d   |  j  d |  j  d |  j  d f D SWn{ t t f k
 r y3 d d   |  j d |  j d |  j d	 f D SWn- t t t f k
 r d
 } t |   Yn XYn Xd  S)Nc             S   s   g  |  ] } t  |   q Sr!   )int).0vr!   r!   r"   
<listcomp>p   s   	 z5TiledGeoJSONLayerView._parse_args.<locals>.<listcomp>r   r	   r0   c             S   s   g  |  ] } t  |   q Sr!   )r:   )r;   r<   r!   r!   r"   r=   s   s   	 zxyz1Tile (z, x, y) parameters could not be processed.)r+   
ValueError
IndexErrorr,   	TypeErrorKeyErrorr   )r   Z	error_msgr!   r!   r"   _parse_argsn   s    3
z!TiledGeoJSONLayerView._parse_argsc                s  |  j    \ |  _ |  _ |  _ |  j |  j |  j |  j  } |  j |  j d |  j d |  j  } t | | d | d f | | d | d f | f  } t t |   j   } | j	 i | d |  j
 6  } | j |  _ |  j p i  } |  j } | j |  |  _ x; |  j d k r>| d k  r>| d 7} | j |  |  _ qW| j j j |  j
  } |  j omt | t  |  _ |  j rt j d k  r| j |  } n | j d t |  j
 |   } d |  _
 n  | S)	zG
        Inspired by Glen Roberton's django-geojson-tiles view
        r	   r   z%s__intersectsN    	   intersection)r	   rG   )rE   r>   r?   r@   r9   r   r)   r.   r   filterr   Zextentr   simplificationsgetr   ZmodelZ_meta	get_fieldtrim_to_boundary
isinstancer
   djangoVERSIONrH   Zannotater   )r   ZnwZser   qsrJ   r>   Zmodel_field)r-   r!   r"   r   {   s0    & 		
		z"TiledGeoJSONLayerView.get_queryset)r$   r%   r&   widthheightr1   rM   rJ   r9   rE   r   r!   r!   )r-   r"   r.   X   s   r.   ) r3   rO   Z&django.contrib.gis.db.models.functionsr   ImportErrorZdjango.views.genericr   Zdjango.utils.decoratorsr   Zdjango.views.decorators.gzipr   Zdjango.core.exceptionsr   r   Zdjango.contrib.gis.geosr   Z django.contrib.gis.geos.geometryZnogeosZdjango.contrib.gis.db.modelsr
   fieldshttpr   Zserializersr   r    r   objectr   r(   r.   r!   r!   r!   r"   <module>   s6   .	