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.
QgsVectorFileWriterhas no method do define a CRS, but you can simply use.setCrs()before forQgsVectorLayer's. See https://gis.stackexchange.com/questions/136378/defining-layer-crs-and-avoiding-crs-dialog-in-pyqgis for example. – MrXsquared Dec 20 '23 at 12:42epsg:25832). Maybe this matters. – i.i.k. Dec 20 '23 at 14:01destCRSparameter 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:25832This also explain that the solution you found (setting the crs on the layer) works. Then you say to QGIS reproject myEPSG:25832layer toEPSG:25832which basically does nothing. – YoLecomte Dec 20 '23 at 15:05