
    (&hOd                     @   d Z ddlZddlmZ ddlmZ ddlmZmZm	Z	m
Z
mZmZmZ ddlmZ ddlmZ g dZ G d	 d
          Z e            Zej        Zej        Zej        Zej        ZddZddZd Zd Zd Zd Zd Z G d d          Z e j!        Z!ddZ"d Z#ddZ$dS )z-Support for various GEOS geometry operations.    N)	polylabel)GeometryTypeError)GeometryCollection
LineStringMultiLineString
MultiPointPointPolygonshape)BaseGeometry)prep)clip_by_rect	linemergenearest_pointsoperatororient
polygonizepolygonize_fullshared_pathssnapsplit	substring	transformtriangulateunary_unionvalidatevoronoi_diagramc                   .    e Zd Zd Zd Zd ZddZd ZdS )	CollectionOperatorc                     t          |t                    r|S 	 t          |          S # t          t          f$ r t          |          cY S w xY wN)
isinstancer   r   
ValueErrorAttributeErrorr   )selfobs     F/var/www/html/reinick/venv/lib/python3.11/site-packages/shapely/ops.pyshapeupzCollectionOperator.shapeup'   s\    b,'' 	&I&Ryy / & & &!"~~%%%&s   (  A
Ac                      t          |dd          p|}	 t          |          }n# t          $ r |g}Y nw xY w fd|D             }n#  fd|D             }w xY wt          j        |          }|j        S )zCreate polygons from a source of lines.

        The source may be a MultiLineString, a sequence of LineString objects,
        or a sequence of objects than can be adapted to LineStrings.
        geomsNc                 :    g | ]}                     |          S  r(   .0liner%   s     r'   
<listcomp>z1CollectionOperator.polygonize.<locals>.<listcomp><   %    999$4<<%%999    )getattriter	TypeErrorshapelyr   r*   )r%   linessourceobs
collections   `    r'   r   zCollectionOperator.polygonize0   s     ..7%	:&\\FF 	 	 	XFFF	 :999&999CC9999&999C9999',,
!   & A 6A 6A Ac                      t          |dd          p|}	 t          |          }n# t          $ r |g}Y nw xY w fd|D             }n#  fd|D             }w xY wt          j        |          S )a  Create polygons from a source of lines.

        The polygons and leftover geometries are returned as well.

        The source may be a MultiLineString, a sequence of LineString objects,
        or a sequence of objects than can be adapted to LineStrings.

        Returns a tuple of objects: (polygons, cut edges, dangles, invalid ring
        lines). Each are a geometry collection.

        Dangles are edges which have one or both ends which are not incident on
        another edge endpoint. Cut edges are connected at both ends but do not
        form part of polygon. Invalid ring lines form rings which are invalid
        (bowties, etc).
        r*   Nc                 :    g | ]}                     |          S r,   r-   r.   s     r'   r1   z6CollectionOperator.polygonize_full.<locals>.<listcomp>V   r2   r3   )r4   r5   r6   r7   r   )r%   r8   r9   r:   s   `   r'   r   z"CollectionOperator.polygonize_full@   s      ..7%	:&\\FF 	 	 	XFFF	 :999&999CC9999&999C9999&s+++r<   Fc                 |   d}t          |dd          dk    r|}nyt          |d          rt          d |j        D                       }nJt          |d          r:	 t          d |D                       }n# t          $ r t          |          }Y nw xY w|t          d|           t          j        ||	          S )
a  Merge all connected lines from a source.

        The source may be a MultiLineString, a sequence of LineString objects,
        or a sequence of objects than can be adapted to LineStrings.  Returns a
        LineString or MultiLineString when lines are not contiguous.
        N	geom_typer   r*   c                     g | ]	}|j         
