I'm trying to write a Python script to convert a vector layer using 4326 to EPSG:54002 World Equidistant Cylindrical for road distance calculations, but the script fails to do the conversion, I've tried numerous methods to get it right:
Using the re-project layer algorithm:
reprojectedLayer = processing.run("native:reprojectlayer", {'INPUT': roadLayer ,'TARGET_CRS': 'EPSG:54002', 'OUTPUT':'TEMPORARY_OUTPUT'})['OUTPUT']
reprojectedLayer = processing.run("native:reprojectlayer", {'INPUT': roadLayer ,'TARGET_CRS': QgsCoordinateReferenceSystem('EPSG:54002'), 'OUTPUT':'TEMPORARY_OUTPUT'})['OUTPUT']
Calling the setCrs method on the layer:
roadLayer.setCrs(QgsCoordinateReferenceSystem(54002, QgsCoordinateReferenceSystem.PostgisCrsId))
roadLayer.setCrs(QgsCoordinateReferenceSystem('EPSG:54002'))
Even just writing the layer to a file while specifying a different CRS:
QgsVectorFileWriter.writeAsVectorFormat(roadLayer, sys.argv[2] + "/roads", "utf-8", QgsCoordinateReferenceSystem('EPSG:54002'), "GPKG")
Unfortunately, none of these have worked. If I save the file after any of the operations above and open it in QGIS it either just opens it fine as 4326 or opens it and displays a message that the CRS is invalid and that it will be defaulting to 4326.
One interesting thing to note is that when I specify the crs while writing the file, this pops up in the console:
proj_create_from_database: crs not found
It doesn't do that with the other methods, only when I write the file. This message doesn't make sense since if I try to reproject the layer to 54002 in QGIS, it works fine. Its only in the script that it fails.