You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Fix PyQGIS QgsLineString constructor only accepts lists of QgsPoint,
not QgsPointXY as indicated by the documentation
Also add support for constructing QgsLineString using arrays of
arrays of floats, given that we're having to hand-roll sip conversion
code anyway!
Now the following is supported:
line = QgsLineString([[1,2], [3,4], [5,6]])
which is much nicer and more "pythonic" then the explicit
QgsPoint/QgsPointXY sequences!
Fixes#43200
Z and M type will be set based on the type of the first point
38
-
in the vector.
36
+
Construct a linestring from a sequence of points (:py:class:`QgsPoint` objects, :py:class:`QgsPointXY` objects, or sequences of float values).
39
37
40
-
.. versionadded:: 3.0
38
+
The linestring Z and M type will be set based on the type of the first point in the sequence.
39
+
40
+
.. versionadded:: 3.20
41
+
%End
42
+
%MethodCode
43
+
if ( !PySequence_Check( a0 ) )
44
+
{
45
+
PyErr_SetString( PyExc_TypeError, QStringLiteral( "A sequence of QgsPoint, QgsPointXY or array of floats is expected" ).toUtf8().constData() );
46
+
sipIsErr = 1;
47
+
}
48
+
else
49
+
{
50
+
int state;
51
+
const int size = PySequence_Size( a0 );
52
+
QVector< double > xl;
53
+
QVector< double > yl;
54
+
bool hasZ = false;
55
+
QVector< double > zl;
56
+
bool hasM = false;
57
+
QVector< double > ml;
58
+
xl.reserve( size );
59
+
yl.reserve( size );
60
+
61
+
bool is25D = false;
62
+
63
+
sipIsErr = 0;
64
+
for ( int i = 0; i < size; ++i )
65
+
{
66
+
PyObject *value = PySequence_GetItem( a0, i );
67
+
if ( !value )
68
+
{
69
+
PyErr_SetString( PyExc_TypeError, QStringLiteral( "Invalid type at index %1." ).arg( i ) .toUtf8().constData() );
70
+
sipIsErr = 1;
71
+
break;
72
+
}
73
+
74
+
if ( PySequence_Check( value ) )
75
+
{
76
+
const int elementSize = PySequence_Size( value );
77
+
if ( elementSize < 2 || elementSize > 4 )
78
+
{
79
+
sipIsErr = 1;
80
+
PyErr_SetString( PyExc_TypeError, QStringLiteral( "Invalid sequence size at index %1. Expected an array of 2-4 float values, got %2." ).arg( i ).arg( elementSize ).toUtf8().constData() );
if ( i == 0 && p->wkbType() == QgsWkbTypes::Point25D )
192
+
is25D = true;
193
+
}
194
+
sipReleaseType( p, sipType_QgsPoint, state );
195
+
}
196
+
else
197
+
{
198
+
sipIsErr = 1;
199
+
}
200
+
201
+
Py_DECREF( value );
202
+
203
+
if ( sipIsErr )
204
+
{
205
+
// couldn't convert the sequence value to a QgsPoint or QgsPointXY
206
+
PyErr_SetString( PyExc_TypeError, QStringLiteral( "Invalid type at index %1. Expected QgsPoint, QgsPointXY or array of floats." ).arg( i ) .toUtf8().constData() );
0 commit comments