S r,   coordsr/   lss     r'   r1   z0CollectionOperator.linemerge.<locals>.<listcomp>e   s    %F%F%FBbi%F%F%Fr3   __iter__c                     g | ]	}|j         
S r,   rB   rD   s     r'   r1   z0CollectionOperator.linemerge.<locals>.<listcomp>h   s    )D)D)D"))D)D)Dr3   zCannot linemerge )directed)r4   hasattrr   r*   r$   r#   r7   
line_merge)r%   r8   rH   r9   s       r'   r   zCollectionOperator.linemergeY   s     5+t,,0AAAFFUG$$ 	0$%F%F%+%F%F%FGGFFUJ'' 	00()D)De)D)D)DEE! 0 0 0(//0>888999!&8<<<<s   A5 5BBc                 .    t          j        |d          S )zReturn the union of a sequence of geometries.

        Usually used to convert a collection into the smallest set of polygons
        that cover the same area.
        N)axis)r7   	union_all)r%   r*   s     r'   r   zCollectionOperator.unary_uniono   s      T2222r3   NF)__name__
__module____qualname__r(   r   r   r   r   r,   r3   r'   r   r   &   sd        & & &      , , ,2= = = =,3 3 3 3 3r3   r           Fc                 X    t          j        | ||          }t          |j                  S )a  Create the Delaunay triangulation and return a list of geometries.

    The source may be any geometry type. All vertices of the geometry will be
    used as the points of the triangulation.

    From the GEOS documentation:
    tolerance is the snapping tolerance used to improve the robustness of
    the triangulation computation. A tolerance of 0.0 specifies that no
    snapping will take place.

    If edges is False, a list of Polygons (triangles) will be returned.
    Otherwise the list of LineString edges is returned.

    )	tolerance
only_edges)r7   delaunay_triangleslistr*   )geomrT   edgesr;   s       r'   r   r      s-     +DIRWXXXJ
 !!!r3   c                     	 t          j        | |||          }n9# t           j        $ r'}d}|d|dz  }|r|dz  }t          |          |d}~ww xY w|j        dk    rt          |g          S |S )aP  Construct a Voronoi Diagram [1] from the given geometry.

    Returns a list of geometries.

    Parameters
    ----------
    geom: geometry
        the input geometry whose vertices will be used to calculate
        the final diagram.
    envelope: geometry, None
        clipping envelope for the returned diagram, automatically
        determined if None. The diagram will be clipped to the larger
        of this envelope or an envelope surrounding the sites.
    tolerance: float, 0.0
        sets the snapping tolerance used to improve the robustness
        of the computation. A tolerance of 0.0 specifies that no
        snapping will take place.
    edges: bool, False
        If False, return regions as polygons. Else, return only
        edges e.g. LineStrings.

    GEOS documentation can be found at [2]

    Returns
    -------
    GeometryCollection
        geometries representing the Voronoi regions.

    Notes
    -----
    The tolerance `argument` can be finicky and is known to cause the
    algorithm to fail in several cases. If you're using `tolerance`
    and getting a failure, try removing it. The test cases in
    tests/test_voronoi_diagram.py show more details.


    References
    ----------
    [1] https://en.wikipedia.org/wiki/Voronoi_diagram
    [2] https://geos.osgeo.org/doxygen/geos__c_8h_source.html  (line 730)

    )rT   	extend_torU   z;Could not create Voronoi Diagram with the specified inputs (z).z0 Try running again with default tolerance value.Nr   )r7   voronoi_polygonsGEOSExceptionr#   r@   r   )rX   enveloperT   rY   resulterrerrstrs          r'   r   r      s    V	*)Ie
 
 
   * * *N-c--- 	IHHF  c)* ///!6(+++Ms    A"AAc                 *    t          j        |           S )z%Return True if the geometry is valid.)r7   is_valid_reason)rX   s    r'   r   r      s    "4(((r3   c           	          |j         r|S |j        dv rZ	 |j        dv r. t          |          t            t          |j                     S |j        dk    rd t          |j                  t            t          |j        j                     } fd|j        D             } t          |          ||          S dS # t          $ r |j        dv r+ t          |           fd|j        D                       cY S |j        dk    ra t          |j                   fd|j        j        D                       } fd|j        D             } t          |          ||          cY S Y dS w xY w|j                            d          s|j        d	k    r) t          |           fd
