[QGIS-Developer] OSGeo4W Gdal meta package and Python init

Axel Andersson axel.n.c.andersson at gmail.com
Mon Jan 7 08:58:57 PST 2019


Hi,

I have a similar question to this that was asked some time ago, or I want
to include gdals polygonize in my plugin to convert a raster to polygons.

As a first approach I added following code to the plugin (where "db" is a
class containing all information required to connect to a database, "s_tbl"
is the name of new table to create and "file_name_with_path" is a text
string with the path).

run_text = """gdal_polygonize.bat {file_w_path} -f PostgreSQL
PG:"host='{host}' port='{port}' dbname='{dbname}' user='{username}'
password=
'{password}'" {s_tbl}
    """.format(host=self.db.dbhost, port=self.db.dbport,
               dbname=self.db.dbname, username=self.db.dbuser,
               password=self.db.dbpass, s_tbl=self.s_tbl,
               file_w_path=self.file_name_with_path)
run = subprocess.Popen(run_text)
run.wait()

It works fine for me, but as was pointed out gdal_polygonize.bat is the
"python 2 solution".

To understand how to run the command properly with python3 I tried to
install a fresh copy of QGIS (via osgeo4w and only choose to install
qgis:desktop version 3.4.3-2) and where "gdal_polygonize.bat" isn't located
in my "bin folder", when I now tries to run the polygonize tool I get
following error:
"GDAL command output:

'gdal_polygonize.bat' is not recognized as an internal or external command,

operable program or batch file."


If I shall use GDAL functions in QGIS3 what would be the working solution
(that works on Windows, Linux and Mac)

Thanks,
Axel

On Thu, 27 Sep 2018 at 00:19, Jürgen E. Fischer <jef at norbit.de> wrote:

> Hi Idan,
>
> On Thu, 27. Sep 2018 at 00:53:32 +0300, Idan Miara wrote:
> > 1. As I'm targeting only Python3 (using python3-gdal-2.2.4-1.tar.bz2), I
> > was wandering if I need to provide also the Python27 for the gdal-python
> > scripts (gdal-python-2.2.4-2.tar.bz2) ?
> > Are they all the gdal-python scripts Python3 compatible? If so, why does
> > gdal-python-2.2.4-2.tar.bz2 include the Python27 libs/site-packages and
> not
> > in a separate file as for Python3?
>
> gdal-python is for python2
> python3-gdal for python3
>
> Which is also what you need to install to get the bindings and the required
> dependencies (except for optional parts a gdal-ecw, gdal-oracle etc.)
>
> > I understand it must be possible because somehow starting qgis-bin.exe
> > works (I guess it has to do with the qgis-bin.env file, but how does it
> > work?
>
> That is actually done by QGIS itself.  QGIS's postinstall runs the qgis*bat
> with --postinstall argument, which in turn will read the qgis*.vars file,
> which
> has a list of environment variables that are required to run qgis and dumps
> them to the qgis*.env file.  The latter is then used whenever the exe is
> started directly - for instance to make it pinnable (see
> src/app/mainwin.cpp).
>
> To enable python3 you need to call py3_env.bat.  That will also add the
> scripts
> directory of python3 to the path, which has the python3 versions of the
> gdal
> scripts.  However the wrapper batch files (eg.  gdal_merge.bat) are only
> generated in gdal-python and use python2.
>
>
> Jürgen
>
> --
> Jürgen E. Fischer           norBIT GmbH             Tel. +49-4931-918175-31
> Dipl.-Inf. (FH)             Rheinstraße 13          Fax. +49-4931-918175-50
> Software Engineer           D-26506 Norden
> http://www.norbit.de
> QGIS release manager (PSC)  Germany                    IRC: jef on FreeNode
> _______________________________________________
> QGIS-Developer mailing list
> QGIS-Developer at lists.osgeo.org
> List info: https://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-developer
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20190107/aa0279f2/attachment-0001.html>


More information about the QGIS-Developer mailing list