
    h@                     <   S SK r S SKJr  S SKJr  S SKJr  S SKJrJ	r	  S SK
Jr  S SKJr  S SKJr  S S	KJr  S S
KJr  S SKJrJr  S SKJr  S SKJr  S SKJrJr  S SKJr  S SK J!r!  SSK"J#r#  SSK$J%r%J&r&  SSK'J(r(  Sr) " S S\5      r* " S S\5      r+ " S S\\5      r,g)    N)settings)BaseSpatialOperations)SpatialOperator)GeometryFieldRasterField)
GDALRaster)GEOSGeometryBase)wkb_r)Distance)ImproperlyConfigured)NotSupportedErrorProgrammingError)DatabaseOperations)is_psycopg3)FuncValue)cached_property)get_version_tuple   )PostGISAdapter)PostGISGeometryColumnsPostGISSpatialRefSysfrom_pgraster	bilateralc                   D   ^  \ rS rSrSU 4S jjrU 4S jrS rS rSrU =r	$ )PostGISOperator   c                 >   > Xl         X l        [        TU ]  " S0 UD6  g )N )	geographyrastersuper__init__)selfr!   r"   kwargs	__class__s       c/var/www/html/env/lib/python3.13/site-packages/django/contrib/gis/db/backends/postgis/operations.pyr$   PostGISOperator.__init__   s!     #
 "6"    c                 l   > U R                  X#5      nU R                  X#5      n[        TU ]  " XU/UQ76 $ N)check_rastercheck_geographyr#   as_sql)r%   