|j	        D                       S t          d|j        d          )am  Apply `func` to all coordinates of `geom`.

    Returns a new geometry of the same type from the transformed coordinates.

    `func` maps x, y, and optionally z to output xp, yp, zp. The input
    parameters may iterable types like lists or arrays or single values.
    The output shall be of the same type. Scalars in, scalars out.
    Lists in, lists out.

    For example, here is an identity function applicable to both types
    of input.

      def id_func(x, y, z=None):
          return tuple(filter(None, [x, y, z]))

      g2 = transform(id_func, g1)

    Using pyproj >= 2.1, this example will accurately project Shapely geometries:

      import pyproj

      wgs84 = pyproj.CRS('EPSG:4326')
      utm = pyproj.CRS('EPSG:32618')

      project = pyproj.Transformer.from_crs(wgs84, utm, always_xy=True).transform

      g2 = transform(project, g1)

    Note that the always_xy kwarg is required here as Shapely geometries only support
    X,Y coordinate ordering.

    Lambda expressions such as the one in

      g2 = transform(lambda x, y, z=None: (x+1.0, y+1.0), g1)

    also satisfy the requirements for `func`.
    )r	   r   
LinearRingr
   )r	   r   rf   r
   c                 l    g | ]0} t          |          t           t          |j                     1S r,   )typeziprC   r/   ringfuncs     r'   r1   ztransform.<locals>.<listcomp>  sL        DJJsDD#t{*;$<=>>  r3   c                     g | ]} | S r,   r,   r/   crl   s     r'   r1   ztransform.<locals>.<listcomp>  s    "A"A"A448"A"A"Ar3   c                     g | ]} | S r,   r,   rn   s     r'   r1   ztransform.<locals>.<listcomp>  s    ,T,T,T!TT1X,T,T,Tr3   c                 b    g | ]+} t          |          fd |j        D                       ,S )c                     g | ]} | S r,   r,   rn   s     r'   r1   z(transform.<locals>.<listcomp>.<listcomp>  s    >>>Qa>>>r3   )rh   rC   rj   s     r'   r1   ztransform.<locals>.<listcomp>  sP        DJJ>>>>$+>>>??  r3   Multir   c                 0    g | ]}t          |          S r,   )r   )r/   partrl   s     r'   r1   ztransform.<locals>.<listcomp>  s#    HHHT9T400HHHr3   zType z not recognizedN)is_emptyr@   rh   ri   rC   exterior	interiorsr6   
startswithr*   r   )rl   rX   shellholess   `   r'   r   r      sK   L } ~III
	0~!FFF!tDzz#ttS$+->'?"@AAA9,,+T]++CsDM<P7Q1R,STT    $   "tDzz%/// -,  		0 		0 		0~!FFF!tDzz"A"A"A"AT["A"A"ABBBBB9,,+T]++,T,T,T,Tt}?S,T,T,TUU    $   "tDzz%///// -,,		0 
	"	"7	+	+ Kt~AU/U/UtDzzHHHHTZHHHIII I I I IJJJs   6B> A.B> ><E,<A*E,+E,c                     t          j        | |          }|%| j        rt          d          t          d          t          j        |d          }t          j        |d          }||fS )zReturn the calculated nearest points in the input geometries.

    The points are returned in the same order as the input geometries.
    Nz!The first input geometry is emptyz"The second input geometry is emptyr      )r7   shortest_linerv   r#   	get_point)g1g2seqp1p2s        r'   r   r     sq    
 
B
'
'C
{; 	C@AAAABBB		3	"	"B		3	"	"B8Or3   c                 .    t          j        | ||          S )a3  Snaps an input geometry (g1) to reference (g2) geometry's vertices.

    Parameters
    ----------
    g1 : geometry
        The first geometry
    g2 : geometry
        The second geometry
    tolerance : float
        The snapping tolerance

    Refer to :func:`shapely.snap` for full documentation.

    )r7   r   )r   r   rT   s      r'   r   r   .  s     <B	***r3   c                     t          | t                    st          d          t          |t                    st          d          t          j        | |          S )a  Find paths shared between the two given lineal geometries.

    Returns a GeometryCollection with two elements:
     - First element is a MultiLineString containing shared paths with the
       same direction for both inputs.
     - Second element is a MultiLineString containing shared paths with the
       opposite direction for the two inputs.

    Parameters
    ----------
    g1 : geometry
        The first geometry
    g2 : geometry
        The second geometry

    z#First geometry must be a LineStringz$Second geometry must be a LineString)r"   r   r   r7   r   )r   r   s     r'   r   r   @  s[    " b*%% G EFFFb*%% H FGGGB'''r3   c                   |    e Zd Zed             Zed             Zed             Zed             Zed             ZdS )SplitOpc                 *    t           t                    st          d          t          |t          t          f          st          d           j                            |          }t                       fdt          |          D             S )z"Split a Polygon with a LineString.z First argument must be a Polygonz+Second argument must be a (Multi)LineStringc                 b    g | ]+}                     |                                          )|,S r,   )containsrepresentative_point)r/   pgpolys     r'   r1   z4SplitOp._split_polygon_with_line.<locals>.<listcomp>k  sF     
 
 
dmmB<S<S<U<U.V.V

 
 
r3   )	r"   r
   r   r   r   boundaryunionr   r   )r   splitterr   s   `  r'   _split_polygon_with_linez SplitOp._split_polygon_with_lineY  s     $(( 	H#$FGGG(Z$ABB 	S#$QRRR##H--
 Dzz

 
 
 
