
    h%                     v    d Z ddlZddlZddlZddlmZmZm	Z	 ddl
mZmZ ddlmZmZmZmZmZ d Zd	dZd
dZy)z+Module to clip vector data using GeoPandas.    N)MultiPolygonPolygonbox)GeoDataFrame	GeoSeries)LINE_GEOM_TYPESPOINT_GEOM_TYPESPOLYGON_GEOM_TYPES
_check_crs_crs_mismatch_warnc                     t         j                  j                  j                  |       xr& t	        | t
        t        z  t        z  t        z         S )a  
    Check if the input mask is list-like and not an instance of
    specific geometric types.

    Parameters
    ----------
    mask : GeoDataFrame, GeoSeries, (Multi)Polygon, list-like
        Polygon vector layer used to clip ``gdf``.

    Returns
    -------
    bool
        True if `mask` is list-like and not an instance of `GeoDataFrame`,
        `GeoSeries`, `Polygon`, or `MultiPolygon`, otherwise False.
    )	pandasapitypesis_list_like
isinstancer   r   r   r   )masks    G/var/www/html/immo/lib/python3.12/site-packages/geopandas/tools/clip.py_mask_is_list_like_rectangler      sF      ::((. zlY&0<?8 4     c                    t        |      }|r	t        | }n|}| j                  | j                  j	                  |d|         }|j
                  dk7  }|j                         s|S t        |t              r|j                         }|r@ |j                  j                  |   j                  | |j                  ||j                  f<   n|j                  j                  |   j                  |      |j                  ||j                  f<   nS|j                         }|r  |j                  |   j                  | ||<   n!|j                  |   j                  |      ||<   |r||j                       }|S )as  
    Clip geometry to the polygon/rectangle extent.

    Clip an input GeoDataFrame to the polygon extent of the polygon
    parameter.

    Parameters
    ----------
    gdf : GeoDataFrame, GeoSeries
        Dataframe to clip.

    mask : (Multi)Polygon, list-like
        Reference polygon/rectangle for clipping.

    sort : boolean, default False
        If True, the results will be sorted in ascending order using the
        geometries' indexes as the primary key.

    Returns
    -------
    GeoDataFrame
        The returned GeoDataFrame is a clipped subset of gdf
        that intersects with polygon/rectangle.
    
intersects)	predicatesortPoint)r   r   ilocsindexquery	geom_typeanyr   r   copygeometryvaluesclip_by_rectloc_geometry_column_nameintersectionis_empty)gdfr   r   clipping_by_rectangleintersection_polygongdf_subnon_point_maskclippeds           r   _clip_gdf_with_maskr/   )   sf   2 9>"Dz#hh

