2

I was wondering if it's possible to use Python and QGIS out of QGIS environment (namely, avoiding the use of the console). I'm pretty new to QGIS, I've been always using ArcGIS 10.1. What I am looking for is a thing similar to that "arcpy" module, since I work with tons of data at a time. Moreover, I'd like to use a good IDE, any suggestion? I'm using qgis 1.8, python 2.7.2 32bit, windows 7 64bit. P.S.: I couldn't really come up with a solution, neither following the instructions found in http://www.qgis.org/pyqgis-cookbook/intro.html#python-applications. Hope someone will enlight me!

DavidF
  • 4,862
  • 1
  • 26
  • 32
umbe1987
  • 3,785
  • 2
  • 23
  • 56

2 Answers2

4

Under the hood QGIS uses GDAL/OGR for most of the functions. So, the Python API for GDAL would be the closest analogy to using ArcPY in a stand-alone situation.

You can use the installation that comes with QGIS or have a separate installation of Python and GDAL. Other addons that complete the 'package' I would include

The last two are particularly relevant for raster processing. Have a look here for a tutorial on stand-alone geoprocessing using GDAL. This will get you going.

MappaGnosis
  • 33,857
  • 2
  • 66
  • 129
  • Thank you so much, this is really helpful, think I'm going through all the documentations t learn more about how everything works! It's a bit old though, but it's superfine! – umbe1987 Jan 09 '13 at 09:45
2

Yes building a standalone application out of QGIS using Python and the QGIS libraries is possible.

I have some small, but hopefully growing, example set at https://github.com/NathanW2/pyqgis-playground

The main thing is having the path the libraries setup correctly. When I'm on windows I use something like this:

@ECHO OFF
set PYTHONPATH=C:\OSGeo4W\apps\qgis-dev\python
Set PATH=C:\OSGeo4W\apps\qgis-dev\bin;%PATH%
set QGISHOME=C:\OSGeo4W\apps\qgis-dev\

python yourfile.py

and I run it from within inside the OSGeo4W shell. You can test you have everything setup correctly by running the above set commands inside a OSGeo4W shell and then doing:

C:\python
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>>>> import qgis.core
>>>>>>

If it works you should get no errors.

Nathan W
  • 34,706
  • 5
  • 97
  • 148
  • I've been trying hard to set the system environment to point to the correct folders like you suggest, but still having troubles. When I try to import the "qgis.core" module, the SHELL tells me a DLL (which I suppose to be "qgis_core.dll") couldn't be load. I'm using a SHELL found in the installation of Python27. Guess if the ArcGIS references in the system environment might conflict with the QGIS ones... – umbe1987 Jan 09 '13 at 09:55
  • Try using the Python that comes with OSGeo4W it has everything setup – Nathan W Jan 09 '13 at 10:05
  • Ehy Nathan I came up with your page googling in the web (http://woostuff.wordpress.com/2011/09/27/generating-contours-using-gdal-via-shell-or-qgis/), really interesting! – umbe1987 Jan 09 '13 at 10:21
  • Howerver, could you please tell me what is the SHELL that comes with the installation of QGIS. I didn't use the OSGeo4W setup, but one that I found in the QGIS official website, so it might have installed different files than those you could have. I'd really like to solve the problem of the system environment setting, maybe if you want, you could have a look – umbe1987 Jan 09 '13 at 10:24
  • at my other question... http://gis.stackexchange.com/questions/45473/how-can-i-setup-python-to-recognize-qgis – umbe1987 Jan 09 '13 at 10:25
  • Any reason you want to use the standalone Python and not install the OSGeo4W version with QGIS and Python? – Nathan W Jan 09 '13 at 11:25
  • I was just use to use Pywin when working with ArcGIS. Now I found Mysys, but I really don't like the way it looks (it's a rude console, while I was looking for a better IDE where to use Python). I don't even know what kind of language it's working with... Anyway, I also tried to install QGIS through the OSGeo4W setup, but all the programs where missing references to the libs (everytime I tried to launch QGIS it was saying that it couldn't find some DLL, and neither importing all the DLLs from the main bin to the one of /apps/qgis/bin worked so far... – umbe1987 Jan 09 '13 at 13:00