
 X~                 @   s  d  d l  Z  d  d l m Z m Z m Z m Z d  d l m Z d  d l m	 Z	 m
 Z
 m Z m Z d  d l m Z d  d l m Z m Z d  d l m Z d  d l m Z d  d	 l m Z d  d
 l m Z d  d l m Z d  d l m Z d  d l m Z Gd d   d e  Z  d S)    N)GeometryFieldLineStringField
PointFieldget_srid_info)	GISLookup)	AreaFieldDistanceField	GeomFieldGMLField)Geometry)AreaDistance)connections)
LOOKUP_SEP)RawSQL)Field)QuerySet)six)RemovedInDjango20Warningc               @   s  e  Z d  Z d Z d d d  Z d d   Z d d   Z d	 d
   Z d d   Z d d   Z	 d d d d d  Z
 d d d  Z d d d d  Z d d   Z d d   Z d d   Z d d    Z d! d"   Z d# d$   Z d% d&   Z d' d(   Z d) d*   Z d+ d, d-  Z d. d/   Z d d d0 d1  Z d2 d3   Z d+ d4 d5  Z d6 d7 d8  Z d9 d:   Z d; d; d; d< d=  Z d; d; d> d?  Z d; d d d@ dA  Z d dB dC  Z  d dD dE  Z! dF dG   Z" d; dH dI  Z# d; d; dJ dK  Z$ d; S)LGeoQuerySetzThe Geographic QuerySet.g?c             K   s  |  j  d d | j d  \ } } i | d 6| d 6d d 6} t |  j } | j } | j r d | d <| | d d	 <t d
  | d <nq | j s | j r | j	 r t d
  | d <q | j
 |  s t t j | j |    | d <q t d   n  |  j d | |  S)z~
        Returns the area of the geographic field in an `area` attribute on
        each element of this GeoQuerySet.
        area
field_nameprocedure_args	geo_fieldFsetupz%(geo_col)s,%(tolerance)sprocedure_fmt	toleranceZsq_mselect_fieldz2Area on geodetic coordinate systems not supported.)_spatial_setupgetr   dbopsoracler   postgis
spatialite	geographygeodeticr   unit_attname
units_name	Exception_spatial_attribute)selfr   kwargsr   r   s
connectionbackend r0   P/home/ubuntu/projects/ifolica/build/django/django/contrib/gis/db/models/query.pyr      s$    

		
	%zGeoQuerySet.areac             K   s   |  j  d |  S)z
        Returns the centroid of the geographic field in a `centroid`
        attribute on each element of this GeoQuerySet.
        centroid)_geom_attribute)r+   r,   r0   r0   r1   r2   8   s    zGeoQuerySet.centroidc             K   s   |  j  d | |  S)z
        Returns the spatial difference of the geographic field in a `difference`
        attribute on each element of this GeoQuerySet.
        
