3

I am trying to create an external Python application. I want to run this as a stand alone program in command line. I am trying to not use the QGIS console.

A first step is to clip a point shapefile using a polygon shapefile.

When I try to use processing.runalg("saga:clippointswithpolygons",...) nothing happens. There are no error messages or warnings. Nothing is written to the output shapefile.

import sys
import urllib

import pathlib

from qgis.core import QgsApplication
from qgis.core import QgsVectorLayer
from qgis.core import QgsVectorFileWriter

# Constants
node_shape_path = r'C:\Temp\TMC_nodes.shp'
clip_path = r'C:\Temp\clip.shp'  # Path to clipping polygon
out_shapefile_path = r"C:\Temp\TMC_Nodes_Clipped_2.shp"

##
# Prepare the QGIS environment and framework
##

# Prepare the QGIS environment
qgis_prefix = u'C:/PROGRA~1/QGISPI~1/apps/qgis'
app = QgsApplication([], True)
QgsApplication.setPrefixPath(qgis_prefix, True)
QgsApplication.initQgis()

# Prepare the QGIS processing framework
sys.path.append(plugins_path)
#from processing.core.Processing import Processing
import processing

##
# Clip the nodes and write to a new shapefile
##
processing.runalg("saga:clippointswithpolygons", node_shape_path, clip_path, "OBJECTID", 0, out_shapefile_path)

I have tried different versions of the processing.runalg line where I use file path strings instead of layers. I even went so far as to take the point layer, made from the csv, write it to a shapefile, and then use that path.

I have used the processing toolbox GUI to successfully conduct the clip. The log file reads:

ALGORITHM|Mon Sep 21 2015 12:26:24|
processing.runalg("saga:clippointswithpolygons","C:/Temp/TMC_nodes.shp","C:/Temp/FCMS_clip.shp","OBJECTID",0,"C:/Temp/TMC_Nodes_Clipped.shp")

I have even tried pasting the command call from the log directly into my external script, but it still does not work.

  • Try using double brackets for the full path or for the directory which contains the shapefile. Eg. node_shape_path = r'C:\\Temp\\TMC_nodes.shp or node_shape_path = r'C:\Temp\\TMC_nodes.shp. (As a side, I never used the raw string variable r when setting paths for external applications but might not work for everyone!). – Joseph Sep 24 '15 at 10:08
3

I'm not sure this is possible as processing requires iface, which is not created in standalone applications:

http://hub.qgis.org/issues/8955

  • 1
    +1 for mentioning the problem which occured initially but you can now use Processing for direct paths to files instead of from iface :). I asked a similar question a while ago. – Joseph Sep 24 '15 at 10:11

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.