3
®?d]6z  ã               @   s   d Z ddlmZ ddlmZmZmZmZ ddlm	Z	 ddl
Z
ddlZddlmZ ddlmZ ddlmZ dd	lmZmZ dd
lmZ ddlmZmZ ddlmZ ddlmZmZ ejd dk rÌeZ e!e"fZ#ne!fZ#yddl$Z%e#e%j&f Z#W n e'k
rþ   Y nX ddddddddgZ(dd„ Z)dd„ Z*d=dd„Z+dd„ Z,dd „ Z-d!d"„ Z.d#d$„ Z/d%d&„ Z0d>d'd(„Z1d)d*„ Z2G d+d,„ d,e3ƒZ4G d-d.„ d.e3ƒZ5e.ed/ƒƒZ6G d0d1„ d1e3ƒZ7G d2d3„ d3e7ƒZ8G d4d5„ d5e3ƒZ9G d6d7„ d7e9ƒZ:G d8d9„ d9e7ƒZ;d:d;„ Z<e=d<k�rüe<ƒ  dS )?a  Base geometry class and utilities

Note: a third, z, coordinate value may be used when constructing
geometry objects, but has no effect on geometric analysis. All
operations are performed in the x-y plane. Thus, geometries with
different z values may intersect or be equal.
é    )Úa2b_hex)ÚpointerÚc_size_tÚc_char_pÚc_void_p)ÚisliceN)Úwarn)Úaffine_transform)ÚCoordinateSequence)ÚWKBReadingErrorÚWKTReadingError)Úwraps)Ú	WKBWriterÚ	WKTWriter)Úlgeos)ÚDefaultImplementationÚ	delegatedé   ÚPointÚ
LineStringÚ
LinearRingÚPolygonZ
MultiPointZMultiLineStringZMultiPolygonÚGeometryCollectionc             C   s˜   t | tƒstd| jj ƒ‚nx| jdkr4| jdd… S | jdkr^| jjdd… dd„ | jD ƒ S | jj	d	ƒst| jd
kr‚dd„ | D ƒS tdt
| jƒ ƒ‚dS )z@Dump coordinates of a geometry in the same order as data packingz,Must be instance of a geometry class; found r   r   r   Nr   c             S   s   g | ]}|j d d … ‘qS )N)Úcoords)Ú.0Úi© r   ú8/tmp/pip-build-op5p2y5_/shapely/shapely/geometry/base.pyú
<listcomp>;   s    zdump_coords.<locals>.<listcomp>ZMultir   c             S   s   g | ]}t |ƒ‘qS r   )Údump_coords)r   Úpartr   r   r   r   >   s    zUnhandled geometry type: )r   r   r   )Ú
isinstanceÚBaseGeometryÚ
ValueErrorÚ	__class__Ú__name__Útyper   ÚexteriorZ	interiorsÚ
startswithÚrepr)Úgeomr   r   r   r   3   s    


 r   c             C   s   | d krt dƒ‚ttj| ƒ S )NzNull geometry has no type)r#   ÚGEOMETRY_TYPESr   ZGEOSGeomTypeId)Úgr   r   r   Úgeometry_type_nameC   s    r-   c             C   sl   | st dƒ‚tƒ }t| ƒ}tdtƒ tƒ |gƒ}t||ƒ|_| |_||_	t
