I would like to change the coordinates of QgsPoints imported from a csv file before saving it in shapefile.
I succeed to do all the step except the crs transformation, the output is keeping EPSG: 4326 instead of EPSG: 31370
#Set up inputs
absolute_path_to_csv_file = '/Users/X/Documents/.../Granulo.csv'
encoding = 'UTF-8'
delimiter = ';'
decimal = '.'
crs = 'epsg:4326'
x = 'X'
y = 'Y'
uri = f"file://{absolute_path_to_csv_file}?encoding={encoding}&delimiter={delimiter}&decimalPoint={decimal}&crs={crs}&xField={x}&yField={y}"
#Make a vector layer
layer = QgsVectorLayer(uri, "Points", "delimitedtext")
#Check if layer is valid
if not layer.isValid():
print ("Layer not loaded")
#Add CSV data
QgsProject.instance().addMapLayer(layer)
#Transform CSV data
sourceCrs = QgsCoordinateReferenceSystem(4326)
destCrs = QgsCoordinateReferenceSystem(31370)
layer = QgsCoordinateTransform(sourceCrs, destCrs, QgsProject.instance())
myGeometryInstance.transform(layer)
#Write shapefile
QgsVectorFileWriter.writeAsVectorFormat(layer, '/Users/X/Documents/.../Points.shp', "UTF-8", layer.crs(), "ESRI Shapefile", layerOptions=['SHPT=POINT'])
The error message I obtain is the following:
Traceback (most recent call last):
File "/Applications/QGIS-LTR.app/Contents/MacOS/../Resources/python/code.py", line 90, in runcode
exec(code, self.locals)
File "<input>", line 1, in <module>
File "<string>", line 29, in <module>
NameError: name 'myGeometryInstance' is not defined
I am working on MacOs with QGIS 3.28.5-Firenze and Python 3.9.5
The code use is adapted from Adding points with coordinates delimited by commas from CSV file in QGIS Python Console & Transforming single QgsGeometry object from one CRS to another using PyQGIS