-DQG
 &&'1N '<(,,. D  ''7DDdK KK(E(EEF
   ''7DDTJ KK(E(EEF
 ,,. &Qgnn^&D&Q&QSW&XGN#&-nn^&D&Q&QRV&WGN#7+++,Nr   c                    t        | t        t        z        st        dt	        |              t        |      }t        |t        t        z  t        z  t        z        s|st        dt	        |             |rt        |      dk7  rt        d      t        |t        t        z        rt        | |      st        | |d       t        |t        t        z        r|j                  }n1|r|}n,|j                  s|j                  nt        j                  fdz  }| j                  }|d   |d   k  r!|d   |d   k  r|d	   |d   k  r|d	   |d   k  s| j                   d
d S t        |t        t        z        r|j"                  j%                         }n|}t'        | ||      }|r8|j(                  dk(  j+                         }	| j(                  dk(  j+                         }
|	xr |
 }|
rt-        j.                  dd       |S t1        | j(                  j3                  t4              j+                         | j(                  j3                  t6              j+                         | j(                  j3                  t8              j+                         g      }t1        |j(                  j3                  t4              j+                         |j(                  j3                  t6              j+                         |j(                  j3                  t8              j+                         g      }||k  }|d	kD  rt-        j.                  dd       |S |s|r| j(                  j                   d   }|r|j;                  d      }|t4        v r.|j<                  |j(                  j3                  t4                 }|S |t6        v r,|j<                  |j(                  j3                  t6                 }|S )as
  Clip points, lines, or polygon geometries to the mask extent.

    Both layers must be in the same Coordinate Reference System (CRS).
    The ``gdf`` will be clipped to the full extent of the clip object.

    If there are multiple polygons in mask, data from ``gdf`` will be
    clipped to the total boundary of all polygons in mask.

    If the ``mask`` is list-like with four elements ``(minx, miny, maxx, maxy)``, a
    faster rectangle clipping algorithm will be used. Note that this can lead to
    slightly different results in edge cases, e.g. if a line would be reduced to a
    point, this point might not be returned.
    The geometry is clipped in a fast but possibly dirty way. The output is not
    guaranteed to be valid. No exceptions will be raised for topological errors.

    Parameters
    ----------
    gdf : GeoDataFrame or GeoSeries
        Vector layer (point, line, polygon) to be clipped to mask.
    mask : GeoDataFrame, GeoSeries, (Multi)Polygon, list-like
        Polygon vector layer used to clip ``gdf``.
        The mask's geometry is dissolved into one geometric feature
        and intersected with ``gdf``.
        If the mask is list-like with four elements ``(minx, miny, maxx, maxy)``,
        ``clip`` will use a faster rectangle clipping (:meth:`~GeoSeries.clip_by_rect`),
        possibly leading to slightly different results.
    keep_geom_type : boolean, default False
        If True, return only geometries of original type in case of intersection
        resulting in multiple geometry types or GeometryCollections.
        If False, return all resulting geometries (potentially mixed-types).
    sort : boolean, default False
        If True, the results will be sorted in ascending order using the
        geometries' indexes as the primary key.

    Returns
    -------
    GeoDataFrame or GeoSeries
         Vector data (points, lines, polygons) from ``gdf`` clipped to
         polygon boundary from mask.

    See Also
    --------
    GeoDataFrame.clip : equivalent GeoDataFrame method
    GeoSeries.clip : equivalent GeoSeries method

    Examples
    --------
    Clip points (grocery stores) with polygons (the Near West Side community):

    >>> import geodatasets
    >>> chicago = geopandas.read_file(
    ...     geodatasets.get_path("geoda.chicago_health")
    ... )
    >>> near_west_side = chicago[chicago["community"] == "NEAR WEST SIDE"]
    >>> groceries = geopandas.read_file(
    ...     geodatasets.get_path("geoda.groceries")
    ... ).to_crs(chicago.crs)
    >>> groceries.shape
    (148, 8)

    >>> nws_groceries = geopandas.clip(groceries, near_west_side)
    >>> nws_groceries.shape
    (7, 8)
    z/'gdf' should be GeoDataFrame or GeoSeries, got zJ'mask' should be GeoDataFrame, GeoSeries,(Multi)Polygon or list-like, got    zIIf 'mask' is list-like, it must have four values (minx, miny, maxx, maxy)   )
stacklevelr         N)r   GeometryCollectionzKkeep_geom_type can not be called on a GeoDataFrame with GeometryCollection.z>keep_geom_type can not be called on a mixed type GeoDataFrame.F)index_parts)r   r   r   	TypeErrortyper   r   r   lenr   r   total_boundsr(   boundsnpnanr   r"   	union_allr/   r   r    warningswarnsumisinr
   r   r	   exploder%   )r)   r   keep_geom_typer   r*   box_maskbox_gdfcombined_maskr.   geomcoll_concatgeomcoll_orignew_collectionorig_types_totalclip_types_total
more_types	orig_types                   r   cliprP   l   sp   B c<)34I$s)UVV8>t\I5?,NO%004T
|=
 	

 TaW
 	
 $y01#t$sDQ7$y01$$	 '+mm4;;"&&QG
1+
#'!**CqkWQZ'gajHQK.Gxx|$y01//1!#}4@G",,0DDIIK*>>CCE(>->MM8R NE  #MM&&'9:>>@MM&&7;;=MM&&'78<<>   #%%**+=>BBD%%**?;??A%%**+;<@@B  *,<<J!#T  N  :MM..q1	!%oo%o@G 22%kk'*;*;*@*@AS*TUG N /1%kk'*;*;*@*@*QRGNr   )F)FF)__doc__r@   numpyr=   pandas.api.typesr   shapely.geometryr   r   r   	geopandasr   r   geopandas.arrayr   r	   r
   r   r   r   r/   rP    r   r   <module>rX      s6    1    7 7 - *@F_r   