1

When I try to execute this code:

 from qgis.core import *

 QgsApplication.setPrefixPath("/usr/lib/qgis", True)
 app = QgsApplication([], True)
 app.initQgis()


 layer = QgsVectorLayer('/home/city.shp',"p", "ogr")
 QgsProject.instance().addMapLayers([layer])

 shp_path = '/home/points.shp'
 csv_path = '/home/a.csv'
 tag_save_as_shp = '/home/sep.shp'

 layerInput = QgsVectorLayer(shp_path, "cells", "ogr")
 csvInput = QgsVectorLayer(csv_path, "Table_csv", "ogr")
 QgsProject.instance().addMapLayer(layerInput, False)
 QgsProject.instance().addMapLayer(csvInput, False)
 # joining csv with shp
 csvField = 'Square-ID'
 shpField = 'cellId'
 joinObject = QgsVectorLayerJoinInfo()
 joinObject.setJoinFieldName(csvField)
 joinObject.setTargetFieldName(shpField)
 joinObject.setJoinLayerId(csvInput.id())
 joinObject.setUsingMemoryCache(True)
 joinObject.setJoinLayer(csvInput)
 joinObject.setPrefix('')
 layerInput.addJoin(joinObject)
   QgsVectorFileWriter.writeAsVectorFormat(layerInput,tag_save_as_shp,"CP120",layerInput.crs(),"ESRI Shapefile")

I get an error in PyCharm:

Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)

But in the python console in QGIS everything works fine. Also, this code worked before two weeks from PyCharm also. I read that is about some memory violation, but I don't understand it. :/

UPDATE: If I deleted the QgsVectorFileWriter line, everything is ok. So I have problem when I try to make output..

  • some odd things here, i would check: setPrefixPath is executed before app is instanciated, QgsProject may not exist in a standalone script, unusual path /home, do you have read/write access? Last line has an indent, also will be overwriting the input shapefile? – Andreas Müller Nov 2 '18 at 7:58
  • Thank you. :)))) It was because of SetPrefixPath. Earlier was working on that way. Please put this as an answer and I will accept it. :) – Anajlim Nov 2 '18 at 8:21
0

In a standalone python script, a qgis app object must be instantiated before you specify the qgis prefix path. So this becomes

app = QgsApplication(argv, True)
app.setPrefixPath(path_to_qgis_libs)

QgsApplication.setPrefixPath() may also work, if you put it after app object instantiation, i didn't try this.

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.