
    `chp                      d dl mZ d dlZd dlZd dlZd dlZd dlZd dlZd dlm	Z	m
Z
 d dlmZmZmZ ddlmZ ddlmZ d ZdZdZd ZdZdZd ZdZdZej8                  j;                         Zd	 ej?                  d
      jA                  d      D        \  Z!Z"Z#e!e"e#fdk  r e$d      g dZ%d Z&d Z' G d d      Z(e(Z) G d d      Z* G d de$      Z+ G d d      Z, G d de,      Z- G d de-      Z.	  G d de-      Z/ G d d e,      Z1 G d! d"      Z2ejf                  Z4 G d# d$ejj                        Z6 G d% d&      Z7 G d' d(e7      Z8 G d) d*e7      Z9 G d+ d,e)      Z:y# e0$ r Y iw xY w)-    )annotationsN)IteratorSequence)AnyLiteraloverload   )core)
RTreeError   c              #  2   K   | ]  }t        |        y wN)int).0ts     >/var/www/html/immo/lib/python3.12/site-packages/rtree/index.py	<genexpr>r      s      /CF/s   utf-8.)r	         z/Rtree requires libspatialindex 1.8.5 or greater)RtreeIndexPropertyc                   t        j                  t        j                               }t        j                  t        j                               }t        j                  d      } || t        j                  |      t        j                  |      t        j                  |             |j
                  dk(  ry t        j                  |t        j                  t         j                  |j
                  z              }t        j                  |t        j                  t         j                  |j
                  z              }t        |j
                        D cg c]  }|j                  |    }	}|	t        |j
                        D cg c]  }|j                  |    c}z  }	t        j                  |t        j                  t         j                              }
t        j                  |t        j                  t         j                              }t        j                  j                  t        j                  |
t        j                  t         j                                     t        j                  j                  t        j                  |t        j                  t         j                                     |r|	S t        j                  |	      S c c}w c c}w Nr   )ctypespointerc_doublec_uint32byrefvaluecastPOINTERrangecontentsr
   rt
Index_Freec_void_pr   deinterleave)handle	bounds_fninterleavedpp_minspp_maxs	dimensionminsmaxsiresultsp_minsp_maxss               r   _get_boundsr7   '   s   nnV__./GnnV__./G"IW%v||G'<fll9>U !;;wv/P QRD;;wv/P QRD).y)?@At}}Q@G@%	*@AQa AAG[[v~~foo>?F[[v~~foo>?FGGv{{66>>&//+JKLGGv{{66>>&//+JKLg&& AAs   KKc                X   t        j                  d      }t        j                  t        j                  d            }t        j
                  j                  | t        j                  |      t        j                  |             t        j                  |t        j                  t         j                              }|j                  dk(  r t        j
                  j                  |       y t        j                  ||j                        }t        j
                  j                  |       |S r   )r   c_uint64r   c_uint8r
   r'   IndexItem_GetDatar!   r#   r$   r)   r"   r(   	string_at)r+   lengthdcss        r   	_get_datarA   A   s    __QFv~~a()AGGffll1ov||F7KLAv~~foo67A||q1FLL)AGGqH    c                  L   e Zd ZdZd=dZd>dZd>dZd?dZd@dZdAdZ	dBdZ
dCd	ZdDd
ZdDdZ	 	 	 	 dEdZed        Zd Zd Zd Z eee      Zd Zd Z eee      ZdFdGdZeZ	 dF	 	 	 	 	 	 	 	 	 	 	 dHdZdIdZ	 	 	 	 	 	 	 	 dJdZedKd       Ze	 dL	 	 	 	 	 dMd       Ze	 	 	 	 	 	 dNd       Z	 dL	 	 	 	 	 dOdZdPdZ dPdZ!e	 	 	 	 	 	 dKd       Z"e	 dL	 	 	 	 	 dQd        Z"e	 	 	 	 	 	 dNd!       Z"	 dL	 	 	 	 	 dRd"Z"dLd#Z#d$ Z$dSd%Z%d& Z&d' Z'd( Z(e	 	 	 	 	 	 	 	 dTd)       Z)e	 dL	 	 	 	 	 	 	 dUd*       Z)e	 	 	 	 	 	 	 	 dVd+       Z)	 	 dW	 	 	 	 	 	 	 dXd-Z)d. Z*d,dddd/d0Z+d1 Z,dWd2Z-dFd3Z. ee.      Z/dYd4Z0	 	 	 	 	 	 	 	 	 	 dZd5Z1d[d6Z2d7 Z3e4d\d8       Z5e4d]d9       Z6d: Z7d; Z8d< Z9y)^r   z0An R-Tree, MVR-Tree, or TPR-Tree indexing objectc                H	   |j                  dt                     | _        | j                  j                  t        k(  r%t        t        j                  d      st        d      t        |j                  dd            | _
        d}d}d}d}|rt        |d   t              st        |d   t              r2|d   }t        |      dkD  rit        |d   t              r|d   }nP|d   }nJt        |d   t               r|d   }t        |      dkD  r$|d   }nt        |d   t              r|d   }n|d   }|r4t"        | j                  _        || j                  _        t        |      d	z   | j                  j(                  z   }t*        j,                  j/                  |      }t*        j,                  j1                  |      rWt        |j                  d
d            | j                  _        | j                  j2                  s	 | j                  j4                   t*        j,                  j9                  |      }	t+        j:                  |	t*        j<                        sd| d}
t?        |
      |rt@        | j                  _        |jB                  rht        |j                  d
d            | j                  _        | j                  j2                  s	 | j                  j4                   n|jE                          || _#        |jI                  | j                         ntJ        | j                  _        |j                  dd      }|rtM        |      | j                  _'        |rS| j                  j                  tP        k(  r6d| _)        | jU                  |      | _+        | jR                  r| jR                  y|rR| j                  j                  tP        k(  r5d| _)        	  | jX                  | | _+        | jR                  r| jR                  yt_        | j                  jV                        | _+        |r|D ]  } | j`                  |   y|rt]        d      y# t6        $ r d| j                  _        Y Vw xY w# t6        $ r d| j                  _        Y w xY w# tZ        $ r t]        d      w xY w)a~  Creates a new index

        :param filename:
            The first argument in the constructor is assumed to be a filename
            determining that a file-based storage for the index should be used.
            If the first argument is not of type basestring, it is then assumed
            to be an instance of ICustomStorage or derived class.
            If the first argument is neither of type basestring nor an instance
            of ICustomStorage, it is then assumed to be an input index item
            stream.

        :param stream:
            If the first argument in the constructor is not of type basestring,
            it is assumed to be an iterable stream of data that will raise a
            StopIteration.  It must be in the form defined by the
            :attr:`interleaved` attribute of the index. The following example
            would assume :attr:`interleaved` is False::

                (id,
                 (minx, maxx, miny, maxy, minz, maxz, ..., ..., mink, maxk),
                 object)

            The object can be None, but you must put a place holder of
            ``None`` there.

            For a TPR-Tree, this would be in the form::

                (id,
                 ((minx, maxx, miny, maxy, ..., ..., mink, maxk),
                  (minvx, maxvx, minvy, maxvy, ..., ..., minvk, maxvk),
                  time),
                 object)

        :param storage:
            If the first argument in the constructor is an instance of
            ICustomStorage then the given custom storage is used.

        :param interleaved: True or False, defaults to True.
            This parameter determines the coordinate order for all methods that
            take in coordinates.

        :param properties: An :class:`index.Property` object.
            This object sets both the creation and instantiation properties
            for the object and they are passed down into libspatialindex.
            A few properties are curried from instantiation parameters
            for you like ``pagesize`` and ``overwrite``
            to ensure compatibility with previous versions of the library.  All
            other properties must be set on the object.

        .. warning::
            The coordinate ordering for all functions are sensitive the
            index's :attr:`interleaved` data member.  If :attr:`interleaved`
            is False, the coordinates must be in the form
            [xmin, xmax, ymin, ymax, ..., ..., kmin, kmax]. If
            :attr:`interleaved` is True, the coordinates must be in the form
            [xmin, ymin, ..., kmin, xmax, ymax, ..., kmax]. This also applies
            to velocities when using a TPR-Tree.

        A basic example
        ::

            >>> from rtree import index
            >>> p = index.Property()

            >>> idx = index.Index(properties=p)
            >>> idx  # doctest: +NORMALIZE_WHITESPACE
            rtree.index.Index(bounds=[1.7976931348623157e+308,
                                    1.7976931348623157e+308,
                                    -1.7976931348623157e+308,
                                    -1.7976931348623157e+308],
                                    size=0)

        Insert an item into the index::

            >>> idx.insert(4321,
            ...            (34.3776829412, 26.7375853734, 49.3776829412,
            ...             41.7375853734),
            ...            obj=42)

        Query::

            >>> hits = idx.intersection((0, 0, 60, 60), objects=True)
            >>> for i in hits:
            ...     if i.id == 4321:
            ...         i.object
            ...         i.bbox
            ... # doctest: +ELLIPSIS
            42
            [34.37768294..., 26.73758537..., 49.37768294..., 41.73758537...]


        Using custom serializers::

            >>> class JSONIndex(index.Index):
            ...     def dumps(self, obj):
            ...         # This import is nested so that the doctest doesn't
            ...         # require simplejson.
            ...         import simplejson
            ...         return simplejson.dumps(obj).encode('ascii')
            ...
            ...     def loads(self, string):
            ...         import simplejson
            ...         return simplejson.loads(string.decode('ascii'))

            >>> stored_obj = {"nums": [23, 45], "letters": "abcd"}
            >>> json_idx = JSONIndex()
            >>> try:
            ...     json_idx.insert(1, (0, 1, 0, 1), stored_obj)
            ...     list(json_idx.nearest((0, 0), 1,
            ...                           objects="raw")) == [stored_obj]
            ... except ImportError:
            ...     True
            True

        
propertiesIndex_InsertTPDataz;TPR-Tree type not supported with version of libspatialindexr-   TNr   r	   r   	overwriteFzUnable to open file 'z' for index storagepagesizez-libspatialindex >= 2.1 needed for bulk insertz%Bulk insert only supported for RTrees)1getr   rE   type
RT_TPRTreehasattrr
   r'   RuntimeErrorboolr-   
isinstancestrbyteslentupleICustomStorageRT_Diskstoragefilenameidx_extensionospathabspathexistsrG   index_idr   dirnameaccessW_OKOSError	RT_CustomhasDataclearcustomstorageregisterCallbacks	RT_Memoryr   rH   RT_RTree
_exception_create_idx_from_streamr+   _create_idx_from_array	NameErrorNotImplementedErrorIndexHandleinsert)selfargskwargsstreamarraysbasenamerV   fpr>   messagepsitems                r   __init__zIndex.__init__Q   s   h !**\8:>??:-gGG)7
 M 
  

=$ ?@$q'3':d1gu+E7t9q=!$q'51!%a!%aDG^4q't9q=!!WFDGU+aa&-DOO#'/DOO$ H#doo&C&CCA"A ww~~a ,0K1O,P) 00500 "A99Q(1!4GHg&&&/DOO#,0K1O,P)00500 MMO!(D%%doo6&/DOO#ZZ
D)'*2wDOO$doo**h6"DO66v>DKoo% ,,8"DO9d996B oo%  &doo&<&<=DK" 'DDKK&')*QRR c & 53405 & 53405.  )C s0   ;Q
 	Q+ R 
