Skip to content

Commit a86f5ea

Browse files
committed
remove PyNames for QgsGeometry::compare methods
single python method with some MethodCode to dispatch to proper cpp methods.
1 parent b0d4a4c commit a86f5ea

File tree

7 files changed

+263
-51
lines changed

7 files changed

+263
-51
lines changed

python/core/geometry/qgsgeometry.sip

+112-34
Original file line numberDiff line numberDiff line change
@@ -1185,45 +1185,123 @@ Ring 0 is outer ring and can't be deleted.
11851185
:rtype: QgsPolygon
11861186
%End
11871187

1188-
static bool compare( const QgsPolyline &p1, const QgsPolyline &p2,
1189-
double epsilon = 4 * DBL_EPSILON ) /PyName=comparePolylines/;
1190-
%Docstring
1191-
Compares two polylines for equality within a specified tolerance.
1192-
\param p1 first polyline
1193-
\param p2 second polyline
1194-
\param epsilon maximum difference for coordinates between the polylines
1195-
:return: true if polylines have the same number of points and all
1196-
points are equal within the specified tolerance
1197-
.. versionadded:: 2.9
1198-
:rtype: bool
1199-
%End
12001188

1201-
static bool compare( const QgsPolygon &p1, const QgsPolygon &p2,
1202-
double epsilon = 4 * DBL_EPSILON ) /PyName=comparePolygons/;
1203-
%Docstring
1204-
Compares two polygons for equality within a specified tolerance.
1205-
\param p1 first polygon
1206-
\param p2 second polygon
1207-
\param epsilon maximum difference for coordinates between the polygons
1208-
:return: true if polygons have the same number of rings, and each ring has the same
1209-
number of points and all points are equal within the specified tolerance
1210-
.. versionadded:: 2.9
1211-
:rtype: bool
1212-
%End
1213-
1214-
static bool compare( const QgsMultiPolygon &p1, const QgsMultiPolygon &p2,
1215-
double epsilon = 4 * DBL_EPSILON ) /PyName=compareMultiPolygons/;
1216-
%Docstring
1217-
Compares two multipolygons for equality within a specified tolerance.
1218-
\param p1 first multipolygon
1219-
\param p2 second multipolygon
1220-
\param epsilon maximum difference for coordinates between the multipolygons
1221-
:return: true if multipolygons have the same number of polygons, the polygons have the same number
1222-
of rings, and each ring has the same number of points and all points are equal within the specified
1189+
static bool compare( PyObject *obj1, PyObject *obj2, double epsilon = 4 * DBL_EPSILON );
1190+
%Docstring
1191+
Compares two geometry objects for equality within a specified tolerance.
1192+
The objects can be of type QgsPolyline, QgsPolygon or QgsMultiPolygon.
1193+
The 2 types should match.
1194+
\param p1 first geometry object
1195+
\param p2 second geometry object
1196+
\param epsilon maximum difference for coordinates between the objects
1197+
:return: true if objects are
1198+
- polylines and have the same number of points and all
1199+
points are equal within the specified tolerance
1200+
- polygons and have the same number of points and all
1201+
points are equal within the specified tolerance
1202+
- multipolygons and have the same number of polygons, the polygons have the same number
1203+
of rings, and each ring has the same number of points and all points are equal
1204+
within the specified
12231205
tolerance
12241206
.. versionadded:: 2.9
12251207
:rtype: bool
12261208
%End
1209+
%MethodCode
1210+
{
1211+
sipRes = false;
1212+
int state0;
1213+
int state1;
1214+
int sipIsErr = 0;
1215+
1216+
if ( PyList_Check( a0 ) && PyList_Check( a1 ) &&
1217+
PyList_GET_SIZE( a0 ) && PyList_GET_SIZE( a1 ) )
1218+
{
1219+
PyObject *o0 = PyList_GetItem( a0, 0 );
1220+
PyObject *o1 = PyList_GetItem( a1, 0 );
1221+
if ( o0 && o1 )
1222+
{
1223+
// compare polyline - polyline
1224+
if ( sipCanConvertToType( o0, sipType_QgsPoint, SIP_NOT_NONE ) &&
1225+
sipCanConvertToType( o1, sipType_QgsPoint, SIP_NOT_NONE ) &&
1226+
sipCanConvertToType( a0, sipType_QVector_0100QgsPoint, SIP_NOT_NONE ) &&
1227+
sipCanConvertToType( a1, sipType_QVector_0100QgsPoint, SIP_NOT_NONE ) )
1228+
{
1229+
QgsPolyline *p0;
1230+
QgsPolyline *p1;
1231+
p0 = reinterpret_cast<QgsPolyline *>( sipConvertToType( a0, sipType_QVector_0100QgsPoint, 0, SIP_NOT_NONE, &state0, &sipIsErr ) );
1232+
p1 = reinterpret_cast<QgsPolyline *>( sipConvertToType( a1, sipType_QVector_0100QgsPoint, 0, SIP_NOT_NONE, &state1, &sipIsErr ) );
1233+
if ( sipIsErr )
1234+
{
1235+
sipReleaseType( p0, sipType_QVector_0100QgsPoint, state0 );
1236+
sipReleaseType( p1, sipType_QVector_0100QgsPoint, state1 );
1237+
}
1238+
else
1239+
{
1240+
sipRes = QgsGeometry::compare( *p0, *p1, a2 );
1241+
}
1242+
}
1243+
else if ( PyList_Check( o0 ) && PyList_Check( o1 ) &&
1244+
PyList_GET_SIZE( o0 ) && PyList_GET_SIZE( o1 ) )
1245+
{
1246+
PyObject *oo0 = PyList_GetItem( o0, 0 );
1247+
PyObject *oo1 = PyList_GetItem( o1, 0 );
1248+
if ( oo0 && oo1 )
1249+
{
1250+
// compare polygon - polygon
1251+
if ( sipCanConvertToType( oo0, sipType_QgsPoint, SIP_NOT_NONE ) &&
1252+
sipCanConvertToType( oo1, sipType_QgsPoint, SIP_NOT_NONE ) &&
1253+
sipCanConvertToType( a0, sipType_QVector_0600QVector_0100QgsPoint, SIP_NOT_NONE ) &&
1254+
sipCanConvertToType( a1, sipType_QVector_0600QVector_0100QgsPoint, SIP_NOT_NONE ) )
1255+
{
1256+
QgsPolygon *p0;
1257+
QgsPolygon *p1;
1258+
p0 = reinterpret_cast<QgsPolygon *>( sipConvertToType( a0, sipType_QVector_0600QVector_0100QgsPoint, 0, SIP_NOT_NONE, &state0, &sipIsErr ) );
1259+
p1 = reinterpret_cast<QgsPolygon *>( sipConvertToType( a1, sipType_QVector_0600QVector_0100QgsPoint, 0, SIP_NOT_NONE, &state1, &sipIsErr ) );
1260+
if ( sipIsErr )
1261+
{
1262+
sipReleaseType( p0, sipType_QVector_0600QVector_0100QgsPoint, state0 );
1263+
sipReleaseType( p1, sipType_QVector_0600QVector_0100QgsPoint, state1 );
1264+
}
1265+
else
1266+
{
1267+
sipRes = QgsGeometry::compare( *p0, *p1, a2 );
1268+
}
1269+
}
1270+
else if ( PyList_Check( oo0 ) && PyList_Check( oo1 ) &&
1271+
PyList_GET_SIZE( oo0 ) && PyList_GET_SIZE( oo1 ) )
1272+
{
1273+
PyObject *ooo0 = PyList_GetItem( oo0, 0 );
1274+
PyObject *ooo1 = PyList_GetItem( oo1, 0 );
1275+
if ( ooo0 && ooo1 )
1276+
{
1277+
// compare multipolygon - multipolygon
1278+
if ( sipCanConvertToType( ooo0, sipType_QgsPoint, SIP_NOT_NONE ) &&
1279+
sipCanConvertToType( ooo1, sipType_QgsPoint, SIP_NOT_NONE ) &&
1280+
sipCanConvertToType( a0, sipType_QVector_0600QVector_0600QVector_0100QgsPoint, SIP_NOT_NONE ) &&
1281+
sipCanConvertToType( a1, sipType_QVector_0600QVector_0600QVector_0100QgsPoint, SIP_NOT_NONE ) )
1282+
{
1283+
QgsMultiPolygon *p0;
1284+
QgsMultiPolygon *p1;
1285+
p0 = reinterpret_cast<QgsMultiPolygon *>( sipConvertToType( a0, sipType_QVector_0600QVector_0600QVector_0100QgsPoint, 0, SIP_NOT_NONE, &state0, &sipIsErr ) );
1286+
p1 = reinterpret_cast<QgsMultiPolygon *>( sipConvertToType( a1, sipType_QVector_0600QVector_0600QVector_0100QgsPoint, 0, SIP_NOT_NONE, &state1, &sipIsErr ) );
1287+
if ( sipIsErr )
1288+
{
1289+
sipReleaseType( p0, sipType_QVector_0600QVector_0600QVector_0100QgsPoint, state0 );
1290+
sipReleaseType( p1, sipType_QVector_0600QVector_0600QVector_0100QgsPoint, state1 );
1291+
}
1292+
else
1293+
{
1294+
sipRes = QgsGeometry::compare( *p0, *p1, a2 );
1295+
}
1296+
}
1297+
}
1298+
}
1299+
}
1300+
}
1301+
}
1302+
}
1303+
}
1304+
%End
12271305