#E**
 
 
 	
r3   c                    |j         dv r|j        }t          | t                    st	          d          t          |t                    s$t          |t
                    st	          d          |                    |           }|d         dk    rt          d          |d         dk    s|d         dk    r|                     |          S | gS )	zDSplit a LineString with another (Multi)LineString or (Multi)Polygon.)r
   MultiPolygon#First argument must be a LineStringz@Second argument must be either a LineString or a MultiLineStringr   1z2Input geometry segment overlaps with the splitter.0   )	r@   r   r"   r   r   r   relater#   
difference)r0   r   relations      r'   _split_line_with_linezSplitOp._split_line_with_lineo  s     !<<<(H$
++ 	K#$IJJJ(J// 	
o9
 9
 	 $R   ??4((A;#QRRRa[C8A;##5#5 ??8,,, 6Mr3   c                 ^   t          | t                    st          d          t          |t                    st          d          |                     |d          s| gS | j        d         |j        d         k    r| gS |                     |          }t          | j                  }d}t          t          |          dz
            D ]}||         }||dz            }|d         |d         z
  }|d         |d         z
  }	|dz  |	dz  z   dz  }
||
z  }||k    r6t          |d	|dz                      t          ||dz   d	                   gc S ||k     rTt          |d	|dz            |j        d         gz             t          |j        d         g||dz   d	         z             gc S | gS )
z Split a LineString with a Point.r   zSecond argument must be a Pointz	0********r   rR   r}            ?N)
r"   r   r   r	   relate_patternrC   projectrW   rangelen)r0   r   distance_on_linerC   current_positionipoint1point2dxdysegment_lengths              r'   _split_line_with_pointzSplitOp._split_line_with_point  s    $
++ 	K#$IJJJ(E** 	G#$EFFF ""8[99 	
 6M[^xq1116M  <<11dk""s6{{Q'' 	 	AAYFAE]FVAY&BVAY&B !eb!em3N.#333"6'AE'?33Zq1uww5P5PQQQQ!$444 vgAg(/!2D1EEFF 23fQUWWoEFF    5 vr3   c                 <   t          | t                    st          d          t          |t                    st          d          | g}|j        D ]H}g }t          d |          D ]0}|                    t                              ||                     1|}I|S )z%Split a LineString with a MultiPoint.r   z$Second argument must be a MultiPointc                     | j          S r!   )rv   )xs    r'   <lambda>z5SplitOp._split_line_with_multipoint.<locals>.<lambda>  s
    aj. r3   )	r"   r   r   r   r*   filterextendr   r   )r0   r   chunkspt
new_chunkschunks         r'   _split_line_with_multipointz#SplitOp._split_line_with_multipoint  s     $
++ 	K#$IJJJ(J// 	L#$JKKK. 	  	 BJ 8 8&AA M M!!'"@"@"K"KLLLLFFr3   c                    | j         dv r t          fd| j        D                       S | j         dk    r^j         dv rt          j        }nj         dk    rt          j        }nj         dk    rt          j        }nit          dj          d          | j         d	k    r.j         d
v rt          j        }n0t          dj          d          t          d| j          d          t           ||                     S )a  Split a geometry by another geometry and return a collection of geometries.

        This function is the theoretical opposite of the union of
        the split geometry parts. If the splitter does not split the geometry, a
        collection with a single geometry equal to the input geometry is
        returned.

        The function supports:
          - Splitting a (Multi)LineString by a (Multi)Point or (Multi)LineString
            or (Multi)Polygon
          - Splitting a (Multi)Polygon by a LineString

        It may be convenient to snap the splitter with low tolerance to the
        geometry. For example in the case of splitting a line by a point, the
        point must be exactly on the line, for the line to be correctly split.
        When splitting a line by a polygon, the boundary of the polygon is used
        for the operation. When splitting a line by another line, a ValueError
        is raised if the two overlap at some segment.

        Parameters
        ----------
        geom : geometry
            The geometry to be split
        splitter : geometry
            The geometry that will split the input geom

        Examples
        --------
        >>> import shapely.ops
        >>> from shapely import Point, LineString
        >>> pt = Point((1, 1))
        >>> line = LineString([(0,0), (2,2)])
        >>> result = shapely.ops.split(line, pt)
        >>> result.wkt
        'GEOMETRYCOLLECTION (LINESTRING (0 0, 1 1), LINESTRING (1 1, 2 2))'

        )r   r   c                 Z    g | ]'}t                               |          j        D ]}|(S r,   )r   r   r*   )r/   ru   r   r   s      r'   r1   z!SplitOp.split.<locals>.<listcomp>  s7    WWWt7==x3P3P3VWWaWWWWr3   r   )r   r   r
   r   r	   r   zSplitting a LineString with a z is not supportedr
   )r   r   zSplitting a Polygon with a z