jd | ƒr\d|_nd|_d|_|S )Nz2No Shapely geometry can be created from null valuezshapely.geometryÚhas_zr   é   F)r#   r"   r-   Ú
__import__ÚglobalsÚlocalsÚgetattrr$   Ú_geomÚ__p__r   ÚmethodsÚ_ndimÚ	_is_empty)r,   ÚparentÚobÚ	geom_typeÚmodr   r   r   Úgeom_factoryI   s"    r=   c             C   sD   t dtƒ tjd dkr"| jdƒ} tjt| ƒƒ}|s<tdƒ‚t	|ƒS )Nz@`geom_from_wkt` is deprecated. Use `geos.wkt_reader.read(data)`.r   r   Úasciiz@Could not create geometry because of errors while reading input.)
r   ÚDeprecationWarningÚsysÚversion_infoÚencoder   ZGEOSGeomFromWKTr   r   r=   )Údatar*   r   r   r   Úgeom_from_wkta   s    
rD   c             C   s0   t dtƒ | d ks| jd kr$tdƒ‚tj| jƒS )Nz=`geom_to_wkt` is deprecated. Use `geos.wkt_writer.write(ob)`.z$Null geometry supports no operations)r   r?   r4   r#   r   ZGEOSGeomToWKT)r:   r   r   r   Úgeom_to_wktm   s
    rE   c             C   s(   t jt| ƒtt| ƒƒƒ}|s$tdƒ‚|S )Nz@Could not create geometry because of errors while reading input.)r   ZGEOSGeomFromWKB_bufr   r   Úlenr   )rC   r*   r   r   r   Údeserialize_wkbu   s
    rG   c             C   s   t dtƒ tt| ƒƒS )Nz@`geom_from_wkb` is deprecated. Use `geos.wkb_reader.read(data)`.)r   r?   r=   rG   )rC   r   r   r   Úgeom_from_wkb}   s    rH   c             C   s@   t dtƒ | d ks| jd kr$tdƒ‚tƒ }tjt| jƒt|ƒƒS )Nz=`geom_to_wkb` is deprecated. Use `geos.wkb_writer.write(ob)`.z$Null geometry supports no operations)	r   r?   r4   r#   r   r   ZGEOSGeomToWKB_bufr   r   )r:   Úsizer   r   r   Úgeom_to_wkbƒ   s    rJ   c             C   sB   |dkrt j| jƒ}nt j| jƒ}t j|ƒ}||ƒ}| j}||fS )a9  Helper function for geos_*_from_py functions in each geom type.

    If a create_func is specified the coodinate sequence is cloned and a new
    geometry is created with it, otherwise the geometry is cloned directly.
    This behaviour is useful for converting between LineString and LinearRing
    objects.
    N)r   ZGEOSGeom_cloner4   ZGEOSGeom_getCoordSeqZGEOSCoordSeq_cloner7   )r:   Zcreate_funcr*   ÚcsÚNr   r   r   Úgeos_geom_from_pyŒ   s    
