# How to remove “ERROR 1” messages from a standalone QGIS script?

I am running a standalone script for QGIS which creates a high resolution grid and clips shapefiles onto it. Works perfect if the cell width and height have a high values (in my case, >200m for both). Anything lower than this and I suddenly get the following error:

ERROR 1: Attempt to write non-polygon (POINT) geometry to POLYGON type shapefile.

I have read this post with a similar problem, and as mentioned in the answer, the results I obtain are correct so I am not worried about that.

Is there a method to remove this error message(s) from appearing? It looks incredibly messy:

I've also included a stripped-down snippet of code I use:

def run():

Cellsize = 10
layerPath = "path\to\shapefile"
extent = QgsVectorLayer( layerPath, '', 'ogr' ).extent()

centerx = (extent.xMinimum() + extent.xMaximum()) / 2
centery = (extent.yMinimum() + extent.yMaximum()) / 2
width = extent.xMaximum() - extent.xMinimum()
height = extent.yMaximum() - extent.yMinimum()

outputs_0=general.runalg("qgis:creategrid", Cellsize, Cellsize, width, height, centerx, centery, 1, 'EPSG:7405', None)
outputs_1=general.runalg("qgis:clip", outputs_0['SAVENAME'], "path\to\shapefile", "create\path\for\result")

try:
run()
except Exception:
pass

• Since you are apparently only looking to write Polygons you can run an if/else statement where you catch the point geometry and only write if its a polygon, you can even write the point data to another shapefile if you want to check the results or to a log – Antonio Locandro Apr 29 '15 at 17:16
• @AntonioLocandro - Many thanks buddy, I found a solution which ignores the 'printing out' of the errors. However, I will definitely look into your suggestion when I evaluate the script in greater detail =) – Joseph Apr 30 '15 at 9:43

Found a solution from this post where I only had to add the following code to the script:

from osgeo import gdal

gdal.PushErrorHandler('CPLQuietErrorHandler')


After adding the above and removing the try statement, the working script ignores the ERROR 1 messages:

from osgeo import gdal
gdal.PushErrorHandler('CPLQuietErrorHandler')

def run():

Cellsize = 10
layerPath = "path\to\shapefile"
extent = QgsVectorLayer( layerPath, '', 'ogr' ).extent()

centerx = (extent.xMinimum() + extent.xMaximum()) / 2
centery = (extent.yMinimum() + extent.yMaximum()) / 2
width = extent.xMaximum() - extent.xMinimum()
height = extent.yMaximum() - extent.yMinimum()

outputs_0=general.runalg("qgis:creategrid", Cellsize, Cellsize, width, height, centerx, centery, 1, 'EPSG:7405', None)
outputs_1=general.runalg("qgis:clip", outputs_0['SAVENAME'], "path\to\shapefile", "create\path\for\result")

run()