difference)_geomset_attribute)r+   geomr,   r0   r0   r1   r4   ?   s    zGeoQuerySet.differencec             K   s   |  j  d | |  S)a~  
        Returns the distance from the given geographic field name to the
        given geometry in a `distance` attribute on each element of the
        GeoQuerySet.

        Keyword Arguments:
         `spheroid`  => If the geometry field is geodetic and PostGIS is
                        the spatial database, then the more accurate
                        spheroid calculation will be used instead of the
                        quicker sphere calculation.

         `tolerance` => Used only for Oracle. The tolerance is
                        in meters -- a default of 5 centimeters (0.05)
                        is used.
        distance)_distance_attribute)r+   r6   r,   r0   r0   r1   r7   F   s    zGeoQuerySet.distancec             K   s   |  j  d |  S)z
        Returns a Geometry representing the bounding box of the
        Geometry field in an `envelope` attribute on each element of
        the GeoQuerySet.
        envelope)r3   )r+   r,   r0   r0   r1   r9   X   s    zGeoQuerySet.envelopec             K   s   |  j  d |  S)z
        Returns a modified version of the Polygon/MultiPolygon in which
        all of the vertices follow the Right-Hand-Rule.  By default,
        this is attached as the `force_rhr` attribute on each element
        of the GeoQuerySet.
        	force_rhr)r3   )r+   r,   r0   r0   r1   r:   `   s    zGeoQuerySet.force_rhr   Fc             K   s   t  |  j j } | j s( t d   n  t | t j  sI t d   n  d } | rd | rd d } n | rs d } n | r d } n  i d d 6i | d	 6| d
 6d 6d d 6} |  j	 d | |  S)a\  
        Returns a GeoJSON representation of the geometry field in a `geojson`
        attribute on each element of the GeoQuerySet.

        The `crs` and `bbox` keywords may be set to True if the user wants
        the coordinate reference system and the bounding box to be included
        in the GeoJSON representation of the geometry.
        z:Only PostGIS and SpatiaLite support GeoJSON serialization.z.Precision keyword must be set with an integer.r            ZGeoJSONdesc	precisionoptionsr   z%%(geo_col)s,%(precision)s,%(options)sr   geojson)
r   r    r!   rB   NotImplementedError
isinstancer   integer_types	TypeErrorr*   )r+   r@   ZcrsZbboxr,   r/   rA   r-   r0   r0   r1   rB   i   s     					

zGeoQuerySet.geojson   c             K   s5   i d d 6i | d 6d 6d d 6} |  j  d | |  S)a  
        Returns a GeoHash representation of the given field in a `geohash`
        attribute on each element of the GeoQuerySet.

        The `precision` keyword may be used to custom the number of
        _characters_ used in the output GeoHash, the default is 20.
        ZGeoHashr?   r@   r   z%(geo_col)s,%(precision)sr   geohash)r*   )r+   r@   r,   r-   r0   r0   r1   rH      s    

zGeoQuerySet.geohashr>   c             K   s   t  |  j j } i d d 6i | d 6d 6} | j rY d | d <i | d 6| d 6| d <n  | j rr t   | d <n  |  j d	 | |  S)
z
        Returns GML representation of the given field in a `gml` attribute
        on each element of the GeoQuerySet.
        ZGMLr?   r@   r   z%%(version)s,%(geo_col)s,%(precision)sr   versionr   gml)r   r    r!   r#   r"   r
   r*   )r+   r@   rI   r,   r/   r-   r0   r0   r1   rJ      s    	
	zGeoQuerySet.gmlc             K   s   |  j  d | |  S)z
        Returns the spatial intersection of the Geometry field in
        an `intersection` attribute on each element of this
        GeoQuerySet.
        intersection)r5   )r+   r6   r,   r0   r0   r1   rK      s    zGeoQuerySet.intersectionc             K   sA   i d d 6d d 6i | j  d d  d 6d 6} |  j d | |  S)	z
        Returns KML representation of the geometry field in a `kml`
        attribute on each element of this GeoQuerySet.
        ZKMLr?   z%(geo_col)s,%(precision)sr   r@   r;   r   kml)popr*   )r+   r,   r-   r0   r0   r1   rL      s    
zGeoQuerySet.kmlc             K   s   |  j  d d |  S)z
        Returns the length of the geometry field as a `Distance` object
        stored in a `length` attribute on each element of this GeoQuerySet.
        lengthN)r8   )r+   r,   r0   r0   r1   rN      s    zGeoQuerySet.lengthc             K   s   |  j  d i  |  S)z
        Returns the memory size (number of bytes) that the geometry field takes
        in a `mem_size` attribute  on each element of this GeoQuerySet.
        mem_size)r*   )r+   r,   r0   r0   r1   rO      s    zGeoQuerySet.mem_sizec             K   s   |  j  d i  |  S)z
        Returns the number of geometries if the field is a
        GeometryCollection or Multi* Field in a `num_geom`
        attribute on each element of this GeoQuerySet; otherwise
        the sets with None.
        num_geom)r*   )r+   r,   r0   r0   r1   rP      s    zGeoQuerySet.num_geomc             K   s   |  j  d i  |  S)z
        Returns the number of points in the first linestring in the
        Geometry field in a `num_points` attribute on each element of
        this GeoQuerySet; otherwise sets with None.
        
num_points)r*   )r+   r,   r0   r0   r1   rQ      s    zGeoQuerySet.num_pointsc             K   s   |  j  d d |  S)z
        Returns the perimeter of the geometry field as a `Distance` object
        stored in a `perimeter` attribute on each element of this GeoQuerySet.
        	perimeterN)r8   )r+   r,   r0   r0   r1   rR      s    zGeoQuerySet.perimeterc             K   s   |  j  d |  S)z
        Returns a Point geometry guaranteed to lie on the surface of the
        Geometry field in a `point_on_surface` attribute on each element
        of this GeoQuerySet; otherwise sets with None.
        point_on_surface)r3   )r+   r,   r0   r0   r1   rS      s    zGeoQuerySet.point_on_surfacec             K   sS   i t    d 6} | j d d  t |  j j j r@ t | d <n  |  j d | |  S)z
        Reverses the coordinate order of the geometry, and attaches as a
        `reverse` attribute on each element of this GeoQuerySet.
        r   	model_attreverse_geomgeo_field_typereverse)r	   
setdefaultr   r    r!   r"   r   r*   )r+   r,   r-   r0   r0   r1   rU      s
    zGeoQuerySet.reverse_geomg        c             K   s   t  |  j j j r] | d k r. t d   n  i d d 6i | d 6| d 6d 6t   d 6} n3 i d	 d 6i | d 6| d 6| d
 6d 6t   d 6} |  j d | |  S)z|
        Scales the geometry to a new size by multiplying the ordinates
        with the given x,y,z scale factors.
        g        z'SpatiaLite does not support 3D scaling.z%(geo_col)s,%(x)s,%(y)sr   xyr   r   z%(geo_col)s,%(x)s,%(y)s,%(z)szscale)r   r    r!   r$   rC   r	   r*   )r+   rY   rZ   r[   r,   r-   r0   r0   r1   r\      s    

zGeoQuerySet.scalec             O   s  d d d   | D k r( t  d   n  t |  } | d k r` | d } d } i | d 6} n | d	 k r | \ } } d
 } i | d 6| d 6} nU | d k r | \ } } }	 }
 d } i | d 6| d 6|	 d 6|
 d 6} n t d   i | d 6| d 6t   d 6} |  j d | |  S)ag  
        Snap all points of the input geometry to the grid.  How the
        geometry is snapped to the grid depends on how many arguments
        were given:
          - 1 argument : A single size to snap both the X and Y grids to.
          - 2 arguments: X and Y sizes to snap the grid to.
          - 4 arguments: X, Y sizes and the X, Y origins.
        Fc             S   s)   g  |  ] } t  | t f t j   q Sr0   )rD   floatr   rE   ).0argr0   r0   r1   
