4

I've created a python standalone script to clip two shapefiles with qgis. I've installed qgis3 with OSgeo4W. The logic is as follows:

import qgis
from qgis.core import *
from qgis.core import QgsApplication
from qgis.core import QgsProject
from qgis.core import QgsProcessingFeedback
from qgis.core import QgsVectorLayer
from qgis.analysis import QgsNativeAlgorithms
import processing
from processing.core.Processing import Processing
path_qgis_installation = "C:/OSGeo4W64/apps/qgis"
QgsApplication.setPrefixPath(path_qgis_installation, True)
qgs = QgsApplication([], False)
qgs.initQgis()
Processing.initialize()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) 
input_file = QgsVectorLayer('.../file_name.shp')
overlay_file = QgsVectorLayer('.../file_name.shp')
output_fil = '.../file_name.shp'
processing.run(
 "native:clip", 
 {
 'INPUT':input_file,
 'OVERLAY':overlay_file,
 'OUTPUT':output_fil
 }
)
qgs.exitQgis()

My problem is that when I run the command (only the processing.run()) in the python console in qgis or in the osgeo shell, everything works.

When I however run it with my python console (bat script), I only get the following (corrupted) files written to disc: .dbf, .shp, .shp. Missing are .prj and .shx for a complete shapefile.

My suspition is that is has something to do with the paths. At one point I executed C:/OSGeo4W64/bin/o4w_env.bat from a location outside of C:/OSGeo464) which then even procued the same problem whilst running on the osgeo shell.

Any suggestion what I am missing to load?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jul 23, 2019 at 14:59
1

1 Answer 1

2

Thank you @Fran Raga. I was able to solve the issue based on your reference question. For full disclosure, my working code looks like this.

set OSGEO4W_ROOT=C:/OSGeo4W64
call "%OSGEO4W_ROOT%/bin/o4w_env.bat"
call "%OSGEO4W_ROOT%/bin/qt5_env.bat"
call "%OSGEO4W_ROOT%/bin/py3_env.bat"
path %OSGEO4W_ROOT%/apps/qgis/bin;%PATH%
set GDAL_FILENAME_IS_UTF8=YES
set VSI_CACHE=TRUE
set VSI_CACHE_SIZE=1000000
set QT_PLUGIN_PATH=%OSGEO4W_ROOT%/apps/qgis/qtplugins;%OSGEO4W_ROOT%/apps/qt5/plugins
set PYTHONPATH=%OSGEO4W_ROOT%/apps/qgis/python
set PYTHONHOME=%OSGEO4W_ROOT%/apps/Python37
set PYTHONPATH=%OSGEO4W_ROOT%/apps/Python37/lib/site-packages;%PYTHONPATH%
set QT_QPA_PLATFORM_PLUGIN_PATH=%OSGEO4W_ROOT%/apps/Qt5/plugins/platforms
set QGIS_PREFIX_PATH=%OSGEO4W_ROOT%/apps/qgis
cd /d %~dp0
call ...path/to/shell_scrit.sh

The shell script looks like this:

python path/to/python/script.py

The python script looks like this:

import os
import sys
import qgis
from qgis.core import *
from qgis.core import QgsApplication
from qgis.core import QgsProject
from qgis.core import QgsProcessingFeedback
from qgis.core import QgsVectorLayer
from qgis.analysis import QgsNativeAlgorithms
path_qgis_installation = "C:/OSGeo4W64/apps/qgis"
os.environ['path_qgis_installation'] = path_qgis_installation
QgsApplication.setPrefixPath(os.environ['path_qgis_installation'], True)
qgs = QgsApplication([], False)
qgs.initQgis()
sys.path.append(r'C:\OSGeo4W64\apps\qgis\python\plugins')
import processing
from processing.core.Processing import Processing
Processing.initialize()
QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms())
input_file = QgsVectorLayer('.../file_name.shp')
overlay_file = QgsVectorLayer('.../file_name.shp')
output_fil = '.../file_name.shp'
processing.run(
 "native:clip", 
 {
 'INPUT':input_file,
 'OVERLAY':overlay_file,
 'OUTPUT':output_fil
 }
)
feedback = QgsProcessingFeedback()
res = processing.run("native:extractvertices", params, feedback=feedback)
qgs.exitQgis()
answered Jul 24, 2019 at 6:49
1
  • Fine! you are welcome ;) Commented Jul 24, 2019 at 6:55

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.