Q('Q(+R	R	R!c                L    t        j                  dt               t        |       S )Nz6index.get_size() is deprecated, use len(index) instead)warningswarnDeprecationWarningrR   rp   s    r   get_sizezIndex.get_size/  s     DFX	
 4yrB   c                X    	 | j                  | j                        S # t        $ r Y yw xY w)zPThe number of entries in the index.

        :return: number of entries
        r   countboundsr   r   s    r   __len__zIndex.__len__5  s,    
	::dkk** 		    	))c                :    d| j                    dt        |        dS )Nzrtree.index.Index(bounds=z, size=))r   rR   r   s    r   __repr__zIndex.__repr__?  s    *4;;-ws4ykKKrB   c                @    | j                   j                         }|d= |S )Nr+   )__dict__copyrp   states     r   __getstate__zIndex.__getstate__B  s     ""$(OrB   c                    | j                   j                  |       t        | j                  j                        | _        y r   )r   updatern   rE   r+   r   s     r   __setstate__zIndex.__setstate__G  s*    U#!$//"8"89rB   c                ,    t        j                  |      S r   )pickledumpsrp   objs     r   r   zIndex.dumpsK  s    ||C  rB   c                ,    t        j                  |      S r   )r   loads)rp   strings     r   r   zIndex.loadsN  s    ||F##rB   c                t    | j                   r"| j                   j                          d| _         yt        d      )zJForce a flush of the index to storage. Renders index
        inaccessible.NzUnclosable index)r+   destroyra   r   s    r   closezIndex.closeQ  s.     ;;KK!DK,--rB   c                R    | j                   r| j                   j                          yy)z&Force a flush of the index to storage.N)r+   flushr   s    r   r   zIndex.flushZ  s    ;;KK rB   c                B   | j                   j                  }t        |      }t        j                  |z  } |       }t        |      |k(  r||d d  |}||fS  |       }| j                  r|d | }||d  }n|d d d   }|dd d   }||d d  ||d d  ||k  st        d      ||fS )Nr   r	   z5Coordinates must not have minimums more than maximums)rE   r0   listr   r   rR   r-   r   )rp   coordinatesr0   arrr1   r2   rw   qs           r   get_coordinate_pointerszIndex.get_coordinate_pointers_  s     OO--	;'oo	)u {y(!DGD, Tz' 5D 
+	
+  !$1%DGDG6 K  TzrB   c                    | d   | d   kD  rt        d      t        j                  | d         }t        j                  | d         }||fS )Nr   r	   z%Start time must be less than end time)r   r   r   )timest_startt_ends      r   _get_time_doubleszIndex._get_time_doubles  sL    8eAhDEE//%(+a)~rB   c                   | j                  |      }t        |      }t        j                  |      }t        j                  |      }|t        j
                  |t        j                  t        j                              |fS r   )r   rR   r   create_string_bufferr   r#   r$   r:   )rp   r   
serializedsizer>   rw   s         r   
_serializezIndex._serialize  s]    ZZ_
:''
3NN1 V[[FNN6>>$BCZOOrB   c                V    t         j                  j                  | j                  |      S r   )r
   r'   Index_SetResultSetOffsetr+   rp   r"   s     r   set_result_limitzIndex.set_result_limit      ww//UCCrB   c                T    t         j                  j                  | j                        S r   )r
   r'   Index_GetResultSetOffsetr+   r   s    r   get_result_limitzIndex.get_result_limit      ww//<<rB   c                V    t         j                  j                  | j                  |      S r   )r
   r'   Index_SetResultSetLimitr+   r   s     r   set_result_offsetzIndex.set_result_offset  s    ww..t{{EBBrB   c                T    t         j                  j                  | j                        S r   )r
   r'   Index_GetResultSetLimitr+   r   s    r   get_result_offsetzIndex.get_result_offset  s    ww..t{{;;rB   Nc           	     x   | j                   j                  t        k(  r | j                  |g|d|iS | j	                  |      \  }}t        j                  d      }d}d}|| j                  |      \  }}}t        j                  j                  | j                  |||| j                   j                  ||       y)a  Inserts an item into the index with the given coordinates.

        :param id: A long integer that is the identifier for this index entry.  IDs
            need not be unique to be inserted into the index, and it is up
            to the user to ensure they are unique if this is a requirement.

        :param coordinates: 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.
            For a TPR-Tree, this must be a 3-element sequence including
            not only the positional coordinate pairs but also the
            velocity pairs `minvk` and `maxvk` and a time value as a float.

        :param obj: a pickleable object.  If not None, this object will be
            stored in the index with the :attr:`id`.

        The following example inserts an entry into the index with id `4321`,
        and the object it stores with that id is the number `42`.  The
        coordinate ordering in this instance is the default (interleaved=True)
        ordering::

            >>> from rtree import index
            >>> idx = index.Index()
            >>> idx.insert(4321,
            ...            (34.3776829412, 26.7375853734, 49.3776829412,
            ...             41.7375853734),
            ...            obj=42)

        This example is inserting the same object for a TPR-Tree, additionally
        including a set of velocities at time `3`::

            >>> p = index.Property(type=index.RT_TPRTree)  # doctest: +SKIP
            >>> idx = index.Index(properties=p)  # doctest: +SKIP
            >>> idx.insert(4321,
            ...            ((34.3776829412, 26.7375853734, 49.3776829412,
            ...             41.7375853734),
            ...             (0.5, 2, 1.5, 2.5),
            ...            3.0),
            ...            obj=42)  # doctest: +SKIP

        r   r   N)rE   rJ   rK   	_insertTPr   r   c_ubyter   r
   r'   Index_InsertDatar+   r0   )	rp   idr   r   r5   r6   datar   pyserializeds	            r   ro   zIndex.insert  s    V ??:-!4>>"<{<<<55kB~~a ?'+s';$D$  KKVVT__-F-Fd	
rB   c                p   | j                  |      \  }}| j                  |      \  }}	| j                  ||dz   f      \  }
}t        j                  d      }d}|| j	                  |      \  }}}t
        j                  j                  | j                  |||||	|
|| j                  j                  ||       y )Nr	   r   )r   r   r   r   r   r
   r'   rF   r+   rE   r0   )rp   r   r   
velocitiestimer   r5   r6   pv_minspv_maxsr   r   r   r   _s                  r   r   zIndex._insertTP  s     55kB77
C//tax0@A~~a ? OOC0MD$""KKOO%%	
rB   c           	     l   | j                   j                  t        k(  r | j                  | S | j	                  |      \  }}t        j                  d      }t        j                  j                  | j                  ||| j                   j                  t        j                  |             |j                  S )a  Return number of objects that intersect the given coordinates.

        :param coordinates: 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.
            For a TPR-Tree, this must be a 3-element sequence including
            not only the positional coordinate pairs but also the
            velocity pairs `minvk` and `maxvk` and a time pair for the
            time range as a float.

        The following example queries the index for any objects any objects
        that were stored in the index intersect the bounds given in the
        coordinates::

            >>> from rtree import index
            >>> idx = index.Index()
            >>> idx.insert(4321,
            ...            (34.3776829412, 26.7375853734, 49.3776829412,
            ...             41.7375853734),
            ...            obj=42)

            >>> print(idx.count((0, 0, 60, 60)))
            1

        This example is similar for a TPR-Tree::

            >>> p = index.Property(type=index.RT_TPRTree)  # doctest: +SKIP
            >>> idx = index.Index(properties=p)  # doctest: +SKIP
            >>> idx.insert(4321,
            ...            ((34.3776829412, 26.7375853734, 49.3776829412,
            ...             41.7375853734),
            ...             (0.5, 2, 1.5, 2.5),
            ...             3.0),
            ...            obj=42)  # doctest: +SKIP

            >>> print(idx.count(((0, 0, 60, 60), (0, 0, 0, 0), (3, 5))))
            ... # doctest: +SKIP
            1

        r   )rE   rJ   rK   _countTPr   r   r9   r
   r'   Index_Intersects_countr+   r0   r!   r"   )rp   r   r5   r6   p_num_resultss        r   r   zIndex.count  s    T ??:- 4==+..55kB*&&KKOO%%LL'	
 """rB   c                l   | j                  |      \  }}| j                  |      \  }}| j                  |      \  }}	t        j                  d      }
t        j
                  j                  | j                  ||||||	| j                  j                  t        j                  |
      	       |
j                  S r   )r   r   r   r9   r
   r'   Index_TPIntersects_countr+   rE   r0   r!   r"   )rp   r   r   r   r5   r6   r   r   r   r   r   s              r   r   zIndex._countTP9  s     55kB77
C//6*((KKOO%%LL'
	
 """rB   c                     y r    rp   r   objectss      r   containszIndex.containsP  s    TWrB   Fc                     y r   r   r   s      r   r   zIndex.containsS  s      #rB   c                     y r   r   r   s      r   r   zIndex.containsX       rB   c           
     &   |r| j                  ||      S | j                  |      \  }}t        j                  d      }t        j                  t        j
                               }	 t        j                  j                   t        j                  j                  | j                  ||| j                  j                  t        j                  |      t        j                  |             | j                  ||j                        S # t        $ r Y yw xY w)a  Return ids or objects in the index that contains within the given
        coordinates.

        :param coordinates: 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.

        :param objects: If True, the intersection 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', the objects
            will be returned without the :class:`rtree.index.Item` wrapper.

        The following example queries the index for any objects any objects
        that were stored in the index intersect the bounds given in the
        coordinates::

            >>> from rtree import index
            >>> idx = index.Index()
            >>> idx.insert(4321,
            ...            (34.3776829412, 26.7375853734, 49.3776829412,
            ...             41.7375853734),
            ...            obj=42)

            >>> hits = list(idx.contains((0, 0, 60, 60), objects=True))
            ... # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS +SKIP
            >>> [(item.object, item.bbox) for item in hits if item.id == 4321]
            ... # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS +SKIP
            [(42, [34.37768294..., 26.73758537..., 49.37768294...,
                   41.73758537...])]

        If the :class:`rtree.index.Item` wrapper is not used, it is faster to
        request the 'raw' objects::

            >>> list(idx.contains((0, 0, 60, 60), objects="raw"))
            ... # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS +SKIP
            [42]

        r   N)_contains_objr   r   r9   r   c_int64r
   r'   Index_Contains_idAttributeErrorr+   rE   r0   r!   _get_idsr"   rp   r   r   r5   r6   r   its          r   r   zIndex.contains]  s    V %%k7;;55kB*^^FNN,-	GG%% 	!!KKOO%%LLLL'	
 }}R!4!455  		s   &D 	DDc           
     
   | j                   |j                   k(  sJ | j                  j                  |j                  j                  k(  sJ d}t        | j                   | j                        }| j	                  | j
                  d      D ]  }| j                   r|j	                  |j                  d      D ]  }g }t        t        |j                              D ]  }|t        |j                        dz  k  r6|j                  t        |j                  |   |j                  |                T|j                  t        |j                  |   |j                  |                 |j                  |||j                  |j                  f       |dz  } |j	                  |j
                  d      D ]  }g }t        t        |j
                              D ]u  }|dz  dk(  r6|j                  t        |j
                  |   |j
                  |                A|j                  t        |j
                  |   |j
                  |                w |j                  |||j                  |j                  f       |dz  }  |S )zTake the intersection of two Index objects.

        :param other: another index
        :return: a new index
        :raises AssertionError: if self and other have different interleave or dimension
        r   r-   rE   Tr   r   r	   )r-   rE   r0   r   intersectionr   bboxr%   rR   appendmaxminro   object)	rp   otherr3   new_idxitem1item2r   jr   s	            r   __and__zIndex.__and__  s    5#4#4444((E,<,<,F,FFFFD$4$4Q &&t{{D&A 	E"//

D/I 
ED"3uzz?3 Ks5::!33 KKEJJqM5::a=(IJ KKEJJqM5::a=(IJ	K NN1dU\\5<<,HIFA
 #//d/K 
