a
    1$eݍ                     @   s$  d dl 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 ZG dd	 d	Ze	jrd dlZd d
lmZ d dlmZ G dd dejjeZG dd dejjZe	jse	jr ddlmZ ddlmZ e	jrd dlZdd ej j!D dhB Z"n d dl#Zdd ej j!D dhB Z"G dd deZ$dS )    N)BaseGeometry   )_compat)docc                   C   s&   t jst jrtS t jrtS tddS )zDynamically chooses a spatial indexing backend.

    Required to comply with _compat.USE_PYGEOS.
    The selection order goes PyGEOS > RTree > Error.
    zwSpatial indexes require either `rtree` or `pygeos`. See installation instructions at https://geopandas.org/install.htmlN)compatUSE_SHAPELY_20Z
USE_PYGEOSPyGEOSSTRTreeIndex	HAS_RTREE
RTreeIndexImportError r   r   L/var/www/html/django/DPS/env/lib/python3.9/site-packages/geopandas/sindex.py_get_sindex_class   s    r   c                   @   sV   e Zd Zedd ZdddZdddZdd
dZdd Zedd Z	edd Z
dS )BaseSpatialIndexc                 C   s   t dS )a  Returns valid predicates for this spatial index.

        Returns
        -------
        set
            Set of valid predicates for this spatial index.

        Examples
        --------
        >>> from shapely.geometry import Point
        >>> s = geopandas.GeoSeries([Point(0, 0), Point(1, 1)])
        >>> s.sindex.valid_query_predicates  # doctest: +SKIP
        {'contains', 'crosses', 'intersects', 'within', 'touches', 'overlaps', None, 'covers', 'contains_properly'}
        NNotImplementedErrorselfr   r   r   valid_query_predicates   s    z'BaseSpatialIndex.valid_query_predicatesNFc                 C   s   t dS )a  
        Return the integer indices of all combinations of each input geometry
        and tree geometries where the bounding box of each input geometry
        intersects the bounding box of a tree geometry.

        If the input geometry is a scalar, this returns an array of shape (n, ) with
        the indices of the matching tree geometries.  If the input geometry is an
        array_like, this returns an array with shape (2,n) where the subarrays
        correspond to the indices of the input geometries and indices of the
        tree geometries associated with each.  To generate an array of pairs of
        input geometry index and tree geometry index, simply transpose the
        result.

        If a predicate is provided, the tree geometries are first queried based
        on the bounding box of the input geometry and then are further filtered
        to those that meet the predicate when comparing the input geometry to
        the tree geometry: ``predicate(geometry, tree_geometry)``.

        Bounding boxes are limited to two dimensions and are axis-aligned
        (equivalent to the ``bounds`` property of a geometry); any Z values
        present in input geometries are ignored when querying the tree.

        Any input geometry that is None or empty will never match geometries in
        the tree.

        Parameters
        ----------
        geometry : shapely.Geometry or array-like of geometries (numpy.ndarray, GeoSeries, GeometryArray)
            A single shapely geometry or array of geometries to query against
            the spatial index. For array-like, accepts both GeoPandas geometry
            iterables (GeoSeries, GeometryArray) or a numpy array of Shapely
            or PyGEOS geometries.
        predicate : {None, "contains", "contains_properly", "covered_by", "covers", "crosses", "intersects", "overlaps", "touches", "within"}, optional
            If predicate is provided, the input geometries are tested
            using the predicate function against each item in the tree
            whose extent intersects the envelope of the input geometry:
            ``predicate(input_geometry, tree_geometry)``.
            If possible, prepared geometries are used to help speed up the
            predicate operation.
        sort : bool, default False
            If True, the results will be sorted in ascending order. In case
            of 2D array, the result is sorted lexicographically using the
            geometries' indexes as the primary key and the sindex's indexes
            as the secondary key.
            If False, no additional sorting is applied (results are often
            sorted but there is no guarantee).

        Returns
        -------
        ndarray with shape (n,) if geometry is a scalar
            Integer indices for matching geometries from the spatial index
            tree geometries.

        OR

        ndarray with shape (2, n) if geometry is an array_like
            The first subarray contains input geometry integer indices.
            The second subarray contains tree geometry integer indices.

        Examples
        --------
        >>> from shapely.geometry import Point, box
        >>> s = geopandas.GeoSeries(geopandas.points_from_xy(range(10), range(10)))
        >>> s
        0    POINT (0.00000 0.00000)
        1    POINT (1.00000 1.00000)
        2    POINT (2.00000 2.00000)
        3    POINT (3.00000 3.00000)
        4    POINT (4.00000 4.00000)
        5    POINT (5.00000 5.00000)
        6    POINT (6.00000 6.00000)
        7    POINT (7.00000 7.00000)
        8    POINT (8.00000 8.00000)
        9    POINT (9.00000 9.00000)
        dtype: geometry

        Querying the tree with a scalar geometry:

        >>> s.sindex.query(box(1, 1, 3, 3))
        array([1, 2, 3])

        >>> s.sindex.query(box(1, 1, 3, 3), predicate="contains")
        array([2])

        Querying the tree with an array of geometries:

        >>> s2 = geopandas.GeoSeries([box(2, 2, 4, 4), box(5, 5, 6, 6)])
        >>> s2
        0    POLYGON ((4.00000 2.00000, 4.00000 4.00000, 2....
        1    POLYGON ((6.00000 5.00000, 6.00000 6.00000, 5....
        dtype: geometry

        >>> s.sindex.query(s2)
        array([[0, 0, 0, 1, 1],
               [2, 3, 4, 5, 6]])

        >>> s.sindex.query(s2, predicate="contains")
        array([[0],
               [3]])

        Notes
        -----
        In the context of a spatial join, input geometries are the "left"
        geometries that determine the order of the results, and tree geometries
        are "right" geometries that are joined against the left geometries. This
        effectively performs an inner join, where only those combinations of
        geometries that can be joined based on overlapping bounding boxes or
        optional predicate are returned.
        Nr   r   geometry	predicatesortr   r   r   query/   s    pzBaseSpatialIndex.queryc                 C   s   t dS )u  
        DEPRECATED: use `query` instead.

        Returns all combinations of each input geometry and geometries in
        the tree where the envelope of each input geometry intersects with
        the envelope of a tree geometry.

        In the context of a spatial join, input geometries are the “left”
        geometries that determine the order of the results, and tree geometries
        are “right” geometries that are joined against the left geometries.
        This effectively performs an inner join, where only those combinations
        of geometries that can be joined based on envelope overlap or optional
        predicate are returned.

        When using the ``rtree`` package, this is not a vectorized function
        and may be slow. If speed is important, please use PyGEOS.

        Parameters
        ----------
        geometry : {GeoSeries, GeometryArray, numpy.array of PyGEOS geometries}
            Accepts GeoPandas geometry iterables (GeoSeries, GeometryArray)
            or a numpy array of PyGEOS geometries.
        predicate : {None, "contains", "contains_properly", "covered_by", "covers", "crosses", "intersects", "overlaps", "touches", "within"}, optional
            If predicate is provided, the input geometries are tested using
            the predicate function against each item in the tree whose extent
            intersects the envelope of the each input geometry:
            predicate(input_geometry, tree_geometry).  If possible, prepared
            geometries are used to help speed up the predicate operation.
        sort : bool, default False
            If True, results sorted lexicographically using
            geometry's indexes as the primary key and the sindex's indexes as the
            secondary key. If False, no additional sorting is applied.

        Returns
        -------
        ndarray with shape (2, n)
            The first subarray contains input geometry integer indexes.
            The second subarray contains tree geometry integer indexes.

        Examples
        --------
        >>> from shapely.geometry import Point, box
        >>> s = geopandas.GeoSeries(geopandas.points_from_xy(range(10), range(10)))
        >>> s
        0    POINT (0.00000 0.00000)
        1    POINT (1.00000 1.00000)
        2    POINT (2.00000 2.00000)
        3    POINT (3.00000 3.00000)
        4    POINT (4.00000 4.00000)
        5    POINT (5.00000 5.00000)
        6    POINT (6.00000 6.00000)
        7    POINT (7.00000 7.00000)
        8    POINT (8.00000 8.00000)
        9    POINT (9.00000 9.00000)
        dtype: geometry
        >>> s2 = geopandas.GeoSeries([box(2, 2, 4, 4), box(5, 5, 6, 6)])
        >>> s2
        0    POLYGON ((4.00000 2.00000, 4.00000 4.00000, 2....
        1    POLYGON ((6.00000 5.00000, 6.00000 6.00000, 5....
        dtype: geometry

        >>> s.sindex.query_bulk(s2)
        array([[0, 0, 0, 1, 1],
                [2, 3, 4, 5, 6]])

        >>> s.sindex.query_bulk(s2, predicate="contains")
        array([[0],
                [3]])
        Nr   r   r   r   r   
query_bulk   s    GzBaseSpatialIndex.query_bulkTc                 C   s   t dS )a  
        Return the nearest geometry in the tree for each input geometry in
        ``geometry``.

        .. note::
            ``nearest`` currently only works with PyGEOS >= 0.10.

            Note that if PyGEOS is not available, geopandas will use rtree
            for the spatial index, where nearest has a different
            function signature to temporarily preserve existing
            functionality. See the documentation of
            :meth:`rtree.index.Index.nearest` for the details on the
            ``rtree``-based implementation.

        If multiple tree geometries have the same distance from an input geometry,
        multiple results will be returned for that input geometry by default.
        Specify ``return_all=False`` to only get a single nearest geometry
        (non-deterministic which nearest is returned).

        In the context of a spatial join, input geometries are the "left"
        geometries that determine the order of the results, and tree geometries
        are "right" geometries that are joined against the left geometries.
        If ``max_distance`` is not set, this will effectively be a left join
        because every geometry in ``geometry`` will have a nearest geometry in
        the tree. However, if ``max_distance`` is used, this becomes an
        inner join, since some geometries in ``geometry`` may not have a match
        in the tree.

        For performance reasons, it is highly recommended that you set
        the ``max_distance`` parameter.

        Parameters
        ----------
        geometry : {shapely.geometry, GeoSeries, GeometryArray, numpy.array of PyGEOS geometries}
            A single shapely geometry, one of the GeoPandas geometry iterables
            (GeoSeries, GeometryArray), or a numpy array of PyGEOS geometries to query
            against the spatial index.
        return_all : bool, default True
            If there are multiple equidistant or intersecting nearest
            geometries, return all those geometries instead of a single
            nearest geometry.
        max_distance : float, optional
            Maximum distance within which to query for nearest items in tree.
            Must be greater than 0. By default None, indicating no distance limit.
        return_distance : bool, optional
            If True, will return distances in addition to indexes. By default False
        exclusive : bool, optional
            if True, the nearest geometries that are equal to the input geometry
            will not be returned. By default False.  Requires Shapely >= 2.0.

        Returns
        -------
        Indices or tuple of (indices, distances)
            Indices is an ndarray of shape (2,n) and distances (if present) an
            ndarray of shape (n).
            The first subarray of indices contains input geometry indices.
            The second subarray of indices contains tree geometry indices.

        Examples
        --------
        >>> from shapely.geometry import Point, box
        >>> s = geopandas.GeoSeries(geopandas.points_from_xy(range(10), range(10)))
        >>> s.head()
        0    POINT (0.00000 0.00000)
        1    POINT (1.00000 1.00000)
        2    POINT (2.00000 2.00000)
        3    POINT (3.00000 3.00000)
        4    POINT (4.00000 4.00000)
        dtype: geometry

        >>> s.sindex.nearest(Point(1, 1))
        array([[0],
               [1]])

        >>> s.sindex.nearest([box(4.9, 4.9, 5.1, 5.1)])
        array([[0],
               [5]])

        >>> s2 = geopandas.GeoSeries(geopandas.points_from_xy([7.6, 10], [7.6, 10]))
        >>> s2
        0    POINT (7.60000 7.60000)
        1    POINT (10.00000 10.00000)
        dtype: geometry

        >>> s.sindex.nearest(s2)
        array([[0, 1],
               [8, 9]])
        Nr   )r   r   
return_allmax_distancereturn_distance	exclusiver   r   r   nearest   s    azBaseSpatialIndex.nearestc                 C   s   t dS )a8  Compatibility wrapper for rtree.index.Index.intersection,
        use ``query`` instead.

        Parameters
        ----------
        coordinates : sequence or array
            Sequence of the form (min_x, min_y, max_x, max_y)
            to query a rectangle or (x, y) to query a point.

        Examples
        --------
        >>> from shapely.geometry import Point, box
        >>> s = geopandas.GeoSeries(geopandas.points_from_xy(range(10), range(10)))
        >>> s
        0    POINT (0.00000 0.00000)
        1    POINT (1.00000 1.00000)
        2    POINT (2.00000 2.00000)
        3    POINT (3.00000 3.00000)
        4    POINT (4.00000 4.00000)
        5    POINT (5.00000 5.00000)
        6    POINT (6.00000 6.00000)
        7    POINT (7.00000 7.00000)
        8    POINT (8.00000 8.00000)
        9    POINT (9.00000 9.00000)
        dtype: geometry

        >>> s.sindex.intersection(box(1, 1, 3, 3).bounds)
        array([1, 2, 3])

        Alternatively, you can use ``query``:

        >>> s.sindex.query(box(1, 1, 3, 3))
        array([1, 2, 3])

        Nr   r   coordinatesr   r   r   intersectionM  s    $zBaseSpatialIndex.intersectionc                 C   s   t dS )a  Size of the spatial index

        Number of leaves (input geometries) in the index.

        Examples
        --------
        >>> from shapely.geometry import Point
        >>> s = geopandas.GeoSeries(geopandas.points_from_xy(range(10), range(10)))
        >>> s
        0    POINT (0.00000 0.00000)
        1    POINT (1.00000 1.00000)
        2    POINT (2.00000 2.00000)
        3    POINT (3.00000 3.00000)
        4    POINT (4.00000 4.00000)
        5    POINT (5.00000 5.00000)
        6    POINT (6.00000 6.00000)
        7    POINT (7.00000 7.00000)
        8    POINT (8.00000 8.00000)
        9    POINT (9.00000 9.00000)
        dtype: geometry

        >>> s.sindex.size
        10
        Nr   r   r   r   r   sizes  s    zBaseSpatialIndex.sizec                 C   s   t dS )a  Check if the spatial index is empty

        Examples
        --------
        >>> from shapely.geometry import Point
        >>> s = geopandas.GeoSeries(geopandas.points_from_xy(range(10), range(10)))
        >>> s
        0    POINT (0.00000 0.00000)
        1    POINT (1.00000 1.00000)
        2    POINT (2.00000 2.00000)
        3    POINT (3.00000 3.00000)
        4    POINT (4.00000 4.00000)
        5    POINT (5.00000 5.00000)
        6    POINT (6.00000 6.00000)
        7    POINT (7.00000 7.00000)
        8    POINT (8.00000 8.00000)
        9    POINT (9.00000 9.00000)
        dtype: geometry

        >>> s.sindex.is_empty
        False

        >>> s2 = geopandas.GeoSeries()
        >>> s2.sindex.is_empty
        True
        Nr   r   r   r   r   is_empty  s    zBaseSpatialIndex.is_empty)NF)NF)TNFF)__name__