12281306
QgsGeometry smooth( const unsigned int iterations = 1, const double offset = 0.25,
12291307
double minimumDistance = -1.0, double maxAngle = 180.0 ) const;

scripts/sipify_all.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ done < <(
4242
${GP}sed -n -r 's/^%Include (.*\.sip)/core\/\1/p' python/core/core.sip
4343
${GP}sed -n -r 's/^%Include (.*\.sip)/gui\/\1/p' python/gui/gui.sip
4444
${GP}sed -n -r 's/^%Include (.*\.sip)/analysis\/\1/p' python/analysis/analysis.sip
45-
${GP}sed -n -r 's/^%Include (.*\.sip)/server\/\1/p' python/analysis/server.sip
45+
${GP}sed -n -r 's/^%Include (.*\.sip)/server\/\1/p' python/server/server.sip
4646
)
4747

4848
echo " => $count files sipified!"

src/core/geometry/qgsgeometry.h

+122-3
Original file line numberDiff line numberDiff line change
@@ -1042,6 +1042,8 @@ class CORE_EXPORT QgsGeometry
10421042
*/
10431043
static QgsPolygon createPolygonFromQPolygonF( const QPolygonF &polygon ) SIP_FACTORY;
10441044

1045+
#ifndef SIP_RUN
1046+
10451047
/** Compares two polylines for equality within a specified tolerance.
10461048
* \param p1 first polyline
10471049
* \param p2 second polyline
@@ -1051,7 +1053,7 @@ class CORE_EXPORT QgsGeometry
10511053
* \since QGIS 2.9
10521054
*/
10531055
static bool compare( const QgsPolyline &p1, const QgsPolyline &p2,
1054-
double epsilon = 4 * std::numeric_limits<double>::epsilon() SIP_PYARGDEFAULT( 4 * DBL_EPSILON ) ) SIP_PYNAME( comparePolylines );
1056+
double epsilon = 4 * std::numeric_limits<double>::epsilon() );
10551057

