Delete all items in list

Thursday, November 6, 2014

To delete items in a list:
listname[:] = []

Example:
>>fruit = ['apple', 'orange','lemon']
>>print fruit
['apple', 'orange', 'lemon']
>>len(fruit)
3
>>fruit[:]=[]
>>print fruit
[]
>>len(fruit)
0

PyQt custom dialog return value after buttonBox click

Monday, October 20, 2014

Task: Open a custom diaglog to let user input necessary info. After "OK" buttonBox clicked, return values to current GUI

1. First you have to built for custom dialog with QtCreater
2. In your GUI, write the following code
from Inputfiles_Dialog import Inputfiles_Dialog
#Inputfiles_Dialog is the ui created by QtCreater
d = Inputfiles_Dialog(self.iface)
d.show()
if d.exec_():
    self.Inputfiles_set = d.getInputs()

3. In your custom dialg,connect your buttonBox:
self.connect(self.OK_buttonBox, SIGNAL("accepted()"),self.ProcessInfo)
.
.
.
def ProcessInfo(self):
    #do somthing
.
.
.
    self.accept()
4. In your custom dialg, add a function to return values:
def getInputs(self):
    return usefulinfo

Python file exist

Monday, October 31, 2011

Just provide a quick reference to myself. I forget the way of checking file exist from time to time.

>>> import os
>>> os.path.exists('c:\\abc\\file.txt')
True
>>>


>>> import os
>>> os.path.isfile('c:\\abc\\file.txt')
True
>>>

Create Spatialite database in QGis

Wednesday, July 6, 2011

Create Spatialite DB in QGis with Python is quite easy.

http://www.gaia-gis.it/spatialite-2.4.0-4/spatialite-cookbook/html/python.html has a very good tutorial.

Some remarks:
1. Initiality I use QGis 1.6 Standalone version, the spatilite version is 2.3.x. We need to add srid info into spatial_ref_sys manually.

2. Sample codes in QGis 1.6 plugin:
from pyspatialite import dbapi2 as sqlite3
dbName = str(self.txtOutputDB.text()) #textOutputDB is a lineEdit object(PyQt)

#if os.path.exists(dbName) :
ocon = sqlite3.connect(dbName)
ocur = ocon.cursor()

sql = 'SELECT InitSpatialMetadata()'
ocur.execute(sql)
#add projection information
sql = "INSERT INTO spatial_ref_sys (srid, auth_name, auth_srid, ref_sys_name, proj4text) VALUES (4326, 'epsg', 4326, 'WGS 84', '+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs');"
ocur.execute(sql)


You can get the INSERT code from the following link:
http://www.gaia-gis.it/spatialite/init_spatialite-2.3.zip

3. After I switch to QGis 1.7, the spatialite version is updated, the projection info will be automatically created. However, an empty Spatialite database will have 3MB in size as there are many projection information in spatial_ref_sys table.

4. To reduce the size, I just keep several rows and delete unwanted srid in spatial_ref_sys table:

#delete unnecessary spatial_ref_sys
sql = 'DELETE FROM "spatial_ref_sys" where srid!= 2326 and srid!= 4326'
ocur.execute(sql)
sql = 'VACUUM'
ocur.execute(sql)

Python strtok equvialent

Thursday, June 2, 2011

C programmers usually use strtok to perform operation on string with standing patterns. In Python, we can use re.split to get similar result.

Example:

>>> import re
>>> st = "9394 811 813 815"
>>> result = [str(x) for x in filter(None, re.split('[ ,\n,\t]',st))]
>>> print result
['9394', '811', '813', '815']
>>>

Python read binary file

Wednesday, June 1, 2011

To read binary file,
fp = open(ifile,"rb")

To read 1 byte from fp,
byte = fp.read(1)

To read 30 bytes from fp,
block = fp.read(30)
block = fp.read(30)

To read the bytes from a block of binary data,
(Year,) = struct.unpack('b',block[3:4])
(Month,) = struct.unpack('b',block[4:5])
(Day,) = struct.unpack('b',block[5:6])
(Hour,) = struct.unpack('b',block[6:7])
(Minute,) = struct.unpack('b',block[7:8])
(Second,) = struct.unpack('b',block[8:9])
recdate = str(Year) + '-' + str(Month) + '-' + str(Day) + ' ' + str(Hour) + ':' + str(Minute)