<listcomp>  s   	 z,GeoQuerySet.snap_to_grid.<locals>.<listcomp>z@Size argument(s) for the grid must be a float or integer values.r=   r   z%(geo_col)s,%(size)ssizer>   z%(geo_col)s,%(xsize)s,%(ysize)sxsizeysize   z7%(geo_col)s,%(xorigin)s,%(yorigin)s,%(xsize)s,%(ysize)sxoriginyoriginz4Must provide 1, 2, or 4 arguments to `snap_to_grid`.r   r   r   snap_to_grid)rF   len
ValueErrorr	   r*   )r+   argsr,   nargsra   r   r   rb   rc   re   rf   r-   r0   r0   r1   rg     s*    	

zGeoQuerySet.snap_to_gridc             K   so   t  t |   } t | t j  s3 t d   n  i d d 6d d 6i | d 6| d 6d 6} |  j d	 | |  S)
a  
        Returns SVG representation of the geographic field in a `svg`
        attribute on each element of this GeoQuerySet.

        Keyword Arguments:
         `relative`  => If set to True, this will evaluate the path in
                        terms of relative moves (rather than absolute).

         `precision` => May be used to set the maximum number of decimal
                        digits used in output (defaults to 8).
        z2SVG precision keyword argument must be an integer.ZSVGr?   z!%(geo_col)s,%(rel)s,%(precision)sr   relr@   r   svg)intboolrD   r   rE   rF   r*   )r+   Zrelativer@   r,   r-   r0   r0   r1   rm   &  s    zGeoQuerySet.svgc             K   s   |  j  d | |  S)z
        Returns the symmetric difference of the geographic field in a
        `sym_difference` attribute on each element of this GeoQuerySet.
        sym_difference)r5   )r+   r6   r,   r0   r0   r1   rp   ?  s    zGeoQuerySet.sym_differencec             K   s   t  |  j j j r] | d k r. t d   n  i d d 6i | d 6| d 6d 6t   d 6} n3 i d	 d 6i | d 6| d 6| d
 6d 6t   d 6} |  j d | |  S)zr
        Translates the geometry to a new location using the given numeric
        parameters as offsets.
        g        z+SpatiaLite does not support 3D translation.z%(geo_col)s,%(x)s,%(y)sr   rY   rZ   r   r   z%(geo_col)s,%(x)s,%(y)s,%(z)sr[   	translate)r   r    r!   r$   rC   r	   r*   )r+   rY   rZ   r[   r,   r-   r0   r0   r1   rq   F  s    

zGeoQuerySet.translatei  c             K   s`   t  | t j  s! t d   n  | j d  } |  j d d | |  j j d |  |  j   S)z
        Transforms the given geometry field to the given SRID.  If no SRID is
        provided, the transformation will default to using 4326 (WGS84).
        z!An integer SRID must be provided.r   	transformtransformed_srid)	rD   r   rE   rF   r   r   queryZadd_contextZ_clone)r+   sridr,   r   r0   r0   r1   rr   Y  s    zGeoQuerySet.transformc             K   s   |  j  d | |  S)z
        Returns the union of the geographic field with the given
        Geometry in a `union` attribute on each element of this GeoQuerySet.
        union)r5   )r+   r6   r,   r0   r0   r1   rv   e  s    zGeoQuerySet.unionNc       	      C   s   t  |  j } t | j | d  } | d k r7 | } n  | s\ t d | | j j f   n  i | d 6} |  j |  } | s t d |   n  | d k	 r t | |  r t d | | j	 f   n  |  j
 | |  | d <| | f S)zE
        Performs set up for executing the spatial function.
        FNz4%s stored procedure not available on the %s backend.functionz+%s output only available on GeometryFields.z1"%s" stored procedures may only be called on %ss.Zgeo_col)r   r    getattrr!   rC   name
_geo_fieldrF   rD   __name___geocol_select)	r+   attr?   r   rV   r.   funcr   r   r0   r0   r1   r   m  s    	zGeoQuerySet._spatial_setupc          	   C   s;  t  j d | t d d | j d d  | j d f   | j d d  | j d i   | j d	 d
  | j d g   t |  j } | j d d  r|  j | d | d d | d | j d  \ } } x> t j	 |  D]  \ } }	 | d j | |	  q Wn
 | d } t
 | t j  s)| } n  x | d D] }
 | j | d |
  } | j d | d | } | j | d |  } d |
 } | d } | d	 j | |  | d	 <| d j |  q4Wd | d	 } | j d  r| d } | j j rd | _ qn	 t   } |  j j t | | d | d |  |  |  S)a  
        DRY routine for calling a spatial stored procedure on a geometry column
        and attaching its output as an attribute of the model.

        Arguments:
         att:
          The name of the spatial attribute that holds the spatial
          SQL function to call.

         settings:
          Dictionary of internal settings to customize for the spatial procedure.

        Public Keyword Arguments:

         field_name:
          The name of the geographic field to call the spatial
          function on.  May also be a lookup to a geometry field
          as part of a foreign key relation.

         model_att:
          The name of the model attribute to attach the output of
          the spatial function to.
        zxThe %s GeoQuerySet method is deprecated. See GeoDjango Functions documentation to find the expression-based replacement.
stacklevelr>   r?   N	geom_argsZ
geom_fieldr   r   z%(geo_col)sselect_paramsr   Tr   rV   r   containsr.   z%%(%s)sz%%sz%%(function)s(%s)r   F)warningswarnr   rX   r   r    r   r   r   	iteritemsrD   string_typesget_prep_value_get_db_prep_lookupZget_placeholderreplaceextendr!   r"   Zempty_strings_allowedr   rt   Zadd_annotationr   )r+   r}   Zsettingsr   rT   r.   Zdefault_argsr   kvry   r6   paramsZgeom_placeholderZold_fmtZnew_fmtfmtr   r0   r0   r1   r*     sL    
	


		zGeoQuerySet._spatial_attributec             K   s  |  j  | d | j d  \ } } t |  j } | j |  }	 | j }
 |	 rX d } n t j | j |   } | d k } | d k } | d k } | p | p | s t	 d |   n  | j
 d k } | p d d	 g } | j } | s| j r|	 r|
 r| r| j d
  n  | j |  } | j d | d | } t |  } | j r| r`d } n | sl| rud } n  | | d <n|  j j d  } | rt | |  \ } } } | j   | j k }	 n  |	 r| j j rt	 d   n  | rq| rd } d | j | f } | j d k s/| j | k r_| j rR| d | j | f 7} q| d 7} q| j r| d | j | j | j | f 7} q| d | j | f 7} n d } |
 r|	 r| j st | t  st	 d   n  t t  t! j" | d	 j#   j$  d k st	 d   qn  | rT| d 7} | j% i | j& d 6| d d
 6 qn| j% i | j' d 6 qn | s}| rd } |
 r|	 r| r| d 7} | j% i | j( d 6| d d
 6 q| r| j j) r| r| j% i | j* d 6 q| r| j% i | j+ d 6 qqn  i t, |  d  6d d! 6| d" 6| d# 6| d$ 6} | rmd( | d& <| | d# d% <n | r| j- |  g | d' <n  |  j. | | |  S))zJ
        DRY routine for GeoQuerySet distance attribute routines.
        r   mr7   rN   rR   zUnknown distance function: %sr<   zPOINT (0 0)r   spheroidZdistance_lter.   z"%(geo_col)s,%(geom)s,%(tolerance)sz%(geo_col)s,%(tolerance)sr   rs   z[This database does not support linear distance calculations on geodetic coordinate systems.Fz%s(%%(geo_col)s, %s)Nz, %s(%%%%s, %s)z, %%sz, %s(%s(%%%%s, %s), %s)z%(geo_col)s,%(geom)sz=Spherical distance calculation only supported on PointFields.ZPointzLSpherical distance calculation only supported with Point Geometry parametersz,'%(spheroid)s'rw   r=   z%(geo_col)sr   r   r   r   r   r6   r   r   )zgeom)/r   r   r   r    r&   r%   r   r'   r(   ri   Zdimr!   r#   appendr   r   ro   r"   rt   Zget_contextr   lowerZgeodetic_unitsfeaturesZsupports_distance_geodeticrr   ru   r$   Z	from_textrD   r   strr   r   
