a
    1$eC                     @   sx  d Z ddlZddlZddlmZmZmZmZm	Z	m
Z
mZ ddlZddlmZmZmZ ddlmZmZ ddlZddlmZ ejjZg dZdd	gZejd
d Zejdd Zejdd Zejdd Zejdd Z ejdd Z!ejdd Z"ejdd Z#ejdd	 Z$ejdd Z%ejdd Z&ejdd Z'ejd d! Z(ejd"d# Z)ejd$d% Z*ejd&d' Z+ejd(d) Z,ejd*d+ Z-d,d- Z.d.d/ Z/ej0d0eG d1d2 d2Z1d3d4 Z2d5d6 Z3d7d8 Z4d9d: Z5d;d< Z6ej0d0ed=d> Z7ej8d?ej0d@e ej9fdA ej9dej9dBfee e gdCdDee e gdCdD: eg dCdDeg dCdD: gdEdF Z;dS )GzTests for the clip module.    N)PolygonPoint
LineString
LinearRingGeometryCollection
MultiPointbox)GeoDataFrame	GeoSeriesclip)assert_geodataframe_equalassert_geoseries_equal)_mask_is_list_like_rectangle)single_rectangle_gdf single_rectangle_gdf_list_bounds!single_rectangle_gdf_tuple_bounds!single_rectangle_gdf_array_boundslarger_single_rectangle_gdf"larger_single_rectangle_gdf_boundsc                  C   s@   t ddgddgddgddgg} tdd	 | D d
gdd}|S )zCreate a point GeoDataFrame.         	      ic                 S   s   g | ]}t |qS  r   .0Zxyr   r   [/var/www/html/django/DPS/env/lib/python3.9/site-packages/geopandas/tools/tests/test_clip.py
<listcomp>*       zpoint_gdf.<locals>.<listcomp>geometry	EPSG:3857columnscrsnparrayr	   ptsgdfr   r   r   	point_gdf&   s    "r-   c                  C   s:   t ddgddgddgg} tdd | D dgdd}|S )	zCreate a point GeoDataFrame. Its points are all outside the single
    rectangle, and its bounds are outside the single rectangle's.         c                 S   s   g | ]}t |qS r   r   r   r   r   r   r    3   r!   z/pointsoutside_nooverlap_gdf.<locals>.<listcomp>r"   r#   r$   r'   r*   r   r   r   pointsoutside_nooverlap_gdf.   s    r1   c                  C   s:   t ddgddgddgg} tdd | D dgdd}|S )zCreate a point GeoDataFrame. Its points are all outside the single
    rectangle, and its bounds are overlapping the single rectangle's.r.   r/   c                 S   s   g | ]}t |qS r   r   r   r   r   r   r    <   r!   z-pointsoutside_overlap_gdf.<locals>.<listcomp>r"   r#   r$   r'   r*   r   r   r   pointsoutside_overlap_gdf7   s    r2   c                  C   s*   t g d} tdg| gdd}d|d< |S )z'Create a single rectangle for clipping.r   r   )r   
   )r5   r5   r5   r   r4      r#   r"   r&   site-boundaryattr2r   r	   Zpoly_intersr,   r   r   r   r   @   s    r   c                 C   s
   t | jS z&Bounds of the created single rectangletupletotal_boundsr   r   r   r   r   I   s    r   c                 C   s
   t | jS r=   )listr@   rA   r   r   r   r   O   s    r   c                 C   s   | j S r=   )r@   rA   r   r   r   r   U   s    r   c                  C   s,   t g d} tdg| gdd}dg|d< |S )zCreate a slightly larger rectangle for clipping.
    The smaller single rectangle is used to test the edge case where slivers
    are returned when you clip polygons. This fixture is larger which
    eliminates the slivers in the clip return.
    )rD   )rD   r/   )r/   r/   )r/   rD   rC   r7   r#   r8   z
study arear:   r;   r<   r   r   r   r   [   s    
c                 C   s
   t | jS r=   r>   )r   r   r   r   r   h   s    c                 C   s   | }| d|d< d|d< |S )z(Buffer points to create a multi-polygon.r   r"   Zplottype)buffer)r-   Zbuffered_locsr   r   r   buffered_locationsn   s    rG   c                 C   s   t j| |dd}|S )z4Make a geometry with a hole in the middle (a donut).symmetric_difference)how)	geopandasoverlay)rG   r   Zdonutr   r   r   donut_geometryw   s    rL   c                  C   s2   t g d} t g d}tddg| |gdd}|S )zCreate Line Objects For Testing)r7   r7   r   r   r   r   r.   r   )r   r   )r.         r   r5   r.   r   g      @r7   r   r#   r8   r   r	   ZlineaZlinebr,   r   r   r   two_line_gdf   s    rY   c                 C   s$   | j }tt|dd}dg|d< |S )z$Create a multi-polygon GeoDataFrame.r#   r8   poolattr)unary_unionr	   r
   )rL   Z
