3
®?d]F  ã            	   @   s,  d Z ddlZejd dk r(ddlmZ neZddlmZmZm	Z	 ddl
mZ ddlmZmZ ddlmZmZmZmZmZmZmZmZmZ d	d
dddddddg	ZG dd„ deƒZeƒ ZejZejZejZej Z ej!Z!d"dd„Z"G dd„ deƒZ#e#ƒ Z$dd„ Z%dd„ Z&dd„ Z'dd„ Z(G d d!„ d!eƒZ)e)j*Z*dS )#z-Support for various GEOS geometry operations
é    Né   )Úizip)ÚbyrefÚc_void_pÚc_double)Úlgeos)Úgeom_factoryÚBaseGeometry)	ÚasShapeÚasLineStringÚasMultiLineStringÚPointÚ
MultiPointÚ
LineStringÚMultiLineStringÚPolygonÚGeometryCollectionÚcascaded_unionÚ	linemergeÚoperatorÚ
polygonizeÚpolygonize_fullÚ	transformÚunary_unionÚtriangulateÚsplitc               @   s<   e Zd Zdd„ Zdd„ Zdd„ Zdd„ Zd	d
„ Zdd„ ZdS )ÚCollectionOperatorc             C   s4   t |tƒr|S yt|ƒS  tk
r.   t|ƒS X d S )N)Ú
isinstancer	   r
   Ú
ValueErrorr   )ÚselfÚob© r!   ú./tmp/pip-build-op5p2y5_/shapely/shapely/ops.pyÚshapeup   s    
zCollectionOperator.shapeupc             #   sÔ   t |ddƒp|}z,yt|ƒ}W n tk
r8   |g}Y nX W d‡ fdd„|D ƒ}X tt|ƒ }|ƒ }xt|ƒD ]\}}|j||< qnW tjt	|ƒt|ƒƒ}t
|ƒ}	x.|	jD ]$}
tj|
jƒ}t
|ƒ}
d|
_|
V  q¨W dS )zÅCreates 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                s   g | ]}ˆ j |ƒ‘qS r!   )r#   )Ú.0Úl)r   r!   r"   ú
<listcomp>-   s    z1CollectionOperator.polygonize.<locals>.<listcomp>F)ÚgetattrÚiterÚ	TypeErrorr   ÚlenÚ	enumerateÚ_geomr   ZGEOSPolygonizer   r   r$   ZGEOSGeom_cloneZ_other_owned)r   ÚlinesÚsourceÚobsZgeom_array_typeZ
geom_arrayÚiÚlineÚproductÚ
collectionÚgÚcloner!   )r   r"   r   !   s"    zCollectionOperator.polygonizec                sÜ   t |ddƒp|}z,yt|ƒ}W n tk
r8   |g}Y nX W d‡ fdd„|D ƒ}X t|ƒ}t| ƒ }xt|ƒD ]\}}|j||< qnW tjd||ƒ}tƒ }	tƒ }
tƒ }tj	|t
|	ƒt
|
ƒt
|ƒƒ}t|ƒt|	ƒt|
ƒt|ƒfS )a…  Creates polygons from a source of lines, returning the polygons
        and leftover geometries.

        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, dangles, cut edges, 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                s   g | ]}ˆ j |ƒ‘qS r!   )r#   )r%   r&   )r   r!   r"   r'   O   s    z6CollectionOperator.polygonize_full.<locals>.<listcomp>é   )r(   r)   r*   r+   r   r,   r-   r   ÚGEOSGeom_createCollectionZGEOSPolygonize_fullr   r   )r   r.   r/   r0   ÚLÚsubsr1   r5   r4   ZdanglesZcutsZinvalidsr3   r!   )r   r"   r   :   s(    
