Skip to content

Commit d8bdcb2

Browse files
committed
add python binding to QgsVectorDataProvider::fieldNameMap()
1 parent 6f2ade7 commit d8bdcb2

File tree

2 files changed

+106
-0
lines changed

2 files changed

+106
-0
lines changed

python/core/conversions.sip

+103
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,109 @@ template<TYPE>
820820
%End
821821
};
822822

823+
%MappedType QMap<QString, int>
824+
{
825+
%TypeHeaderCode
826+
#include <QMap>
827+
#if (SIP_VERSION >= 0x040900)
828+
#define sipClass_QString ((sipWrapperType *) sipTypeAsPyTypeObject (sipType_QString))
829+
#endif
830+
%End
831+
832+
%ConvertFromTypeCode
833+
// Create the dictionary.
834+
PyObject *d = PyDict_New();
835+
836+
if (!d)
837+
return NULL;
838+
839+
// Set the dictionary elements.
840+
QMap<QString, int>::const_iterator i = sipCpp->constBegin();
841+
842+
while (i != sipCpp->constEnd())
843+
{
844+
QString *t1 = new QString(i.key());
845+
846+
PyObject *t1obj = sipConvertFromNewInstance(t1, sipClass_QString, sipTransferObj);
847+
PyObject *t2obj = PyInt_FromLong( (long) i.value() );
848+
849+
if (t1obj == NULL || t2obj == NULL || PyDict_SetItem(d, t1obj, t2obj) < 0)
850+
{
851+
Py_DECREF(d);
852+
853+
if (t1obj) {
854+
Py_DECREF(t1obj);
855+
} else {
856+
delete t1;
857+
}
858+
859+
if (t2obj) {
860+
Py_DECREF(t2obj);
861+
}
862+
863+
return NULL;
864+
}
865+
866+
Py_DECREF(t1obj);
867+
Py_DECREF(t2obj);
868+
869+
++i;
870+
}
871+
872+
return d;
873+
%End
874+
875+
%ConvertToTypeCode
876+
PyObject *t1obj, *t2obj;
877+
#if PY_VERSION_HEX >= 0x02050000
878+
Py_ssize_t i = 0;
879+
#else
880+
int i = 0;
881+
#endif
882+
883+
884+
// Check the type if that is all that is required.
885+
if (sipIsErr == NULL)
886+
{
887+
if (!PyDict_Check(sipPy))
888+
return 0;
889+
890+
while (PyDict_Next(sipPy, &i, &t1obj, &t2obj))
891+
{
892+
if (!sipCanConvertToInstance(t1obj, sipClass_QString, SIP_NOT_NONE))
893+
return 0;
894+
}
895+
896+
return 1;
897+
}
898+
899+
QMap<QString, int> *qm = new QMap<QString, int>;
900+
901+
while (PyDict_Next(sipPy, &i, &t1obj, &t2obj))
902+
{
903+
int state;
904+
905+
QString *t1 = reinterpret_cast<QString *>(sipConvertToInstance(t1obj, sipClass_QString, sipTransferObj, SIP_NOT_NONE, &state, sipIsErr));
906+
int t2 = PyInt_AsLong(t1obj);
907+
908+
if (*sipIsErr)
909+
{
910+
sipReleaseInstance(t1, sipClass_QString, state);
911+
delete qm;
912+
return 0;
913+
}
914+
915+
qm->insert(*t1, t2);
916+
917+
sipReleaseInstance(t1, sipClass_QString, state);
918+
}
919+
920+
*sipCppPtr = qm;
921+
922+
return sipGetState(sipTransferObj);
923+
%End
924+
};
925+
823926
template<TYPE1, TYPE2>
824927
%MappedType QMap<TYPE1, TYPE2*>
825928
{

python/core/qgsvectordataprovider.sip

+3
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@ class QgsVectorDataProvider : QgsDataProvider
261261
* Returns the index of a field name or -1 if the field does not exist
262262
*/
263263
int fieldNameIndex(const QString& fieldName) const;
264+
265+
/**Return a map where the key is the name of the field and the value is its index*/
266+
QMap<QString, int> fieldNameMap() const;
264267

265268
/**
266269
* Return list of indexes to fetch all attributes in nextFeature()

0 commit comments

Comments
 (0)