memoryviewZewkbZ	geom_typeupdateZdistance_spheroidZdistance_sphereZlength_spheroidZsupports_3d_functionsZperimeter3dZlength3dr   ZAdapterr*   )r+   r~   r6   r   r   r,   r   r   r.   r&   r%   Zdist_attr7   rN   rR   Zgeom_3dZlookup_paramsr/   r   r   r   ru   uZ	unit_namer-   r0   r0   r1   r8     s    $									+
%
% 

zGeoQuerySet._distance_attributec             K   sT   i t    d 6} t |  j j j rA d | d <i | d 6| d <n  |  j | | |  S)z
        DRY routine for setting up a GeoQuerySet method that attaches a
        Geometry attribute (e.g., `centroid`, `point_on_surface`).
        r   z%(geo_col)s,%(tolerance)sr   r   r   )r	   r   r    r!   r"   r*   )r+   r~   r   r,   r-   r0   r0   r1   r3   ~  s
    
zGeoQuerySet._geom_attributec             K   sq   i d	 d 6t    d 6d d 6i | d 6d 6} t |  j j j r^ | d d 7<| | d d <n  |  j | | |  S)
a  
        DRY routine for setting up a GeoQuerySet method that attaches a
        Geometry attribute and takes a Geoemtry parameter.  This is used
        for geometry set-like operations (e.g., intersection, difference,
        union, sym_difference).
        r6   r   r   z%(geo_col)s,%(geom)sr   r   z,%(tolerance)sr   )zgeom)r	   r   r    r!   r"   r*   )r+   r~   r6   r   r,   r-   r0   r0   r1   r5     s    
