Saturday 21 November 2015

Adding a Python module to QGIS Python in Windows with multiple Python installations

Both Python and (if I rememeber well) QGIS were originally created for Linux. Their use in Windows is somewhat complicated by the difficulties to adapt these tools to a different environment.
When working with an installation of QGIS in Windows created with the standalone installer, one may need to use Python modules that are not present in the default QGIS installation. Modules are generally installed in the folder ..\Python27\Lib\site-packages.
The installation can be confusing if you have also other Python installations, for instance one derived from the Python(x,y) distribution, from Anaconda or from ArcGis.
The QGIS Python installation from the standalone GGIS installer is not system-wide, and QGIS Python is separated and independent from the other Pythons.
After a bit of struggling, I found a way to install a Python module from the source files (tar.gz) in QGIS Python.

Source module

The Python module that I wanted to install is pyserial, that I downloaded as a tar.gz file from https://pypi.python.org/pypi/pyserial.
I uncompressed it in a temp folder, in my particular case:
C:\Temp\dist\pyserial-2.7\pyserial-2.7
This folder contains the required setup.py file, plus some other stuff.

Target QGIS installation 

The target QGIS installation is 2.10.1 Pisa 32bit (from standalone installer), with installation folder:
C:\Program Files (x86)\QGIS Pisa

In QGIS Python, the path to the standard Python module folder is:
..\QGIS Pisa\apps\Python27\Lib\site-packages.
Installing a module in this last directory, we have it recognized by QGIS Python.

Prerequisites

As a prerequisite, you have to be sure to launch from the DOS shell the QGIS Python and not other Python interpreters. To achieve that, one possible (but there are others) way is to add the path to the QGIS python.exe file as the first item in the system PATH.
In Windows 8, follow Control Panel -> System and Security -> System and then , by opening Advanced system settings, modify the Path variable (user or system are equivalent ways, I suppose, in my case I had the system one modified) by adding as the first voice:
C:\Program Files (x86)\QGIS Pisa\bin
i.e., the bin folder of the QGIS installation, where python.exe is contained.

Procedure

Being sure that from the DOS shell you are invoking the QGIS Python, you can then proceed as follows:

1) launch an administrator dos shell, to be sure to have write permissions in the target directory

2) in the dos shell, change directory to the uncompressed folder of the new module to install, (it should contain the setup.py file), in my case with the command:
  cd C:\Temp\dist\pyserial-2.7\pyserial-2.7

3) always in the dos shell, run the python setup.py install command on the module, in my case:
  python setup.py install --prefix="\Program Files (x86)\QGIS Pisa\apps\Python27"
Change path in the prefix option accordingly to your particular QGIS installation. Note two things:
  • do not put the drive character at the beginning of the prefix path; 
  • point to the Python27 folder, not to the  Python27\Lib\site-packages


Checking the results

In addition to the installation messages in the DOS shell, that should present no errors, you may then check that the module installation was successful by trying to import the specific module in the QGIS Python console, in my case:
import serial
You should receive no import error messages..



Monday 6 April 2015

beePen: a tool for drawing freehand annotations in QGis

beePen is an experimental Python plugin for drawing freeand annotations and sketches in an ad-hoc layer in QGis. It is inspired by the corresponding tool in BeeGIS and re-use the code by Pavol Kapusta in his Frehand Editing plugin.

beePen allows to create an annotation layer, that is characterized by three fields, storing the user-defined width, color and transparency of the pen. These value are automatically inserted based on the user choice in the plugin window (Fig. 1).

Fig. 1

To draw annotations, first you have to open the beePen window with the "bee" command (see Fig. 2, second icon from the right), eventually create a new annotation layer from the beePen window (see Fig. 1) or use an old one, put in edit mode using the standard tool of QGis (command at the left in Fig. 2) and then draw after selecting the pen tool (rightmost command in Fig. 2).

Fig. 2

The conception of beePen is by Mauro Dedonatis (Urbino Univ., Italy), while the implementation is by Mauro Alberti.



Monday 9 March 2015

Some minor enhancements in qProf

qProf is a QGIS plugin for the creation of topographic and geologic profiles. Some minor enhancements were added to qProf in the new, experimental releases 0.2.9 and 0.3.0.

In version 0.2.9 the following options were added:
- the calculation of the absolute slope along the profile; previously only the directional slope was calculated;
- the possibility to flip a profile horizontally (option "Reverse profile direction"), as well as to reverse the orientation of the x axis (option "Reverse x axes direction) were added.

See examples in Figs. 1-4 (below).


Fig. 1. Map of Mt. Alpi zone (Basilicata, Southern Italy), with profile in red (direction from left to right). DEM data: TINITALY.
Fig. 2. Topographic (top) and absolute slope (bottom) profiles (see map trace in Fig. 1).

Fig. 3. Same profile as in Fig. 2, but with profile direction reversed.

Fig. 4. Same profile as in previous figures, but with reversed x axes directions.

In version 0.3.0, released on 2015-03-08, the possibility of configuring the figure save parameters (width, resolution, font size, subplot configuration parameters) was added. These graphic parameters can be saved in a text file and subsequently loaded for being applied to future plots.
This option is available from Export - Figure. Fig. 5 represents the window for defining the parameters, saving or loading them, and also for saving the figure as a file in PDF, SVG or TIF formats.

Fig. 5. Window for saving a figure as a graphic file, and for configuring graphic parameters, and saving or loading them.


The two versions are experimental since the impact of reversing the profile on the geological plotting has not been fully tested. For geological plots, the suggested version is 0.2.8.

Gerrit Tombrink, PhD student at the Goettingen University (Germany), is thanked for proposing the implemented options.

Saturday 3 January 2015

Cross-section intersections of line and polygon layers with qProf in QGis

A new release of qProf, plugin for QGis, allows to determine the intersections of line and polygon layers, representing for instance faults and geological outcrops, on a vertical cross-section.
These new functionalities complement those already available for projecting geological attitudes and lines on a cross-section.
The resulting data can be saved as shapefiles and csv.

Example of cross-section representing geological polygon layer intersections with a topographic profile.

The plugin can be installed/updated via the plugin manager of QGis. Alternatively it can be downloaded from the QGis plugin repository:
http://plugins.qgis.org/plugins/qProf/
or from the Bitbucket repository:
https://bitbucket.org/mauroalberti/qprof/downloads