a
    1$e"                     @   sn   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 dd Zdd	 ZdddZdS )zO
geopandas.clip
==============

A module to clip vector data using GeoPandas.

    N)PolygonMultiPolygonbox)GeoDataFrame	GeoSeries)
_check_crs_crs_mismatch_warnc                 C   s"   t jj| o t| ttttf S )N)	pandasapitypesZis_list_like
isinstancer   r   r   r   )mask r   P/var/www/html/django/DPS/env/lib/python3.9/site-packages/geopandas/tools/clip.py_mask_is_list_like_rectangle   s    r   c                 C   s   t |}|rt| }n|}| j| jj|dd }|jdk}| sF|S t|tr|	 }|r||j
j| j| |j||jf< q|j
j| ||j||jf< n6|	 }|r|j| j| ||< n|j| |||< |r||j  }|S )a  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.

    Returns
    -------
    GeoDataFrame
        The returned GeoDataFrame is a clipped subset of gdf
        that intersects with polygon/rectangle.
    Z
intersects)	predicatePoint)r   r   ilocZsindexquery	geom_typeanyr   r   copygeometryvaluesZclip_by_rectlocZ_geometry_column_nameintersectionis_empty)gdfr   Zclipping_by_rectangleZintersection_polygonZgdf_subZnon_point_maskclippedr   r   r   _clip_gdf_with_mask   s4    


r   Fc                 C   s  t | ttfs tdt| t|}t |ttttfsP|sPtdt| |rht	|dkrhtdt |ttfrt
| |st| |dd t |ttfr|j}n"|r|}n|js|jn
tjfd }| j}|d |d kr|d |d kr|d	 |d kr|d	 |d ks"| jd
d S t |ttfr<|jj}n|}t| |}|r|jdk }| jdk }	|ov|	 }
|	rtjddd nddg}g d}ddg}t| j| | j| | j| g}t|j| |j| |j| g}||k }|d	kr.tjddd nd|
s:|r| jjd }|
rX|jdd}||v rv|j|j| }n||v r|j|j| }|S )a	  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).

    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)
    z1'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         NZGeometryCollectionzKkeep_geom_type can not be called on a GeoDataFrame with GeometryCollection.r   r   )Z
LineStringZMultiLineStringZ
LinearRingr   Z
MultiPointz>keep_geom_type can not be called on a mixed type GeoDataFrame.F)Zindex_parts)r   r   r   	TypeErrorformattyper   r   r   lenr   r   Ztotal_boundsr   Zboundsnpnanr   r   Zunary_unionr   r   r   warningswarnsumisinZexploder   )r   r   Zkeep_geom_typeZmask_is_list_likeZbox_maskZbox_gdfZcombined_maskr   Zgeomcoll_concatZgeomcoll_origZnew_collectionZpolyslinesZpointsZorig_types_totalZclip_types_totalZ
more_types	orig_typer   r   r   clipT   s    >



		


r1   )F)__doc__r+   numpyr)   Zpandas.api.typesr	   Zshapely.geometryr   r   r   Z	geopandasr   r   Zgeopandas.arrayr   r   r   r   r1   r   r   r   r   <module>   s   <