z"CollectionOperator.polygonize_fullc             C   s„   d}t |dƒr|jdkr|}n>t |dƒr\ytdd„ |D ƒƒ}W n tk
rZ   t|ƒ}Y nX |dkrptd| ƒ‚tj|jƒ}t|ƒS )a  Merges 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Útyper   Ú__iter__c             S   s   g | ]
}|j ‘qS r!   )Úcoords)r%   Zlsr!   r!   r"   r'   m   s    z0CollectionOperator.linemerge.<locals>.<listcomp>zCannot linemerge %s)	Úhasattrr;   r   ÚAttributeErrorr   r   ZGEOSLineMerger-   r   )r   r.   r/   Úresultr!   r!   r"   r   a   s    
zCollectionOperator.linemergec             C   sv   yt |ƒ}W n tk
r*   |g}d}Y nX t| ƒ }xt|ƒD ]\}}|j||< q@W tjd||ƒ}ttjd |ƒƒS )z~Returns the union of a sequence of geometries

        This is the most efficient method of dissolving many polygons.
        é   é   r   )	r+   r*   r   r,   r-   r   r8   r   Úmethods)r   r$   r9   r:   r1   r5   r4   r!   r!   r"   r   u   s    

z!CollectionOperator.cascaded_unionc             C   sv   yt |ƒ}W n tk
r*   |g}d}Y nX t| ƒ }xt|ƒD ]\}}|j||< q@W tjd||ƒ}ttjd |ƒƒS )z©Returns the union of a sequence of geometries

        This method replaces :meth:`cascaded_union` as the
        prefered method for dissolving many polygons.

        rA   rB   r   )	r+   r*   r   r,   r-   r   r8   r   rC   )r   r$   r9   r:   r1   r5   r4   r!   r!   r"   r   …   s    

zCollectionOperator.unary_unionN)	Ú__name__Ú
__module__Ú__qualname__r#   r   r   r   r   r   r!   r!   r!   r"   r      s   	'r   ç        Fc             C   s0   t jd }t|| j|t|ƒƒƒ}dd„ |jD ƒS )a  Creates the Delaunay triangulation and returns 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.

    Zdelaunay_triangulationc             S   s   g | ]}|‘qS r!   r!   )r%   r5   r!   r!   r"   r'   °   s    ztriangulate.<locals>.<listcomp>)r   rC   r   r-   Úintr$   )ÚgeomÚ	toleranceÚedgesÚfuncÚgcr!   r!   r"   r   Ÿ   s    
c               @   s   e Zd Zdd„ ZdS )Ú
ValidateOpc             C   s   t j|jƒS )N)r   ZGEOSisValidReasonr-   )r   Úthisr!   r!   r"   Ú__call__³   s    zValidateOp.__call__N)rD   rE   rF   rP   r!   r!   r!   r"   rN   ²   s   rN   c                sf  |j r
|S |jdk�ryv|jdkr<t|ƒtˆ t|jŽ Ž Ž ƒS |jdkrŠt|jƒtˆ t|jjŽ Ž Ž ƒ}t‡ fdd„|jD ƒƒ}t|ƒ||ƒS W nŽ tk
�r   |jdkrÄt|ƒ‡ fdd„|jD ƒƒS |jdk�rt|jƒ‡ fd	d„|jjD ƒƒ}t‡ fd
d„|jD ƒƒ}t|ƒ||ƒS Y nX nD|jj	dƒ�s8|jdk�rTt|ƒ‡ fdd„|j
D ƒƒS td|j ƒ‚dS )a  Applies `func` to all coordinates of `geom` and 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)

    A partially applied transform function from pyproj satisfies the
    requirements for `func`.

      from functools import partial
      import pyproj

      project = partial(
          pyproj.transform,
          pyproj.Proj(init='epsg:4326'),
          pyproj.Proj(init='epsg:26913'))

      g2 = transform(project, g1)

    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   c             3   s(   | ] }t |ƒtˆ t|jŽ Ž Ž ƒV  qd S )N)r;   Úzipr   r=   )r%   Úring)rL   r!   r"   ú	<genexpr>ë   s   ztransform.<locals>.<genexpr>c                s   g | ]}ˆ |Ž ‘qS r!   r!   )r%   Úc)rL   r!   r"   r'   ó   s    ztransform.<locals>.<listcomp>c                s   g | ]}ˆ |Ž ‘qS r!   r!   )r%   rU   )rL   r!   r"   r'   ö   s    c             3   s*   | ]"}t |ƒ‡ fd d„|jD ƒƒV  qdS )c                s   g | ]}ˆ |Ž ‘qS r!   r!   )r%   rU   )rL   r!   r"   r'   ÷   s    z'transform.<locals>.<genexpr>.<listcomp>N)r;   r=   )r%   rS   )rL   r!   r"   rT   ÷   s   ZMultir   c                s   g | ]}t ˆ |ƒ‘qS r!   )r   )r%   Úpart)rL   r!   r"   r'   ü   s    zType %r not recognizedN)r   r   rQ   r   )r   r   rQ   )r   r   rQ   )Úis_emptyr;   rR   r   r=   ZexteriorÚlistZ	interiorsr*   Ú