multi_polyout_dfr   r   r   multi_poly_gdf   s    
r^   c                 C   s6   | j }tg d}tt||gdd}ddg|d< |S )zVCreate a multi-line GeoDataFrame.
    This GDF has one multiline and one regular line.))r   r7   )r   r7   )r   r7   r.   r   r#   r8   Zroadstreamr[   )r\   r   r	   r
   )rY   Zmultiline_featZlinecr]   r   r   r   
multi_line   s
    ra   c                 C   s@   | j }tt|tddtddtddgdd}g d	|d
< |S )z"Create a multi-point GeoDataFrame.r   r.   iiir   r#   r8   )treezanother treeZshrubZberriesr[   )r\   r	   r
   r   )r-   multi_pointr]   r   r   r   rc      s    rc   c                  C   sL   t dd} tg d}tg d}tg d}tg d| |||gdd}|S )1Create a Mixed Polygon and LineString For Testingr   r   rM   rN   rO   rP   )rT   r7   rQ   r_   rS   rU   rV   )r7   r   r   r   r#   r8   )r   r   r   r   r	   )pointlinepolyringr,   r   r   r   	mixed_gdf   s    
rk   c                  C   s8   t dd} tg d}t| |g}tdg|gdd}|S )rd   r   r   rf   r7   r#   r8   )r   r   r   r	   )rg   ri   Zcollr,   r   r   r   geomcol_gdf   s
    
rl   c                  C   s2   t g d} t g d}tddg| |gdd}|S )z4Create a line that will create a point when clipped.)rU   )   r.   )r/   r.   re   r7   r   r#   r8   rW   rX   r   r   r   sliver_line   s    rn   c                 C   s   t t td|  W d   n1 s*0    Y  t t t| d W d   n1 s^0    Y  t t t| d W d   n1 s0    Y  t t t| d W d   n1 s0    Y  dS )z/Non-GeoDataFrame inputs raise attribute errors.)r   r   NZfoobar)r7   r   r   )r7   r   r   r   r.   )pytestZraises	TypeErrorr   rA   r   r   r   test_not_gdf   s    (((rq   c                  C   sv   t g d} tdg| gdd}| }|jdd }t||}t||jdd  t|j|}t|t	|j
d	 dS )
zCTest that a bounding box returns empty if the extents don't overlap)r4   )r   r7   rM   )r7   r   r4   r7   r#   r8   c                 S   s   t jj| ddS )Nr0   )Zxoff)shapelyZaffinity	translate)xr   r   r   <lambda>   r!   z,test_non_overlapping_geoms.<locals>.<lambda>Nr   r&   )r   r	   copyr"   applyr   r   ilocr   r
   r&   )Zunit_boxZunit_gdfZnon_overlapping_gdfoutZout2r   r   r   test_non_overlapping_geoms   s    
r{   mask_fixture_namec                   @   s   e Zd Zejdd Zdd Zdd Zdd Zd	d
 Z	dd Z
dd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd Zdd  Zd!d" Zd#d$ Zd%d& Zd'S )(TestClipWithSingleRectangleGdfc                 C   s
   | |S )N)getfixturevalue)selfr|   requestr   r   r   mask   s    z#TestClipWithSingleRectangleGdf.maskc                 C   s   t ||}t|tsJ dS )z?Test that function returns a GeoDataFrame (or GDF-like) object.N)r   