EF"3u||#45 Qq5A:"MM#ell1ou||A*OP"MM#ell1ou||A*OP	Q NN1fu||U\\.JKFA
#	: rB   c                   | j                   |j                   k(  sJ | j                  j                  |j                  j                  k(  sJ t        | j                   | j                        }| |fD ]  }|j	                  |j
                  d      D ]q  }| j                   r2|j                  |j                  |j                  |j                         A|j                  |j                  |j
                  |j                         s  |S )zTake the union of two Index objects.

        :param other: another index
        :return: a new index
        :raises AssertionError: if self and other have different interleave or dimension
        r   Tr   )
r-   rE   r0   r   r   r   ro   r   r   r   )rp   r   r   old_idxrz   s        r   __or__zIndex.__or__  s     5#4#4444((E,<,<,F,FFFFD$4$4Q e} 	FG,,W^^T,J F##NN477DIIt{{CNN477DKKE	F	F rB   c                     y r   r   r   s      r   r   zIndex.intersection       rB   c                     y r   r   r   s      r   r   zIndex.intersection       rB   c                     y r   r   r   s      r   r   zIndex.intersection  r   rB   c           
     0   | j                   j                  t        k(  r | j                  |d|iS |r| j	                  ||      S | j                  |      \  }}t        j                  d      }t        j                  t        j                               }t        j                  j                  | j                  ||| j                   j                  t        j                  |      t        j                  |             | j!                  ||j"                        S )a
  Return ids or objects in the index that intersect the given
        coordinates.

        :param coordinates: 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.
            For a TPR-Tree, this must be a 3-element sequence including
            not only the positional coordinate pairs but also the
            velocity pairs `minvk` and `maxvk` and a time pair for the
            time range as a float.

        :param objects: If True, the intersection 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', the objects
            will be returned without the :class:`rtree.index.Item` wrapper.

        The following example queries the index for any objects any objects
        that were stored in the index intersect the bounds given in the
        coordinates::

            >>> from rtree import index
            >>> idx = index.Index()
            >>> idx.insert(4321,
            ...            (34.3776829412, 26.7375853734, 49.3776829412,
            ...             41.7375853734),
            ...            obj=42)

            >>> hits = list(idx.intersection((0, 0, 60, 60), objects=True))
            >>> [(item.object, item.bbox) for item in hits if item.id == 4321]
            ... # doctest: +NORMALIZE_WHITESPACE +ELLIPSIS
            [(42, [34.37768294..., 26.73758537..., 49.37768294...,
                   41.73758537...])]

        If the :class:`rtree.index.Item` wrapper is not used, it is faster to
        request the 'raw' objects::

            >>> list(idx.intersection((0, 0, 60, 60), objects="raw"))
            [42]

        Similar for the TPR-Tree::

            >>> p = index.Property(type=index.RT_TPRTree)  # doctest: +SKIP
            >>> idx = index.Index(properties=p)  # doctest: +SKIP
            >>> idx.insert(4321,
            ...            ((34.3776829412, 26.7375853734, 49.3776829412,
            ...             41.7375853734),
            ...             (0.5, 2, 1.5, 2.5),
            ...             3.0),
            ...            obj=42)  # doctest: +SKIP

            >>> hits = list(idx.intersection(
            ...     ((0, 0, 60, 60), (0, 0, 0, 0), (3, 5)), objects=True))
            ...  # doctest: +SKIP
            >>> [(item.object, item.bbox) for item in hits if item.id == 4321]
            ... # doctest: +SKIP
            [(42, [34.37768294..., 26.73758537..., 49.37768294...,
                   41.73758537...])]

        r   r   )rE   rJ   rK   _intersectionTP_intersection_objr   r   r9   r   r   r
   r'   Index_Intersects_idr+   r0   r!   r   r"   r   s          r   r   zIndex.intersection  s    ~ ??:-'4''&-  ))+w??55kB*^^FNN,-##KKOO%%LLLL'	
 }}R!4!455rB   c                   | j                  |      \  }}| j                  |      \  }}| j                  |      \  }	}
t        j                  d      }|rBt        j
                  j                  }t        j                  t        j                               }nAt        j
                  j                  }t        j                  t        j                               } || j                  |||||	|
| j                  j                  t        j                  |      t        j                  |      
       |r| j                  ||j                   |      S | j#                  ||j                         S r   )r   r   r   r9   r
   r'   Index_TPIntersects_objr   r)   Index_TPIntersects_idr   r+   rE   r0   r!   _get_objectsr"   r   )rp   r   r   r   r   r5   r6   r   r   r   r   r   callr   s                 r   r   zIndex._intersectionTPJ  s   55kB77
C//6*7711D 12B7700D 01BKKOO%%LLLL'	
 $$R)<)<gFF==]%8%899rB   c           
        | j                  |      \  }}t        j                  d      }t        j                  t        j                               }t
        j                  j                  | j                  ||| j                  j                  t        j                  |      t        j                  |             | j                  ||j                  |      S r   )r   r   r9   r   r)   r
   r'   Index_Intersects_objr+   rE   r0   r!   r  r"   r   s          r   r   zIndex._intersection_objj  s    55kB*^^FOO-.$$KKOO%%LLLL'	
   ]%8%8'BBrB   c           
         | j                  |      \  }}t        j                  d      }t        j                  t        j                               }	 t
        j                  j                   t
        j                  j                  | j                  ||| j                  j                  t        j                  |      t        j                  |             | j                  ||j                  |      S # t        $ r Y y w xY wr   )r   r   r9   r   r)   r
   r'   Index_Contains_objr   r+   rE   r0   r!   r  r"   r   s          r   r   zIndex._contains_obj{  s    55kB*^^FOO-.	GG&& 	""KKOO%%LLLL'	
   ]%8%8'BB  		s   C1 1	C=<C=c           	   #    K   t        j                  |t        j                  t        j                  t         j                  |z                    }t        j                  |t        j                  t        j                  t         j                                    }	 |dk7  r,t	        |      D ]  }t        | j                  ||           n8t	        |      D ]*  }t        ||         }|| | j                  |       , t        j                  j                  ||       y # t        j                  j                  ||       w xY ww)Nraw)r   r#   r$   r)   r%   Itemr   rA   r
   r'   Index_DestroyObjResults)rp   r   num_resultsr   itemsitsr3   r   s           r   r  zIndex._get_objects  s     v~~foo.KLM
 kk%v0O!PQ	>%{+ 5Atzz58445 {+ /A$U1X.D|"
"jj../ GG++C=DGG++C=s   BE
A)D% !E
%"EE
c              #    K   t        j                  |t        j                  t         j                  |z              }t        j                  |t        j                  t         j                              }	 t        |      D ]  }|j                  |     	 t        j                  j                  |       y # t        j                  j                  |       w xY wwr   )
r   r#   r$   r   r)   r%   r&   r
   r'   r(   )rp   r   r  r  r  r3   s         r   r   zIndex._get_ids  s     Bv~~/K LMkk%!@A	$;' (nnQ''( GGs#DGGs#s   A2C5!B7  C7!CCc           	        | j                  |      \  }}t        j                  t        j                  |            }t        j                  t        j                               }t
        j                  j                  | j                  ||| j                  j                  t        j                  |      |       | j                  ||j                  j                  |      S r   )r   r   r   r9   r)   r
   r'   Index_NearestNeighbors_objr+   rE   r0   r!   r  r&   r"   rp   r   r  r   r5   r6   r   r   s           r   _nearest_objzIndex._nearest_obj  s    55kBv{'CD^^FOO-.**KKOO%%LL	
   ]%;%;%A%A7KKrB   c                     y r   r   rp   r   r  r   s       r   nearestzIndex.nearest  r   rB   c                     y r   r   r  s       r   r  zIndex.nearest  r   rB   c                     y r   r   r  s       r   r  zIndex.nearest  r   rB   r	   c           	     F   | j                   j                  t        k(  r | j                  |d|iS |r| j	                  |||      S | j                  |      \  }}t        j                  t        j                  |            }t        j                  t        j                               }t        j                  j                  | j                  ||| j                   j                  t        j                  |      |       | j!                  ||j"                  j$                        S )a8  Returns the ``k``-nearest objects to the given coordinates.

        :param coordinates: 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.
            For a TPR-Tree, this must be a 3-element sequence including
            not only the positional coordinate pairs but also the
            velocity pairs `minvk` and `maxvk` and a time pair for the
            time range as a float.

        :param num_results: The number of results to return nearest to the given
            coordinates. If two index entries are equidistant, *both* are returned.
            This property means that :attr:`num_results` may return more
            items than specified

        :param objects: 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 :class:`rtree.index.Item` wrapper.

        .. warning::
            This is currently not implemented for the TPR-Tree.

        Example of finding the three items nearest to this one::

            >>> from rtree import index
            >>> idx = index.Index()
            >>> idx.insert(4321, (34.37, 26.73, 49.37, 41.73), obj=42)
            >>> hits = idx.nearest((0, 0, 10, 10), 3, objects=True)
        r   )rE   rJ   rK   
_nearestTPr  r   r   r   r9   r   r
   r'   Index_NearestNeighbors_idr+   r0   r!   r   r&   r"   r  s           r   r  zIndex.nearest  s    L ??:-"4??KAAA$$[+wGG55kB v{'CD^^FNN,-))KKOO%%LL	
 }}R!7!7!=!=>>rB   c                   ddl }| j                  ||      \  }}|j                  \  }}|j                  d   |j                  z  }|j                  d   |j                  z  }|j                  d|z  |j                        }|j                  ||j                        }	t        j                  d      }
d\  }}	 t        j                  j                  | j                  ||z
  |t        |      |z
  ||||d j                  j                  ||d j                  j                  ||d j                  j                  |	|d j                  j                  t        j                   |
             |
j"                  ||z
  k(  r|d|	j%                          |	fS ||	|||
j"                  z    j%                         z  }||
j"                  z  }|j'                  dt        |      z  |	|   z   d       1)	a  Bulk intersection query for obtaining the ids of entries
        which intersect with the provided bounding boxes.  The return
        value is a tuple consisting of two 1D NumPy arrays: one of
        intersecting ids and another containing the counts for each
        bounding box.

        :param mins: A NumPy array of shape `(n, d)` containing the
            minima to query.

        :param maxs: A NumPy array of shape `(n, d)` containing the
            maxima to query.
        r   Nr	   r   dtyper   r   Frefcheck)numpy_prepare_v_arraysshapestridesitemsizeemptyint64uint64r   r   r
   r'   Index_Intersects_id_vr+   rR   r   r!   r"   sumresize)rp   r1   r2   npnr>   d_i_strid_j_striidscountsnroffnoffis                r   intersection_vzIndex.intersection_v  s    	++D$7
d zz1 <<?dmm3<<?dmm3hhq1uBHHh-!299-^^A
dGG))DC4TU""''TU""''DE
!!&&tu$$))R  xx1t8#>VZZ\*F22 tdRXXo6::<< 

1s3x<&,6
G/ rB   )r  	max_distsstrictreturn_max_distsc               <   ddl }| j                  ||      \  }}|j                  \  }}	|j                  d   |j                  z  }
|j                  d   |j                  z  }|j                  ||z  |j                        }|j                  ||j                        }t        j                  d      }d\  }}|b|j                  |j                  |      |j                        }|j                  dk7  rt        d      t        |      |k7  r$t        d|       |r|j!                  |      }nd}	 t"        j$                  j'                  | j(                  |s|n| ||z
  |	t        |      |z
  |
