1

I want to access the explodelines function in Python outside of QGIS from a standalone script.

import sip
for api in ["QDate", "QDateTime", "QString", "QTextStream", "QTime", "QUrl", "QVariant"]:
    sip.setapi(api, 2)


import sys
from qgis.core import *
from PyQt4.QtGui import *
app = QApplication([])
QgsApplication.setPrefixPath("C:/OSGeo4W64/apps/qgis", True)
QgsApplication.initQgis()

# Prepare processing framework
sys.path.append('C:/OSGeo4W64/apps/qgis/python/plugins')

from processing.core.Processing import Processing
Processing.initialize()
from processing.tools import *

# Run the algorithm
layerInput = QgsVectorLayer("files/centre/colour/streets.shp", 'test', 'ogr')
general.runalg('qgis:explodelines', layerInput, 'temp.shp')

# Exit applications
QgsApplication.exitQgis()
QApplication.exit()

EDIT:

The result of the algorithm is: 'NoneType' object has no attribute 'error' I used various shapefiles but none of them worked.

  • 1
    Did you try using the full path to the shapefile? (e.g. C:/someDirectory/files/centre/colour/streets.shp. What happens if you type print layerInput? – Joseph Jan 23 '18 at 12:31
  • Yes, I tried, but no change.

    <qgis._core.QgsVectorLayer object at 0x00000000144D68C8>

    – freddykrueger Jan 23 '18 at 12:33
  • Sorry, I meant using the full path as the input parameter: general.runalg('qgis:explodelines', 'C:/someDirectory/files/centre/colour/streets.shp', 'temp.shp') – Joseph Jan 23 '18 at 12:55
  • No, it don't change anything.. The error: 'NoneType' object has no attribute 'error' appears – freddykrueger Jan 23 '18 at 13:09
  • There could be errors in your shapefile. Try loading the shapefile into QGIS and running the Check validity tool. – Joseph Jan 23 '18 at 13:32
  • Hey @Joseph .. I test various shapefiles, but none of them worked... Did You test my algorithm? I use the algorithm from this thread: https://gis.stackexchange.com/questions/129513/accessing-processing-with-python but it doesn't work :/ – freddykrueger Jan 23 '18 at 14:14
  • Perhaps you actually don't need the first 3 lines of your script, as you can see in the thread you mentioned. – Germán Carrillo Jan 23 '18 at 14:33
  • @GermánCarrillo: This is right, but the error stays...Can You help me please (Processing-version: 2.12.99) – freddykrueger Jan 23 '18 at 14:46

1 Answers1

1

On the following link the problem with the processing framework has been solved: Link 1

--> Just change two lines in AlgorithmExecutor.py like this.