Thursday, 25 January 2018

How to run GDAL vector (OGR) tools from inside QGIS?


I want fingertip access to the GDAL/OGR tools, particularly ogrinfo, from the main QGIS screen.


I currently run ogrinfo.exe in a DOS shell but this seems clumsy.


Installing the GDAL Tools plugin only accesses the raster tools. (The User Guide confirms this.)


But in my Windows QGIS folder there is a file:


 C:\Program Files\QGIS283\apps\qgis-ltr\python\plugins\processing\algs\gdal\ogrinfo.py

Does this perhaps somehow let me run ogrinfo from the Python console?



Answer




Assuming you downloaded and installed QGIS through the OSGeo4W Network Installer you will have access to the QGIS Processing Framework. Among other algorithm providers this will provide you access to the GDAL/OGR Provider which includes (under OGR miscellaneous) the Information tool (which is ogrinfo). The results will be output to a temporary HTML file.


If you do want to access it from the Python console you can run the algorithm from there as well, for example:


>>> lyr = iface.activeLayer()
>>> import processing
>>> processing.runalg('gdalogr:information', lyr, None)
{'OUTPUT': u'/var/folders/pd/ydjvghhj0xx7jr0_0bzz85n80000gn/T/processing/6feb7dc9a3894ed5a5276a3d48f94180/OUTPUT.html'}

Note that you still have to open the HTML file yourself, so it may be easier to just use the processing GUI. Or you could try:


>>> import os
>>> os.startfile(processing.runalg('gdalogr:information', lyr, None)["OUTPUT"])

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...