Wednesday, November 25, 2015

Plotting Charts with PyQGIS

In this post, I will show an example of how to plot a simple line graph in PyQGIS, with matplotlib library. This library is already included in PyQGIS.

The example code is below.

import matplotlib.pyplot as plt



# x and y data as same length lists

radius = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0]

area = [3.14159, 12.56636, 28.27431, 50.26544, 78.53975, 113.09724]



plt.plot(radius, area)

plt.xlabel('Radius')

plt.ylabel('Area')

plt.title('Area of a Circle')

# show grid lines

ax = plt.axes()

ax.grid(True)

# show plot window

plt.show()

Monday, November 23, 2015

Asking for User Input

In QGIS python (PyQGIS), the python comman raw_input() does not work as expected. And it will be not very practical to enter any input in Python Console.

One good alternative is to use "Qdialog". It is a module of PyQt (PyQt4.QtGui) library.
It can show a window asking for input, or asking to choose a file or folder.

We have to import it:

from PyQt4.QtGui import QFileDialog, QInputDialog, QMessageBox

QfileDialog - Window to select files or folders. It can be getOpenFileName - for opening files, getSaveFileName, for saving files, getExistingDirectory to select a folder. Example code for these types is shown below.

fileOpen = QFileDialog.getOpenFileName(None,'Select File:', 'c:/' , '.xls (*.xls)')

saveFileName = QtGui.QFileDialog.getSaveFileName(self, 'Dialog Title', '/path/to/default/directory', selectedFilter='*.txt')

folderSel = QFileDialog.getExistingDirectory(None,"Select Folder: ", 'c:/')


QInputDialog - customizable window. An example is shown below, showing a function that calls a customizable input window with defined width and height:

def windowSize(title, question, widthW, heightW):
    winD = QInputDialog(iface.mainWindow())
    winD.setInputMode(QInputDialog.TextInput)
    winD.setWindowTitle(title);
    winD.setLabelText(question)
    winD.resize(widthW,heightW)
    ok = winD.exec_()
    textInput = winD.textValue()
    return textInput

QMessageBox - It shows only a MessageBox With some information you want. Example:

QMessageBox.information(iface.mainWindow(), "Title", "Message")


Monday, November 16, 2015

Using the Windows Clipboard

The Windows Clipboard may be very useful for transferring information for processing in quick python scripts.

To this purpose, we are going to use the "win32clipboard" library, included in python distribution.

First, we have to call the library, as usual:

import win32clipboard

Then, in the code, we have to open the Clipboard:

win32clipboard.OpenClipboard()

Once opened, we can retrieve information that is in the Clipboard, copy information to it, or clear all contentents.

To get information in Clipboard, and store its information in a variable:

clipbVar = win32clipboard.GetClipboardData()

To set it to any value:

win32clipboard.SetClipboardText('testing 123')

To clear the Clipboard:

win32clipboard.EmptyClipboard()

After using the Clipboard, we have to close it, as following:

win32clipboard.CloseClipboard()


Friday, November 13, 2015

Reading Text files in Python

This is a basic funcionality, but very useful for many applications.

It opens a pure text file. You do not have to import any extra library.

The main sintax for opening files is:

file_object = open(file_name [, access_mode][, buffering])

where:
file_object -> variable
file_name -> the file you want to open
access_mode -> 'r' for read, 'rw' for read and wite text files
buffering -> size of data you read from file. If you leave this blank, it uses the system default - recommended.

To read only text files, it becomes:

file = open('yourfile.txt', 'r')

'yourfile.txt' must contain the correct folder address to it.
'r' stands for read only.

If you want all the contents of your file in one string, you can call read() method:

print file.read()

A good way to deal with text files is to turn the text into a list, each line as an list element. To do this, use the readlines() method:

lstLines = file.readlines()

After all the readings and processings you want to do with the file, you have to close it as following:

file.close()

Wednesday, November 11, 2015

How to use active layer in QGIS python - PyQGIS

Just use the activeLayer() as in the following code.


mylayer = qgis.utils.iface.activeLayer()
print mylayer.name(), " - ",mylayer

Monday, November 9, 2015

Step by Step - Making QGIS Python Plugins

Plugin QGIS

1. Download Plugin Builder

2. Fill in all fields. Pay attention to the tips shown when you put the mouse pointer over the field.


3. After finishing, choose the folder where the plugin folder will be created.

4. Open QTDESIGNER. It is included with QGIS. Filename is "designer.exe";

5. Make the Graphical Layout of your plugin, and save it;

6. In QGIS: Download the plugin named "PLUGIN RELOAD" - as it is experimental, you have to set your QGIS to list experimental plugins;

7. Edit your source code to do whatever you want - edit the code named “YOURPLUGINNAME”.py

8. After you finish any modifications in your graphical layout or source code, save it, and then execute the PLUGIN RELOAD plugin.

9. Your Plugin is ready.

Sunday, November 8, 2015

SWMM5 in Python !

Do you use SWMM5?

Have you already wanted some nicer output?
Or some kind of presentation of results that the programs does not fit you?

So, you might be interested in the SWMM5 package for python

https://pypi.python.org/pypi/SWMM5

It gathers all information from .INP files, letting you to play with all the components and the results.

How to install:

If you have already installed pip , just open OSGEO4W and type:

pip install SWMM5

If QGIS is opened, you will have to restart it.

Documentation and examples are included in the website given.

Friday, November 6, 2015

Installing pip in PyQGIS

"pip" is a package management system used to install and manage software packages written in Python. Many packages can be found in the Python Package Index (PyPI).

Many modules are installed through "pip", so as the SWMM5 module.

Installing pip in QGIS - python - PyQgis - OSGeo4w

1 - Download pip - https://bootstrap.pypa.io/get-pip.py ;
2 - Open OSGeo4W;
3 - go to the get-pip.py folder and type:
python get-pip.py

Wednesday, November 4, 2015

How to Install New Python Modules / Packages in QGIS - method #1



Steps:


1 - copy source folder to C:\Program Files\QGIS Wien\apps\Python27\Lib\site-packages

2 - Open OSGeo4W Shell; go to unpacked folder, where setup.py lies - (>> cd "C:\Program Files\QGIS Wien\apps\Python27\Lib\site-packages")

3 - Type: >> python setup.py install



-----


If it shows errors regarding setuptools: you will have to install setuptools.


https://pypi.python.org/packages/source/s/setuptools/setuptools-18.4.zip#md5=38d5cd321ca9de2cdb1dafcac4cb7007


Download setuptools package, then repeat the same steps for installation.

Ps: in my opinion, this is the worst method to install modules. Next post I will tell how to use "pip" with QGIS Python.


Monday, November 2, 2015

"Hello World" in QGIS python

Steps:

1 - open code editor as described in last post
2 - copy following code in python editor window:

print 'HELLO WORLD!'

3 - press the save script button - and save it in a proper folder;
Save Button Location

4 - press the 'Run Script' Button;

Run Script Location


5 - See the result in the Console.

Result


Please leave comments and opinions.