Skip to content

Commit abcaba8

Browse files
committed
Add method to force a detach for QgsSpatialIndex
Since the underlying libspatialindex is not thread safe on some platforms (e.g. Windows), manual calls to detach() must be made if a QgsSpatialIndex is to be accessed across multiple threads. Note that for platforms on which libspatialindex is thread safe, calling detach() has no effect and does not force the deep copy.
1 parent 98d43ea commit abcaba8

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

python/core/qgsspatialindex.sip.in

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,20 @@ Copy constructor
6060
~QgsSpatialIndex();
6161

6262

63+
void detach();
64+
%Docstring
65+
Detaches the index, forcing a deep copy of the underlying
66+
spatial index data.
67+
68+
Since the underlying libspatialindex is not thread safe on some platforms (e.g. Windows),
69+
manual calls to detach() must be made if a QgsSpatialIndex is to be accessed across multiple threads.
70+
71+
Note that for platforms on which libspatialindex is thread safe, calling
72+
detach() has no effect and does not force the deep copy.
73+
74+
.. versionadded:: 3.0
75+
%End
76+
6377

6478
bool insertFeature( const QgsFeature &f );
6579
%Docstring

src/core/qgsspatialindex.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,14 @@ QgsSpatialIndex &QgsSpatialIndex::operator=( const QgsSpatialIndex &other )
266266
return *this;
267267
}
268268

269+
void QgsSpatialIndex::detach()
270+
{
271+
// libspatialindex is not thread safe on windows - so force the deep copy
272+
#if defined(Q_OS_WIN)
273+
d.detach();
274+
#endif
275+
}
276+
269277
SpatialIndex::Region QgsSpatialIndex::rectToRegion( const QgsRectangle &rect )
270278
{
271279
double pt1[2] = { rect.xMinimum(), rect.yMinimum() },

src/core/qgsspatialindex.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,20 @@ class CORE_EXPORT QgsSpatialIndex
9797
//! Implement assignment operator
9898
QgsSpatialIndex &operator=( const QgsSpatialIndex &other );
9999

100+
/**
101+
* Detaches the index, forcing a deep copy of the underlying
102+
* spatial index data.
103+
*
104+
* Since the underlying libspatialindex is not thread safe on some platforms (e.g. Windows),
105+
* manual calls to detach() must be made if a QgsSpatialIndex is to be accessed across multiple threads.
106+
*
107+
* Note that for platforms on which libspatialindex is thread safe, calling
108+
* detach() has no effect and does not force the deep copy.
109+
*
110+
* \since QGIS 3.0
111+
*/
112+
void detach();
113+
100114
/* operations */
101115

102116
//! Add feature to index

0 commit comments

Comments
 (0)