|||d j                  j*                  ||d j                  j*                  ||d j                  j*                  ||d j                  j*                  |||d j                  j*                  ndt        j,                  |             |j.                  ||z
  k(  r-|r|d|j1                          ||fS |d|j1                          |fS |||||j.                  z    j1                         z  }||j.                  z  }|j3                  dt        |      z  ||   z   d	
       k)a  Bulk ``k``-nearest query for the given bounding boxes.  The
        return value is a tuple consisting of, by default, two 1D NumPy
        arrays: one of intersecting ids and another containing the
        counts for each bounding box.

        :param mins: A NumPy array of shape `(n, d)` containing the
            minima to query.

        :param maxs: A NumPy array of shape `(n, d)` containing the
            maxima to query.

        :param num_results: The maximum number of neighbors to return
            for each bounding box.  If there are multiple equidistant
            furthest neighbors then, by default, they are *all*
            returned.  Hence, the actual number of results can be
            greater than requested.

        :param max_dists: Optional; a NumPy array of shape `(n,)`
            containing the maximum distance to consider for each
            bounding box.

        :param strict: If True then each point will never return more
            than `num_results` even in cases of equidistant furthest
            neighbors.

        :param return_max_dists: If True, the distance of the furthest
            neighbor for each bounding box will also be returned.
        r   Nr	   r  r  zmax_dists must have 1 dimensionzmax_dists must have length r   Fr  )r!  r"  r#  r$  r%  r&  r'  r(  r   r   ascontiguousarray
atleast_1dfloat64ndim
ValueErrorrR   zerosr
   r'   Index_NearestNeighbors_id_vr+   r   r!   r"   r*  r+  )rp   r1   r2   r  r6  r7  r8  r,  r-  r>   r.  r/  r0  r1  r2  r3  r4  distss                     r   	nearest_vzIndex.nearest_vO  so   L 	++D$7
d zz1 <<?dmm3<<?dmm3hhq;bhhh7!299-^^A
d ((y)A(TEzzQ !BCC5zQ #>qc!BCCHHQKEEGG//#)|DC4TU""''TU""''DE
!!&&tu$$)),1,=de##((4R " xx1t8##~.==~.66 tdRXXo6::<< 

1s3x<&,6
G9 rB   c                   dd l }|u r-|j                        j                  |j                        x}nT|j                        j                  |j                        |j                  |      j                  |j                        }j                  dk7  s|j                  dk7  rt        d      j                  |j                  k7  rt        d      j                  |j                  k7  rt        d      t        fdj                  D              r:|u rj                         x}|fS j                         |j                         }|fS )Nr   r   z(mins/maxs must have 2 dimensions: (n, d)zmins and maxs shapes not equalzmins and maxs strides not equalc              3  <   K   | ]  }|j                   z    y wr   )r%  )r   r@   r1   s     r   r   z*Index._prepare_v_arrays.<locals>.<genexpr>  s     7Qq4== 7s   )
r!  
atleast_2dastyper<  r=  r>  r#  r$  anyr   )rp   r1   r2   r,  s    `  r   r"  zIndex._prepare_v_arrays  s    4<---44RZZ@@D4==&--bjj9D==&--bjj9D99>TYY!^GHH::#=>><<4<<'>?? 7$,,77t|"iik)t
 Tz yy{yy{TzrB   c                   | j                  |      \  }}| j                  |      \  }}	| j                  |      \  }
}t        j                  t        j                  |            }|rBt        j                  t        j
                               }t        j                  j                  }nAt        j                  t        j                               }t        j                  j                  } || j                  ||||	|
|| j                  j                  t        j                  |      |
       |r'| j                  ||j                   j"                  |      S | j%                  ||j                   j"                        S r   )r   r   r   r   r9   r)   r
   r'   Index_TPNearestNeighbors_objr   Index_TPNearestNeighbors_idr+   rE   r0   r!   r  r&   r"   r   )rp   r   r   r   r  r   r5   r6   r   r   r   r   r   r   r  s                  r   r  zIndex._nearestTP  s!   55kB77
C//6v{'CD 12B7777D 01B7766DKKOO%%LL	
 $$R)?)?)E)EwOO==]%;%;%A%ABBrB   c                |    || j                   }t        | j                  t        j                  j
                  |      S )a  Returns the bounds of the index

        :param coordinate_interleaved: If True, the coordinates are turned
            in the form [xmin, ymin, ..., kmin, xmax, ymax, ..., kmax],
            otherwise they are returned as
            [xmin, xmax, ymin, ymax, ..., ..., kmin, kmax].  If not specified,
            the :attr:`interleaved` member of the index is used, which
            defaults to True.
        )r-   r7   r+   r
   r'   Index_GetBounds)rp   coordinate_interleaveds     r   
get_boundszIndex.get_bounds  s4     ")%)%5%5"4;;(?(?AWXXrB   c                   | j                   j                  t        k(  r | j                  |g| S | j	                  |      \  }}t
        j                  j                  | j                  |||| j                   j                         y)aQ  Deletes an item from the index with the given ``'id'`` and
           coordinates given by the ``coordinates`` sequence. As the index can
           contain multiple items with the same ID and coordinates, deletion
           is not guaranteed to delete all items in the index with the given ID
           and coordinates.

        :param id: A long integer ID for the entry, which need not be unique. The
            index can contain multiple entries with identical IDs and
            coordinates. Uniqueness of items should be enforced at the
            application level by the user.

        :param coordinates: Dimension * 2 coordinate pairs, representing the min
            and max coordinates in each dimension of the item to be
            deleted from the index. Their ordering will depend on the
            index's :attr:`interleaved` data member.
            These are not the coordinates of a space containing the
            item, but those of the item itself. Together with the
            id parameter, they determine which item will be deleted.
            This may be an object that satisfies the numpy array protocol.
            For a TPR-Tree, this must be a 3-element sequence including
            not only the positional coordinate pairs but also the
            velocity pairs `minvk` and `maxvk` and a time pair for the
            original time the object was inserted and the current time
            as a float.

        Example::

            >>> from rtree import index
            >>> idx = index.Index()
            >>> idx.delete(4321,
            ...            (34.3776829412, 26.7375853734, 49.3776829412,
            ...             41.7375853734))

        For the TPR-Tree::

            >>> p = index.Property(type=index.RT_TPRTree)  # doctest: +SKIP
            >>> idx = index.Index(properties=p)  # doctest: +SKIP
            >>> idx.delete(4321,
            ...            ((34.3776829412, 26.7375853734, 49.3776829412,
            ...             41.7375853734),
            ...             (0.5, 2, 1.5, 2.5),
            ...             (3.0, 5.0)))  # doctest: +SKIP

        N)
rE   rJ   rK   	_deleteTPr   r
   r'   Index_DeleteDatar+   r0   )rp   r   r   r5   r6   s        r   deletezIndex.delete  sl    Z ??:-!4>>"3{3355kB  KKVVT__-F-F	
rB   c                   | j                  |      \  }}| j                  |      \  }}| j                  |      \  }	}
t        j                  j	                  | j
                  ||||||	|
| j                  j                  	       y r   )r   r   r
   r'   Index_DeleteTPDatar+   rE   r0   )rp   r   r   r   r   r5   r6   r   r   r   r   s              r   rP  zIndex._deleteTP-  s|     55kB77
C//6""KKOO%%
	
rB   c                f    t        t        j                  j                  | j                              S r   )rN   r
   r'   Index_IsValidr+   r   s    r   validzIndex.validC  s     DGG))$++677rB   c                T    t         j                  j                  | j                        S r   )r
   r'   Index_ClearBufferr+   r   s    r   clearBufferzIndex.clearBufferF  s    ww((55rB   c                    t        |      dz  dk(  sJ d       t        |      dz  }g }t        |      D ]  }|j                  ||   |||z      g         |S )z
        [xmin, ymin, xmax, ymax] => [xmin, xmax, ymin, ymax]

        >>> Index.deinterleave([0, 10, 1, 11])
        [0, 1, 10, 11]

        >>> Index.deinterleave([0, 1, 2, 10, 11, 12])
        [0, 10, 1, 11, 2, 12]

        r   r   must be a pairwise listrR   r%   extend)rp   r-   r0   dir3   s        r   r*   zIndex.deinterleaveI  so     ;!#q(C*CC($)	y! 	DAII{1~{1y='ABC	D	rB   c           	         t        |      dz  dk(  sJ d       g }t        d      D ];  }|j                  t        dt        |      d      D cg c]
  }|||z       c}       = |S c c}w )aZ  
        [xmin, xmax, ymin, ymax, zmin, zmax]
            => [xmin, ymin, zmin, xmax, ymax, zmax]

        >>> Index.interleave([0, 1, 10, 11])
        [0, 10, 1, 11]

        >>> Index.interleave([0, 10, 1, 11, 2, 12])
        [0, 1, 2, 10, 11, 12]

        >>> Index.interleave((-1, 1, 58, 62, 22, 24))
        [-1, 58, 22, 1, 62, 24]

        r   r   r\  r]  )rp   deinterleavedr-   r3   r   s        r   
interleavezIndex.interleave\  s{      =!A%*E,EE*q 	A/4QM8JA/NO!q1u%O	  Ps   A&
c                    t        |       j                  j                  t        j                  z  } |        |       t        j
                  t        j                  t        j                  d            t        j                  t        j                               fd}t        j                  |      }t         j                  j                  |      S )zXThis function is used to instantiate the index given an
        iterable stream of data.r   c                   	 t              \  | d<   }}j                  r|d dd |d dd n|ddd   dd |ddd   dd |d<   |d<   |d<   ||d<   d|d<   yj                  |      \  |d<   }	}
t        j                  |	t        j                  t        j                              |d<   y# t        $ r Y yt        $ r}|_        Y d}~yd}~ww xY w)a!  This function must fill pointers to individual entries that will
            be added to the index.  The C API will actually call this function
            to fill out the pointers.  If this function returns anything other
            than 0, it is assumed that the stream of data is done.r   Nr   r	   )
