Skip to content

Commit 1a4e1a7

Browse files
ptitjanolbartoletti
authored andcommitted
qgsgeometry: Add contains method from x,y coordinates
1 parent ef2f07b commit 1a4e1a7

File tree

6 files changed

+38
-0
lines changed

6 files changed

+38
-0
lines changed

python/PyQt6/core/auto_generated/geometry/qgsgeometry.sip.in

+7
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,13 @@ geometries.
13221322
bool contains( const QgsPointXY *p ) const;
13231323
%Docstring
13241324
Returns ``True`` if the geometry contains the point ``p``.
1325+
%End
1326+
1327+
bool contains( double x, double y ) const;
1328+
%Docstring
1329+
Returns ``True`` if the geometry contains the point at (``x``, ``y``).
1330+
1331+
.. versionadded:: 3.38
13251332
%End
13261333

13271334
bool contains( const QgsGeometry &geometry ) const;

python/core/auto_generated/geometry/qgsgeometry.sip.in

+7
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,13 @@ geometries.
13221322
bool contains( const QgsPointXY *p ) const;
13231323
%Docstring
13241324
Returns ``True`` if the geometry contains the point ``p``.
1325+
%End
1326+
1327+
bool contains( double x, double y ) const;
1328+
%Docstring
1329+
Returns ``True`` if the geometry contains the point at (``x``, ``y``).
1330+
1331+
.. versionadded:: 3.38
13251332
%End
13261333

13271334
bool contains( const QgsGeometry &geometry ) const;

src/core/geometry/qgsgeometry.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -1492,6 +1492,19 @@ bool QgsGeometry::contains( const QgsPointXY *p ) const
14921492
return geos.contains( &pt, &mLastError );
14931493
}
14941494

1495+
bool QgsGeometry::contains( double x, double y ) const
1496+
{
1497+
if ( !d->geometry )
1498+
{
1499+
return false;
1500+
}
1501+
1502+
QgsPoint pt( x, y );
1503+
QgsGeos geos( d->geometry.get() );
1504+
mLastError.clear();
1505+
return geos.contains( &pt, &mLastError );
1506+
}
1507+
14951508
bool QgsGeometry::contains( const QgsGeometry &geometry ) const
14961509
{
14971510
if ( !d->geometry || geometry.isNull() )

src/core/geometry/qgsgeometry.h

+7
Original file line numberDiff line numberDiff line change
@@ -1417,6 +1417,13 @@ class CORE_EXPORT QgsGeometry
14171417
*/
14181418
bool contains( const QgsPointXY *p ) const;
14191419

1420+
/**
1421+
* Returns TRUE if the geometry contains the point at (\a x, \a y).
1422+
*
1423+
* \since QGIS 3.38
1424+
*/
1425+
bool contains( double x, double y ) const;
1426+
14201427
/**
14211428
* Returns TRUE if the geometry completely contains another \a geometry.
14221429
*

tests/src/core/geometry/testqgsgeometry.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -2085,10 +2085,12 @@ void TestQgsGeometry::contains()
20852085
QgsPointXY pointInside( 1, 2 );
20862086
QVERIFY( geomTest.contains( &pointInside ) );
20872087
QVERIFY( geomTest.contains( QgsGeometry::fromWkt( QStringLiteral( "Point(1 2)" ) ) ) );
2088+
QVERIFY( geomTest.contains( pointInside.x(), pointInside.y() ) );
20882089

20892090
QgsPointXY pointOutside( 3, 1 );
20902091
QVERIFY( !geomTest.contains( &pointOutside ) );
20912092
QVERIFY( !geomTest.contains( QgsGeometry::fromWkt( QStringLiteral( "Point(3 1)" ) ) ) );
2093+
QVERIFY( !geomTest.contains( pointOutside.x(), pointOutside.y() ) );
20922094
}
20932095

20942096
void TestQgsGeometry::reshapeGeometryLineMerge()

tests/src/python/test_qgsgeometry.py

+2
Original file line numberDiff line numberDiff line change
@@ -1116,10 +1116,12 @@ def testContains(self):
11161116
pointInside = QgsPointXY(1, 1)
11171117
self.assertTrue(myPoly.contains(pointInside))
11181118
self.assertTrue(myPoly.contains(QgsGeometry.fromPointXY(pointInside)))
1119+
self.assertTrue(myPoly.contains(pointInside.x(), pointInside.y()))
11191120

11201121
pointOutside = QgsPointXY(3, 3)
11211122
self.assertFalse(myPoly.contains(pointOutside))
11221123
self.assertFalse(myPoly.contains(QgsGeometry.fromPointXY(pointOutside)))
1124+
self.assertFalse(myPoly.contains(pointOutside.x(), pointOutside.y()))
11231125

11241126
def testTouches(self):
11251127
myLine = QgsGeometry.fromPolylineXY([

0 commit comments

Comments
 (0)