1

In QGIS I am running a script in th python toolbar to export all visible vector layers to a GEOJSON with this snippet:

canvas = qgis.utils.iface.mapCanvas()
allLayers = canvas.layers()
for i in allLayers: 

    if i.type() != 0 :
        print(i.name() + " skipped as it is not a vector layer")  
    if i.type() == 0 :
        exp_crs = QgsCoordinateReferenceSystem(4326,QgsCoordinateReferenceSystem.PostgisCrsId)
        print i.crs()
        print exp_crs
        qgis.core.QgsVectorFileWriter.writeAsVectorFormat(i, '/home/' + str(i.name()) + '.js', 'utf-8', exp_crs, 'GeoJson')

If my vector file has a different CRS it keeps using the vector file CRS and doesn't use the defined CRS EPSG:4326... What am I doing wrong? The whole project context can be found on GITHUB

1 Answer 1

2

the main problem was the setting of the crs. this snippet should work now:

canvas = qgis.utils.iface.mapCanvas()
allLayers = canvas.layers()
exp_crs = QgsCoordinateReferenceSystem(4326, QgsCoordinateReferenceSystem.EpsgCrsId)
for i in allLayers: 

    if i.type() != 0 :
        print(i.name() + " skipped as it is not a vector layer")  
    if i.type() == 0 :
        #exp_crs = QgsCoordinateReferenceSystem(4326,QgsCoordinateReferenceSystem.PostgisCrsId)
        print i.crs()
        print exp_crs
        qgis.core.QgsVectorFileWriter.writeAsVectorFormat(i, i.name() + '.js', 'utf-8', exp_crs, 'GeoJSON')

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.