I'm exporting xml-data files to geopackage, which seems to work. However, the coordinate reference system is not included in the xml-data, but I know it is EPSG:25832. How can I set the CRS system when writing the layer to file with the QGSVectorFileWriter?
My code looks like this. :
dlayer = QgsVectorLayer(filename, 'layer_name', 'ogr')
writer = QgsVectorFileWriter.writeAsVectorFormat(dlayer, outputFilename, "utf-8", dlayer.crs() , "GPKG")
Here I use the CRS from the XML-file, which doesn't have one . How can I set the CRS to EPSG:25832 ? I did try already to include the crs as string, but this didn't work and I created a QgsReferenceObject, but his didn't throw any errors but it didn't set the CRS.
QgsVectorFileWriter
has no method do define a CRS, but you can simply use.setCrs()
before forQgsVectorLayer
's. See gis.stackexchange.com/questions/136378/… for example.epsg:25832
). Maybe this matters.destCRS
parameter is use to reproject the data from current crs. I guess your problem is that your source layer (your xml loaded in aQgsVectorLayer
) does not have any crs associated. QGIS seems to consider your layer as valid but I'm pretty sure it's not. Consequently, QGIS is not able to reproject from nothing toEPSG:25832
This also explain that the solution you found (setting the crs on the layer) works. Then you say to QGIS reproject myEPSG:25832
layer toEPSG:25832
which basically does nothing.