isinstancer	   r   r-   r   rz   r   r   r   test_returns_gdf   s    
z/TestClipWithSingleRectangleGdf.test_returns_gdfc                 C   s   t |j|}t|tsJ dS )z>Test that function returns a GeoSeries if GeoSeries is passed.N)r   r"   r   r
   r   r   r   r   test_returns_series   s    z2TestClipWithSingleRectangleGdf.test_returns_seriesc                 C   sN   t ||}tddgddgddgg}tdd |D dgd	d
}t|| dS );Test clipping a points GDF with a generic polygon geometry.r   r   r   r   r   c                 S   s   g | ]}t |qS r   r   r   r   r   r   r       r!   zCTestClipWithSingleRectangleGdf.test_clip_points.<locals>.<listcomp>r"   r#   r$   N)r   r(   r)   r	   r   )r   r-   r   clip_ptsr+   expr   r   r   test_clip_points   s    
z/TestClipWithSingleRectangleGdf.test_clip_pointsc                 C   sZ   | d}t||}tddgddgddgg}tdd |D dgd	dd
}t|| dS )r   	geometry2r   r   r   r   r   c                 S   s   g | ]}t |qS r   r   r   r   r   r   r      r!   zSTestClipWithSingleRectangleGdf.test_clip_points_geom_col_rename.<locals>.<listcomp>r#   )r%   r&   r"   N)rename_geometryr   r(   r)   r	   r   )r   r-   r   Zpoint_gdf_geom_col_renamer   r+   r   r   r   r    test_clip_points_geom_col_rename   s    

z?TestClipWithSingleRectangleGdf.test_clip_points_geom_col_renamec                 C   s2   t ||}t|jdksJ t|jdks.J dS <Test clipping a polygon GDF with a generic polygon geometry.r   r   N)r   lenr"   all	geom_typer   rG   r   clipped_polyr   r   r   test_clip_poly  s    
z-TestClipWithSingleRectangleGdf.test_clip_polyc                 C   sJ   | d}t||}t|jdks&J d| vs6J d| v sFJ dS )r   r   r   r"   N)r   r   r   r"   keys)r   rG   r   Zpoly_gdf_geom_col_renamer   r   r   r   test_clip_poly_geom_col_rename  s
    

z=TestClipWithSingleRectangleGdf.test_clip_poly_geom_col_renamec                 C   s2   t |j|}t|dksJ t|jdks.J dS r   )r   r"   r   r   r   r   r   r   r   test_clip_poly_series  s    z4TestClipWithSingleRectangleGdf.test_clip_poly_seriesc                 C   sN   t ||dd}t|r|n|j}t|j|s2J |jddg sJJ dS )zTest a multi poly object where the return includes a sliver.
        Also the bounds of the object should == the bounds of the clip object
        if they fully overlap (as they do in these fixtures).TZkeep_geom_typer   ZMultiPolygonN)r   r   r@   r(   array_equalr   isinr   )r   r^   r   clippedZexpected_boundsr   r   r   "test_clip_multipoly_keep_geom_type!  s
    zATestClipWithSingleRectangleGdf.test_clip_multipoly_keep_geom_typec                 C   s    t ||}|jd dksJ dS )zSTest that clipping a multiline feature with a poly returns expected
        output.r   ZMultiLineStringNr   r   )r   ra   r   r   r   r   r   test_clip_multiline-  s    
z2TestClipWithSingleRectangleGdf.test_clip_multilinec                 C   s   t ||}|jd dksJ t|ds*J t|dks:J ttddtddtddg}|jd jj|jksrJ t	|rt
| n|j}t||sJ d	S )
zClipping a multipoint feature with a polygon works as expected.
        should return a geodataframe with a single multi point featurer   r   r[   r   r   r   r   r   N)r   r   hasattrr   r   r   ry   r"   Zwktr   r   r\   r   Z
intersects)r   rc   r   r   Zclipped_mutltipointZshape_for_pointsr   r   r   test_clip_multipoint3  s    
z3TestClipWithSingleRectangleGdf.test_clip_multipointc                 C   s    t ||}t|jdksJ dS )z;Test what happens when you give the clip_extent a line GDF.r   N)r   r   r"   )r   rY   r   Z	clip_liner   r   r   test_clip_linesH  s    
z.TestClipWithSingleRectangleGdf.test_clip_linesc                 C   s<   t ||}|jd dkr4|jd dkr4|jd dks8J dS )z"Test clipping a mixed GeoDataFramer   r   r7   r   r   r   Nr   r   rk   r   r   r   r   r   test_mixed_geomM  s    
z.TestClipWithSingleRectangleGdf.test_mixed_geomc                 C   s>   t |j|}|jd dkr6|jd dkr6|jd dks:J dS )zTest clipping a mixed GeoSeriesr   r   r7   r   r   r   N)r   r"   r   r   r   r   r   test_mixed_seriesV  s    z0TestClipWithSingleRectangleGdf.test_mixed_seriesc                 C   s6   t ||dd}t|jdks J |jdk r2J dS )zWhen the output of a clipped line returns a geom collection,
        and keep_geom_type is True, no geometry collections should be returned.Tr   r7   r   N)r   r   r"   r   any)r   rn   r   r   r   r   r   test_clip_with_line_extra_geom_  s    z=TestClipWithSingleRectangleGdf.test_clip_with_line_extra_geomc                 C   s   t ||}t|dksJ dS )z>Test clip when intersection is empty and boxes do not overlap.r   Nr   r   )r   r1   r   r   r   r   r   test_clip_no_box_overlapg  s    