nextStopIteration	Exceptionri   r-   r   r   r#   r$   r   )p_idr5   r6   p_dimensionp_datap_lengthr   r   excr   r   r0   r2   r1   no_datarp   stream_iters              r   py_next_itemz3Index._create_idx_from_stream.<locals>.py_next_item  s   ,0,=)Qc %jy1Q%ij1Q%cc*Q%add+QF1IF1I 'KN{#q	
  (,s';$T1"KKfnnV^^.LMq	5 !  "%s   B7 7	CC
CC)iterrE   r0   r   r   r#   r   r   r$   r
   NEXTFUNCIndexStreamHandler+   )	rp   rs   darrayrp  r0   r2   r1   rn  ro  s	   `   @@@@@r   rj   zIndex._create_idx_from_streamu  s     6lOO--	9,xx++NN6>>!,-v~~fnn/M
"	 "	H |, !7!7@@rB   c                   dd l }|j                  |j                        }| j                  ||      \  }}t	        |      t	        |      k7  rt        d      |j                  d   |j                  z  r|j                         }|j                  \  }}|j                  d   |j                  z  }|j                  d   |j                  z  }|j                  d   |j                  z  }	t        | j                  j                  |||||	|j                  j                  |j                  j                  |j                  j                  	      S )Nr   z index and point counts differentr	   )r!  rF  r'  r"  rR   r>  r$  r%  r   r#  IndexArrayHandlerE   r+   r   r   )
rp   ibufminbufmaxbufr,  r-  r>   i_strir.  r/  s
             r   rk   zIndex._create_idx_from_array  s    {{288$//?t9F#?@@ <<?T]]*99;D ||1 aDMM1>>!$7>>!$7OO""KKMMMM

 
	
rB   c                   t        j                         }t        j                  t        j                               }t        j                  t        j                               }t        j                  t        j                  t        j                                     }t        j                  t        j                  t        j                                     }t        j                  t        j                  t        j                                     }t        j                  d      }t
        j                  j                  | j                  t        j                  |      t        j                  |      t        j                  |      t        j                  |      t        j                  |      t        j                  |      t        j                  |             g }|j                  }	t        j                  |t        j                  t         j                  |	z              }
t        j                  |t        j                  t         j                  |	z              }t        j                  |t        j                  t        j                  t         j                        |	z              }t        j                  |t        j                  t        j                  t         j                        |	z              }t        j                  |t        j                  t        j                  t         j                        |	z              }t        |	      D ]"  }|j                  |   }|j                  |   }|
j                  |   }t        j                  |t        j                  t         j                  |z              }g }t        |      D ]   }|j                  |j                  |          " t
        j                  j!                  t        j                  |t        j                  t         j"                                     |j                  |   }|j                  |   }t        j                  |t        j                  t         j                  |j                  z              }t        j                  |t        j                  t         j                  |j                  z              }g }t        |j                        D cg c]  }|j                  |    }}|t        |j                        D cg c]  }|j                  |    c}z  }t        j                  |t        j                  t         j                              }t        j                  |t        j                  t         j                              }t
        j                  j!                  t        j                  |t        j                  t         j"                                     t
        j                  j!                  t        j                  |t        j                  t         j"                                     |j                  |||f       % |S c c}w c c}w r   )r   r    r   r   r   r
   r'   Index_GetLeavesr+   r!   r"   r#   r$   r%   r&   r   r(   r)   )rp   leaf_node_countp_leafsizes	p_leafidspp_childidsr.   r/   r0   outputr   sizesr0  childr1   r2   r3   p_child_idsr   r   child_ids_array	child_idsr   r5   r6   r   s                            r   leaveszIndex.leaves  s0    //+nnV__%67NN6>>#34	nnV^^FNN4D%EF..0A!BC..0A!BCOOA&	KKLL)LL%LL#LL%LL!LL!LL#		
 %%K%8O)PQkk)V^^FNNU4J%KLv~~(F(NO
 {{V^^FNN6??$Ce$KL
 {{V^^FNN6??$Ce$KL
 u &	3A..+KaB>>!$D$kkV^^FNNT,ABO I4[ >  !9!9!!<=> GGK)HI ]]1%F]]1%F[[v'HIF [[v'HIF F27	2HIQfooa(IFI53IJavq)JJF [[)HIF[[)HIFGGv{{66>>&//3RSTGGv{{66>>&//3RSTMM2y&12M&	3P  JJs   =Y7-Y<rq   r   rr   r   returnNoner  r   r  rP   r  dict[str, Any]r   r  r  r  )r   r   r  rQ   )r   rQ   r  r   r  r  )r   Sequence[float]r  ztuple[float, float]r   )r   r   r   r   r   r   r  r  )r   r   r   r  r   r  r   floatr   r   r  r  )r   r   r  r   )r   r  r   r  r   r  r  r   )r   r   r   Literal[True]r  Iterator[Item]F)r   r   r   Literal[False]r  zIterator[int] | None)r   r   r   Literal['raw']r  Iterator[object])r   r   r   bool | Literal['raw']r  z$Iterator[Item | int | object] | None)r   r   r  r   )r   r   r   r  r  Iterator[int])r   r   r   r  r  Iterator[Item | int | object])r   r   )r   r   r  r   r   r  r  r  )r   r   r  r   r   r  r  r  )r   r   r  r   r   r  r  r  r	   F)r   r   r  r   r   r  r  r  )r   r   r   r   r  r  )
r   r   r   r  r   r  r   r  r  r  )r  rN   )r-   zSequence[object]r  zlist[object])ra  r  r  list[float]):__name__
__module____qualname____doc__r{   r   r   r   r   r   r   r   r   r   r   staticmethodr   r   r   r   propertyresult_limitr   r   result_offsetro   addr   r   r   r   r   r   r   r   r   r   r   r  r   r  r  r5  rB  r"  r  rN  r   rR  rP  rW  rZ  classmethodr*   rb  rj   rk   r  r   rB   r   r   r   N   s   :\S|L
:!$. 
"*"	"H  	PD= ,.>?LC< .0ABM7
r C 

 %
 $	

 
 
 

<8#t#*#8G#PU#	#. W W:?##)7#	# # )7	 
 BGA6A6)>A6	-A6F+Z. )6	  :?)7	  )7	 
 BGU6U6)>U6	&U6n:@C"C,>,
$L$ -0;H	  LQ-0;I	  -0;I	  ).	C?C? C? '	C?
 
'C?J4Hv ]H~6C@Y j!F2
h

 %
 $	

 
 

,86  $  02Ah 
DKrB   r   c                  F    e Zd ZdZdZd	d
dZddZddZedd       Z	d Z
y)r	  zA container for index entries)r+   ownedr   r   r   c                "   |r|| _         || _        t        j                  j	                  | j                         | _        d| _        | j                  |      | _        t        | j                   t        j                  j                  d      | _
        y)zThere should be no reason to instantiate these yourself. Items are
        created automatically when you call
        :meth:`rtree.index.Index.intersection` (or other index querying
        methods) with objects=True given the parameters of the function.NF)r+   r  r
   r'   IndexItem_GetIDr   r   
get_objectr7   IndexItem_GetBoundsr   )rp   r   r+   r  s       r   r{   zItem.__init__"  sd      DK
''))$++6ooe,!$++tww/J/JERrB   c                4    | j                   |j                   k  S r   r   rp   r   s     r   __lt__zItem.__lt__3      ww!!rB   c                4    | j                   |j                   kD  S r   r  r  s     r   __gt__zItem.__gt__6  r  rB   c                @    t         j                  | j                        S )z+Returns the bounding box of the index entry)r   rb  r   r   s    r   r   z	Item.bbox9  s     ,,rB   c                r    | j                   | j                   S t        | j                        }|y  ||      S r   )r   rA   r+   )rp   r   r   s      r   r  zItem.get_object>  s6    ;;";;%<T{rB   Nr  r  )r   r	  r  rN   )r  r  )r  r  r  r  	__slots__r{   r  r  r  r   r  r   rB   r   r	  r	    s3    '=IS""" - -rB   r	  c                      e Zd ZdZy)InvalidHandleExceptionz3Handle has been destroyed and can no longer be usedN)r  r  r  r  r   rB   r   r  r  H  s    =rB   r  c                  B    e Zd ZddZd	dZd Zd
dZed        Zd
dZ	y)Handlec                2     | j                   |i || _        y r   )_create_ptrrp   rq   rr   s      r   r{   zHandle.__init__M  s     DLL$1&1	rB   c                    t         r   rm   r  s      r   r  zHandle._createP      !!rB   c                    t         r   r  )rp   ptrs     r   _destroyzHandle._destroyS  r  rB   c                    	 | j                   #| j                  | j                          d | _         y y # t        $ r Y y w xY wr   )r  r  r   r   s    r   r   zHandle.destroyV  sA    	yy$dii( 	 %  		s   .2 	>>c                >    | j                   t        | j                   S r   )r  r  r   s    r   _as_parameter_zHandle._as_parameter_^  s    99((yyrB   c                D    	 | j                          y # t        $ r Y y w xY wr   )r   rl   r   s    r   __del__zHandle.__del__d  s$    	LLN 	
 	s    	Nr  )rq   r   rr   r   r  )
r  r  r  r{   r  r  r   r  r  r  r   rB   r   r  r  L  s/    2""  
rB   r  c                  l    e Zd Zej                  j
                  Zej                  j                  ZddZ	y)rn   c                    	 t         j                  j                   | j                  *t         j                  j                  | j                         y y # t        $ r Y y w xY wr   )r
   r'   Index_Flushr  r   r   s    r   r   zIndexHandle.flushs  sK    	GGyy$##DII. % 		s   AA 	AANr  )
r  r  r  r
   r'   Index_Creater  Index_Destroyr  r   r   rB   r   rn   rn   o  s%    gg""Gww$$HrB   rn   c                  8    e Zd Zej                  j
                  Zy)rs  N)r  r  r  r
   r'   Index_CreateWithStreamr  r   rB   r   rs  rs  |  s    gg,,GrB   rs  c                  8    e Zd Zej                  j
                  Zy)rv  N)r  r  r  r
   r'   Index_CreateWithArrayr  r   rB   r   rv  rv    s    ''//rB   rv  c                  d    e Zd Zej                  j
                  Zej                  j                  Zy)PropertyHandleN)	r  r  r  r
   r'   IndexProperty_Creater  IndexProperty_Destroyr  r   rB   r   r  r    s     gg**Gww,,HrB   r  c                     e Zd ZdZdZd=d>dZd?dZd@dZd ZdAdZ	dBd	Z
dBd
ZdCdZdDdZ eee      Z	 dCdZdDdZ eee      Z	 dCdZdDdZ eee      Z	 dCdZdDdZ eee      Z	 dCdZdDdZ eee      Z	 dCdZdDdZ eee      Z	 dCdZdDdZ  eee       Z!	 dCdZ"dDdZ# ee"e#      Z$	 dCdZ%dDdZ& ee%e&      Z'	 dCdZ(dDdZ) ee(e)      Z*	 dCdZ+dDd Z, ee+e,      Z-	 d! Z.d" Z/ ee.e/      Z0	 d# Z1d$ Z2 ee1e2      Z3	 dCd%Z4dDd&Z5 ee4e5      Z6	 d' Z7d( Z8 ee7e8      Z9	 dCd)Z:dDd*Z; ee:e;      Z<	 dCd+Z=dDd,Z> ee=e>      Z?	 d- Z@d. ZA ee@eA      ZB	 d/ ZCd0 ZD eeCeD      ZE	 d1 ZFd2 ZG eeFeG      ZH	 d3 ZId4 ZJ eeIeJ      ZK	 d5 ZLd6 ZM eeLeM      ZN	 dCd7ZOdDd8ZP eeOeP      ZQ	 d9 ZRd: ZS eeReS      ZT	 d; ZUd< ZV eeUeV      ZWy)Er   zAn index property object is a container that contains a number of
    settable index properties.  Many of these properties must be set at
    index creation times, while others can be used to adjust performance
    or behavior.)buffering_capacitycustom_storage_callbackscustom_storage_callbacks_sizedat_extensionr0   rW   fill_factorrX   index_capacityr]   leaf_capacitynear_minimum_overlap_factorrG   rH   point_pool_capacityregion_pool_capacityreinsert_factorsplit_distribution_factorrV   	tight_mbrtpr_horizonrJ   variantwritethroughNc                L    |
t               }|| _        | j                  |       y r   r  r+   initialize_from_dict)rp   r+   r  rr   s       r   r{   zProperty.__init__  s$    >#%F!!&)rB   c                    |j                         D ]  \  }}|	t        | ||        d|vr@| j                  }t        | j                  | j
                        }||k\  r|dz  dz   | _        y y y )Nr     r	   )r  setattrr  r   r  r  )rp   r   kvnmofilcs         r   r  zProperty.initialize_from_dict  sz    KKM 	$DAq}a#	$
 )533Dd))4+=+=>Cs{36!8a<0  6rB   c                "    | j                         S r   )as_dictr   s    r   r   zProperty.__getstate__  s    ||~rB   c                D    t               | _        | j                  |       y r   r  r   s     r   r   zProperty.__setstate__  s    $&!!%(rB   c                r    i }| j                   D ]  }	 t        | |      }|||<    |S # t        $ r d }Y w xY wr   )pkeysgetattrr   )rp   r>   r  r  s       r   r  zProperty.as_dict  sS     	AD!$ AaD	   s   (66c                4    t        | j                               S r   )reprr  r   s    r   r   zProperty.__repr__  s    DLLN##rB   c                H    t        j                  | j                               S r   )pprintpformatr  r   s    r   __str__zProperty.__str__  s    ~~dlln--rB   c                    	 | j                   S # t        $ r5 t        j                  j	                  | j
                        }|| _         |cY S w xY wr   )_typer   r
   r'   IndexProperty_GetIndexTyper+   )rp   rJ   s     r   get_index_typezProperty.get_index_type  sD    	:: 	7755dkkBD"DJK	    ;AAc                d    || _         t        j                  j                  | j                  |      S r   )r   r
   r'   IndexProperty_SetIndexTyper+   r   s     r   set_index_typezProperty.set_index_type  s$    
ww11$++uEErB   c                T    t         j                  j                  | j                        S r   )r
   r'   IndexProperty_GetIndexVariantr+   r   s    r   get_variantzProperty.get_variant      ww44T[[AArB   c                V    t         j                  j                  | j                  |      S r   )r
   r'   IndexProperty_SetIndexVariantr+   r   s     r   set_variantzProperty.set_variant      ww44T[[%HHrB   c                    	 | j                   S # t        $ r5 t        j                  j	                  | j
                        }|| _         |cY S w xY wr   )
_dimensionr   r
   r'   IndexProperty_GetDimensionr+   )rp   dims     r   get_dimensionzProperty.get_dimension  sD    	??" 	''44T[[AC#&DOJ	r  c                    |dk  rt        d      || _        t        j                  j	                  | j
                  |      S )Nr   z1Negative or 0 dimensional indexes are not allowed)r   r  r
   r'   IndexProperty_SetDimensionr+   r   s     r   set_dimensionzProperty.set_dimension  s7    A:PQQww11$++uEErB   c                T    t         j                  j                  | j                        S r   )r
   r'   IndexProperty_GetIndexStorager+   r   s    r   get_storagezProperty.get_storage  r
  rB   c                V    t         j                  j                  | j                  |      S r   )r
   r'   IndexProperty_SetIndexStorager+   r   s     r   set_storagezProperty.set_storage  r  rB   c                T    t         j                  j                  | j                        S r   )r
   r'   IndexProperty_GetPagesizer+   r   s    r   get_pagesizezProperty.get_pagesize  s    ww00==rB   c                v    |dk  rt        d      t        j                  j                  | j                  |      S )Nr   zPagesize must be > 0)r   r
   r'   IndexProperty_SetPagesizer+   r   s     r   set_pagesizezProperty.set_pagesize  s0    A:344ww00eDDrB   c                T    t         j                  j                  | j                        S r   )r
   r'   IndexProperty_GetIndexCapacityr+   r   s    r   get_index_capacityzProperty.get_index_capacity   s    ww55dkkBBrB   c                v    |dk  rt        d      t        j                  j                  | j                  |      S )Nr   zindex_capacity must be > 0)r   r
   r'   IndexProperty_SetIndexCapacityr+   r   s     r   set_index_capacityzProperty.set_index_capacity#  s0    A:9::ww55dkk5IIrB   c                T    t         j                  j                  | j                        S r   )r
   r'   IndexProperty_GetLeafCapacityr+   r   s    r   get_leaf_capacityzProperty.get_leaf_capacity+  r
  rB   c                v    |dk  rt        d      t        j                  j                  | j                  |      S )Nr   zleaf_capacity must be > 0)r   r
   r'   IndexProperty_SetLeafCapacityr+   r   s     r   set_leaf_capacityzProperty.set_leaf_capacity.  s0    A:899ww44T[[%HHrB   c                T    t         j                  j                  | j                        S r   )r
   r'   "IndexProperty_GetIndexPoolCapacityr+   r   s    r   get_index_pool_capacityz Property.get_index_pool_capacity6      ww99$++FFrB   c                v    |dk  rt        d      t        j                  j                  | j                  |      S )Nr   zindex_pool_capacity must be > 0)r   r
   r'   "IndexProperty_SetIndexPoolCapacityr+   r   s     r   set_index_pool_capacityz Property.set_index_pool_capacity9  0    A:>??ww99$++uMMrB   c                T    t         j                  j                  | j                        S r   )r
   r'   "IndexProperty_GetPointPoolCapacityr+   r   s    r   get_point_pool_capacityz Property.get_point_pool_capacityA  r2  rB   c                v    |dk  rt        d      t        j                  j                  | j                  |      S )Nr   zpoint_pool_capacity must be > 0)r   r
   r'   "IndexProperty_SetPointPoolCapacityr+   r   s     r   set_point_pool_capacityz Property.set_point_pool_capacityD  r6  rB   c                T    t         j                  j                  | j                        S r   )r
   r'   #IndexProperty_GetRegionPoolCapacityr+   r   s    r   get_region_pool_capacityz!Property.get_region_pool_capacityL  s    ww::4;;GGrB   c                v    |dk  rt        d      t        j                  j                  | j                  |      S )Nr   z region_pool_capacity must be > 0)r   r
   r'   #IndexProperty_SetRegionPoolCapacityr+   r   s     r   set_region_pool_capacityz!Property.set_region_pool_capacityO  s0    A:?@@ww::4;;NNrB   c                T    t         j                  j                  | j                        S r   )r
   r'   "IndexProperty_GetBufferingCapacityr+   r   s    r   get_buffering_capacityzProperty.get_buffering_capacityW  r2  rB   c                v    |dk  rt        d      t        j                  j                  | j                  |      S )Nr   zbuffering_capacity must be > 0)r   r
   r'   "IndexProperty_SetBufferingCapacityr+   r   s     r   set_buffering_capacityzProperty.set_buffering_capacityZ  s0    A:=>>ww99$++uMMrB   c                f    t        t        j                  j                  | j                              S r   )rN   r
   r'    IndexProperty_GetEnsureTightMBRsr+   r   s    r   get_tight_mbrzProperty.get_tight_mbrb  s     DGG<<T[[IJJrB   c                ~    t        |      }t        t        j                  j                  | j                  |            S r   )rN   r
   r'    IndexProperty_SetEnsureTightMBRsr+   r   s     r   set_tight_mbrzProperty.set_tight_mbre  s+    UDGG<<T[[%PQQrB   c                f    t        t        j                  j                  | j                              S r   )rN   r
   r'   IndexProperty_GetOverwriter+   r   s    r   get_overwritezProperty.get_overwritel  s     DGG66t{{CDDrB   c                ~    t        |      }t        t        j                  j                  | j                  |            S r   )rN   r
   r'   IndexProperty_SetOverwriter+   r   s     r   set_overwritezProperty.set_overwriteo  s+    UDGG66t{{EJKKrB   c                T    t         j                  j                  | j                        S r   )r
   r'   )IndexProperty_GetNearMinimumOverlapFactorr+   r   s    r   get_near_minimum_overlap_factorz(Property.get_near_minimum_overlap_factorv  s    ww@@MMrB   c                v    |dk  rt        d      t        j                  j                  | j                  |      S )Nr   z'near_minimum_overlap_factor must be > 0)r   r
   r'   )IndexProperty_SetNearMinimumOverlapFactorr+   r   s     r   set_near_minimum_overlap_factorz(Property.set_near_minimum_overlap_factory  s0    A:FGGww@@eTTrB   c                f    t        t        j                  j                  | j                              S r   )rN   r
   r'   IndexProperty_GetWriteThroughr+   r   s    r   get_writethroughzProperty.get_writethrough  s     DGG99$++FGGrB   c                ~    t        |      }t        t        j                  j                  | j                  |            S r   )rN   r
   r'   IndexProperty_SetWriteThroughr+   r   s     r   set_writethroughzProperty.set_writethrough  s+    UDGG99$++uMNNrB   c                T    t         j                  j                  | j                        S r   )r
   r'   IndexProperty_GetFillFactorr+   r   s    r   get_fill_factorzProperty.get_fill_factor      ww224;;??rB   c                V    t         j                  j                  | j                  |      S r   )r
   r'   IndexProperty_SetFillFactorr+   r   s     r   set_fill_factorzProperty.set_fill_factor      ww224;;FFrB   c                T    t         j                  j                  | j                        S r   )r
   r'   (IndexProperty_GetSplitDistributionFactorr+   r   s    r   get_split_distribution_factorz&Property.get_split_distribution_factor  s    ww??LLrB   c                V    t         j                  j                  | j                  |      S r   )r
   r'   (IndexProperty_SetSplitDistributionFactorr+   r   s     r   set_split_distribution_factorz&Property.set_split_distribution_factor  s    ww??USSrB   c                T    t         j                  j                  | j                        S r   )r
   r'   IndexProperty_GetTPRHorizonr+   r   s    r   get_tpr_horizonzProperty.get_tpr_horizon  rd  rB   c                V    t         j                  j                  | j                  |      S r   )r
   r'   IndexProperty_SetTPRHorizonr+   r   s     r   set_tpr_horizonzProperty.set_tpr_horizon  rh  rB   c                T    t         j                  j                  | j                        S r   )r
   r'   IndexProperty_GetReinsertFactorr+   r   s    r   get_reinsert_factorzProperty.get_reinsert_factor  s    ww66t{{CCrB   c                V    t         j                  j                  | j                  |      S r   )r
   r'   IndexProperty_SetReinsertFactorr+   r   s     r   set_reinsert_factorzProperty.set_reinsert_factor  s    ww66t{{EJJrB   c                p    t         j                  j                  | j                        j	                         S r   )r
   r'   IndexProperty_GetFileNamer+   decoder   s    r   get_filenamezProperty.get_filename  s$    ww00=DDFFrB   c                    t        |t              r|j                  d      }t        j                  j                  | j                  |      S Nr   )rO   rP   encoder
   r'   IndexProperty_SetFileNamer+   r   s     r   set_filenamezProperty.set_filename  s5    eS!LL)Eww00eDDrB   c                t    t         j                  j                  | j                        }|j	                         S r   )r
   r'   %IndexProperty_GetFileNameExtensionDatr+   r}  rp   exts     r   get_dat_extensionzProperty.get_dat_extension  &    gg;;DKKHzz|rB   c                    t        |t              r|j                  d      }t        j                  j                  | j                  |      S r  )rO   rP   r  r
   r'   %IndexProperty_SetFileNameExtensionDatr+   r   s     r   set_dat_extensionzProperty.set_dat_extension  5    eS!LL)Eww<<T[[%PPrB   c                t    t         j                  j                  | j                        }|j	                         S r   )r
   r'   %IndexProperty_GetFileNameExtensionIdxr+   r}  r  s     r   get_idx_extensionzProperty.get_idx_extension  r  rB   c                    t        |t              r|j                  d      }t        j                  j                  | j                  |      S r  )rO   rP   r  r
   r'   %IndexProperty_SetFileNameExtensionIdxr+   r   s     r   set_idx_extensionzProperty.set_idx_extension  r  rB   c                T    t         j                  j                  | j                        S r   )r
   r'   +IndexProperty_GetCustomStorageCallbacksSizer+   r   s    r   !get_custom_storage_callbacks_sizez*Property.get_custom_storage_callbacks_size  s    wwBB4;;OOrB   c                V    t         j                  j                  | j                  |      S r   )r
   r'   +IndexProperty_SetCustomStorageCallbacksSizer+   r   s     r   !set_custom_storage_callbacks_sizez*Property.set_custom_storage_callbacks_size  s    wwBB4;;PUVVrB   c                T    t         j                  j                  | j                        S r   )r
   r'   'IndexProperty_GetCustomStorageCallbacksr+   r   s    r   get_custom_storage_callbacksz%Property.get_custom_storage_callbacks  s    ww>>t{{KKrB   c                V    t         j                  j                  | j                  |      S r   )r
   r'   'IndexProperty_SetCustomStorageCallbacksr+   r   s     r   set_custom_storage_callbacksz%Property.set_custom_storage_callbacks  s    ww>>t{{ERRrB   c                T    t         j                  j                  | j                        S r   )r
   r'   IndexProperty_GetIndexIDr+   r   s    r   get_index_idzProperty.get_index_id  r   rB   c                V    t         j                  j                  | j                  |      S r   )r
   r'   IndexProperty_SetIndexIDr+   r   s     r   set_index_idzProperty.set_index_id  r   rB   )NT)r  rN   rr   r   r  r  r  )r  zdict[Any, Any]r  r  r  )r"   r   r  r  )Xr  r  r  r  r  r{   r  r   r   r  r   r  r  r  r  rJ   r	  r  r  r  r  r0   r  r  rV   r  r"  rH   r%  r(  r  r+  r.  r  r1  r5  index_pool_capacityr9  r<  r  r?  rB  r  rE  rH  r  rK  rN  r  rQ  rT  rG   rW  rZ  r  r]  r`  r  rc  rg  r  rk  rn  r  rq  rt  r  rw  rz  r  r~  r  rW   r  r  r  r  r  rX   r  r  r  r  r  r  r  r  r]   r   rB   r   r   r     sB   
