3
®?d]  ã               @   s>   d dl mZ d dlZG dd„ dƒZedkr:d dlZejƒ  dS )é    )ÚlgeosNc               @   s(   e Zd ZdZdd„ Zdd„ Zdd„ ZdS )	ÚSTRtreea’  
    STRtree is an R-tree that is created using the Sort-Tile-Recursive
    algorithm. STRtree takes a sequence of geometry objects as initialization
    parameter. After initialization the query method can be used to make a
    spatial query over those objects.

    >>> from shapely.geometry import Polygon
    >>> polys = [ Polygon(((0, 0), (1, 0), (1, 1))), Polygon(((0, 1), (0, 0), (1, 0))), Polygon(((100, 100), (101, 100), (101, 101))) ]
    >>> s = STRtree(polys)
    >>> query_geom = Polygon(((-1, -1), (2, 0), (2, 2), (-1, 2)))
    >>> result = s.query(query_geom)
    >>> polys[0] in result
    True
    >>> polys[1] in result
    True
    >>> polys[2] in result
    False
    >>> # Test empty tree
    >>> s = STRtree([])
    >>> s.query(query_geom)
    []
    >>> # Test tree with one object
    >>> s = STRtree([polys[0]])
    >>> result = s.query(query_geom)
    >>> polys[0] in result
    True
    c             C   sb   dd„ |D ƒ}t |ƒ| _tjtdt |ƒƒƒ| _x$|D ]}tj| j|jtj	|ƒƒ q4W t
|ƒ| _d S )Nc             S   s   g | ]}|j s|‘qS © )Zis_empty)Ú.0Úgeomr   r   ú2/tmp/pip-build-op5p2y5_/shapely/shapely/strtree.pyú
<listcomp>#   s    z$STRtree.__init__.<locals>.<listcomp>é   )ÚlenÚ_n_geomsr   ZGEOSSTRtree_createÚmaxÚ_tree_handleZGEOSSTRtree_insertÚ_geomÚctypesÚ	py_objectÚlistZ_geoms)ÚselfZgeomsr   r   r   r   Ú__init__!   s    

zSTRtree.__init__c             C   s    | j d k	rtj| j ƒ d | _ d S )N)r   r   ZGEOSSTRtree_destroy)r   r   r   r   Ú__del__-   s    
zSTRtree.__del__c                s<   | j dkrg S g ‰ ‡ fdd„}tj| j|jtj|ƒd ƒ ˆ S )Nr   c                s   t j| t jƒj}ˆ j|ƒ d S )N)r   Úcastr   ÚvalueÚappend)ÚitemZuserdatar   )Úresultr   r   Úcallback8   s    zSTRtree.query.<locals>.callback)r   r   ZGEOSSTRtree_queryr   r   ZGEOSQueryCallback)r   r   r   r   )r   r   Úquery2   s    
zSTRtree.queryN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r      s   r   Ú__main__)Zshapely.geosr   r   r   r   ÚdoctestÚtestmodr   r   r   r   Ú<module>   s
   <