Showing posts with label qgis. Show all posts
Showing posts with label qgis. Show all posts

Friday, February 17, 2017

PyQGIS - Screen Capture as Image with coordinates


To capture the current screen extents, including a world file, use this command in PyQGIS:

qgis.utils.iface.mapCanvas().saveAsImage(fileName)

where 'fileName' is que path and fileName you want. It will be a .PNG file

Then, you can use some more code to get this fileName from a Open File Window, as below.

from PyQt4.QtGui import QFileDialog

projP = QgsProject.instance().readPath("./")
fileN =QFileDialog.getSaveFileName(None, "Save Image as:",projP,"Image Files (*.png)")
print 'file=',fileN
if fileN!='':
    qgis.utils.iface.mapCanvas().saveAsImage(fileN)




Thursday, November 17, 2016

Drawing Coordinates as Line in QGIS - PyQGIS

In this post I'll show some code for drawing a line from a list of coordinates, and saving it in a temporary vector layer.

You can implement the code below to get the coordinates from a text file, clipboard, or any user input.

Below are two functions that, called with the layer name and list of points as arguments, build a Line or Point temporary layer.

+1 / share if you like the post and the blog!

-----------


def createLineLay(name,lstP):
    vl = QgsVectorLayer("Linestring", name, "memory")
    #pr = vl.dataProvider()
    vl.startEditing()
    vl.addAttribute(QgsField("id", QVariant.Int))
    vl.updateFields()
    fet = QgsFeature(vl.pendingFields())
    lstPP = []
    for p in lstP:
        lstPP.append(QgsPoint(float(p[0]),float(p[1])))
    lstPP.append(QgsPoint(float(lstP[0][0]),float(lstP[0][1])))
    fet.setGeometry(QgsGeometry.fromPolyline(lstPP))
    fields = vl.pendingFields()
    fet.setFields( fields, True )
    fet['id'] = 0
    pr = vl.dataProvider()
    pr.addFeatures( [ fet ] )
    vl.commitChanges()
    QgsMapLayerRegistry.instance().addMapLayer(vl)    

def createLayPts(name, lstP):
    vl = QgsVectorLayer("Point", name, "memory")
    #pr = vl.dataProvider()
    vl.startEditing()
    vl.addAttribute(QgsField("ID", QVariant.Int))
    vl.addAttribute(QgsField("X", QVariant.Double))
    vl.addAttribute(QgsField("Y", QVariant.Double))
    vl.updateFields()
    idP = 0
    for p in lstP:
        fet = QgsFeature(vl.pendingFields())
        fet.setGeometry(QgsGeometry.fromPoint(QgsPoint(float(p[0]),float(p[1]))))
        fields = vl.pendingFields()
        fet.setFields( fields, True )
        fet["ID"] = idP
        fet['X'] = p[0]
        fet['Y'] = p[1]
        pr = vl.dataProvider()
        pr.addFeatures( [ fet ] )
        vl.commitChanges()
        idP +=1
    QgsMapLayerRegistry.instance().addMapLayer(vl)

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.

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.


Tuesday, October 27, 2015

How to Use Python

In this post I will try to explain how I started using PYTHON to solve everyday work needs.

You can go to Python Official site - https://www.python.org/ - and dowload the latest packages for you operating system:
https://www.python.org/downloads/

----

However, some programs already come with a python console included - that is the case of QGIS (Quantum GIS). That is how I started to code in python - with QGIS -  http://www.qgis.org/en/site/

QGIS is a Open-Source desktop GIS - it is a computer software which is really very useful for hydrologists and hydraulic engineers. And it is continuosly evolving, getting better in every new version. At the time of thise post, the current version is the 2.12 - Lyon.

QGIS has depository of plugins - users make their own plugins and share with the community. And these plugins are all python coded.

QGIS comes with a python console. And with a code editor. And there is where the show begins!!

Upcoming posts will show reference material, screenshots, and a lot of examples.

Thanks for visiting.