
    h3B                         d dl Z d dlmZ d dlZd dlZd dlmZm	Z	 d dl
mZ d dlmZmZmZmZmZ d Zd Zd Zd	 Zd
 Zd ZddZd Zy)    N)reduce)GeoDataFrame	GeoSeries)PANDAS_GE_30)LINE_GEOM_TYPESPOINT_GEOM_TYPESPOLYGON_GEOM_TYPES
_check_crs_crs_mismatch_warnc                    | j                   dk(  sot        r4d| j                  v r| j                  dd      } | j	                  d      } | S d| j                  v r| j                  ddd       | j	                  dd       | S )z|Ensure that the geometry column is called 'geometry'.

    If another column with that name exists, it will be dropped.
    geometry   axisTr   inplace)r   )_geometry_column_namer   columnsdroprename_geometry)dfs    J/var/www/html/immo/lib/python3.12/site-packages/geopandas/tools/overlay.py_ensure_geometry_columnr      s    
 ##z1RZZ'WWZaW0##J/B
 I RZZ'
D9z48I    c                    |j                   j                  | j                  dd      \  }}|j                  dkD  rq|j                  dkD  ra| j                  j	                  |      }|j                  dd       |j                  j	                  |      }|j                  dd       |j                  |      }|j                  j                  t              }||   j                         |j                  |<   t        j                  ||d      }|}	| j                  d      } |j                  d      }|j                  | j                  | j                   d	      d
d      }
|
j                  |j                  |j                   d	      ddd      }
t#        |
|	| j$                        S | j&                  dd j                  |j&                  dd j                  |j                  j(                  d	      ddd      }t*        j,                  |d
<   t*        j,                  |d<   ||j.                  j                  | j                  j(                        j1                         | j                  j(                  gz      S )z8Overlay Intersection operation used in overlay function.
intersectsT	predicatesortr   r   r   )__idx1__idx2r   r   r   r!   )left_onright_indexr"   _1_2)r$   r%   suffixesr   crsN
left_indexr%   r)   )sindexqueryr   sizetakereset_indexintersection	geom_typeisinr	   
make_validlocpd	DataFramemerger   r   r   r+   ilocnamenpnanr   tolist)df1df2idx1idx2leftrightintersectionspoly_ixpairs_intersectgeom_intersectdfinterresults               r   _overlay_intersectionrL   #   s6    !!#,,,T!RJD$yy1}Q||  &dD1!!$'tT2))%0))../AB%27%;%F%F%H'" ,,$$'GH& oo4o(oo4o(!''HHS..QH7 ( 

 --HHS..QH7!	   
 Gn#''JJ"1##HHRaLcll//a8!	 $ 
 66x66xNN 1 1299;s||?P?P>QQ
 	
r   c           	      j   |j                   j                  | j                  dd      \  }}t        j                  |d      \  }}t        j
                  ||dd       }t        | j                  j                        D cg c]  }||v r|j                  d      ng  }}g }	t        | j                  |      D ]G  \  }
}t        d |
gt        |j                  j                  |         z         }|	j                  |       I t        |	| j                  | j                   	      }|j"                  j%                  t&              }||   j)                         |j*                  |<   ||j,                      j/                         }| |j,                      j/                         }|||j0                  <   |S c c}w )
z6Overlay Difference operation used in overlay function.r   Tr   )return_indexr   Nr   c                 $    | j                  |      S )N)
difference)xys     r   <lambda>z%_overlay_difference.<locals>.<lambda>a   s    a r   )indexr+   )r.   r/   r   r=   uniquesplitranger0   popzipr   listr;   appendr   rT   r+   r4   r5   r	   r6   r7   is_emptycopyr   )r@   rA   rB   rC   idx1_uniqueidx1_unique_indices
idx2_splitidxsidxnew_ggeom
neighboursnewdifferencesrG   	geom_diffdfdiffs                    r   _overlay_differencerj   S   s    !!#,,,T!RJD$')yyD'I$K$$ 3AB 78J **+ !K/
qR7D 
 Ed3 j(4&48I8I*8U3V*V
 	S	
 E@K##(();<G*73>>@KOOG[1112779I+&&&',,.F+4F6''(M#s    F0c                 ~   t        | |      }t        | |      }t        |      }|j                  D cg c]  }||j                  v r|n| d }}||_        t	        j
                  ||gdd      }t        |j                        }|j                  d       |j                  d       |j                  |      S c c}w )z4Overlay Identity operation used in overlay function.r'   TFignore_indexr   r   r   )