E6*
@)$.F NN3DEBI {K0GDF 6I BI {K0G>E
 l3HDCJ
 02DENBI
 .0ABMGN
 ##:<STGN
 ##:<STHO
 $$<>VWGN
 ""8:PQKR 6I(EL 6I(NU
 #+')H# &HO ,.>?L@G ?O<K1MT !)%'D! $@G ?O<KDK 24GHOGE
 l3H)Q
 .0ABM!Q
 .0ABM!PW %-)+L%! /LS  ($&B  '=D l3HrB   r   c                     e Zd Z ej                  dej
                   ej                  ej                              Z ej                  dej
                   ej                  ej                              Z	 ej                  dej
                   ej                  ej                              Z
 ej                  dej
                  e ej                  ej                         ej                   ej                  ej                               ej                  ej                              Z ej                  dej
                   ej                  e      ej                   ej                  ej                         ej                  ej                              Z ej                  dej
                  e ej                  ej                              Zdej
                  fdefde	fde
fdefdefdefgZ	 	 d
d	Zy)CustomStorageCallbacksNcontextcreateCallbackdestroyCallbackflushCallbackloadCallbackstoreCallbackdeleteCallbackc                *   t         j                  j                  | t        j                  |      | j	                  |      | j                  |      | j                  |      | j                  |      | j                  |      | j                  |             y r   )
r   	Structurer{   r)   createCallbackTypedestroyCallbackTypeflushCallbackTypeloadCallbackTypestoreCallbackTypedeleteCallbackType)rp   r  r  r  r  r  r  r  s           r   r{   zCustomStorageCallbacks.__init__%  s{     	!!OOG$##N3$$_5""=1!!,/""=1##N3		
rB   r  )r  r  r  r   	CFUNCTYPEr)   r$   c_intr  r  r  id_typer    r:   r  r  r  _fields_r{   r   rB   r   r  r    s   )))foo~v~~fll; +&**foo~v~~fll; )((foo~v~~fll; (v''v'~v~~fnn56v||$ )((wv~~&v||$ *))foowv||(D
 
FOO$	-.	/0	+,	)*	+,	-.H
 

rB   r  c                  D    e Zd ZdZdZdZdZdZd Zd Z	d Z
 ed       Zy	)
rT   r   r	   r   re  c                @    t         j                  j                  |      S r   )r
   r'   SIDX_NewBuffer)rp   r=   s     r   allocateBufferzICustomStorage.allocateBufferE  s    ww%%f--rB   c                    t               r   r  )rp   rE   s     r   rf   z ICustomStorage.registerCallbacksH      !##rB   c                    t               r   r  r   s    r   rd   zICustomStorage.clearK  r  rB   c                     y)NFr   r   s    r   <lambda>zICustomStorage.<lambda>N  s    rB   N)r  r  r  NoErrorInvalidPageErrorIllegalStateError	EmptyPageNewPager  rf   rd   r  rc   r   rB   r   rT   rT   ;  s>    G IG.$$ )*GArB   rT   c                  :    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