z7TestClipWithSingleRectangleGdf.test_clip_no_box_overlapc                 C   s   t ||}t|dksJ dS )z:Test clip when intersection is empty and boxes do overlap.r   Nr   )r   r2   r   r   r   r   r   test_clip_box_overlapl  s    
z4TestClipWithSingleRectangleGdf.test_clip_box_overlapc                 C   s<   t t t||dd W d   n1 s.0    Y  dS )zWTest the correct warnings are raised if keep_geom_type is
        called on a mixed GDFTr   Nro   warnsUserWarningr   )r   rk   r   r   r   r   test_warning_extra_geoms_mixedq  s    z=TestClipWithSingleRectangleGdf.test_warning_extra_geoms_mixedc                 C   s<   t t t||dd W d   n1 s.0    Y  dS )ziTest the correct warnings are raised if keep_geom_type is
        called on a GDF with GeometryCollectionTr   Nr   )r   rl   r   r   r   r   test_warning_geomcollw  s    z4TestClipWithSingleRectangleGdf.test_warning_geomcollN)__name__
__module____qualname__ro   fixturer   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r}      s(   
				r}   c                 C   s2   t | |}d|jd ksJ d|jd ks.J dS )zRTest the correct output if a point is returned
    from a line only geometry type.r   r   r   r7   Nr   )rn   r   r   r   r   r   test_clip_line_keep_slivers~  s    
r   c                 C   s4   t | |}t|j|jsJ d|jd v s0J dS )zTest a multi poly object where the return includes a sliver.
    Also the bounds of the object should == the bounds of the clip object
    if they fully overlap (as they do in these fixtures).r   r   N)r   r(   r   r@   r   )r^   r   r   r   r   r    test_clip_multipoly_keep_slivers  s    
r   c                 C   sB   t jtdd  t| |d W d    n1 s40    Y  d S )NzCRS mismatch between the CRS)matchi  )ro   r   r   r   Zto_crs)r-   r   r   r   r   test_warning_crs_mismatch  s    r   c                 C   sP   t g d}t| |}|t g d}tdg|gdd}d|d< t|| dS )	z%Test clip when using a shapely object)r4   )r.   rT   r6   r4   r3   r7   r#   r8   r9   r:   N)r   r   intersectionr	   r   )r   Zpolygonr   Zexp_polyr   r   r   r   test_clip_with_polygon  s    

r   c                 C   s0   | j dd }t||}|jd dks,J dS )z,Test clipping a polygon with a multipolygon.rE   Zbyr   r   N)dissolvereset_indexr   r   )rG   r   multir   r   r   r   test_clip_with_multipolygon  s    
r   c                 C   s:   | |}| jdd }t||}|jd dks6J dS )zWWhen clipping a multi-polygon feature, no additional geom types
    should be returned.rE   r   r   r   N)r~   r   r   r   r   )rG   r|   r   masksr   r   r   r   r   )test_clip_single_multipoly_no_extra_geoms  s    	

r   z ignore:All-NaN slice encounteredr   r   r7   r#   rv   c                 C   sH   t | |}t|tg ddgdddd t | j|}t|tg dd dS )	z;Test that clipping with empty mask returns an empty result.r"   rE   r#   r$   F)Zcheck_index_typerv   N)r   r   r	   r"   r   r
   )rG   r   r   r   r   r   test_clip_empty_mask  s    
r   )<__doc__numpyr(   rr   Zshapely.geometryr   r   r   r   r   r   r   rJ   r	   r
   r   Zgeopandas.testingr   r   ro   Zgeopandas.tools.clipr   markZskip_no_sindexZ
pytestmarkZmask_variants_single_rectangleZmask_variants_large_rectangler   r-   r1   r2   r   r   r   r   r   r   rG   rL   rY   r^   ra   rc   rk   rl   rn   rq   r{   Zparametrizer}   r   r   r   r   r   r   filterwarningsnanZto_framer   r   r   r   r   <module>   s   $

















	
 	




