6

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)
Matthias Kuhn
  • 27,780
  • 3
  • 88
  • 129
Comrade Che
  • 7,091
  • 27
  • 58
  • Try with a nightly for 3.2 - there's much more explicit and useful error messages in 3.2 – ndawson Jun 14 '18 at 09:00
  • @ndawson Current latest release (3.2.0-Bonn) gives the same errors. – Comrade Che Jun 25 '18 at 07:25
  • What happens when you run the algorithm inside QGIS? – Matthias Kuhn Jun 25 '18 at 10:16
  • @MatthiasKuhn It works just fine see picure: https://i.stack.imgur.com/nZSih.png – Comrade Che Jun 25 '18 at 10:31
  • @Mr.Che - Have you tried this using QGIS installed from the standalone installer and not using OSGeo4W? You would have to change the paths accordingly (i.e. QgsApplication.setPrefixPath(r"C:\Program Files\QGIS 3.2\apps\qgis", True) etc. – Joseph Jul 18 '18 at 12:56
  • just for clarifying your code works well in standalone. Have you tried with another shapefile? – Fran Raga Jul 18 '18 at 17:10
  • @FranciscoRaga On which operating system did you test the code? I use Windows 7, PyCharm IDE to launch this standalone code. – Comrade Che Jul 19 '18 at 03:53
  • windows 10 and Qgis 3.2 – Fran Raga Jul 19 '18 at 05:04
  • @FranciscoRaga Sorry, I don't understand which IDE do you use to launch this standalone code? Did you launch this code using QGIS 3.2 console window? Please, share your path variables and batch file content (which you use to launch your IDE) like this one: http://i.stack.imgur.com/5CbNW.png – Comrade Che Jul 19 '18 at 05:54
  • I'm lauch the code using a bat,but i will try using pycharm too – Fran Raga Jul 19 '18 at 06:04
  • @Mr. Che tested using pycharm your code and work perfectly!you can test your code using other shapefile?If you want share my commets and .bat in a new answer. – Fran Raga Jul 19 '18 at 10:00
  • @FranciscoRaga New polygonal shapefile (created using QGIS) give the same error. I think this error caused by wrong PyCharm initialization or wrong paths. Could you please post your paths and PyCharm launcher (.cmd or .bat file). – Comrade Che Jul 19 '18 at 10:35
  • 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
    – StefanBrand_EOX Jun 25 '18 at 10:08
  • Thank for your responce, but I don't think this error caused by memory access violation because i can run script in QGIS console: https://i.stack.imgur.com/nZSih.png – Comrade Che Jun 25 '18 at 10:45
  • @Mr.Che Maybe QGIS console has access to parts of the memory that are not accessible to the standalone script. I'm by no means an expert in this, so that's just guessing. – StefanBrand_EOX Jun 25 '18 at 10:50

2 Answers2

16

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

Fran Raga
  • 7,838
  • 3
  • 26
  • 47
  • It works! Qgis ownz my brain for a month and now it's over. OMG – Comrade 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: http://i.stack.imgur.com/5CbNW.png Replacing it with the text in this answer solved the problem. – Comrade Che Jul 19 '18 at 12:01
  • @FranRaga Great stuff, still when I run the script I get na error qt.qpa.plugin: Could not find the Qt platform plugin "windows" in "C:\OSGEO4~1\apps\Qt5\plugins\platforms , I've read stackoverflow on this but no anwser seems to work, I'm using PyCharm 2020.1 / QGIS 3.12, appreciate help! – cincin21 May 22 '20 at 14:25
  • Sorry, I've never had that problem. Look here https://gis.stackexchange.com/a/308290/49538 or on the QT Forum https://forum.qt.io/topic/110540/qt-qpa-plugin-could-not-find-the-qt-platform-plugin-windows-in-qtlib-plugins-platforms – Fran Raga May 22 '20 at 15:20
  • 1
    @FranRaga Two years later and this helped me overcome the same problem – auslander Jun 04 '20 at 15:44
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)
Matthias Kuhn
  • 27,780
  • 3
  • 88
  • 129
  • No error while running inside QGIS console: https://i.stack.imgur.com/nZSih.png 2. Including your code in my code - does nothing (same error message)
  • – Comrade Che Jun 25 '18 at 10:58
  • I added some bits and hope it does more now – Matthias Kuhn Jun 25 '18 at 11:00
  • Thank you, but the error is still the same. – Comrade Che Jun 25 '18 at 11:23
  • Can you try adding the logging.log() line also to the pushXyz() methods? – Matthias Kuhn Jun 25 '18 at 12:03
  • Additionally, you can add a FileHandler to have everything in a file https://docs.python.org/2/library/logging.handlers.html#filehandler – Matthias Kuhn Jun 25 '18 at 12:04