y	)
CustomStorageBasez_Derive from this class to create your own storage manager with access
    to the raw C buffers.c           	     |   t        t        j                         | j                  | j                  | j
                  | j                  | j                  | j                        }t        j                  |      |_
        || _        t        j                  t        j                  |      t        j                        |_        y r   )r  r   r)   creater   r   loadByteArraystoreByteArraydeleteByteArraysizeofr  	callbacksr#   r   r  rp   rE   r  s      r   rf   z#CustomStorageBase.registerCallbacksV  s    *OOKKLLJJ  
	 4:==3K
0".4kkNN9%v/

+rB   c                N    | j                   |j                  _        t        d      )NYou must override this method.r  r&   r"   rm   rp   r  returnErrors      r   r  zCustomStorageBase.createg  s"    %)%;%;"!"BCCrB   c                N    | j                   |j                  _        t        d      zplease overrider  r  r  s      r   r   zCustomStorageBase.destroyk  "    %)%;%;"!"BCCrB   c                N    | j                   |j                  _        t        d      r  r  )rp   r  page	resultLen
resultDatar  s         r   r  zCustomStorageBase.loadByteArrayp  r  rB   c                N    | j                   |j                  _        t        d      r  r  )rp   r  r  rR   r   r  s         r   r  z CustomStorageBase.storeByteArrayu  r  rB   c                N    | j                   |j                  _        t        d      r  r  rp   r  r  r  s       r   r  z!CustomStorageBase.deleteByteArrayz  r  rB   c                N    | j                   |j                  _        t        d      r  r  r  s      r   r   zCustomStorageBase.flush  r  rB   N)r  r  r  r  rf   r  r   r  r  r  r   r   rB   r   r  r  R  s0    
"DD
D
D
D
DrB   r  c                  ^    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 Zd
 Zd Zd Zd Zd Zy)CustomStoragezProvides a useful default custom storage implementation which marshals
    the buffers on the C side from/to python strings.
    Derive from this class and override the necessary methods to provide
    your own custom storage manager.c           	     X   t        d| j                  | j                  | j                  | j                  | j
                  | j                        }t        j                  |      |_	        || _
        t        j                  t        j                  |      t        j                        |_        y r   )r  r  r  _flush_loadByteArray_storeByteArray_deleteByteArrayr   r  r  r  r#   r   r)   r  r  s      r   rf   zCustomStorage.registerCallbacks  s|    *LLMMKK  !!
	 4:==3K
0".4kkNN9%v/

+rB   c                &    | j                  |       y r   )r  r  s      r   r  zCustomStorage._create  s    K rB   c                &    | j                  |       y r   )r   r  s      r   r  zCustomStorage._destroy  s    [!rB   c                &    | j                  |       y r   )r   r  s      r   r  zCustomStorage._flush  s    

