2

QGIS Version - 3.0 I developed a script which creates layers and saves it the to the disk. When I run the script, it runs successfully the first time. But when I run it the second time, QGIS crashes. Can someone help me to figure out what could be the problem?

Here is the script.

from qgis.core import *
import qgis.utils

QgsApplication.setPrefixPath("C:/Program Files/QGIS 3.0", True)
qgs = QgsApplication([], False) ###
qgs.initQgis() ###

_layer1 = QgsVectorLayer("C:/AR/Automation Test Script/Radio Signal/MasterLine.shp", "MASTERLINE", "ogr")
_layer2 = QgsVectorLayer("C:/AR/Automation Test Script/Radio Signal/SIGNAL.shp", "SIGNAL", "ogr")
_layer3 = QgsVectorLayer("C:/AR/Automation Test Script/Radio Signal/ClearancePoint.shp", "CLEARANCEPOINT", "ogr")

if not _layer1.isValid():
  print("Layer failed to load!")

else:
    print("Layer was loaded successfully!")

if not _layer2.isValid():
  print("Layer failed to load!")

else:
    print("Layer was loaded successfully!")

if not _layer3.isValid():
  print("Layer failed to load!")

else:
    print("Layer was loaded successfully!")

_writer1 = QgsVectorFileWriter.writeAsVectorFormat(_layer1,"C:/AR/Automation Test Script/Radio Signal/MasterLine12.shp",'utf-8',QgsCoordinateReferenceSystem(),'ESRI Shapefile')
_writer2 = QgsVectorFileWriter.writeAsVectorFormat(_layer2,"C:/AR/Automation Test Script/Radio Signal/SIGNAL.shp",'utf-8',QgsCoordinateReferenceSystem(),'ESRI Shapefile')
_writer3 = QgsVectorFileWriter.writeAsVectorFormat(_layer3,"C:/AR/Automation Test Script/Radio Signal/ClearancePoint.shp",'utf-8',QgsCoordinateReferenceSystem(),'ESRI Shapefile')

qgs.exitQgis()

#_writer = QgsVectorFileWriter.writeAsVectorFormat(_layer,r"C:/AR/RadioSignal/MasterLine123.shp","utf-8",None,"ESRI Shapefile",True)

This is after first run

enter image description here

This is after second run.

enter image description here

3

With your current code, you are writing an application to be launched without QGIS desktop. If you are running your script inside QGIS already, you shouldn't use the QgsApplication. Keep only your variable _writerX and _layerX and remove QgsApplication.

_layer1 = QgsVectorLayer("/path/to/data.shp", "MASTERLINE", "ogr")

if not _layer1.isValid():
  print("Layer failed to load!")

else:
    print("Layer was loaded successfully!")

_writer1 = QgsVectorFileWriter.writeAsVectorFormat(_layer1,"/tmp/MasterLine12.shp",'utf-8',QgsCoordinateReferenceSystem(),'ESRI Shapefile')
  • I eventually intend to launch this from command line. That's the reason I used QGSApplication. Is that the reason it is failing? – Jay May 9 '18 at 18:27
  • I think you need to choose either a QGIS script or the standalone application. Maybe you should add our QgsApplication in a if statement. Is your script working from commandline? – etrimaille May 9 '18 at 18:48

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.