__module____qualname__propertyr   r   r   r   r"   r#   r$   r   r   r   r   r      s   

r
L    
c&
r   )
RTreeError)prepc                       sx   e Zd ZdZ fddZeej fddZeej fddZe	eej
dd	 Z
e	eejd
d Z  ZS )SpatialIndexz9Original rtree wrapper, kept for backwards compatibility.c                    s    t jdtdd t j|  d S )NzDirectly using SpatialIndex is deprecated, and the class will be removed in a future version. Access the spatial index through the `GeoSeries.sindex` attribute, or use `rtree.index.Index` directly.   
stacklevel)warningswarnFutureWarningsuper__init__)r   args	__class__r   r   r3     s    zSpatialIndex.__init__c                    s   t  j|g|R i |S Nr2   r"   )r   r!   r4   kwargsr5   r   r   r"     s    zSpatialIndex.intersectionc                    s   t  j|i |S r7   )r2   r   )r   r4   r9   r5   r   r   r     s    zSpatialIndex.nearestc                 C   s   t |  d d S )Nr   r   )lenleavesr   r   r   r   r#     s    zSpatialIndex.sizec                 C   s   t |  dkrdS | jdk S )Nr   F)r:   r;   r#   r   r   r   r   r$     s    zSpatialIndex.is_empty)r%   r&   r'   __doc__r3   r   r   r"   r   r(   r#   r$   __classcell__r   r   r5   r   r+     s   
r+   c                       s   e Zd ZdZ fddZeeejdd Zeej	ddd	Z	eej
dd
dZ
d fdd	Zeej fddZeeejdd Zeeejdd Zdd Z  ZS )r
   zA simple wrapper around rtree's RTree Index

        Parameters
        ----------
        geometry : np.array of Shapely geometries
            Geometries from which to build the spatial index.
        c                    sd   dd t |D }zt | W n ty>   t   Y n0 || _tjd g| jj td| _	d S )Nc                 s   s.   | ]&\}}t |r|js||jd fV  qd S r7   )pdZnotnullr$   bounds).0iitemr   r   r   	<genexpr>  s   z&RTreeIndex.__init__.<locals>.<genexpr>Zdtype)
	enumerater2   r3   r)   