10561058
/** Compares two polygons for equality within a specified tolerance.
10571059
* \param p1 first polygon
@@ -1062,7 +1064,7 @@ class CORE_EXPORT QgsGeometry
10621064
* \since QGIS 2.9
10631065
*/
10641066
static bool compare( const QgsPolygon &p1, const QgsPolygon &p2,
1065-
double epsilon = 4 * std::numeric_limits<double>::epsilon() SIP_PYARGDEFAULT( 4 * DBL_EPSILON ) ) SIP_PYNAME( comparePolygons );
1067+
double epsilon = 4 * std::numeric_limits<double>::epsilon() );
10661068

10671069
/** Compares two multipolygons for equality within a specified tolerance.
10681070
* \param p1 first multipolygon
@@ -1074,7 +1076,124 @@ class CORE_EXPORT QgsGeometry
10741076
* \since QGIS 2.9
10751077
*/
10761078
static bool compare( const QgsMultiPolygon &p1, const QgsMultiPolygon &p2,
1077-
double epsilon = 4 * std::numeric_limits<double>::epsilon() SIP_PYARGDEFAULT( 4 * DBL_EPSILON ) ) SIP_PYNAME( compareMultiPolygons );
1079+
double epsilon = 4 * std::numeric_limits<double>::epsilon() );
1080+
#else
1081+
1082+
/** Compares two geometry objects for equality within a specified tolerance.
1083+
* The objects can be of type QgsPolyline, QgsPolygon or QgsMultiPolygon.
1084+
* The 2 types should match.
1085+
* \param p1 first geometry object
1086+
* \param p2 second geometry object
1087+
* \param epsilon maximum difference for coordinates between the objects
1088+
* \returns true if objects are
1089+
* - polylines and have the same number of points and all
1090+
* points are equal within the specified tolerance
1091+
* - polygons and have the same number of points and all
1092+
* points are equal within the specified tolerance
1093+
* - multipolygons and have the same number of polygons, the polygons have the same number
1094+
* of rings, and each ring has the same number of points and all points are equal
1095+
* within the specified
1096+
* tolerance
1097+
* \since QGIS 2.9
1098+
*/
1099+
static bool compare( PyObject *obj1, PyObject *obj2, double epsilon = 4 * DBL_EPSILON );
1100+
% MethodCode
1101+
{
1102+
sipRes = false;
1103+
int state0;
1104+
int state1;
1105+
int sipIsErr = 0;
1106+
1107+
if ( PyList_Check( a0 ) && PyList_Check( a1 ) &&
1108+
PyList_GET_SIZE( a0 ) && PyList_GET_SIZE( a1 ) )
1109+
{
1110+
PyObject *o0 = PyList_GetItem( a0, 0 );
1111+
PyObject *o1 = PyList_GetItem( a1, 0 );
1112+
if ( o0 && o1 )
1113+
{
1114+
// compare polyline - polyline
1115+
if ( sipCanConvertToType( o0, sipType_QgsPoint, SIP_NOT_NONE ) &&
1116+
sipCanConvertToType( o1, sipType_QgsPoint, SIP_NOT_NONE ) &&
1117+
sipCanConvertToType( a0, sipType_QVector_0100QgsPoint, SIP_NOT_NONE ) &&
1118+
sipCanConvertToType( a1, sipType_QVector_0100QgsPoint, SIP_NOT_NONE ) )
1119+
{
1120+
QgsPolyline *p0;
1121+
QgsPolyline *p1;
1122+
p0 = reinterpret_cast<QgsPolyline *>( sipConvertToType( a0, sipType_QVector_0100QgsPoint, 0, SIP_NOT_NONE, &state0, &sipIsErr ) );
1123+
p1 = reinterpret_cast<QgsPolyline *>( sipConvertToType( a1, sipType_QVector_0100QgsPoint, 0, SIP_NOT_NONE, &state1, &sipIsErr ) );
1124+
if ( sipIsErr )
1125+
{
1126+
sipReleaseType( p0, sipType_QVector_0100QgsPoint, state0 );
1127+
sipReleaseType( p1, sipType_QVector_0100QgsPoint, state1 );
1128+
}
1129+
else
1130+
{
1131+
sipRes = QgsGeometry::compare( *p0, *p1, a2 );
1132+
}
1133+
}
1134+
else if ( PyList_Check( o0 ) && PyList_Check( o1 ) &&
1135+
PyList_GET_SIZE( o0 ) && PyList_GET_SIZE( o1 ) )
1136+
{
1137+
PyObject *oo0 = PyList_GetItem( o0, 0 );
1138+
PyObject *oo1 = PyList_GetItem( o1, 0 );
1139+
if ( oo0 && oo1 )
1140+
{
1141+
// compare polygon - polygon
1142+
if ( sipCanConvertToType( oo0, sipType_QgsPoint, SIP_NOT_NONE ) &&
1143+
sipCanConvertToType( oo1, sipType_QgsPoint, SIP_NOT_NONE ) &&
1144+
sipCanConvertToType( a0, sipType_QVector_0600QVector_0100QgsPoint, SIP_NOT_NONE ) &&
1145+
sipCanConvertToType( a1, sipType_QVector_0600QVector_0100QgsPoint, SIP_NOT_NONE ) )
1146+
{
1147+
QgsPolygon *p0;
1148+
QgsPolygon *p1;
1149+
p0 = reinterpret_cast<QgsPolygon *>( sipConvertToType( a0, sipType_QVector_0600QVector_0100QgsPoint, 0, SIP_NOT_NONE, &state0, &sipIsErr ) );
1150+
p1 = reinterpret_cast<QgsPolygon *>( sipConvertToType( a1, sipType_QVector_0600QVector_0100QgsPoint, 0, SIP_NOT_NONE, &state1, &sipIsErr ) );
1151+
if ( sipIsErr )
1152+
{
1153+
sipReleaseType( p0, sipType_QVector_0600QVector_0100QgsPoint, state0 );
1154+
sipReleaseType( p1, sipType_QVector_0600QVector_0100QgsPoint, state1 );
1155+
}
1156+
else
1157+
{
1158+
sipRes = QgsGeometry::compare( *p0, *p1, a2 );
1159+
}
1160+
}
1161+
else if ( PyList_Check( oo0 ) && PyList_Check( oo1 ) &&
1162+
PyList_GET_SIZE( oo0 ) && PyList_GET_SIZE( oo1 ) )
1163+
{
1164+
PyObject *ooo0 = PyList_GetItem( oo0, 0 );
1165+
PyObject *ooo1 = PyList_GetItem( oo1, 0 );
1166+
if ( ooo0 && ooo1 )
1167+
{
1168+
// compare multipolygon - multipolygon
1169+
if ( sipCanConvertToType( ooo0, sipType_QgsPoint, SIP_NOT_NONE ) &&
1170+
sipCanConvertToType( ooo1, sipType_QgsPoint, SIP_NOT_NONE ) &&
1171+
sipCanConvertToType( a0, sipType_QVector_0600QVector_0600QVector_0100QgsPoint, SIP_NOT_NONE ) &&
1172+
sipCanConvertToType( a1, sipType_QVector_0600QVector_0600QVector_0100QgsPoint, SIP_NOT_NONE ) )
1173+
{
1174+
QgsMultiPolygon *p0;
1175+
QgsMultiPolygon *p1;
1176+
p0 = reinterpret_cast<QgsMultiPolygon *>( sipConvertToType( a0, sipType_QVector_0600QVector_0600QVector_0100QgsPoint, 0, SIP_NOT_NONE, &state0, &sipIsErr ) );
1177+
p1 = reinterpret_cast<QgsMultiPolygon *>( sipConvertToType( a1, sipType_QVector_0600QVector_0600QVector_0100QgsPoint, 0, SIP_NOT_NONE, &state1, &sipIsErr ) );
1178+
if ( sipIsErr )
1179+
{
1180+
sipReleaseType( p0, sipType_QVector_0600QVector_0600QVector_0100QgsPoint, state0 );
1181+
sipReleaseType( p1, sipType_QVector_0600QVector_0600QVector_0100QgsPoint, state1 );
1182+
}
1183+
else
1184+
{
1185+
sipRes = QgsGeometry::compare( *p0, *p1, a2 );
1186+
}
1187+
}
1188+
}
1189+
}
1190+
}
1191+
}
1192+
}
1193+
}
1194+
}
1195+
% End
1196+
#endif
10781197

