1

I have multiple polygons stored in a list like this :

def drawParcel(self, propriete):
    length = len(propriete.name)
    i = 0
    while i < length:
       layer = QgsVectorLayer('Polygon', propriete.name[i], "memory")
       allLayers.append(layer)

I have tried this code but i only get in the json file the last element of my list :

uri ="C:/Users/ilyasse2.0/Documents/SQRLAND/ils.json"  
context = QgsProject.instance().transformContext()  
options = QgsVectorFileWriter.SaveVectorOptions()  
options.driverName = "GeoJson"  
QgsVectorFileWriter.writeAsVectorFormatV2(allLayers[0],uri,context,options) 

#switch mode to append layer instead of overwriting the file  
options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer 
for layer in allLayers:
    QgsVectorFileWriter.writeAsVectorFormatV2(layer,uri,context,options)

How can i create a GeoJSON file with the list of layers ?

4
  • 3
    GeoJSON only has a single "layer" - you will need to combine the features and then write them to the GeoJSON
    – Ian Turton
    Commented Aug 3, 2021 at 12:20
  • thank you i just need somehow to get all the features of my layers (points,polygons and lines) and put them into geojson right ? is there any solution to this ? Commented Aug 3, 2021 at 16:07
  • I wonder if your code runs QgsVectorFileWriter.CreateOrOverwriteLayer for each layer that you want to append.
    – user30184
    Commented Aug 3, 2021 at 17:24
  • Have you tried with options.actionOnExistingFile = QgsVectorFileWriter.AppendToLayerNoNewFields? Commented Aug 4, 2021 at 1:38

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.