rL   rj   r   r   r8   concatrZ   remover[   reindex)r@   rA   dfintersectiondfdifferencecolnew_columnsrK   r   s           r   _overlay_identityrv   m   s    *34N&sC0L*<8L  '' n,,,SE*<K  'L YY5DuUF >))*GNN:NN:>>'>**s   B:c                    t        | |      }t        ||       }t        t        |            |d<   t        t        |            |d<   t        j                  |d<   t        j                  |d<   t        |      }t        |      }|j                  |ddgdd      }|j                  j                         }d|_	        |j                  |j                  j                         df   |j                  |j                  j                         <   |j                  ddgd	d
       |j                  d
d
       t        ||| j                        }|S )z@Overlay Symmetric Difference operation used in overlay function.r!   r"   outerr&   )onhowr)   r   
geometry_2
geometry_1r   Tr   r    r*   )rj   rW   lenr=   r>   r   r:   r|   r]   r<   r7   isnullr   r2   r   r+   )r@   rA   dfdiff1dfdiff2dfsymr   s         r   _overlay_symmetric_diffr      s.   !#s+G!#s+Gc'l+GHc'l+GHGHGH%g.G%g.GMMXx(g  E $$&HHM.3ii!<//HLL!!((*+ 
JJl+!TJB	4.sww?ELr   c                     t        | |      }t        | |      }t        j                  ||gdd      }t	        |j
                        }|j                  d       |j                  d       |j                  |      S )z1Overlay Union operation used in overlay function.TFrl   r   rn   )	rL   r   r8   ro   rZ   r   rp   r[   rq   )r@   rA   rJ   r   dfunionr   s         r   _overlay_unionr      sh    #C-G#C-Eii%(t%HG7??#GNN:NN:??7?++r   c                 H   g d}||vrt        d| d|       t        | t              st        |t              rt        d      t	        | |      st        | |d       |d}d}nd	}t        | |g      D ]  \  }}|j                  j                  t              j                         }	|j                  j                  t              j                         }