10791198
/** Smooths a geometry by rounding off corners using the Chaikin algorithm. This operation
10801199
* roughly doubles the number of vertices in a geometry.

tests/src/python/providertestbase.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ def testExtent(self):
711711
QgsRectangle(-71.123, 66.33, -65.32, 78.3))
712712
provider_extent = QgsGeometry.fromRect(self.provider.extent())
713713

714-
self.assertTrue(QgsGeometry.comparePolylines(provider_extent.asPolygon()[0], reference.asPolygon()[0], 0.00001))
714+
self.assertTrue(QgsGeometry.compare(provider_extent.asPolygon()[0], reference.asPolygon()[0], 0.00001))
715715

716716
def testUnique(self):
717717
self.assertEqual(set(self.provider.uniqueValues(1)), set([-200, 100, 200, 300, 400]))

tests/src/python/test_provider_ogr_gpkg.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ def testGeopackageExtentUpdate(self):
191191
self.assertTrue(vl.commitChanges())
192192
reference = QgsGeometry.fromRect(QgsRectangle(0.5, 0.0, 1.0, 1.0))
193193
provider_extent = QgsGeometry.fromRect(vl.extent())
194-
self.assertTrue(QgsGeometry.comparePolylines(provider_extent.asPolygon()[0], reference.asPolygon()[0], 0.00001),
194+
self.assertTrue(QgsGeometry.compare(provider_extent.asPolygon()[0], reference.asPolygon()[0], 0.00001),
195195
provider_extent.asPolygon()[0])
196196

197197
# Test deleting a geometry that touches the bbox
@@ -200,7 +200,7 @@ def testGeopackageExtentUpdate(self):
200200
self.assertTrue(vl.commitChanges())
201201
reference = QgsGeometry.fromRect(QgsRectangle(0.5, 0.0, 1.0, 0.5))
202202
provider_extent = QgsGeometry.fromRect(vl.extent())
203-
self.assertTrue(QgsGeometry.comparePolylines(provider_extent.asPolygon()[0], reference.asPolygon()[0], 0.00001),
203+
self.assertTrue(QgsGeometry.compare(provider_extent.asPolygon()[0], reference.asPolygon()[0], 0.00001),
204204
provider_extent.asPolygon()[0])
205205

206206
def testSelectSubsetString(self):

0 commit comments

Comments
 (0)