3
g=                 @   s  d dl Z d dlmZ d dl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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#m$Z$ ddl%m&Z& dZ'G dd deZ(G dd deZ)G dd deeZ*dS )    N)settings)BaseSpatialOperations)SpatialOperator)GeometryFieldRasterField)
GDALRaster)GEOSGeometryBase)wkb_r)Distance)ImproperlyConfigured)NotSupportedErrorProgrammingError)DatabaseOperations)FuncValue)cached_property)get_version_tuple   )PostGISAdapter)PostGISGeometryColumnsPostGISSpatialRefSys)from_pgrasterZ	bilateralc                   s2   e Zd Zd fdd	Z fddZdd Z  ZS )	PostGISOperatorFc                s   || _ || _t jf | d S )N)	geographyrastersuper__init__)selfr   r   kwargs)	__class__ o/var/www/tester-filtro-web/env/lib/python3.6/site-packages/django/contrib/gis/db/backends/postgis/operations.pyr      s    zPostGISOperator.__init__c                sJ   |j jjr(| j r(td| jp | jf | j||}t j|||f| S )Nz>PostGIS geography does not support the "%s" function/operator.)	lhsoutput_fieldr   
ValueErrorfuncopcheck_rasterr   as_sql)r   
connectionlookuptemplate_paramsargs)r   r    r!   r(   (   s
    zPostGISOperator.as_sqlc             C   s  |j o|j d	 dk}|jjjdk}t|jt}|jd k	r`|r`| jsJt	dd|d |jf |d< |j
d k	r|r| js|t	dd|d |j
f |d< | j s|r|rd|d  |d< |rd|d  |d< nD| jtkr|r| rd|d  |d< n|o| rd|d  |d< |S )
Nr   spheroidRASTERzFBand indices are not allowed for this operator, it works on bbox only.z%s, %sr"   rhszST_Polygon(%s))Z
rhs_paramsr"   field	geom_type
isinstancer/   r   Zband_lhsr%   r$   Zband_rhsr   	BILATERAL)r   r*   r+   r-   Zlhs_is_rasterZrhs_is_rasterr    r    r!   r'   0   s,    
zPostGISOperator.check_raster)FF)__name__
__module____qualname__r   r(   r'   __classcell__r    r    )r   r!   r      s   r   c                   s,   e Zd Zd Z fddZedd Z  ZS )
ST_Polygonc                sJ   t  j| | jd }t|trF|j rFt|jt|jjdd| jd< d S )Nr   )srid)r#   )	r   r   source_expressionsr3   r   Z_output_field_or_nonevaluer   r:   )r   expr)r   r    r!   r   W   s    
zST_Polygon.__init__c             C   s   t | jd jjdS )Nr   )r:   )r   r;   r1   r:   )r   r    r    r!   r#   ]   s    zST_Polygon.output_field)r5   r6   r7   functionr   r   r#   r8   r    r    )r   r!   r9   T   s   r9   c                   s0  e Zd ZdZdZdZeZed Zed Z	ed Z
ed Zed Zed	 Zed
 Zedddeddddedddedededededdeddeddeddeddeddedededededededededdededdeded d!ed"eded#d!ed$deded%eded&d!ed'eded(eded)dedd*Ze Zd+Zd,Zed-d. Zed/d0 Zd1d2 Zd3d4 Zd5d6 Zd7d8 Zd9d: Zd;d< Zd=d> Zd?d@ Z dAdB Z!dCdD Z"dEdF Z#dGdH Z$dIdJ Z%dKdL Z&dMdN Z'dOdP Z(dQdR Z) fdSdTZ*e+dUdV Z,dWdX Z-dYdZ Z.  Z/S )[PostGISOperationspostgisTZST_ZCollectZExtentZ3DExtentZ3DLengthZMakeLineZ3DPerimeterUnion~)r&   r   z&&)r&   r   r   @z&<z&>z&<|)r&   z|&>z<<z>>z<<|z|>>z~=ZST_Contains)r%   r   ZST_ContainsProperlyZST_CoveredBy)r%   r   r   Z	ST_CoversZ