zGeoQuerySet._geomset_attributec             C   s   |  j  j |  j  } |  j j } | | j k r |  j  j | j t d  d g  | j	   x@ | j
 D]5 \ } } } | j | k rg | j | | j  d Sqg Wt d |   nG | | j k r | j j j } |  j | | | j j  S|  j | |  Sd S)z
        Helper routine for constructing the SQL to select the geographic
        column.  Takes into account if the geographic field is in a
        ForeignKey relation to the current model.
        r=   r   z(%r not in compiler's related_select_colsN)rt   Zget_compilerr    model_metafieldsZadd_select_relatedrsplitr   Zpre_sql_setupselectZoutput_fieldZas_sqlr.   ri   Zlocal_fieldsZconcrete_model_field_columndb_table)r+   r   r   compileroptscol_Zparent_modelr0   r0   r1   r|     s    #
zGeoQuerySet._geocol_selectc             C   sW   | d k r= x* |  j  j j D] } t | t  r | Sq Wd St j |  j  j |  Sd S)a'  
        Returns the first Geometry field encountered or the one specified via
        the `field_name` keyword. The `field_name` may be a string specifying
        the geometry field on this GeoQuerySet's model, or a lookup string
        to a geometry field via a ForeignKey relation.
        NF)r   r   r   rD   r   r   Z_check_geo_field)r+   r   fieldr0   r0   r1   rz     s    zGeoQuerySet._geo_fieldc             C   sP   | d k r! | j  j   j } n  d | j |  | j j j | pH | j  f S)a  
        Helper function that returns the database column for the given field.
        The table and column are returned (quoted) in the proper format, e.g.,
        `"geoapp_city"."point"`.  If `table_alias` is not specified, the
        database table associated with the model of this `GeoQuerySet` will be
        used.  If `column` is specified, it will be used instead of the value
        in `field.column`.
        Nz%s.%s)rt   Zget_metar   Zquote_name_unless_aliasr.   r!   Z
quote_namecolumn)r+   r   r   Ztable_aliasr   r0   r0   r1   r     s    	zGeoQuerySet._field_column)%r{   
__module____qualname____doc__r   r2   r4   r7   r9   r:   rB   rH   rJ   rK   rL   rN   rO   rP   rQ   rR   rS   rU   r\   rg   rm   rp   rq   rr   rv   r   r*   r8   r3   r5   r|   rz   r   r0   r0   r0   r1   r      sD   		$!Wr   )!r   Z#django.contrib.gis.db.models.fieldsr   r   r   r   Z$django.contrib.gis.db.models.lookupsr   Z django.contrib.gis.db.models.sqlr   r   r	   r
   Z#django.contrib.gis.geometry.backendr   Zdjango.contrib.gis.measurer   r   Z	django.dbr   Zdjango.db.models.constantsr   Zdjango.db.models.expressionsr   Zdjango.db.models.fieldsr   Zdjango.db.models.queryr   Zdjango.utilsr   Zdjango.utils.deprecationr   r   r0   r0   r0   r1   <module>   s   ""