rM   c                s   t ˆ ƒ‡ fdd„ƒ}|S )z=Decorator which helps avoid GEOS operations on null pointers.c                 s(   | d j  s| d jrtdƒ‚ˆ | |ŽS )Nr   z*Null/empty geometry supports no operations)r4   Úis_emptyr#   )ÚargsÚkwargs)Úfuncr   r   Úwrapper¢   s    zexceptNull.<locals>.wrapper)r   )rQ   rR   r   )rQ   r   Ú
exceptNull    s    rS   c               @   s   e Zd ZdZdZdZdS )Ú	CAP_STYLEé   r/   r   N)r%   Ú
__module__Ú__qualname__ÚroundZflatZsquarer   r   r   r   rT   ª   s   rT   c               @   s   e Zd ZdZdZdZdS )Ú
JOIN_STYLErU   r/   r   N)r%   rV   rW   rX   ZmitreZbevelr   r   r   r   rY   °   s   rY   s   010700000000000000c               @   sú  e Zd ZdZeZdZdZdZdZ	dZ
dZeZeZefdd„Zdd„ Zd	d
„ Zdd„ Zdd„ Zed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Zedd„ ƒZed d!„ ƒZ ed"d#„ ƒZ!d$d%„ Z"d&d'„ Z#ee"e#ƒZ$ed(d)„ ƒZ%ed*d+„ ƒZ&d,d-„ Z'ed.d/„ ƒZ(d0d1„ Z)d2d3„ Z*ed4d5„ ƒZ+ed6d7„ ƒZ,ed8d9„ ƒZ-d�d;d<„Z.d=d>„ Z/ed?d@„ ƒZ0edAdB„ ƒZ1dCdD„ Z2dEdF„ Z3edGdH„ ƒZ4edIdJ„ ƒZ5edKdL„ ƒZ6edMdN„ ƒZ7e8dOdP„ ƒZ9edQdR„ ƒZ:edSdT„ ƒZ;edUdV„ ƒZ<dWde=j>e?j>dXfdYdZ„Z@e8d‘d[d\„ƒZAd]d^„ ZBd_d`„ ZCdadb„ ZDdcdd„ ZEededf„ ƒZFedgdh„ ƒZGedidj„ ƒZHedkdl„ ƒZIedmdn„ ƒZJedodp„ ƒZKdqdr„ ZLdsdt„ ZMdudv„ ZNdwdx„ ZOdydz„ ZPd{d|„ ZQd}d~„ ZRdd€„ ZSd�d‚„ ZTdƒd„„ ZUd…d†„ ZVd’dˆd‰„ZWdŠd‹„ ZXe8d“dŒd�„ƒZYe8d”dŽd�„ƒZZdS )•r"   zG
    Provides GEOS spatial predicates and topological operations.

    NFTc             C   sR   | j  rB| j rB| jrBy| jj| jƒ W n ttfk
r@   Y nX d| _ || _d S )NT)r8   Ú_other_ownedÚ__geom__Ú_lgeosZGEOSGeom_destroyÚAttributeErrorÚ	TypeError)ÚselfÚvalr   r   r   ÚemptyÞ   s    zBaseGeometry.emptyc             C   s   | j d d� d | _d S )N)r`   )ra   r5   )r_   r   r   r   Ú__del__é   s    zBaseGeometry.__del__c             C   s   | j S )N)Úwkt)r_   r   r   r   Ú__str__í   s    zBaseGeometry.__str__c             C   s   | j f | jfS )N)r$   Úwkb)r_   r   r   r   Ú
__reduce__ñ   s    zBaseGeometry.__reduce__c             C   s:   | j ƒ  t|ƒ| _d| _tjd | jƒr0d| _nd| _d S )NFr.   r   r/   )ra   rG   r[   r8   r   r6   r7   )r_   Ústater   r   r   Ú__setstate__ô   s    
zBaseGeometry.__setstate__c             C   s   | j S )N)r[   )r_   r   r   r   r4   ý   s    zBaseGeometry._geomc             C   s    | j ƒ  |td gk| _|| _d S )N)ra   ÚEMPTYr8   r[   )r_   r`   r   r   r   r4     s    c             C   s
   | j |ƒS )N)Úintersection)r_   Úotherr   r   r   Ú__and__
  s    zBaseGeometry.__and__c             C   s
   | j |ƒS )N)Úunion)r_   rk   r   r   r   Ú__or__  s    zBaseGeometry.__or__c             C   s
   | j |ƒS )N)Ú
difference)r_   rk   r   r   r   Ú__sub__  s    zBaseGeometry.__sub__c             C   s
   | j |ƒS )N)Úsymmetric_difference)r_   rk   r   r   r   Ú__xor__  s    zBaseGeometry.__xor__c             C   s$   t |ƒt | ƒko"t| jƒt|jƒkS )N)r&   Útupler   )r_   rk   r   r   r   Ú__eq__  s    zBaseGeometry.__eq__c             C   s   | j |ƒ S )N)rt   )r_   rk   r   r   r   Ú__ne__  s    zBaseGeometry.__ne__c             C   s   t ‚dS )zReturn ctypes bufferN)ÚNotImplementedError)r_   r   r   r   Úctypes$  s    zBaseGeometry.ctypesc             C   s6   t jdkrd}nt jdkr d}ntdƒ‚d|| jdœS )NÚlittlez<f8Úbigz>f8z4Unsupported byteorder: neither little nor big-endianr   )ÚversionÚtypestrrC   )r@   Ú	byteorderr#   rw   )r_   r{   r   r   r   Úarray_interface_base)  s    

z!BaseGeometry.array_interface_basec             C   s   t ‚dS )z!Provide the Numpy array protocol.N)rv   )r_   r   r   r   Ú__array_interface__8  s    z BaseGeometry.__array_interface__c             C   s   | j r
g S t| ƒS )z5Access to geometry's coordinates (CoordinateSequence))rN   r
   )r_   r   r   r   Ú_get_coords@  s    zBaseGeometry._get_coordsc             C   s   t dƒ‚d S )Nz.set_coords must be provided by derived classes)rv   )r_   r:   r   r   r   Ú_set_coordsF  s    zBaseGeometry._set_coordsc             C   s   t ‚dS )z,Separate arrays of X and Y coordinate valuesN)rv   )r_   r   r   r   ÚxyL  s    zBaseGeometry.xyc             C   s   t ‚dS )z)Dictionary representation of the geometryN)rv   )r_   r   r   r   Ú__geo_interface__S  s    zBaseGeometry.__geo_interface__c             C   s
   t | jƒS )N)r-   r4   )r_   r   r   r   ÚgeometryType[  s    zBaseGeometry.geometryTypec             C   s   | j ƒ S )N)rƒ   )r_   r   r   r   r&   ^  s    zBaseGeometry.typec             C   s   t dtƒ t| ƒS )Nz/`to_wkb` is deprecated. Use the `wkb` property.)r   r?   rJ   )r_   r   r   r   Úto_wkbb  s    zBaseGeometry.to_wkbc             C   s   t dtƒ t| ƒS )Nz/`to_wkt` is deprecated. Use the `wkt` property.)r   r?   rE   )r_   r   r   r   Úto_wktg  s    zBaseGeometry.to_wktc             K   s   t tf|Žj| ƒS )z"WKT representation of the geometry)r   r   Úwrite)r_   Úkwr   r   r   rc   l  s    zBaseGeometry.wktc             C   s   t tƒj| ƒS )z"WKB representation of the geometry)r   r   r†   )r_   r   r   r   re   q  s    zBaseGeometry.wkbc             C   s   t tƒj| ƒS )z&WKB hex representation of the geometry)r   r   Z	write_hex)r_   r   r   r   Úwkb_hexv  s    zBaseGeometry.wkb_hexç      ð?c             K   s   t ‚dS )zRaises NotImplementedErrorN)rv   )r_   Úscale_factorrP   r   r   r   Úsvg{  s    zBaseGeometry.svgc             C   s2  d}| j r|d S | j\}}}}||krF||krF| jdƒj\}}}}n@d}t|| || gƒ}|| }||8 }||8 }||7 }||7 }|| }	|| }
ttd|	gƒdgƒ}ttd|
gƒdgƒ}yt|	|
gƒt||gƒ }W n tk
rò   d}Y nX dj|||	|
ƒ}d	j|| ƒ}|d
j||||| j|ƒƒ S dS )z'SVG representation for iPython notebookzS<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" z/>rU   g{®Gáz¤?g      Y@i,  g      ð?z{0} {1} {2} {3}zmatrix(1,0,0,-1,0,{0})zkwidth="{1}" height="{2}" viewBox="{0}" preserveAspectRatio="xMinYMin meet"><g transform="{3}">{4}</g></svg>N)rN   ÚboundsÚbufferÚmaxÚminÚZeroDivisionErrorÚformatr‹   )r_   Zsvg_topZxminZyminZxmaxZymaxÚexpandZwidest_partZexpand_amountÚdxÚdyÚwidthÚheightrŠ   Zview_boxZ	transformr   r   r   Ú
_repr_svg_  s6    
zBaseGeometry._repr_svg_c             C   s   | j ƒ S )z,Name of the geometry's type, such as 'Point')rƒ   )r_   r   r   r   r;   ¥  s    zBaseGeometry.geom_typec             C   s   | j d | ƒS )z%Unitless area of the geometry (float)Úarea)Úimpl)r_   r   r   r   r˜   ­  s    zBaseGeometry.areac             C   s   | j d | |ƒS )z+Unitless distance to other geometry (float)Údistance)r™   )r_   rk   r   r   r   rš   ²  s    zBaseGeometry.distancec             C   s   | j d | |ƒS )z5Unitless hausdorff distance to other geometry (float)Úhausdorff_distance)r™   )r_   rk   r   r   r   r›   ¶  s    zBaseGeometry.hausdorff_distancec             C   s   | j d | ƒS )z'Unitless length of the geometry (float)Úlength)r™   )r_   r   r   r   rœ   º  s    zBaseGeometry.lengthc             C   s   t | jd | ƒƒS )zéReturns a lower dimension geometry that bounds the object

        The boundary of a polygon is a line, the boundary of a line is a
        collection of points. The boundary of a point is an empty (null)
        collection.
        Úboundary)r=   r™   )r_   r   r   r   r�   Â  s    zBaseGeometry.boundaryc             C   s   | j r
f S | jd | ƒS dS )z8Returns minimum bounding region (minx, miny, maxx, maxy)rŒ   N)rN   r™   )r_   r   r   r   rŒ   Ì  s    zBaseGeometry.boundsc             C   s   t | jd | ƒƒS )z*Returns the geometric center of the objectÚcentroid)r=   r™   )r_   r   r   r   rž   Ô  s    zBaseGeometry.centroidc             C   s   t | jd | ƒƒS )z<Returns a point guaranteed to be within the object, cheaply.Úrepresentative_point)r=   r™   )r_   r   r   r   rŸ   Ù  s    z!BaseGeometry.representative_pointc             C   s   t | jd | ƒƒS )zÏImagine an elastic band stretched around the geometry: that's a
        convex hull, more or less

        The convex hull of a three member multipoint, for example, is a
        triangular polygon.
        Úconvex_hull)r=   r™   )r_   r   r   r   r    Þ  s    zBaseGeometry.convex_hullc             C   s   t | jd | ƒƒS )z$A figure that envelopes the geometryÚenvelope)r=   r™   )r_   r   r   r   r¡   è  s    zBaseGeometry.envelopec                sr   | j ‰yˆjj}W n tk
r&   ˆS X dd„ t|t|ddƒƒD ƒ‰ ‡ ‡fdd„}t|ƒ dd„ d	�\}}t||ƒS )
zÜReturns the general minimum bounding rectangle of
        the geometry. Can possibly be rotated. If the convex hull
        of the object is a degenerate (line or point) this same degenerate
        is returned.
        c             s   s2   | ]*\}}|d  |d   |d |d  fV  qdS )r   rU   Nr   )r   Zpt1Zpt2r   r   r   ú	<genexpr>û  s    z9BaseGeometry.minimum_rotated_rectangle.<locals>.<genexpr>rU   Nc           	   3   sx   xrˆ D ]j\} }t j| d |d  ƒ}| | ||  }}| | }}tˆ||||ddfƒj}|||||ddffV  qW d S )Nr/   r   )ÚmathÚsqrtr	   r¡   )r“   r”   rœ   ZuxZuyZvxZvyÚtransf_rect)ÚedgesÚhullr   r   Ú_transformed_rectsþ  s    zBBaseGeometry.minimum_rotated_rectangle.<locals>._transformed_rectsc             S   s
   | d j S )Nr   )r˜   )Úrr   r   r   Ú<lambda>  s    z8BaseGeometry.minimum_rotated_rectangle.<locals>.<lambda>)Úkey)r    r'   r   r]   Úzipr   r�   r	   )r_   r   r¨   r¥   Z
inv_matrixr   )r¦   r§   r   Úminimum_rotated_rectangleí  s    z&BaseGeometry.minimum_rotated_rectangleé   g      @c             C   s„   |dk	rt dtƒ |}n|}|dkr,tdƒ‚|tjkrV|tjkrVt| jd | ||ƒƒS d| jkrhtdƒ‚t| jd | |||||ƒƒS )a  Returns a geometry with an envelope at a distance from the object's
        envelope

        A negative distance has a "shrink" effect. A zero distance may be used
        to "tidy" a polygon. The resolution of the buffer around each vertex of
        the object increases by increasing the resolution keyword parameter
        or second positional parameter. Note: the use of a `quadsegs` parameter
        is deprecated and will be gone from the next major release.

        The styles of caps are: CAP_STYLE.round (1), CAP_STYLE.flat (2), and
        CAP_STYLE.square (3).

        The styles of joins between offset segments are: JOIN_STYLE.round (1),
        JOIN_STYLE.mitre (2), and JOIN_STYLE.bevel (3).

        The mitre limit ratio is used for very sharp corners. The mitre ratio
        is the ratio of the distance from the corner to the end of the mitred
        offset corner. When two line segments meet at a sharp angle, a miter
        join will extend the original geometry. To prevent unreasonable
        geometry, the mitre limit allows controlling the maximum length of the
        join corner. Corners with a ratio which exceed the limit will be
        beveled.

        Example:

          >>> from shapely.wkt import loads
          >>> g = loads('POINT (0.0 0.0)')
          >>> g.buffer(1.0).area        # 16-gon approx of a unit radius circle
          3.1365484905459389
          >>> g.buffer(1.0, 128).area   # 128-gon approximation
          3.1415138011443009
          >>> g.buffer(1.0, 3).area     # triangle approximation
          3.0
          >>> list(g.buffer(1.0, cap_style='square').exterior.coords)
          [(1.0, 1.0), (1.0, -1.0), (-1.0, -1.0), (-1.0, 1.0), (1.0, 1.0)]
          >>> g.buffer(1.0, cap_style='square').area
          4.0
        Nz8The `quadsegs` argument is deprecated. Use `resolution`.g        z3Cannot compute offset from zero-length line segmentr�   Zbuffer_with_stylez7Styled buffering not available for GEOS versions < 3.2.)	r   r?   r#   rT   rX   rY   r=   r™   rv   )r_   rš   Ú
resolutionZquadsegsZ	cap_styleZ
join_styleZmitre_limitÚresr   r   r   r�     s"    )
zBaseGeometry.bufferc             C   s(   |r| j d }n
| j d }t|| |ƒƒS )a[  Returns a simplified geometry produced by the Douglas-Peucker
        algorithm

        Coordinates of the simplified geometry will be no more than the
        tolerance distance from the original. Unless the topology preserving
        option is used, the algorithm may produce self-intersecting or
        otherwise invalid geometries.
        Ztopology_preserve_simplifyÚsimplify)r™   r=   )r_   Ú	toleranceZpreserve_topologyÚopr   r   r   r±   S  s    

zBaseGeometry.simplifyc             C   s   t | jd | |ƒƒS )z(Returns the difference of the geometriesro   )r=   r™   )r_   rk   r   r   r   ro   f  s    zBaseGeometry.differencec             C   s   t | jd | |ƒƒS )z*Returns the intersection of the geometriesrj   )r=   r™   )r_   rk   r   r   r   rj   j  s    zBaseGeometry.intersectionc             C   s   t | jd | |ƒƒS )zMReturns the symmetric difference of the geometries
        (Shapely geometry)rq   )r=   r™   )r_   rk   r   r   r   rq   n  s    z!BaseGeometry.symmetric_differencec             C   s   t | jd | |ƒƒS )z6Returns the union of the geometries (Shapely geometry)rm   )r=   r™   )r_   rk   r   r   r   rm   s  s    zBaseGeometry.unionc             C   s   t | jd | ƒƒS )zWTrue if the geometry's coordinate sequence(s) have z values (are
        3-dimensional)r.   )Úboolr™   )r_   r   r   r   r.   z  s    zBaseGeometry.has_zc             C   s   | j dkpt| jd | ƒƒS )z?True if the set of points in this geometry is empty, else FalseNrN   )r4   r´   r™   )r_   r   r   r   rN   €  s    zBaseGeometry.is_emptyc             C   s   t | jd | ƒƒS )z1True if the geometry is a closed ring, else FalseÚis_ring)r´   r™   )r_   r   r   r   rµ   …  s    zBaseGeometry.is_ringc             C   sR   | j dkrdS | j dkrJd| jkr4t| jd | ƒƒS | jd | jd	 kS ndS dS )
zVTrue if the geometry is closed, else False

        Applicable only to 1-D geometries.r   Tr   Ú	is_closedr   rU   FNéÿÿÿÿ)r;   r™   r´   r   )r_   r   r   r   r¶   Š  s    


zBaseGeometry.is_closedc             C   s   t | jd | ƒƒS )zsTrue if the geometry is simple, meaning that any self-intersections
        are only at boundary points, else FalseÚ	is_simple)r´   r™   )r_   r   r   r   r¸   ™  s    zBaseGeometry.is_simplec             C   s   t | jd | ƒƒS )zSTrue if the geometry is valid (definition depends on sub-class),
        else FalseÚis_valid)r´   r™   )r_   r   r   r   r¹   Ÿ  s    zBaseGeometry.is_validc             C   s   | j d | |ƒS )zNReturns the DE-9IM intersection matrix for the two geometries
        (string)Úrelate)r™   )r_   rk   r   r   r   rº   ¨  s    zBaseGeometry.relatec             C   s   t | jd | |ƒƒS )z9Returns True if the geometry covers the other, else FalseÚcovers)r´   r™   )r_   rk   r   r   r   r»   ­  s    zBaseGeometry.coversc             C   s   t | jd | |ƒƒS )z;Returns True if the geometry contains the other, else FalseÚcontains)r´   r™   )r_   rk   r   r   r   r¼   ±  s    zBaseGeometry.containsc             C   s   t | jd | |ƒƒS )z0Returns True if the geometries cross, else FalseÚcrosses)r´   r™   )r_   rk   r   r   r   r½   µ  s    zBaseGeometry.crossesc             C   s   t | jd | |ƒƒS )z3Returns True if geometries are disjoint, else FalseÚdisjoint)r´   r™   )r_   rk   r   r   r   r¾   ¹  s    zBaseGeometry.disjointc             C   s   t | jd | |ƒƒS )zËReturns True if geometries are equal, else False
        
        Refers to point-set equality (or topological equality), and is equivalent to
        (self.within(other) & self.contains(other))
        Úequals)r´   r™   )r_   rk   r   r   r   r¿   ½  s    zBaseGeometry.equalsc             C   s   t | jd | |ƒƒS )z0Returns True if geometries intersect, else FalseÚ
intersects)r´   r™   )r_   rk   r   r   r   rÀ   Å  s    zBaseGeometry.intersectsc             C   s   t | jd | |ƒƒS )z.Returns True if geometries overlap, else FalseÚoverlaps)r´   r™   )r_   rk   r   r   r   rÁ   É  s    zBaseGeometry.overlapsc             C   s   t | jd | |ƒƒS )z,Returns True if geometries touch, else FalseÚtouches)r´   r™   )r_   rk   r   r   r   rÂ   Í  s    zBaseGeometry.touchesc             C   s   t | jd | |ƒƒS )z8Returns True if geometry is within the other, else FalseÚwithin)r´   r™   )r_   rk   r   r   r   rÃ   Ñ  s    zBaseGeometry.withinc             C   s   t | jd | ||ƒƒS )zìReturns True if geometries are equal to within a specified
        tolerance
        
        Refers to coordinate equality, which requires coordinates to be equal 
        and in the same order for all components of a geometry
        Úequals_exact)r´   r™   )r_   rk   r²   r   r   r   rÄ   Õ  s    zBaseGeometry.equals_exacté   c             C   s   | j |dd|   ƒS )a  Returns True if geometries are equal at all coordinates to a
        specified decimal place

        Refers to approximate coordinate equality, which requires coordinates be
        approximately equal and in the same order for all components of a geometry.
        g      à?é
   )rÄ   )r_   rk   Údecimalr   r   r   Úalmost_equalsÞ  s    zBaseGeometry.almost_equalsc             C   s$   t |jdƒƒ}t| jd | ||ƒƒS )z|Returns True if the DE-9IM string code for the relationship between
        the geometries satisfies the pattern, else Falser>   Úrelate_pattern)r   rB   r´   r™   )r_   rk   Úpatternr   r   r   rÉ   ç  s    zBaseGeometry.relate_patternc             C   s$   |r| j d }n
| j d }|| |ƒS )zÕReturns the distance along this geometry to a point nearest the
        specified point

        If the normalized arg is True, return the distance normalized to the
        length of the linear geometry.
        Zproject_normalizedÚproject)r™   )r_   rk   Ú
normalizedr³   r   r   r   rË   ð  s    
zBaseGeometry.projectc             C   s(   |r| j d }n
| j d }t|| |ƒƒS )zÂReturn a point at the specified distance along a linear geometry

        If the normalized arg is True, the distance will be interpreted as a
        fraction of the geometry's length.
        Zinterpolate_normalizedÚinterpolate)r™   r=   )r_   rš   rÌ   r³   r   r   r   rÍ   þ  s    
zBaseGeometry.interpolate)r‰   )T)rÅ   )F)F)[r%   rV   rW   Ú__doc__ri   r[   r5   Z_ctypes_datar7   Z_crsrZ   r8   r   r™   r   r\   ra   rb   rd   rf   rh   Úpropertyr4   Úsetterrl   rn   rp   rr   rt   ru   Ú__hash__rw   r}   r~   r   r€   r   r�   r‚   rƒ   r&   r„   r…   rc   re   rˆ   r‹   r—   r;   r˜   rš   r›   rœ   r�   rŒ   rž   r   rŸ   r    r¡   r­   rT   rX   rY   r�   r±   ro   rj   rq   rm   r.   rN   rµ   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               @   sŠ   e Zd Zdd„ Zedd„ ƒZedd„ ƒZdd„ Zd	d
„ Zedd„ ƒZ	edd„ ƒZ
dd„ Zdd„ Zdd„ Zdd„ Zdd„ ZdZddd„ZdS )ÚBaseMultipartGeometryc             G   s   t dƒ‚d S )Nz$To be implemented by derived classes)rv   )r_   rO   r   r   r   Úshape_factory  s    z#BaseMultipartGeometry.shape_factoryc             C   s   t dƒ‚d S )Nz4Multi-part geometries have no ctypes representations)rv   )r_   r   r   r   rw     s    zBaseMultipartGeometry.ctypesc             C   s   t dƒ‚dS )z!Provide the Numpy array protocol.zCMulti-part geometries do not themselves provide the array interfaceN)rv   )r_   r   r   r   r~     s    z)BaseMultipartGeometry.__array_interface__c             C   s   t dƒ‚d S )NzDSub-geometries may have coordinate sequences, but collections do not)rv   )r_   r   r   r   r     s    z!BaseMultipartGeometry._get_coordsc             C   s   t dƒ‚d S )NzDSub-geometries may have coordinate sequences, but collections do not)rv   )r_   r:   r   r   r   r€   !  s    z!BaseMultipartGeometry._set_coordsc             C   s   t dƒ‚d S )Nz:Multi-part geometries do not provide a coordinate sequence)rv   )r_   r   r   r   r   %  s    zBaseMultipartGeometry.coordsc             C   s   | j r
g S t| | jƒS )N)rN   ÚGeometrySequencerÓ   )r_   r   r   r   Úgeoms*  s    zBaseMultipartGeometry.geomsc             C   s   | j st| jƒS tg ƒS d S )N)rN   ÚiterrÕ   )r_   r   r   r   Ú__iter__0  s    
zBaseMultipartGeometry.__iter__c             C   s   | j st| jƒS dS d S )Nr   )rN   rF   rÕ   )r_   r   r   r   Ú__len__6  s    
zBaseMultipartGeometry.__len__c             C   s   | j s| j| S f | S d S )N)rN   rÕ   )r_   Úindexr   r   r   Ú__getitem__<  s    
z!BaseMultipartGeometry.__getitem__c             C   s8   t |ƒt | ƒko6t| ƒt|ƒko6tdd„ t| |ƒD ƒƒS )Nc             s   s   | ]\}}||kV  qd S )Nr   )r   ÚxÚyr   r   r   r¢   F  s    z/BaseMultipartGeometry.__eq__.<locals>.<genexpr>)r&   rF   Úallr¬   )r_   rk   r   r   r   rt   B  s    zBaseMultipartGeometry.__eq__c             C   s   | j |ƒ S )N)rt   )r_   rk   r   r   r   ru   I  s    zBaseMultipartGeometry.__ne__Nç      ð?c                sB   | j r
dS ˆ dkr | jrdnd‰ ddj‡ ‡fdd„| D ƒƒ d	 S )
az  Returns a group of SVG elements for the multipart geometry.

        Parameters
        ==========
        scale_factor : float
            Multiplication factor for the SVG stroke-width.  Default is 1.
        color : str, optional
            Hex string for stroke or fill color. Default is to use "#66cc99"
            if geometry is valid, and "#ff3333" if invalid.
        z<g />Nz#66cc99z#ff3333z<g>Ú c             3   s   | ]}|j ˆˆ ƒV  qd S )N)r‹   )r   Úp)ÚcolorrŠ   r   r   r¢   ^  s    z,BaseMultipartGeometry.svg.<locals>.<genexpr>z</g>)rN   r¹   Újoin)r_   rŠ   rá   r   )rá   rŠ   r   r‹   N  s    zBaseMultipartGeometry.svg)rÞ   N)r%   rV   rW   rÓ   rÏ   rw   r~   r   r€   r   rÕ   r×   rØ   rÚ   rt   ru   rÑ   r‹   r   r   r   r   rÒ     s   rÒ   c               @   s\   e Zd ZdZdZdZdZdZdd„ Zdd„ Z	dd„ Z
d	d
„ Zdd„ Zdd„ Zedd„ ƒZdS )rÔ   zJ
    Iterative access to members of a homogeneous multipart geometry.
    Nc             C   s   || _ || _d S )N)rÓ   r5   )r_   r9   r&   r   r   r   Ú__init__v  s    zGeometrySequence.__init__c             C   s   | j j| _| j j| _d S )N)r5   r4   r7   )r_   r   r   r   Ú_updatez  s    
zGeometrySequence._updatec             C   s0   | j ƒ }d|_tj| j|ƒ|_| j|_| |_|S )NT)rÓ   rZ   r   ÚGEOSGetGeometryNr4   r7   r5   )r_   r   r,   r   r   r   Ú_get_geom_item~  s    zGeometrySequence._get_geom_itemc             c   s.   | j ƒ  x t| jƒ ƒD ]}| j|ƒV  qW d S )N)rä   ÚrangerØ   ræ   )r_   r   r   r   r   r×   †  s    zGeometrySequence.__iter__c             C   s   | j ƒ  tj| jƒS )N)rä   r   ZGEOSGetNumGeometriesr4   )r_   r   r   r   rØ   ‹  s    zGeometrySequence.__len__c             C   sÌ   | j ƒ  | jƒ }t|tƒrV|| dk s.||kr6tdƒ‚|dk rH|| }n|}| j|ƒS t|tƒrÀt| ƒtkrtt	dƒ‚g }|j
|ƒ\}}}x$t|||ƒD ]}|j| j|ƒƒ q–W t| jƒ|p¼d ƒS t	dƒ‚d S )Nr   zindex out of rangez3Heterogenous geometry collections are not sliceablezkey must be an index or slice)rä   rØ   r!   Úinteger_typesÚ
IndexErrorræ   Úslicer&   ÚHeterogeneousGeometrySequencer^   Úindicesrç   Úappendr5   )r_   r«   Úmr   r°   ÚstartÚstopZstrider   r   r   rÚ   �  s&    



zGeometrySequence.__getitem__c             C   s0   d}x&t | ƒD ]}t|jƒ}||kr|}qW d S )Nr   )rÖ   rF   r   )r_   rŽ   r,   Úlr   r   r   Ú_longest¦  s
    
zGeometrySequence._longest)r%   rV   rW   rÎ   rÓ   r4   r5   r7   rã   rä   ræ   r×   rØ   rÚ   rÏ   rò   r   r   r   r   rÔ   b  s   rÔ   c                   s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )rë   zE
    Iterative access to a heterogeneous sequence of geometries.
    c                s   t t| ƒj|d ƒ d S )N)Úsuperrë   rã   )r_   r9   )r$   r   r   rã   ´  s    z&HeterogeneousGeometrySequence.__init__c             C   s$   t j| j|ƒ}t|| d�}d|_|S )N)r9   T)r   rå   r4   r=   rZ   )r_   r   Úsubr,   r   r   r   ræ   ·  s    z,HeterogeneousGeometrySequence._get_geom_item)r%   rV   rW   rÎ   rã   ræ   Ú__classcell__r   r   )r$   r   rë   ¯  s   rë   c               @   s   e Zd Zdd„ ZdS )ÚEmptyGeometryc             C   s   t j| ƒ dS )zCreate an empty geometry.N)r"   rã   )r_   r   r   r   rã   ¿  s    zEmptyGeometry.__init__N)r%   rV   rW   rã   r   r   r   r   rö   ¾  s   rö   c              C   s   ddl } | jƒ  dS )zTest runnerr   N)ÚdoctestÚtestmod)r÷   r   r   r   Ú_testÄ  s    rù   Ú__main__)N)N)>rÎ   Úbinasciir   rw   r   r   r   r   Ú	itertoolsr   r£   r@   Úwarningsr   Zshapely.affinityr	   Zshapely.coordsr
   Zshapely.errorsr   r   Zshapely.ftoolsr   Zshapely.geosr   r   r   Zshapely.implr   r   rA   Úxrangerç   ÚintÚlongrè   ZnumpyÚnpÚintegerÚImportErrorr+   r   r-   r=   rD   rE   rG   rH   rJ   rM   rS   ÚobjectrT   rY   ri   r"   rÒ   rÔ   rë   rö   rù   r%   r   r   r   r   Ú<module>   sn   

	

    XVM