startswithr$   r   )rL   rI   ÚshellZholesr!   )rL   r"   r   ¹   s0    $


c       	      C   s¸   t jd | j|jƒ}|dkr4| jr,tdƒ‚ntdƒ‚tƒ }tƒ }tƒ }tƒ }t j|dt|ƒƒ t j|dt|ƒƒ t j|dt|ƒƒ t j|dt|ƒƒ t	|j
|j
ƒ}t	|j
|j
ƒ}||fS )zŠReturns the calculated nearest points in the input geometries

    The points are returned in the same order as the input geometries.
    Únearest_pointsNz!The first input geometry is emptyz"The second input geometry is emptyr   rA   )r   rC   r-   rW   r   r   ZGEOSCoordSeq_getXr   ZGEOSCoordSeq_getYr   Úvalue)	Úg1Úg2ÚseqÚx1Úy1Zx2Úy2Úp1Úp2r!   r!   r"   r[     s     
r[   c             C   s   t tjd | j|j|ƒƒS )aŸ  Snap one geometry to another with a given tolerance

    Vertices of the first geometry are snapped to vertices of the second
    geometry. The resulting snapped geometry is returned. The input geometries
    are not modified.

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

    Example
    -------
    >>> square = Polygon([(1,1), (2, 1), (2, 2), (1, 2), (1, 1)])
    >>> line = LineString([(0,0), (0.8, 0.8), (1.8, 0.95), (2.6, 0.5)])
    >>> result = snap(line, square, 0.5)
    >>> result.wkt
    'LINESTRING (0 0, 1 1, 2 1, 2.6 0.5)'
    Úsnap)r   r   rC   r-   )r]   r^   rJ   r!   r!   r"   re     s    re   c             C   s<   t | tƒstdƒ‚t |tƒs$tdƒ‚ttjd | j|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Úshared_paths)r   r   r*   r   r   rC   r-   )r]   r^   r!   r!   r"   rf   2  s
    