|j                  j                  t              j                         }t        |	|
|g      d
kD  st        d|d
z    d       |dk(  r| j                  }|j                  }|d   |d   k  r!|d   |d   k  r|d
   |d   k  r|d
   |d   k  s| j                  dd j!                  |j                  dd j#                  |j$                  j&                  d
      ddd      }||j(                  j#                  | j$                  j&                        j+                         | j$                  j&                  gz      S fd}|r| j                  j                  d   } ||       }  ||      }t-        j.                         5  t-        j0                  dd       |dk(  rt3        | |      }nG|dk(  rt5        | |      }n5|dk(  rt7        | |      }n#|dk(  rt9        | |      }n|dk(  rt;        | |      }|dv rj#                  ddgd
d       ddd       |rt=        |      }j?                  dd       |S # 1 sw Y   -xY w) an  Perform spatial overlay between two GeoDataFrames.

    Currently only supports data GeoDataFrames with uniform geometry types,
    i.e. containing only (Multi)Polygons, or only (Multi)Points, or a
    combination of (Multi)LineString and LinearRing shapes.
    Implements several methods that are all effectively subsets of the union.

    See the User Guide page :doc:`../../user_guide/set_operations` for details.

    Parameters
    ----------
    df1 : GeoDataFrame
    df2 : GeoDataFrame
    how : string
        Method of spatial overlay: 'intersection', 'union',
        'identity', 'symmetric_difference' or 'difference'.
    keep_geom_type : bool
        If True, return only geometries of the same geometry type as df1 has,
        if False, return all resulting geometries. Default is None,
        which will set keep_geom_type to True but warn upon dropping
        geometries.
    make_valid : bool, default True
        If True, any invalid input geometries are corrected with a call to make_valid(),
        if False, a `ValueError` is raised if any input geometries are invalid.

    Returns
    -------
    df : GeoDataFrame
        GeoDataFrame with new set of polygons and attributes
        resulting from the overlay

    Examples
    --------
    >>> from shapely.geometry import Polygon
    >>> polys1 = geopandas.GeoSeries([Polygon([(0,0), (2,0), (2,2), (0,2)]),
    ...                               Polygon([(2,2), (4,2), (4,4), (2,4)])])
    >>> polys2 = geopandas.GeoSeries([Polygon([(1,1), (3,1), (3,3), (1,3)]),
    ...                               Polygon([(3,3), (5,3), (5,5), (3,5)])])
    >>> df1 = geopandas.GeoDataFrame({'geometry': polys1, 'df1_data':[1,2]})
    >>> df2 = geopandas.GeoDataFrame({'geometry': polys2, 'df2_data':[1,2]})

    >>> geopandas.overlay(df1, df2, how='union')
        df1_data  df2_data                                           geometry
    0       1.0       1.0                POLYGON ((2 2, 2 1, 1 1, 1 2, 2 2))
    1       2.0       1.0                POLYGON ((2 2, 2 3, 3 3, 3 2, 2 2))
    2       2.0       2.0                POLYGON ((4 4, 4 3, 3 3, 3 4, 4 4))
    3       1.0       NaN      POLYGON ((2 0, 0 0, 0 2, 1 2, 1 1, 2 1, 2 0))
    4       2.0       NaN  MULTIPOLYGON (((3 4, 3 3, 2 3, 2 4, 3 4)), ((4...
    5       NaN       1.0  MULTIPOLYGON (((2 3, 2 2, 1 2, 1 3, 2 3)), ((3...
    6       NaN       2.0      POLYGON ((3 5, 5 5, 5 3, 4 3, 4 4, 3 4, 3 5))

    >>> geopandas.overlay(df1, df2, how='intersection')
       df1_data  df2_data                             geometry
    0         1         1  POLYGON ((2 2, 2 1, 1 1, 1 2, 2 2))
    1         2         1  POLYGON ((2 2, 2 3, 3 3, 3 2, 2 2))
    2         2         2  POLYGON ((4 4, 4 3, 3 3, 3 4, 4 4))

    >>> geopandas.overlay(df1, df2, how='symmetric_difference')
        df1_data  df2_data                                           geometry
    0       1.0       NaN      POLYGON ((2 0, 0 0, 0 2, 1 2, 1 1, 2 1, 2 0))
    1       2.0       NaN  MULTIPOLYGON (((3 4, 3 3, 2 3, 2 4, 3 4)), ((4...
    2       NaN       1.0  MULTIPOLYGON (((2 3, 2 2, 1 2, 1 3, 2 3)), ((3...
    3       NaN       2.0      POLYGON ((3 5, 5 5, 5 3, 4 3, 4 4, 3 4, 3 5))

    >>> geopandas.overlay(df1, df2, how='difference')
                                                geometry  df1_data
    0      POLYGON ((2 0, 0 0, 0 2, 1 2, 1 1, 2 1, 2 0))         1
    1  MULTIPOLYGON (((3 4, 3 3, 2 3, 2 4, 3 4)), ((4...         2

    >>> geopandas.overlay(df1, df2, how='identity')
       df1_data  df2_data                                           geometry
    0         1       1.0                POLYGON ((2 2, 2 1, 1 1, 1 2, 2 2))
    1         2       1.0                POLYGON ((2 2, 2 3, 3 3, 3 2, 2 2))
    2         2       2.0                POLYGON ((4 4, 4 3, 3 3, 3 4, 4 4))
    3         1       NaN      POLYGON ((2 0, 0 0, 0 2, 1 2, 1 1, 2 1, 2 0))
    4         2       NaN  MULTIPOLYGON (((3 4, 3 3, 2 3, 2 4, 3 4)), ((4...

    See Also
    --------
    sjoin : spatial join
    GeoDataFrame.overlay : equivalent method

    Notes
    -----
    Every operation in GeoPandas is planar, i.e. the potential third
    dimension is not taken into account.
    )r3   unionidentitysymmetric_differencerP   z`how` was 'z' but is expected to be in z4overlay currently only implemented for GeoDataFrames   
stacklevelNTFr   r   z contains mixed geometry types.r3   r      r   r&   r,   c                    | j                         } | j                  j                  t              j	                         r| j
                  j                   }| j                  }rN| j                  ||f   j                         | j                  ||f<   |j                         rt        | dd      } | S |j                         rt        d|j                          d      | S )NPolygonF)r4   keep_geom_type_warningz,You have passed make_valid=False along with zo invalid input geometries. Use make_valid=True or make sure that all geometries are valid before using overlay.)r]   r4   r5   r	   allr   is_validr   r7   r6   any_collection_extract
ValueErrorsum)r   maskrt   r6   s      r   _make_validzoverlay.<locals>._make_valid8  s    WWY<</0446KK(((D**C$&FF49$5$@$@$BtSy!88:,iB 	  Bxxzl #66  	r   ignorezCRS mismatch between the CRS)messagerP   r   r   r   )r3   r   r   r   r!   r"   r   r    ) r   
isinstancer   NotImplementedErrorr
   r   	enumerater4   r5   r	   r   r   r   r   total_boundsr;   r:   r   r   r<   r   r?   warningscatch_warningsfilterwarningsrj   rL   r   r   rv   r   r2   )r@   rA   rz   keep_geom_typer6   allowed_howsr   ir   
poly_checklines_checkpoints_checkbox_gdf1box_gdf2rK   r   r4   s       `            r   overlayr      s1   rL ,;se+F|nUVV#y!ZY%?!B
 	
 c332!%!&C:& S2\\&&'9:>>@
ll''8<<>||(()9:>>@
K67!;%1q5'1P&QRRS n#### qkXa[(x{hqk/I1+!,8A;(1+3MXXbq\''!!!#,,"3"3!!< %	 ( F ##CLL$5$56==?3<<CTCTBUU 
. MM&&q)	
c
C
c
C		 	 	" D2PQ,(c2FN"*34F**,S#6FG^#C-FJ&sC0FOOKK8,1dKCD  $VY8NO
D$/M)D Ds   !BLL!c                    |t         v rt         }n-|t        v rt        }n|t        v rt        }nt        d| d      | j	                         }|j
                  dk(  }|j                         r|j                  }|j                  ||gf   }|j                  d      j                  d      }|j                  d      }|j                  d   }	d |j                  |j
                  j                  |       |f<   |	|j                  j                         j                         z
  }
|j!                  d	
      }||   j"                  |j                  ||f<   nd}
|j                  d   }|j                  |j
                  j                  |         }||j                  d   z
  }|dkD  s|
dkD  r%|r#t%        j&                  d||
z    dt(        d       |S )Nz`geom_type` does not support .GeometryCollectionTr#   )index_partsr   )levellevel_0)byz-`keep_geom_type=True` in overlay resulted in zq dropped geometries of different geometry types than df1 has. Set `keep_geom_type=False` to retain all geometriesr   r   )r	   r   r   	TypeErrorr]   r4   r   r   r7   r2   explodeshaper5   r   isnar   dissolvevaluesr   warnUserWarning)r   r4   r   
geom_typesrK   is_collectiongeom_colcollectionsexplodedorig_num_geoms_explodednum_dropped_collection	dissolvedorig_num_geomsnum_droppeds                 r   r   r   l  s   &&'
	o	%$
	&	&%
7	{!DEEWWYF $$(<<M//jj
!:;***5==$=O''a'0"*.."3GKh((--j998CD#h&7&7&<&<&>&B&B&DD 	 %%%3	.7.A.H.H

=(*+!" \\!_NZZ((--j9:F 6<<?2Ka1A5;Q;334 5 	
 Mr   )r3   NT)r   	functoolsr   numpyr=   pandasr8   	geopandasr   r   geopandas._compatr   geopandas.arrayr   r   r	   r
   r   r   rL   rj   rv   r   r   r   r    r   r   <module>r      sL        - * "-
`4+06	,}@;r   