[Qgis-developer] QgsPoint as a python list

Stefanie Tellex stefie10 at media.mit.edu
Tue Jun 3 08:54:33 EDT 2008


This patch (against r8576) extends the QgsPoint python interface so it 
can act like a  two element immutable python list.  This makes idioms 
the following possible:

x, y = point

I also write a lot of code that assumes points are tuples, and with this
patch I can pass QgsPoints to those methods without any conversion
code.

Stefanie

(PS:  Sorry for sending all these patches at once.  I had a problem
applying my patch stream to the latest revision of qgis and I was too
busy to fix it for a while... )
-------------- next part --------------
Index: qgis/qgis_unstable/python/core/qgspoint.sip
===================================================================
--- qgis.orig/qgis_unstable/python/core/qgspoint.sip	2008-06-02 12:54:53.000000000 -0400
+++ qgis/qgis_unstable/python/core/qgspoint.sip	2008-06-02 12:57:20.000000000 -0400
@@ -82,6 +82,39 @@
     sipRes = PyString_FromString(str.toLocal8Bit().data());
 %End
 
+  int __len__();
+%MethodCode
+    sipRes = 2;
+%End
+
+
+  SIP_PYOBJECT __getitem__(int);
+%MethodCode    
+    if (a0 == 0) {
+        sipRes = Py_BuildValue("d",sipCpp->x());
+    } else if (a0 == 1) {
+        sipRes = Py_BuildValue("d",sipCpp->y());
+    } else {
+        QString msg = QString("Bad index: %1").arg(a0);
+        PyErr_SetString(PyExc_IndexError, msg.toAscii().constData());
+    }
+%End
+
+  void __setitem__(int, double);
+%MethodCode    
+    if (a0 == 0) {
+        sipCpp->setX(a1);
+    } else if (a0 == 1) {
+        sipCpp->setY(a1);
+    } else {
+        QString msg = QString("Bad index: %1").arg(a0);
+        PyErr_SetString(PyExc_IndexError, msg.toAscii().constData());
+    }
+%End
+
+
+
+
   
 }; // class QgsPOint
 



More information about the Qgis-developer mailing list