;rB   c                   | j                  ||      }|j                  j                  | j                  k7  ry t	        |      }||j                  _        | j                  |      }t        j                  |t        j                  |      |       t        j                  |t        j                  t        j                              |d<   y r   )r  r&   r"   r  rR   r  r   memmovec_char_pr#   r$   r:   )	rp   r  r  r  r  r  resultStringr   buffers	            r   r  zCustomStorage._loadByteArray  s    ))$<%%5 L!#(	 $$U+vv|<eDFFNN6>>,JK
1rB   c                    t        j                  ||      }| j                  |j                  j                  ||      }||j                  _        y r   )r   r<   r  r&   r"   )rp   r  r  rR   r   r  rP   	newPageIds           r   r  zCustomStorage._storeByteArray  s>    tS)''(;(;S+N	'rB   c                (    | j                  ||       y r   )r  r  s       r   r  zCustomStorage._deleteByteArray  s    T;/rB   c                N    | j                   |j                  _        t        d      z$Must be overridden. No return value.r  r  rp   r  s     r   r  zCustomStorage.create  r  rB   c                N    | j                   |j                  _        t        d      r  r  r  s     r   r   zCustomStorage.destroy  r  rB   c                N    | j                   |j                  _        t        d      r  r  r  s     r   r   zCustomStorage.flush  r  rB   c                N    | j                   |j                  _        t        d      )z>Must be overridden. Must return a string with the loaded data.r  r  rp   r  r  s      r   r  zCustomStorage.loadByteArray  r  rB   c                N    | j                   |j                  _        t        d      )zMust be overridden. Must return the new 64-bit page ID of the stored
        data if a new page had to be created (i.e. page is not NewPage).r  r  )rp   r  r   r  s       r   r  zCustomStorage.storeByteArray  s$     &*%;%;"!"BCCrB   c                N    | j                   |j                  _        t        d      r  r  r  s      r   r  zCustomStorage.deleteByteArray  r  rB   N)r  r  r  r  rf   r  r  r  r  r  r  r  r   r   r  r  r  r   rB   r   r  r    sM    (

$!" L (
0D
D
D
DrB   r  c                  $    e Zd ZdZd fdZddZddZddZddZddZ	d fdZ
e
Zedd	       Ze	 d	 	 	 	 	 dd
       Z	 d	 	 	 	 	 d fdZe	 d	 	 	 	 	 	 	 dd       Ze	 d	 	 	 	 	 	 	 dd       Z	 d	 	 	 	 	 	 	 d fdZd fdZ fdZ xZS ) RtreeContainerzEAn R-Tree, MVR-Tree, or TPR-Tree indexed container for python objectsc                    |rQt        |d   t              s&t        |d   t              st        |d   t              rt	        | j
                   d      i | _        t        |    |i |S )aT  Creates a new index

        :param stream:
            If the first argument in the constructor is not of type basestring,
            it is assumed to be an iterable stream of data that will raise a
            StopIteration.  It must be in the form defined by the
            :attr:`interleaved` attribute of the index. The following example
            would assume :attr:`interleaved` is False::

                (obj,
                 (minx, maxx, miny, maxy, minz, maxz, ..., ..., mink, maxk))

            For a TPR-Tree, this would be in the form::

                (id,
                 ((minx, maxx, miny, maxy, ..., ..., mink, maxk),
                  (minvx, maxvx, minvy, maxvy, ..., ..., minvk, maxvk),
                  time),
                 object)

        :param interleaved: True or False, defaults to True.
            This parameter determines the coordinate order for all methods that
            take in coordinates.

        :param properties: This object sets both the creation and instantiation
            properties for the object and they are passed down into libspatialindex.
            A few properties are curried from instantiation parameters
            for you like ``pagesize`` to ensure compatibility with previous
            versions of the library.  All other properties must be set on the
            object.

        .. warning::
            The coordinate ordering for all functions are sensitive the
            index's :attr:`interleaved` data member.  If :attr:`interleaved`
            is False, the coordinates must be in the form
            [xmin, xmax, ymin, ymax, ..., ..., kmin, kmax]. If
            :attr:`interleaved` is True, the coordinates must be in the form
            [xmin, ymin, ..., kmin, xmax, ymax, ..., kmax]. This also applies
            to velocities when using a TPR-Tree.

        A basic example
        ::

            >>> from rtree import index
            >>> p = index.Property()

            >>> idx = index.RtreeContainer(properties=p)
            >>> idx  # doctest: +NORMALIZE_WHITESPACE
            rtree.index.RtreeContainer(bounds=[1.7976931348623157e+308,
                                     1.7976931348623157e+308,
                                     -1.7976931348623157e+308,
                                     -1.7976931348623157e+308],
                                     size=0)

        Insert an item into the index::

            >>> idx.insert(object(),
            ...            (34.3776829412, 26.7375853734, 49.3776829412,
            ...             41.7375853734))

        Query::

            >>> hits = idx.intersection((0, 0, 60, 60), bbox=True)
            >>> for obj in hits:
            ...     obj.object
            ...     obj.bbox  # doctest: +ELLIPSIS
            <object object at 0x...>
            [34.37768294..., 26.73758537..., 49.37768294..., 41.73758537...]
        r   z  supports only in-memory indexes)	rO   rP   rQ   rT   r>  	__class___objectssuperr{   )rp   rq   rr   r  s      r   r{   zRtreeContainer.__init__  sg    L 47C(d1gu-d1g~6 DNN#33S!TUU79w000rB   c                X    	 | j                  | j                        S # t        $ r Y yw xY wr   r   r   s    r   r   zRtreeContainer.get_size4	  s,    	::dkk** 		r   c                Z    d}|j                  | j                  | j                               S )Nz.rtree.index.RtreeContainer(bounds={}, size={}))formatr   r   )rp   ms     r   r   zRtreeContainer.__repr__:	  s"    <xxT]]_55rB   c                0    t        |      | j                  v S r   )r   r  r   s     r   __contains__zRtreeContainer.__contains__>	  s    #w$--''rB   c                V    t        d | j                  j                         D              S )Nc              3  &   K   | ]	  \  }}|  y wr   r   r   r   r   s      r   r   z)RtreeContainer.__len__.<locals>.<genexpr>B	  s     BZUC5B   )r*  r  valuesr   s    r   r   zRtreeContainer.__len__A	  s     B4==+?+?+ABBBrB   c                V    t        d | j                  j                         D              S )Nc              3  &   K   | ]	  \  }}|  y wr   r   r  s      r   r   z*RtreeContainer.__iter__.<locals>.<genexpr>E	  s     AJE3CAr  )rq  r  r  r   s    r   __iter__zRtreeContainer.__iter__D	  s     A$--*>*>*@AAArB   c                    	 | j                   t        |         d   dz   }||f| j                   t        |      <   t        |   t        |      |d      S # t        $ r d}Y Aw xY w)a  Inserts an item into the index with the given coordinates.

        :param obj: Any object.

        :param coordinates: 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.
            For a TPR-Tree, this must be a 3-element sequence including
            not only the positional coordinate pairs but also the
            velocity pairs `minvk` and `maxvk` and a time value as a float.

        The following example inserts a simple object into the container.
        The coordinate ordering in this instance is the default
        (interleaved=True) ordering::

            >>> from rtree import index
            >>> idx = index.RtreeContainer()
            >>> idx.insert(object(),
            ...            (34.3776829412, 26.7375853734, 49.3776829412,
            ...             41.7375853734))

        Similar for TPR-Tree::

            >>> p = index.Property(type=index.RT_TPRTree)  # doctest: +SKIP
            >>> idx = index.RtreeContainer(properties=p)  # doctest: +SKIP
            >>> idx.insert(object(),
            ...            ((34.3776829412, 26.7375853734, 49.3776829412,
            ...             41.7375853734),
            ...             (0.5, 2, 1.5, 2.5),
            ...            3.0))  # doctest: +SKIP

        r   r	   N)r  r   KeyErrorr  ro   rp   r   r   r   r  s       r   ro   zRtreeContainer.insertG	  sj    D	MM"S'*1-1E #(bgw~bg{D99  	E	s   A A#"A#c                     y r   r   rp   r   r   s      r   r   zRtreeContainer.intersectionr	  s    UXrB   c                     y r   r   r  s      r   r   zRtreeContainer.intersectionu	  r   rB   c              #  
  K   |du r*t         |   ||      D ]  }| j                  |   d     y|du rBt         |   ||      D ].  }| j                  |j                     d   |_        d|_        | 0 yt        d      w)a	  Return ids or objects in the index that intersect the given
        coordinates.

        :param coordinates: 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.
            For a TPR-Tree, this must be a 3-element sequence including
            not only the positional coordinate pairs but also the
            velocity pairs `minvk` and `maxvk` and a time pair for the
            time range as a float.

        :param bbox: If True, the intersection method will return the stored objects,
            as well as the bounds of the entry.

        The following example queries the container for any stored objects that
        intersect the bounds given in the coordinates::

            >>> from rtree import index
            >>> idx = index.RtreeContainer()
            >>> idx.insert(object(),
            ...            (34.3776829412, 26.7375853734, 49.3776829412,
            ...             41.7375853734))

            >>> hits = list(idx.intersection((0, 0, 60, 60), bbox=True))
            >>> [(item.object, item.bbox) for item in hits]
            ... # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
            [(<object object at 0x...>, [34.3776829412, 26.7375853734,
            49.3776829412, 41.7375853734])]

        If the :class:`rtree.index.Item` wrapper is not used, it is faster to
        request only the stored objects::

            >>> list(idx.intersection((0, 0, 60, 60)))   # doctest: +ELLIPSIS
            [<object object at 0x...>]

        Similar for the TPR-Tree::

            >>> p = index.Property(type=index.RT_TPRTree)  # doctest: +SKIP
            >>> idx = index.RtreeContainer(properties=p)  # doctest: +SKIP
            >>> idx.insert(object(),
            ...            ((34.3776829412, 26.7375853734, 49.3776829412,
            ...             41.7375853734),
            ...             (0.5, 2, 1.5, 2.5),
            ...             3.0))  # doctest: +SKIP

            >>> hits = list(idx.intersection(
            ...     ((0, 0, 60, 60), (0, 0, 0, 0), (3, 5)), bbox=True))
            ... # doctest: +SKIP
            >>> [(item.object, item.bbox) for item in hits]
            ... # doctest: +SKIP
            [(<object object at 0x...>, [34.3776829412, 26.7375853734,
            49.3776829412, 41.7375853734])]

        Fr	   TN5valid values for the bbox argument are True and False)r  r   r  r   r   r>  )rp   r   r   r   r"   r  s        r   r   zRtreeContainer.intersectionz	  s     t 5=g*;= +mmB'**+T\-k4@ #}}UXX6q9
 TUUs   B Bc                     y r   r   rp   r   r  r   s       r   r  zRtreeContainer.nearest	  r   rB   c                     y r   r   r  s       r   r  zRtreeContainer.nearest	  r   rB   c              #    K   |du r+t         |   |||      D ]  }| j                  |   d     y|du rCt         |   |||      D ].  }| j                  |j                     d   |_        d|_        | 0 yt        d      w)a{  Returns the ``k``-nearest objects to the given coordinates
        in increasing distance order.

        :param coordinates: 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.
            For a TPR-Tree, this must be a 3-element sequence including
            not only the positional coordinate pairs but also the
            velocity pairs `minvk` and `maxvk` and a time pair for the
            time range as a float.

        :param num_results: The number of results to return nearest to the given
            coordinates. If two entries are equidistant, *both* are returned.
            This property means that :attr:`num_results` may return more
            items than specified.

        :param bbox: If True, the nearest method will return the stored objects, as
            well as the bounds of the entry.

        .. warning::
            This is currently not implemented for the TPR-Tree.

        Example of finding the three items nearest to this one::

            >>> from rtree import index
            >>> idx = index.RtreeContainer()
            >>> idx.insert(object(), (34.37, 26.73, 49.37, 41.73))
            >>> hits = idx.nearest((0, 0, 10, 10), 3, bbox=True)
        Fr	   TNr  )r  r  r  r   r   r>  )rp   r   r  r   r   r"   r  s         r   r  zRtreeContainer.nearest	  s     B 5=gok;E +mmB'**+T\k4H #}}UXX6q9
 TUUs   BBc                   	 | j                   t        |         d   dz
  }|dk(  r| j                   t        |      = n||f| j                   t        |      <   t        |   t        |      |      S # t        $ r t        d      w xY w)ar  Deletes the item from the container within the specified
        coordinates.

        :param obj: Any object.

        :param coordinates: Dimension * 2 coordinate pairs, representing the min
            and max coordinates in each dimension of the item to be
            deleted from the index. Their ordering will depend on the
            index's :attr:`interleaved` data member.
            These are not the coordinates of a space containing the
            item, but those of the item itself. Together with the
            id parameter, they determine which item will be deleted.
            This may be an object that satisfies the numpy array protocol.
            For a TPR-Tree, this must be a 3-element sequence including
            not only the positional coordinate pairs but also the
            velocity pairs `minvk` and `maxvk` and a time pair for the
            original time the object was inserted and the current time
            as a float.

        Example::

            >>> from rtree import index
            >>> idx = index.RtreeContainer()
            >>> idx.delete(object(),
            ...            (34.3776829412, 26.7375853734, 49.3776829412,
            ...             41.7375853734))
            Traceback (most recent call last):
             ...
            IndexError: object is not in the index

        For the TPR-Tree::

            >>> p = index.Property(type=index.RT_TPRTree)  # doctest: +SKIP
            >>> idx = index.RtreeContainer(properties=p)  # doctest: +SKIP
            >>> idx.delete(object(),
            ...            ((34.3776829412, 26.7375853734, 49.3776829412,
            ...             41.7375853734),
            ...             (0.5, 2, 1.5, 2.5),
            ...             (3.0, 5.0)))  # doctest: +SKIP
            Traceback (most recent call last):
             ...
            IndexError: object is not in the index

        r   r	   zobject is not in the index)r  r   r  
IndexErrorr  rR  r  s       r   rR  zRtreeContainer.delete	  s    Z	;MM"S'*1-1E A:bg&&+S\DMM"S'"w~bg{33  	;9::	;s   A0 0Bc                    t         |          D cg c]9  \  }}}| j                  |   d   |D cg c]  }| j                  |   d    c}|f; c}}}}S c c}w c c}}}}w )Nr	   )r  r  r  )rp   r   r  r   child_idr  s        r   r  zRtreeContainer.leaves,
  sq     */)9
 
 &Iv	 b!!$<EFx(+F
 	
 G
s   A
AA
A
r  r  r  )r   r   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   rN   r  Iterator[Item | object])r	   T)r   r   r  r   r   r  r  r  r  )r   r   r  r   r   r  r  r  )r   r   r  r   r   rN   r  r#  )r  r  r  r  r{   r   r   r
  r   r  ro   r  r   r   r  rR  r  __classcell__)r  s   @r   r   r     sK   ON1`6(CB':R CX X7<&4	 
 .3CVCV&*CV	 CVJ LP-0<I	  MR-0<J	 
 DI*V*V-0*V<@*V	 *VX54n
 
rB   r   );
__future__r   r   rY   os.pathr   r  r}   collections.abcr   r   typingr   r   r    r
   
exceptionsr   rg   rU   rb   	RT_LinearRT_QuadraticRT_Starrh   
RT_MVRTreerK   r'   SIDX_Version__c_api_version__r}  splitmajor_versionminor_versionpatch_versionrh  __all__r7   rA   r   r   r	  r  r  rn   rs  rv  r   r  r   r   r  r  r  rT   r  r  r   r   rB   r   <module>r6     s   "  	     . ) )  "	
		


GG((* /%,,W5;;C@/ +}m =-09<
E
FF
('4
H HX. 	( (V>Y >   F
& 
- -	0; 0-V -
f fV ..=
V-- =
@B B.0D 0DfYDN YDxS
U S
{  		s   0E E%$E%