geometriesnparrayr#   object_prepared_geometries)r   r   streamr5   r   r   r3     s    zRTreeIndex.__init__c                 C   s   h dS )N>
   overlapscontains_properly
intersectsZtouchesNcoversZcrosseswithincontains
covered_byr   r   r   r   r   r     s    z!RTreeIndex.valid_query_predicatesNFc                    s  j vrtdj t drt tsg }g }t D ]6\}}j||d}|| ||gt	|  q@t
||gS  d u rt
jg t
jdS t tstdt d  jrt
jg t
jdS  j}	t|	}
|
st
jg t
jdS dkr\g }|
D ]F}j| d u r6tj| j|< j|  r|| q|}
n2d urdv rxt   fd	d
|
D }
|rt
t
j|
t
jdS t
j|
t
jdS )Nz5Got `predicate` = `{}`, `predicate` must be one of {}Z	__array__r   r   rD   z0Got `geometry` of type `{}`, `geometry` must be za shapely geometry.rP   )rQ   rN   rR   rO   rM   c                    s$   g | ]}t  j| r|qS r   )getattrrF   )r@   index_in_treer   r   r   r   r   
<listcomp>S  s   z$RTreeIndex.query.<locals>.<listcomp>)r   
ValueErrorformathasattr
isinstancer   rE   r   extendr:   rG   vstackrH   Zintp	TypeErrortyper$   r?   listr"   rJ   r*   rF   rQ   appendr   )r   r   r   r   Z
tree_indexZinput_geometry_indexrA   Zgeoresr?   tree_idxrU   r   rV   r   r     sf    


	



