4

I execute standalone python code (almost identical to the code in the accepted answer):

The output file does not contain data, and I get this error message:

Warning 6: Normalized/laundered field name: 'vertex_index' to 'vertex_ind'
Warning 6: Normalized/laundered field name: 'vertex_part' to 'vertex_par'
Warning 6: Normalized/laundered field name: 'vertex_part_index' to 'vertex_p_1'

Process finished with exit code -1073741819 (0xC0000005)

Field name limitation is known shapefile issue, but why output file is empty and why I get exit code -1073741819 (0xC0000005)?

Here is input and output shapefiles:

enter image description here________enter image description here

What can cause this problem?

Here is the code:

import sys

from qgis.core import (
     QgsApplication,
     QgsProcessingFeedback,
     QgsVectorLayer
)
from qgis.analysis import QgsNativeAlgorithms

QgsApplication.setPrefixPath(r'C:\OSGeo4W64\apps\qgis', 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())

layer = QgsVectorLayer(r"G:\test\input.shp", 'my layer', 'ogr')
output = r"G:\test\output.shp"
params = {
    'INPUT': layer,
    'OUTPUT': output,
}
feedback = QgsProcessingFeedback()
res = processing.run("native:extractvertices", params, feedback=feedback)
print(res)
  • Try with a nightly for 3.2 - there's much more explicit and useful error messages in 3.2 – ndawson Jun 14 '18 at 9:00
  • @ndawson Current latest release (3.2.0-Bonn) gives the same errors. – Mr. Che Jun 25 '18 at 7:25
  • Searching for -1073741819 (0xC0000005) in DuckDuckGo results in many referrals to memory access violation, for example: > The exception code 0xc0000005 indicates a memory access violation. > [...] it looks like the OCX control you're using is performing an illegal access to the memory, therefore the system raised the exception and halted the program's execution. Source ### Some questions arise - Are – Stefan Jun 25 '18 at 10:08
  • What happens when you run the algorithm inside QGIS? – Matthias Kuhn Jun 25 '18 at 10:16
  • @MatthiasKuhn It works just fine see picure: i.stack.imgur.com/nZSih.png – Mr. Che Jun 25 '18 at 10:31
8
+100

Based on my comments.

This is my py3-env.bat

@ECHO OFF 

set OSGEO4W_ROOT=C:\OSGeo4W64

@echo off
call "%OSGEO4W_ROOT%\bin\o4w_env.bat"
call "%OSGEO4W_ROOT%\bin\qt5_env.bat"
call "%OSGEO4W_ROOT%\bin\py3_env.bat"

@echo off
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 PYCHARM="C:\Program Files\JetBrains\PyCharm 2018.1.4\bin\pycharm64.exe"

set PYTHONPATH=%OSGEO4W_ROOT%\apps\qgis\python
set PYTHONHOME=%OSGEO4W_ROOT%\apps\Python36
set PYTHONPATH=%OSGEO4W_ROOT%\apps\Python36\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
::python3 extractvertices_standalone.py
::pause
start "PyCharm aware of QGIS" /B %PYCHARM% %*

if execute the file directly from .bat work,and if open pycharm.This is my project using the osge4w python interpreter

enter image description here

Run and pycharm console show :

{'OUTPUT': 'C:\datos\output\output.shp'}

Open the file to see the result in QGIS.

enter image description here

Finally the standalone script,it's just like his worst simply by using my shapefiles

#native:extractvertices
import sys

from qgis.core import (
     QgsApplication,
     QgsProcessingFeedback,
     QgsVectorLayer
)
from qgis.analysis import QgsNativeAlgorithms

QgsApplication.setPrefixPath(r'C:\OSGeo4W64\apps\qgis', 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())

layer = QgsVectorLayer(r"C:\datos\shapefiles\regions.shp", 'my layer', 'ogr')
output = r"C:\datos\output\output.shp"
params = {
    'INPUT': layer,
    'OUTPUT': output,
}
feedback = QgsProcessingFeedback()
res = processing.run("native:extractvertices", params, feedback=feedback)
print(res)

Tested using Windows 10 and QGIS 3.2

I hope this helps you

  • It works! Qgis ownz my brain for a month and now it's over. OMG – Mr. Che Jul 19 '18 at 11:46
  • Nice!I'm so happy for you. – Fran Raga Jul 19 '18 at 11:51
  • 1
    The problem were caused by incorrect QGIS libraries initialization while using PyCharm IDE. My old launcher contain such text lines: i.stack.imgur.com/5CbNW.png Replacing it with the text in this answer solved the problem. – Mr. Che Jul 19 '18 at 12:01
1

If you are running the algorithm in a standalone application, the standard error reporting does not work because the app to handle this is not there.

What you can do is:

  1. Run the same algorithm inside QGIS and check what kind of error it reports

  2. If that doesn't work (some things from the environment might have an influence...) you can also add your own progress handler in the standalone script which will produce additional feedback.


import logging
import sys
from qgis.core import QgsProcessingFeedback

class QgsLoggingFeedback(QgsProcessingFeedback):

    def __init__(self):
        super().__init__()
        self.handler = logging.StreamHandler(sys.stdout)
        # Also show INFO and DEBUG messages
        # self.handler.setLevel(logging.DEBUG)
        logging.getLogger().addHandler(self.handler)

    def reportError(self, msg, fatalError=False):
        logging.log(logging.ERROR, msg)

    def setProgressText(self, text):
        logging.log(logging.INFO, msg)

    def pushInfo(self, info):
        super().pushInfo(info)

    def pushCommandInfo(self, info):
        super().pushCommandInfo(info)

    def pushDebugInfo(self, info):
        super().pushDebugInfo(info)

    def pushConsoleInfo(self, info):
        super().pushConsoleInfo(info)


feedback = QgsLoggingFeedback()
res = processing.run("native:extractvertices", params, feedback=feedback)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.