ST_Crosses)r%   ZST_DisjointZ	ST_EqualsZST_IntersectsZST_OverlapsZ	ST_RelateZ
ST_TouchesZ	ST_WithinZ
ST_DWithin)Z
bbcontainsZ
bboverlapsZ	containedZoverlaps_leftZoverlaps_rightZoverlaps_belowZoverlaps_aboveleftrightZstrictly_belowZstrictly_aboveZsame_asexactcontainsZcontains_properlyZ	coveredbyZcoversZcrossesZdisjointequalsZ
intersectsoverlapsZrelateZtouchesZwithindwithinz	%s::byteaNc             C   s$   ddddd}| j dk r d	|d
< |S )NZST_AsBinaryZ	ST_AsTextZST_MinimumBoundingCircleZ
ST_NPoints)ZAsWKBZAsWKTZBoundingCircleZ	NumPoints      r   ZST_ForceRHRZForcePolygonCW)rK   rL   r   )spatial_version)r   function_namesr    r    r!   rN      s    
z PostGISOperations.function_namesc             C   sb   t tdrtj}nL| jd y| j }W n( tk
rP   td| jjd  Y nX |dd }|S )z-Determine the version of the PostGIS library.POSTGIS_VERSIONversionzCannot determine PostGIS version for database "%s" using command "SELECT postgis_lib_version()". GeoDjango requires at least PostGIS version 2.3. Was the database created from a spatial database template?NAMEr   N)	hasattrr   rO   _get_postgis_funcpostgis_version_tupler   r   r)   Zsettings_dict)r   rP   Zvtupr    r    r!   rM      s    	

z!PostGISOperations.spatial_versionc             C   sR   |dkrdS |dd j d\}}tt|j  \}}tt|j  \}}||||fS )z
        Return a 4-tuple extent for the `Extent` aggregate by converting
        the bounding box text returned by PostGIS (`box` argument), for
        example: "BOX(-90.0 30.0, -85.0 40.0)".
        NrL   r   ,r0   )splitmapfloat)r   Zboxllurxminyminxmaxymaxr    r    r!   convert_extent   s    z PostGISOperations.convert_extentc       
      C   sZ   |dkrdS |dd j d\}}tt|j  \}}}tt|j  \}}}	||||||	fS )z
        Return a 6-tuple extent for the `Extent3D` aggregate by converting
        the 3d bounding-box text returned by PostGIS (`box3d` argument), for
        example: "BOX3D(-90.0 30.0 1, -85.0 40.0 2)".
        N   r   rU   r0   )rV   rW   rX   )
r   Zbox3drY   rZ   r[   r\   Zzminr]   r^   Zzmaxr    r    r!   convert_extent3d   s    z"PostGISOperations.convert_extent3dc             C   sb   |j dkrdS |jdkr$|j d }n|j }|jrP|jdkrBtdd||jf S d||jf S d	S )
zM
        Return the database field type for the given spatial field.
        r.   r      Zi  z=PostGIS only supports geography columns with an SRID of 4326.zgeography(%s,%d)zgeometry(%s,%d)N)r2   Zdimr   r:   r   )r   fr2   r    r    r!   geo_db_type   s    


zPostGISOperations.geo_db_typec             C   sp   |d }|j | j}|j}t|trf|r0|j}qj|rL|dkrDtd|j}qjt|tj|j	| j}n|}|gS )a  
        Retrieve the distance parameters for the given geometry field,
        distance lookup value, and the distance lookup type.

        This is the most complex implementation of the spatial backends due to
        what is supported on geodetic geometry columns vs. what's available on
        projected geometry columns.  In addition, it has to take into account
        the geography column type.
        r   rJ   zNOnly numeric values of degree units are allowed on geographic DWithin queries.)
geodeticr)   r   r3   r
   mr$   getattrZunit_attnameZ