zRTreeIndex.queryc                 C   s    t jdtdd | j|||dS NzwThe `query_bulk()` method is deprecated and will be removed in GeoPandas 1.0. You can use the `query()` method instead.r,   r-   rS   r/   r0   r1   r   r   r   r   r   r   a  s    zRTreeIndex.query_bulkr   c                    s"   t jdtdd t j|||dS )u  
            Returns the nearest object or objects to the given coordinates.

            Requires rtree, and passes parameters directly to
            :meth:`rtree.index.Index.nearest`.

            This behaviour is deprecated and will be updated to be consistent
            with the pygeos PyGEOSSTRTreeIndex in a future release.

            If longer-term compatibility is required, use
            :meth:`rtree.index.Index.nearest` directly instead.

            Examples
            --------
            >>> s = geopandas.GeoSeries(geopandas.points_from_xy(range(3), range(3)))
            >>> s
            0    POINT (0.00000 0.00000)
            1    POINT (1.00000 1.00000)
            2    POINT (2.00000 2.00000)
            dtype: geometry

            >>> list(s.sindex.nearest((0, 0)))  # doctest: +SKIP
            [0]

            >>> list(s.sindex.nearest((0.5, 0.5)))  # doctest: +SKIP
            [0, 1]

            >>> list(s.sindex.nearest((3, 3), num_results=2))  # doctest: +SKIP
            [2, 1]

            >>> list(super(type(s.sindex), s.sindex).nearest((0, 0),
            ... num_results=2))  # doctest: +SKIP
            [0, 1]

            Parameters
            ----------
            coordinates : sequence or array
                This may be an object that satisfies the numpy array protocol,
                providing the index’s dimension * 2 coordinate pairs
                representing the mink and maxk coordinates in each dimension
                defining the bounds of the query window.
            num_results : integer
                The number of results to return nearest to the given
                coordinates. If two index entries are equidistant, both are
                returned. This property means that num_results may return more
                items than specified
            objects : True / False / ‘raw’
                If True, the nearest method will return index objects that were
                pickled when they were stored with each index entry, as well as
                the id and bounds of the index entries. If ‘raw’, it will
                return the object as entered into the database without the
                rtree.index.Item wrapper.
            a  sindex.nearest using the rtree backend was not previously documented and this behavior is deprecated in favor of matching the function signature provided by the pygeos backend (see PyGEOSSTRTreeIndex.nearest for details). This behavior will be updated in a future release.r,   r-   )num_resultsobjects)r/   r0   r1   r2   r   )r   r!   rf   rg   r5   r   r   r   k  s    6	zRTreeIndex.nearestc                    s   t  j|ddS )NF)rg   r8   r    r5   r   r   r"     s    zRTreeIndex.intersectionc                 C   s0   t | dr| j}nt|  d d }|| _|S )N_sizer   r   )rZ   rh   r:   r;   )r   r#   r   r   r   r#     s
    