Splitting z geometry is not supported)	r@   r   r*   r   r   r   r   r   r   )rX   r   
split_funcs    ` r'   r   zSplitOp.split  sU   N >@@@%WWWWtzWWW   ^|++! &   %:

#w..$;

#|33$@

'$X5G $ $ $  
 ^y((!%FFF$=

'W(2DWWW  
 $GT^GGG   "**T8"<"<===r3   N)	rO   rP   rQ   staticmethodr   r   r   r   r   r,   r3   r'   r   r   X  s        
 
 \
*   \B ' ' \'R   \" J> J> \J> J> J>r3   r   c                    t          | t                    st          d| j         d          ||k    r|                     ||          S |s2|| j        k    r'|| j        k    r|                     | j        |          S |s/| | j        k    r#| | j        k    r|                     d|          S |r#|dk    r|dk    r|                     d|          S |r%| dk    r| dk    r|                     d|          S |r|| j        z  }|| j        z  }|dcxk     r|k     r3n n0t          |          |z   | j        k    r|                     |          S |dcxk     r|k     r3n n0t          |          |z   | j        k    r|                     |          S |                     |          }|                     |          }|dk     r
| j        |z   }|dk     r
| j        |z   }||k    }|r||}}t          |d          }|rt          |j	         g}nt          |j	         g}t          | j	                  }d}	t          ||dd                   D ]b\  }
}||	cxk     r|k     rn n|                    |
           n|	|k    r n0|	|d         |
d         z
  dz  |d         |
d         z
  dz  z   dz  z  }	c|r1|                    t          |j	                    t          |          }n!|                    t          |j	                    t          |          S )	aa  Return a line segment between specified distances along a LineString.

    Negative distance values are taken as measured in the reverse
    direction from the end of the geometry. Out-of-range index
    values are handled by clamping them to the valid range of values.

    If the start distance equals the end distance, a Point is returned.

    If the start distance is actually beyond the end distance, then the
    reversed substring is returned such that the start distance is
    at the first coordinate.

    Parameters
    ----------
    geom : LineString
        The geometry to get a substring of.
    start_dist : float
        The distance along `geom` of the start of the substring.
    end_dist : float
        The distance along `geom` of the end of the substring.
    normalized : bool, False
        Whether the distance parameters are interpreted as a
        fraction of the geometry's length.

    Returns
    -------
    Union[Point, LineString]
        The substring between `start_dist` and `end_dist` or a Point
        if they are at the same location.

    Raises
    ------
    TypeError
        If `geom` is not a LineString.

    Examples
    --------
    >>> from shapely.geometry import LineString
    >>> from shapely.ops import substring
    >>> ls = LineString((i, 0) for i in range(6))
    >>> ls.wkt
    'LINESTRING (0 0, 1 0, 2 0, 3 0, 4 0, 5 0)'
    >>> substring(ls, start_dist=1, end_dist=3).wkt
    'LINESTRING (1 0, 2 0, 3 0)'
    >>> substring(ls, start_dist=3, end_dist=1).wkt
    'LINESTRING (3 0, 2 0, 1 0)'
    >>> substring(ls, start_dist=1, end_dist=-3).wkt
    'LINESTRING (1 0, 2 0)'
    >>> substring(ls, start_dist=0.2, end_dist=-0.6, normalized=True).wkt
    'LINESTRING (1 0, 2 0)'

    Returning a `Point` when `start_dist` and `end_dist` are at the
    same location.

    >>> substring(ls, 2.5, -2.5).wkt
    'POINT (2.5 0)'

    z;Can only calculate a substring of LineString geometries. A z was provided.)
normalizedr   r}   Nr   r   )r"   r   r   r@   interpolatelengthabsmaxtuplerC   rW   ri   appendreversed)rX   
start_distend_distr   start_point	end_pointreversevertex_listrC   current_distancer   r   s               r'   r   r     s   v dJ'' 
00 0 0
 
 	
 X
zBBB :J$+55(dk:Q:Q
CCC :ZK4;66H9;S;Sj999	 :
aHMMj999	 :q((hY!^^j999  dk!
DK A         S__x%?4;%N%N)))	A	"	"	"	"
	"	"	"	"	"s8}}z'AT['P'P
+++"":..K  **IA~~[:-
!||;)8#G 4'H
Z##J 3i./0k012$+FffQRRj)) Q QB(3333833333r""""))Ebebem1RURU]q4HHSPP 55+"45666{++5)"23444k"""r3   c                 D    | j         r| S t          j        | ||||          S )aC  Return the portion of a geometry within a rectangle.

    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
    ----------
    geom : geometry
        The geometry to be clipped
    xmin : float
        Minimum x value of the rectangle
    ymin : float
        Minimum y value of the rectangle
    xmax : float
        Maximum x value of the rectangle
    ymax : float
        Maximum y value of the rectangle

    Notes
    -----
    New in 1.7.

    )rv   r7   r   )rX   xminyminxmaxymaxs        r'   r   r     s,    2 } dD$===r3         ?c                 6    t          j        | |dk               S )a6  Return a properly oriented copy of the given geometry.

    The signed area of the result will have the given sign. A sign of
    1.0 means that the coordinates of the product's exterior rings will
    be oriented counter-clockwise.

    It is recommended to use :func:`shapely.orient_polygons` instead.

    Parameters
    ----------
    geom : Geometry
        The original geometry. May be a Polygon, MultiPolygon, or
        GeometryCollection.
    sign : float, optional.
        The sign of the result's signed area.

    Returns
    -------
    Geometry

    r   )exterior_cw)r7   orient_polygons)rX   signs     r'   r   r     s    , "4TAX>>>>r3   )rR   F)NrR   FrN   )r   )%__doc__r7   shapely.algorithms.polylabelr   shapely.errorsr   shapely.geometryr   r   r   r   r	   r
   r   shapely.geometry.baser   shapely.preparedr   __all__r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r,   r3   r'   <module>r      s%   3 3  2 2 2 2 2 2 , , , , , ,                  / . . . . . ! ! ! ! ! !  (O3 O3 O3 O3 O3 O3 O3 O3d  
*	"" " " "&8 8 8 8v) ) )
HK HK HKV  "+ + +$( ( (0@> @> @> @> @> @> @> @>F 	{# {# {# {#|> > ><? ? ? ? ? ?r3   