You should include "import struct" so that you can use the struct module.

To read text from binary data:
(Cellname,) = struct.unpack('8s',blk[2:10])

The format characters are repeated here for quick reference.
Format C Type Python type Standard size Notes
x pad byte no value
c char string of length 1 1
b signed char integer 1 (3)
B unsigned char integer 1 (3)
? _Bool bool 1 (1)
h short integer 2 (3)
H unsigned short integer 2 (3)
i int integer 4 (3)
I unsigned int integer 4 (3)
l long integer 4 (3)
L unsigned long integer 4 (3)
q long long integer 8 (2), (3)
Q unsigned long
long
integer 8 (2), (3)
f float float 4 (4)
d double float 8 (4)
s char[] string
p char[] string
P void * integer (5), (3)

More info of using struct can be found here:
http://docs.python.org/library/struct.html

Python Bitwise Operators:

Bitwise operator works on bits and perform bit by bit operation.
Assume if a = 60; and b = 13; Now in binary format they will be as follows:
a = 0011 1100
b = 0000 1101
-----------------
a&b = 0000 1100

a|b = 0011 1101
a^b = 0011 0001
~a  = 1100 0011
There are following Bitwise operators supported by Python language
OperatorDescriptionExample
&Binary AND Operator copies a bit to the result if it exists in both operands. (a & b) will give 12 which is 0000 1100
|Binary OR Operator copies a bit if it exists in eather operand. (a | b) will give 61 which is 0011 1101
^Binary XOR Operator copies the bit if it is set in one operand but not both. (a ^ b) will give 49 which is 0011 0001
~Binary Ones Complement Operator is unary and has the efect of 'flipping' bits. (~a ) will give -60 which is 1100 0011
<<Binary Left Shift Operator. The left operands value is moved left by the number of bits specified by the right operand. a << 2 will give 240 which is 1111 0000
>>Binary Right Shift Operator. The left operands value is moved right by the number of bits specified by the right operand. a >> 2 will give 15 which is 0000 1111

Install 3rd party Python packages into QGIS

Thursday, March 24, 2011

When you write plug-in for QGIS, you may need to rely on additional python packages to work with. However, I find no clue from qgis.org and forum search. After trial and error, here is the approach I used to install 3rd party python packages to QGIS.

Configuration

In my Windows XP, I have 2 copy of Python 2.5.2. One is the original stock version downloaded from www.python.org and installed at c:\python25. Another of course is the one come with QGIS. If you issue
import sys
sys.path

in stock Python console and QGIS Python console respectively, you will get two different results:

Result from stock Python console:
>>> import sys
>>> sys.path
['', 'C:\\windows\\system32\\python25.zip', 'c:\\python25\\DLLs', 'c:\\python25\\lib', 'c:\\python25
\\lib\\plat-win', 'c:\\python25\\lib\\lib-tk', 'c:\\python25', 'c:\\python25\\lib\\site-packages']
Result from QGIS Python Console:
>>> import sys
>>> sys.path
['C:/PROGRA~1/QUANTU~1/apps/qgis/./python', 'C:/Documents and Settings/19/.qgis/python', 'C:/Documents and Settings/19/.qgis/python/plugins', 'C:/PROGRA~1/QUANTU~1/apps/qgis/./python/plugins', 'C:\\PROGRA~1\\QUANTU~1\\apps\\gdal-17\\pymod', 'C:\\Program Files\\Quantum GIS Copiapo', 'C:\\PROGRA~1\\QUANTU~1\\bin\\python25.zip', 'C:\\PROGRA~1\\QUANTU~1\\apps\\Python25\\DLLs', 'C:\\PROGRA~1\\QUANTU~1\\apps\\Python25\\lib', 'C:\\PROGRA~1\\QUANTU~1\\apps\\Python25\\lib\\plat-win', 'C:\\PROGRA~1\\QUANTU~1\\apps\\Python25\\lib\\lib-tk', 'C:\\PROGRA~1\\QUANTU~1\\apps\\qgis\\bin', 'C:\\PROGRA~1\\QUANTU~1\\apps\\Python25', 'C:\\PROGRA~1\\QUANTU~1\\apps\\Python25\\lib\\site-packages', 'C:\\PROGRA~1\\QUANTU~1\\apps\\Python25\\lib\\site-packages\\win32', 'C:\\PROGRA~1\\QUANTU~1\\apps\\Python25\\lib\\site-packages\\win32\\lib', 'C:\\PROGRA~1\\QUANTU~1\\apps\\Python25\\lib\\site-packages\\Pythonwin', 'C:\\PROGRA~1\\QUANTU~1\\apps\\Python25\\lib\\site-packages\\wx-2.8-msw-unicode', 'C:\\PROGRA~1\\QUANTU~1\\apps\\qgis\\python\\plugins\\fTools\\tools']

The above info tell us that Python package install in c:\python25 cannot be shared by Python in QGIS.

easy_install

If you want to install easy_install into QGIS, the setuptool will just tell you that it can only find one copy of Python in c:\python25 in registry:


To let setuptools find QGIS's python copy, go to Registry and look for HKEY_LOCAL_MACHINE\SOFTWARE\Python and export the selected branch to python25.reg beofore any modification. The exported python25.reg will look like this:

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Python]

[HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore]

[HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.5]

[HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.5\Help]

[HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.5\Help\Main Python Documentation]
@="C:\\Python25\\Doc\\Python25.chm"

[HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.5\InstallPath]
@="C:\\Python25\\"

[HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.5\InstallPath\InstallGroup]
@="Python 2.5"

[HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.5\Modules]

[HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\2.5\PythonPath]
@="C:\\Python25\\Lib;C:\\Python25\\DLLs;C:\\Python25\\Lib\\lib-tk"

In the registry, there is only information of the Python installed in c:\python25 folder. Select HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonPath and change the path to QGIS one. That is:

replace c:\python25 to C:\Program Files\Quantum GIS Copiapo\apps\Python25

After change, export the Python branch again and save it as qgispython.reg for future use.

Also, you also need to add C:\Program Files\Quantum GIS Copiapo\bin and C:\Program Files\Quantum GIS Copiapo\apps\Python25 into PATH. I removed c:\python25 from the PATH when I add the above items into PATH. After installation, I revert the change by adding back c:\python25 and remove the two items.

If you haven't install MinGW, install it and at c:\mingw\bin into PATH. You may also need to tweak the cgwincompiler. Check here for detail.

After the above setups, setuptools should be able to install in QGIS folder:


However, if go to C:\Program Files\Quantum GIS Copiapo\apps\Python25\Scripts and execute easy_install, you will get no responds. The program just return nothing. In this case, you can use easy_install-script.py to install packages. For example, to install Jinja package,

C:\Program Files\Quantum GIS Copiapo\apps\Python25\Scripts>python easy_install-2.5-script.py pyodbc
Traceback (most recent call last):
File "easy_install-2.5-script.py", line 5, in
from pkg_resources import load_entry_point
ImportError: No module named pkg_resources

C:\Program Files\Quantum GIS Copiapo\apps\Python25\Scripts>python eas_install-script.py jinja
python: can't open file 'eas_install-script.py': [Errno 2] No such file or directory

C:\Program Files\Quantum GIS Copiapo\apps\Python25\Scripts>python easy_install-script.py jinja
Searching for jinja
Reading http://pypi.python.org/simple/jinja/
Reading http://wsgiarea.pocoo.org/jinja/
Reading http://jinja.pocoo.org/
Best match: Jinja 1.2
Downloading http://pypi.python.org/packages/2.5/J/Jinja/Jinja-1.2-py2.5-win32.egg#md5=27b0804a126c2c
0ebd4a9dacffb9dcbb
Processing Jinja-1.2-py2.5-win32.egg
creating c:\progra~1\quantu~1\apps\python25\lib\site-packages\Jinja-1.2-py2.5-win32.egg
Extracting Jinja-1.2-py2.5-win32.egg to c:\progra~1\quantu~1\apps\python25\lib\site-packages
Adding Jinja 1.2 to easy-install.pth file

Installed c:\progra~1\quantu~1\apps\python25\lib\site-packages\jinja-1.2-py2.5-win32.egg
Processing dependencies for jinja
Finished processing dependencies for jinja

If you can issue "import jinja" in QGIS Python console without error, it means the installation is done.

Hope the above can help.

p.s. after install the 3rd party package, I will revert the registry (by double click python25.reg saved before )and edit the PATH (add c:\python25 and remove QGIS entries)

Recent search find another approach:
http://forum.qgis.org/viewtopic.php?f=2&t=8201