zRTreeIndex.sizec                 C   s   | j jdkp| jdkS Nr   )rF   r#   r   r   r   r   r$     s    zRTreeIndex.is_emptyc                 C   s   | j S r7   )r#   r   r   r   r   __len__  s    zRTreeIndex.__len__)NF)NF)r   F)r%   r&   r'   r<   r3   r(   r   r   r   r   r   r   r"   r#   r$   rj   r=   r   r   r5   r   r
     s&   ]	Cr
   )	geoseries)rH   c                 C   s   h | ]
}|j qS r   namer@   pr   r   r   	<setcomp>      rp   c                 C   s   h | ]
}|j qS r   rl   rn   r   r   r   rp     rq   c                   @   s   e Zd ZdZdd Zedd Zeej	ddd	Z	e
d
d ZeejdddZeejdddZeejdd Zeeejdd Zeeejdd Zdd ZdS )r   zA simple wrapper around pygeos's STRTree.


        Parameters
        ----------
        geometry : np.array of PyGEOS geometries
            Geometries from which to build the spatial index.
        c                 C   s0   |  }d |t|< t|| _|  | _d S r7   )copymodr$   ZSTRtree_treerF   )r   r   Z	non_emptyr   r   r   r3     s    zPyGEOSSTRTreeIndex.__init__c                 C   s   t S )a$  Returns valid predicates for the used spatial index.

            Returns
            -------
            set
                Set of valid predicates for this spatial index.

            Examples
            --------
            >>> from shapely.geometry import Point
            >>> s = geopandas.GeoSeries([Point(0, 0), Point(1, 1)])
            >>> s.sindex.valid_query_predicates  # doctest: +SKIP
            {None, "contains", "contains_properly", "covered_by", "covers", "crosses", "intersects", "overlaps", "touches", "within"}
            )_PYGEOS_PREDICATESr   r   r   r   r     s    z)PyGEOSSTRTreeIndex.valid_query_predicatesNFc                 C   s   || j vr$td|d| j  | |}tjrF| jj||d}n.t|t	j