connectionlookuptemplate_paramsargsr'   s        r(   r/   PostGISOperator.as_sql(   s9    ++FD..vGw~j/IDIIr*   c                    UR                   =(       a    UR                   S   S:H  nUR                  R                  R                  S:H  n[	        UR
                  [        5      nUR                  b<  U(       a5  U R                  (       d  [        S5      eUS   < SUR                  < 3US'   UR                  b<  U(       a5  U R                  (       d  [        S5      eUS   < SUR                  < 3US'   U R                  (       a  U(       a&  U(       a  SUS   -  US'   U(       a  SUS   -  US'   U$ U R                  [        :X  a4  U(       a  U(       d  SUS   -  US'   U$ U(       a  U(       d  SUS   -  US'   U$ )	NspheroidRASTERzFBand indices are not allowed for this operator, it works on bbox only.lhsz, rhszST_Polygon(%s))
rhs_paramsr9   field	geom_type
isinstancer:   r   band_lhsfunc
ValueErrorband_rhsr"   	BILATERAL)r%   r1   r2   r7   lhs_is_rasterrhs_is_rasters         r(   r-   PostGISOperator.check_raster-   si   $$L):):2)>*)L 

((22h>"6::z: ??&=99  
  &&OE"
 ??&=99  
  &&OE" {{h)9OE<R)R&)9OE<R)R&  [[I% ])9OE<R)R&  })9OE<R)R&r*   c                     UR                   R                  R                  (       a  U R                  (       d  US==   S-  ss'   U$ )z9Convert geography fields to geometry types, if necessary.r9   z
::geometry)r9   output_fieldr!   )r%   r1   r2   s      r(   r.   PostGISOperator.check_geography\   s1    ::"",,T^^E"l2"r*   )r!   r"   )FF)
__name__
__module____qualname____firstlineno__r$   r/   r-   r.   __static_attributes____classcell__r'   s   @r(   r   r      s    
#J
-^ r*   r   c                   <   ^  \ rS rSrS rU 4S jr\S 5       rSrU =r	$ )
ST_Polygonc   c                 
  > [         TU ]  U5        U R                  S   n[        U[        5      (       aO  UR
                  (       d=  [	        UR                  [        UR                  R                  S9S9U R                  S'   g g g )Nr   srid)rH   )	r#   r$   source_expressionsr>   r   _output_field_or_nonevaluer   rV   )r%   exprr'   s     r(   r$   ST_Polygon.__init__f   si    &&q)dE""4+E+E).

$**//)J*D##A& ,F"r*   c                 V    [        U R                  S   R                  R                  S9$ )Nr   rU   )r   rW   r<   rV   r%   s    r(   rH   ST_Polygon.output_fieldn   s$    $"9"9!"<"B"B"G"GHHr*   r    )
rJ   rK   rL   rM   functionr$   r   rH   rN   rO   rP   s   @r(   rR   rR   c   s#    H I Ir*   rR   c                     ^  \ rS rSrSrSrSr\r\S-   r	\S-   r
\S-   r\S-   r\S	-   r\S
-   r\S-   r0 S\" SSS9_S\" SSSS9_S\" SSS9_S\" S\S9_S\" S\S9_S\" SS9_S\" SS9_S\" SS9_S\" S S9_S!\" S"S9_S#\" S$S9_S%\" S&\S9_S'\" S&\S9_S(\" S)\S*9_S+\" S,\S*9_S-\" S.S\S/9_S0\" S1S\S/9_\" S2S39\" S4\S*9\" S5S39\" S6S\S/9\" S7\S*9\" S8S39\" S9\S*9\" S:\S*9\" S;S\S/9S<.	Er\" 5       r\(       a  S=OS>rS?r\S@ 5       r\SA 5       rSB rSC rSD rSE rSF rSG r SH r!SI r"SJ r#SK r$SL r%SM r&SN r'SO r(SP r)SQ r*SR r+U 4SS jr,\-ST 5       r.SU r/SV r0SWr1U =r2$ )XPostGISOperationss   postgisTST_CollectExtent3DExtent3DLengthMakeLine3DPerimeterUnion
bbcontains~)opr"   
bboverlapsz&&)rn   r!   r"   	contained@overlaps_leftz&<overlaps_rightz&>overlaps_belowz&<|)rn   overlaps_abovez|&>leftz<<rightz>>strictly_belowz<<|strictly_abovez|>>same_asz~=exactcontainsST_Contains)r@   r"   contains_properlyST_ContainsProperly	coveredbyST_CoveredBy)r@   r!   r"   covers	ST_Covers
ST_Crosses)r@   ST_Disjoint	ST_EqualsST_IntersectsST_Overlaps	ST_Relate
ST_Touches	ST_Within
ST_DWithin)	crossesdisjointequals
intersectsoverlapsrelatetoucheswithindwithin%sz	%s::byteaNc                     SSSSSSS.nU$ )NST_AsBinary	ST_AsTextST_MinimumBoundingCircleST_GeomFromWKBST_GeomFromText
ST_NPoints)AsWKBAsWKTBoundingCircleFromWKBFromWKT	NumPointsr    )r%   function_namess     r(   r    PostGISOperations.function_names   s%     # 8'(%
 r*   c                    [        [        S5      (       a  [        R                  nU$ U R                  S5         U R	                  5       nUSS nU$ ! [
         a&    [        SU R                  R                  S   -  5      ef = f)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 3.0. Was the database created from a spatial database template?NAMEr   N)	hasattrr   r   _get_postgis_funcpostgis_version_tupler   r   r0   settings_dict)r%   r   vtups      r(   spatial_version!PostGISOperations.spatial_version   s     8.//..G$  ""9-	113 12hG $ *  #'//"?"?"G	H s   A 0Bc                     Uc  gUSS R                  S5      u  p#[        [        UR                  5       5      u  pE[        [        UR                  5       5      u  pgXEXg4$ )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)".
N   r6   ,splitmapfloat)r%   boxllurxminyminxmaxymaxs           r(   convert_extent PostGISOperations.convert_extent   sU     ;Qr%
+

+
D''r*   c                     Uc  gUSS R                  S5      u  p#[        [        UR                  5       5      u  pEn[        [        UR                  5       5      u  pxn	XEXgX4$ )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   r6   r   r   )
r%   box3dr   r   r   r   zminr   r   zmaxs
             r(   convert_extent3d"PostGISOperations.convert_extent3d   s_     =q""3'ubhhj1Dubhhj1DD33r*   c                    UR                   S:X  a  gUR                  S:X  a  UR                   S-   nOUR                   nUR                  (       a+  UR                  S:w  a  [	        S5      eSX!R                  4-  $ SX!R                  4-  $ )	z=
Return the database field type for the given spatial field.
r8   r"      Zi  z=PostGIS only supports geography columns with an SRID of 4326.zgeography(%s,%d)zgeometry(%s,%d))r=   dimr!   rV   r   )r%   fr=   s      r(   geo_db_typePostGISOperations.geo_db_type   s     ;;(" 55A:c)II;;vv~'S  &FF(;;;$	66':::r*   c                    US   nUR                  U R                  5      nUR                  n[        U[        5      (       az  U(       a  UR
                  nU/$ U(       a   US:X  a  [        S5      eUR
                  nU/$ [        U[        R                  " UR                  U R                  5      5      5      n U/$ UnU/$ )ae  
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   r   zNOnly numeric values of degree units are allowed on geographic DWithin queries.)
geodeticr0   r!   r>   r   mrA   getattrunit_attname
units_name)r%   r   dist_vallookup_typerY   r   r!   
dist_params           r(   get_distancePostGISOperations.get_distance  s      ::doo.KK	eX&&"WW
  | )+$A  #WW
 | %800doo1NO
 | J|r*   c                 >   U R                  S5      n[        US5      (       a>  UR                  R                  UR                  :X  a  SnU$ U< SUR                  < S3nU$ Uc  SnOUR                  nUb  XaR                  :X  a  SnU$ U< SUR                  < S3nU$ )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.
	Transformr/   r   z(%s, )N)spatial_function_namer   r<   rV   )r%   r   rY   compilertransform_funcplaceholder
value_srids          r(   get_geom_placeholder&PostGISOperations.get_geom_placeholder+  s     33K@5(##{{166)"  0>qvvF =JJ vv!5K  ,:166BKr*   c                     U R                   R                  5        nUR                  SU-  5        UR                  5       S   sSSS5        $ ! , (       d  f       g= f)zJ
Helper routine for calling PostGIS functions and returning their result.
zSELECT %s()r   N)r0   temporary_connectionexecutefetchone)r%   r@   cursors      r(   r   #PostGISOperations._get_postgis_funcH  sB    
 __113vNN=4/0??$Q' 433s   'A
Ac                 $    U R                  S5      $ )z9Return the version of the GEOS library used with PostGIS.postgis_geos_versionr   r]   s    r(   r   &PostGISOperations.postgis_geos_versionQ      %%&<==r*   c                 $    U R                  S5      $ )zFReturn the version number of the PostGIS library used with PostgreSQL.postgis_lib_versionr   r]   s    r(   r   %PostGISOperations.postgis_lib_versionU  s    %%&;<<r*   c                 $    U R                  S5      $ )z9Return the version of the PROJ library used with PostGIS.postgis_proj_versionr   r]   s    r(   r   &PostGISOperations.postgis_proj_versionY  r   r*   c                 $    U R                  S5      $ )7Return PostGIS version number and compile-time options.postgis_versionr   r]   s    r(   r   !PostGISOperations.postgis_version]  s    %%&788r*   c                 $    U R                  S5      $ )r   postgis_full_versionr   r]   s    r(   r   &PostGISOperations.postgis_full_versiona  r   r*   c                 @    U R                  5       nU4[        U5      -   $ )zQ
Return the PostGIS version as a tuple (version string, major,
minor, subminor).
)r   r   )r%   r   s     r(   r   'PostGISOperations.postgis_version_tuplee  s%    
 **,z-g666r*   c                     [         R                  " S5      nU R                  5       nUR                  U5      nU(       a'  [	        [        [        UR                  5       5      5      $ [        S5      e)zj
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.)	recompiler   searchtupler   intgroups	Exception)r%   
proj_regexproj_ver_strr   s       r(   proj_version_tuple$PostGISOperations.proj_version_tuplem  sX    
 ZZ 67
002l+S!((*-..LMMr*   c                 D    US:X  a  U R                   $ U R                  U-   $ )NExtent3D)extent3dgeom_func_prefix)r%   agg_names     r(   spatial_aggregate_name(PostGISOperations.spatial_aggregate_namez  s%    z!== ((833r*   c                     [         $ r,   )r   r]   s    r(   geometry_columns"PostGISOperations.geometry_columns  s    %%r*   c                     [         $ r,   )r   r]   s    r(   spatial_ref_sys!PostGISOperations.spatial_ref_sys  s    ##r*   c                     [        U5      $ )z@Convert a PostGIS HEX String into a dict readable by GDALRaster.r   )r%   rY   s     r(   parse_rasterPostGISOperations.parse_raster  s    U##r*   c                 d   > [         TU ]  " U R                  U5      U R                  U5      40 UD6$ r,   )r#   distance_expr_for_lookup_normalize_distance_lookup_arg)r%   r9   r:   r&   r'   s       r(   r  *PostGISOperations.distance_expr_for_lookup  s;    w///4//4
 
 	
r*   c                     [        U S5      (       a  U R                  R                  S:H  O[        U [        5      nU(       a  [        U 5      $ U $ )Nr<   r8   )r   r<   r=   r>   r   rR   )arg	is_rasters     r(   r  0PostGISOperations._normalize_distance_lookup_arg  sG     sG$$ II8+C, 	
 #,z#44r*   c                 l   ^^ [        5       R                  mUR                  R                  mUU4S jnU$ )Nc                 ~   > [        U [        5      (       a  U R                  S5      n U c  S $ [        T" U 5      T5      $ )Nascii)r>   strencoder	   )rY   
expressionr0   
geom_classreads      r(   	converter;PostGISOperations.get_geometry_converter.<locals>.converter  s9    %%%W- =4W.>tE{J.WWr*   )r
   r)  rH   r(  )r%   r'  r*  r(  r)  s      @@r(   get_geometry_converter(PostGISOperations.get_geometry_converter  s-    w||,,77
	X
 r*   c                     g)Nsq_mr    )r%   r<   s     r(   get_area_att_for_field(PostGISOperations.get_area_att_for_field  s    r*   r    )3rJ   rK   rL   rM   namerc   r  r   Adaptercollectextentr  length3dmakelineperimeter3dunionaggr   rC   gis_operatorssetunsupported_functionsr   selectselect_extentr   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  staticmethodr  r,  r0  rN   rO   rP   s   @r(   ra   ra   s   s   DGG*G(F*,H*,H*,H"]2K')H!oT:!odK! 	_D9! 	DC	!
 	/T)D! 	/U3! 	/U3! 	4(! 	D)! 	/U3! 	/U3! 	?d9=! 	D;! 	OyI! 	_&y
!$ 	_4	
%!* 	/{d9U+!, #5#yI!{3% D
 $yI!{3"YG!{9E"YWA!MF  E TkFM	 	  :(4;.#J:(>=>9>7N4&$$
 5 5	 r*   ra   )-r   django.confr   .django.contrib.gis.db.backends.base.operationsr   $django.contrib.gis.db.backends.utilsr   django.contrib.gis.db.modelsr   r   django.contrib.gis.gdalr    django.contrib.gis.geos.geometryr	   %django.contrib.gis.geos.prototypes.ior
   django.contrib.gis.measurer   django.core.exceptionsr   	django.dbr   r   (django.db.backends.postgresql.operationsr   )django.db.backends.postgresql.psycopg_anyr   django.db.modelsr   r   django.utils.functionalr   django.utils.versionr   adapterr   modelsr   r   pgrasterr   rC   r   rR   ra   r    r*   r(   <module>rR     st    	   P @ C . = 7 / 7 9 G A ( 3 2 # @ # 	Eo EPI I t-/A tr*   