I'm trying to get a voronoi polygon from points using Pycharm. I've tried:

processing.runalg("qgis:voronoipolygons", file ,1, "C:\Voronoi.shp" )

but it doesn't work. It works in the python console in QGIS but not in Pycharm .

This is my code:

import shapefile

import ProcessingLog
import processing
import sys
from sets import Set

from qgis.core import QGis, QgsFeatureRequest, QgsFeature, QgsGeometry, QgsPoint

from processing.core.GeoAlgorithm import GeoAlgorithm
from processing.core.GeoAlgorithmExecutionException import GeoAlgorithmExecutionException
from processing.core.parameters import ParameterVector
from processing.core.parameters import ParameterNumber
from processing.core.outputs import OutputVector
import voronoi
from processing.gui.SilentProgress import SilentProgress
from processing.tools import dataobjects, vector

def runalg(alg, progress=None):

  if progress is None:
      progress = SilentProgress()
      return True
  except GeoAlgorithmExecutionException as e:
      ProcessingLog.addToLog(sys.exc_info()[0], ProcessingLog.LOG_ERROR)
      return False

file = "C:\Armoire.shp"
r = shapefile.Reader("C:\Armoire.shp")
processing.runalg("qgis:voronoipolygons", file ,1, "C:\Voronoi.shp" )  

The error is:

QPixmap: Must construct a QApplication before a QPaintDevice


I found a solution for my problem . There is the code:

from qgis.core import *

# supply path to qgis install location
QgsApplication.setPrefixPath("C:\\Program Files\\QGIS Lyon\\apps\\qgis", True)

# create a reference to the QgsApplication, setting the
# second argument to False disables the GUI
qgs = QgsApplication([], True)
# load providers

# Write your code here to load some layers, use processing algorithms, etc.

layerInput = QgsVectorLayer("C:/shape.shp", "test", "ogr")

if not layerInput.isValid():
  print "Layer failed to load!"
    print "Layer was loaded successfully!"

from processing.core.Processing import Processing

Processing.runAlgorithm("qgis:voronoipolygons", None, layerInput,1, "C:/tmp.shp")

# When your script is complete, call exitQgis() to remove the provider and
# layer registries from memory

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.