rf   c               @   sH   e Zd Zedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZed	d
„ ƒZdS )ÚSplitOpc                s>   t ˆ tƒst‚t |tƒst‚ˆ jj|ƒ}‡ fdd„t|ƒD ƒS )z!Split a Polygon with a LineStringc                s   g | ]}ˆ j |jƒ ƒr|‘qS r!   )ÚcontainsZrepresentative_point)r%   Zpg)Úpolyr!   r"   r'   W  s    z4SplitOp._split_polygon_with_line.<locals>.<listcomp>)r   r   ÚAssertionErrorr   ÚboundaryÚunionr   )ri   Úsplitterrl   r!   )ri   r"   Ú_split_polygon_with_lineK  s    z SplitOp._split_polygon_with_linec             C   sj   |j dkr|j}t| tƒst‚t|tƒs6t|tƒs6t‚|j| ƒrJ| j|ƒS |j| dƒr`t	dƒ‚n| gS dS )zCSplit a LineString with another (Multi)LineString or (Multi)Polygonr   ÚMultiPolygonz	1********z2Input geometry segment overlaps with the splitter.N)r   ro   )
r;   rk   r   r   rj   r   ZcrossesÚ
differenceÚrelate_patternr   )r2   rm   r!   r!   r"   Ú_split_line_with_lineY  s    



zSplitOp._split_line_with_linec       
      C   s  t | tƒst‚t |tƒst‚| j|dƒs.| gS | jd |jd krH| gS | j|ƒ}t| jƒ}xªt|ƒD ]ž\}}| jt|ƒƒ}||kr¨t|d|d … ƒt||d… ƒgS ||k rf| j	|ƒ}|d|… }|j
|jd ƒ |jd g}	|	j||d… ƒ t|ƒt|	ƒgS qfW dS )zSplit a LineString with a Pointz	0********r   NrA   )r   r   rj   r   rq   r=   ÚprojectrX   r,   ZinterpolateÚappendÚextend)
r2   rm   Zdistance_on_liner=   r1   ÚpÚpdÚcpZ
ls1_coordsZ
ls2_coordsr!   r!   r"   Ú_split_line_with_pointn  s(    


zSplitOp._split_line_with_pointc             C   sh   t | tƒst‚t |tƒst‚| g}x@|jD ]6}g }x(tdd„ |ƒD ]}|jtj||ƒƒ qBW |}q*W |S )z$Split a LineString with a MultiPointc             S   s   | j  S )N)rW   )Úxr!   r!   r"   Ú<lambda>Ÿ  s    z5SplitOp._split_line_with_multipoint.<locals>.<lambda>)	r   r   rj   r   r$   Úfilterru   rg   ry   )r2   rm   ÚchunksÚptZ
new_chunksÚchunkr!   r!   r"   Ú_split_line_with_multipoint•  s    z#SplitOp._split_line_with_multipointc                sº   | j dkr"t‡ fdd„| jD ƒƒS | j dkrrˆ j dkr>tj}q¬ˆ j dkrPtj}q¬ˆ j dkrbtj}q¬td	ˆ j  ƒ‚n:| j dkržˆ j dkrŽtj}q¬td
ˆ j  ƒ‚ntd| j  ƒ‚t|| ˆ ƒƒS )aS  
        Splits a geometry by another geometry and returns 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

        Example
        -------
        >>> pt = Point((1, 1))
        >>> line = LineString([(0,0), (2,2)])
        >>> result = split(line, pt)
        >>> result.wkt
        'GEOMETRYCOLLECTION (LINESTRING (0 0, 1 1), LINESTRING (1 1, 2 2))'
        r   ro   c                s$   g | ]}t j|ˆ ƒjD ]}|‘qqS r!   )rg   r   r$   )r%   rV   r1   )rm   r!   r"   r'   Æ  s    z!SplitOp.split.<locals>.<listcomp>r   r   r   r   z1Splitting a LineString with a %s is not supportedz.Splitting a Polygon with a %s is not supportedz&Splitting %s geometry is not supported)r   ro   )r   r   r   ro   )	r;   r   r$   rg   rr   ry   r€   r   rn   )rI   rm   Z
split_funcr!   )rm   r"   r   ¦  s     






zSplitOp.splitN)	rD   rE   rF   Ústaticmethodrn   rr   ry   r€   r   r!   r!   r!   r"   rg   I  s
   'rg   )rG   F)+Ú__doc__ÚsysÚversion_infoÚ	itertoolsr   rR   Úctypesr   r   r   Zshapely.geosr   Zshapely.geometry.baser   r	   Zshapely.geometryr
   r   r   r   r   r   r   r   r   Ú__all__Úobjectr   r   r   r   r   r   r   r   rN   Úvalidater   r[   re   rf   rg   r   r!   r!   r!   r"   Ú<module>   s8   , 
H 