rd| jj||d}n| jj||d}|r|jdkrt	|S |\}}t	||f}t	|| || fS |S )NzGot `predicate` = `{}`; z`predicate` must be one of {})r   r   )r   rX   rY   _as_geometry_arrayr   r   rt   r   r[   rG   ndarrayr   ndimr   Zlexsortr]   )r   r   r   r   indicesZgeo_idxrc   Zsort_indexerr   r   r   r     s*    



zPyGEOSSTRTreeIndex.queryc                 C   s   t | tjrt| } t | tjr.t| jS t | t	j
rB| jjS t | tjrT| jS t | trht| S | du rtdS t | trtdd | D S t| S dS )a  Convert geometry into a numpy array of PyGEOS geometries.

            Parameters
            ----------
            geometry
                An array-like of PyGEOS geometries, a GeoPandas GeoSeries/GeometryArray,
                shapely.geometry or list of shapely geometries.

            Returns
            -------
            np.ndarray
                A numpy array of pygeos geometries.
            Nc                 S   s$   g | ]}t |trt|n|qS r   )r[   r   rH   _shapely_to_geom)r@   elr   r   r   rW   =  s   z9PyGEOSSTRTreeIndex._as_geometry_array.<locals>.<listcomp>)r[   rs   ZGeometryrH   Z_geom_to_shapelyrG   rw   Zfrom_shapely_datark   Z	GeoSeriesvaluesZGeometryArrayr   rz   r`   Zasarray)r   r   r   r   rv     s&    



	z%PyGEOSSTRTreeIndex._as_geometry_arrayc                 C   s    t jdtdd | j|||dS rd   re   r   r   r   r   r   G  s    zPyGEOSSTRTreeIndex.query_bulkTc           
      C   s  t jst jstd|r&t js&td| |}t|tsB|d u rH|g}t jrf| jj|||||d}n.|s|d u r|s| j	|S | jj
|||d}|r|\}}n|}|st jst|dd d f d}	t|	dd}	|d d |	f }|r||	 }|r||fS |S d S )Nz8sindex.nearest requires shapely >= 2.0 or pygeos >= 0.10z:sindex.nearest exclusive parameter requires shapely >= 2.0)r   r   Zall_matchesr   )r   r   r   boolT)r   r   ZPYGEOS_GE_010r   rv   r[   r   rt   Zquery_nearestr   Znearest_allrG   diffZastypeinsert)
r   r   r   r   r   r   resultry   Z	distancesmaskr   r   r   r   Q  sH    	



zPyGEOSSTRTreeIndex.nearestc                 C   s   zt | W n  ty,   td|Y n0 t|dkrN| jtj| }n.t|dkrn| jtj| }ntd||S )NzInvalid coordinates, must be iterable in format (minx, miny, maxx, maxy) (for bounds) or (x, y) (for points). Got `coordinates` = {}.   r,   )	iterr^   rY   r:   rt   r   rs   boxZpoints)r   r!   indexesr   r   r   r"     s$    
zPyGEOSSTRTreeIndex.intersectionc                 C   s
   t | jS r7   r:   rt   r   r   r   r   r#     s    zPyGEOSSTRTreeIndex.sizec                 C   s   t | jdkS ri   r   r   r   r   r   r$     s    zPyGEOSSTRTreeIndex.is_emptyc                 C   s
   t | jS r7   r   r   r   r   r   rj     s    zPyGEOSSTRTreeIndex.__len__)NF)NF)TNFF)r%   r&   r'   r<   r3   r(   r   r   r   r   staticmethodrv   r   r   r"   r#   r$   rj   r   r   r   r   r     s2   	

)	    9
r   )%r/   Zshapely.geometry.baser   Zpandasr>   numpyrG    r   r   Z
_decoratorr   r   r   r	   Zrtree.indexZrtreeZ
rtree.corer)   Zshapely.preparedr*   indexIndexr+   r
   ZSHAPELY_GE_20Z
HAS_PYGEOSrk   rH   r   Zshapelyrs   ZstrtreeZBinaryPredicateru   Zpygeosr   r   r   r   r   <module>   s4      ! u