units_name)r   rd   Zdist_valZlookup_typer<   rf   r   Z
dist_paramr    r    r!   get_distance   s    
zPostGISOperations.get_distancec             C   sx   | j d}t|dr:|jj|jkr(d}nd||jf }|S |dkrHd}n|j}|dks`||jkrfd}nd||jf }|S )z
        Provide a proper substitution value for Geometries or rasters that are
        not in the SRID of the field. Specifically, this routine will
        substitute in the ST_Transform() function call.
        Z	Transformr(   z%sz%s(%%s, %s)N)Zspatial_function_namerR   r1   r:   )r   rd   r<   compilerZtransform_funcplaceholderZ
value_sridr    r    r!   get_geom_placeholder  s    

z&PostGISOperations.get_geom_placeholderc          
   C   s0   | j j }|jd|  |j d S Q R X dS )zZ
        Helper routine for calling PostGIS functions and returning their result.
        zSELECT %s()r   N)r)   Ztemporary_connectionexecuteZfetchone)r   r%   cursorr    r    r!   rS   *  s    z#PostGISOperations._get_postgis_funcc             C   s
   | j dS )z9Return the version of the GEOS library used with PostGIS.postgis_geos_version)rS   )r   r    r    r!   ro   3  s    z&PostGISOperations.postgis_geos_versionc             C   s
   | j dS )zFReturn the version number of the PostGIS library used with PostgreSQL.postgis_lib_version)rS   )r   r    r    r!   rp   7  s    z%PostGISOperations.postgis_lib_versionc             C   s
   | j dS )z9Return the version of the PROJ library used with PostGIS.postgis_proj_version)rS   )r   r    r    r!   rq   ;  s    z&PostGISOperations.postgis_proj_versionc             C   s
   | j dS )z7Return PostGIS version number and compile-time options.postgis_version)rS   )r   r    r    r!   rr   ?  s    z!PostGISOperations.postgis_versionc             C   s
   | j dS )z7Return PostGIS version number and compile-time options.postgis_full_version)rS   )r   r    r    r!   rs   C  s    z&PostGISOperations.postgis_full_versionc             C   s   | j  }|ft| S )zi
        Return the PostGIS version as a tuple (version string, major,
        minor, subminor).
        )rp   r   )r   rP   r    r    r!   rT   G  s    z'PostGISOperations.postgis_version_tuplec             C   s>   t jd}| j }|j|}|r2ttt|j S tddS )z
        Return the version of PROJ used by PostGIS as a tuple of the
        major, minor, and subminor release numbers.
        z(\d+)\.(\d+)\.(\d+)z.Could not determine PROJ version from PostGIS.N)	recompilerq   searchtuplerW   intgroups	Exception)r   Z
proj_regexZproj_ver_strrg   r    r    r!   proj_version_tupleO  s    

z$PostGISOperations.proj_version_tuplec             C   s   |dkr| j S | j| S d S )NZExtent3D)extent3dgeom_func_prefix)r   Zagg_namer    r    r!   spatial_aggregate_name\  s    z(PostGISOperations.spatial_aggregate_namec             C   s   t S )N)r   )r   r    r    r!   geometry_columnsc  s    z"PostGISOperations.geometry_columnsc             C   s   t S )N)r   )r   r    r    r!   spatial_ref_sysf  s    z!PostGISOperations.spatial_ref_sysc             C   s   t |S )z@Convert a PostGIS HEX String into a dict readable by GDALRaster.)r   )r   r<   r    r    r!   parse_rasteri  s    zPostGISOperations.parse_rasterc                s   t  j| j|| j|f|S )N)r   distance_expr_for_lookup_normalize_distance_lookup_arg)r   r"   r/   r   )r   r    r!   r   m  s    
z*PostGISOperations.distance_expr_for_lookupc             C   s0   t | dr| jjdknt| t}|r,t| S | S )Nr1   r.   )rR   r1   r2   r3   r   r9   )argZ	is_rasterr    r    r!   r   t  s    
z0PostGISOperations._normalize_distance_lookup_argc                s"   t  j|jj  fdd}|S )Nc                s   | d krd S t |  S )N)r   )r<   
expressionr)   )
geom_classreadr    r!   	converter  s    z;PostGISOperations.get_geometry_converter.<locals>.converter)r	   r   r#   r   )r   r   r   r    )r   r   r!   get_geometry_converter}  s    z(PostGISOperations.get_geometry_converterc             C   s   dS )NZsq_mr    )r   r1   r    r    r!   get_area_att_for_field  s    z(PostGISOperations.get_area_att_for_field)0r5   r6   r7   namer@   r}   r   ZAdapterZcollectZextentr|   Zlength3dZmakelineZperimeter3dZunionaggr   r4   Zgis_operatorssetZunsupported_functionsselectZselect_extentr   rN   rM   r_   ra   re   ri   rl   rS   ro   rp   rq   rr   rs   rT   r{   r~   r   r   r   r   staticmethodr   r   r   r8   r    r    )r   r!   r?   b   s~   











!		r?   )+rt   Zdjango.confr   Z.django.contrib.gis.db.backends.base.operationsr   Z$django.contrib.gis.db.backends.utilsr   Zdjango.contrib.gis.db.modelsr   r   Zdjango.contrib.gis.gdalr   Z django.contrib.gis.geos.geometryr   Z%django.contrib.gis.geos.prototypes.ior	   Zdjango.contrib.gis.measurer
   Zdjango.core.exceptionsr   Z	django.dbr   r   Z(django.db.backends.postgresql.operationsr   Zdjango.db.modelsr   r   Zdjango.utils.functionalr   Zdjango.utils.versionr   adapterr   modelsr   r   Zpgrasterr   r4   r   r9   r?   r    r    r    r!   <module>   s*   8