Se stai cercando di riproiettare i file CSV dalla console Python in QGIS, puoi utilizzare il seguente script. Tutto ciò che dovresti cambiare sono i tre percorsi che sono menzionati nei commenti.
In sostanza, lo script importa i tuoi file CSV in QGIS come shapefile (supponendo che i tuoi campi geometrici siano chiamati X
e Y
). Quindi utilizza gli algoritmi qgis:reprojectlayer
e qgis:fieldcalculator
dalla Casella degli strumenti di elaborazione per riproiettare e aggiornare i campi X
e Y
con le nuove coordinate. Quindi li salva in una cartella e li converte in file CSV nel percorso specificato. Quindi, alla fine, hai aggiornato shapefile e file CSV in cartelle separate.
import glob, os, processing
path_to_csv = "C:/Users/You/Desktop/Testing//" # Change path to the directory of your csv files
shape_result = "C:/Users/You/Desktop/Testing/Shapefile results//" # Change path to where you want the shapefiles saved
os.chdir(path_to_csv) # Sets current directory to path of csv files
for fname in glob.glob("*.csv"): # Finds each .csv file and applies following actions
uri = "file:///" + path_to_csv + fname + "?delimiter=%s&crs=epsg:4326&xField=%s&yField=%s" % (",", "x", "y")
name = fname.replace('.csv', '')
lyr = QgsVectorLayer(uri, name, 'delimitedtext')
QgsMapLayerRegistry.instance().addMapLayer(lyr) # Imports csv files to QGIS canvas (assuming 'X' and 'Y' fields exist)
crs = 'EPSG:32633' # Set crs
shapefiles = QgsMapLayerRegistry.instance().mapLayers().values() # Identifies loaded layers before transforming and updating 'X' and 'Y' fields
for shapes in shapefiles:
outputs_0 = processing.runalg("qgis:reprojectlayer", shapes, crs, None)
outputs_1 = processing.runalg("qgis:fieldcalculator", outputs_0['OUTPUT'], 'X', 0, 10, 10, False, '$x', None)
outputs_2 = processing.runalg("qgis:fieldcalculator", outputs_1['OUTPUT_LAYER'], 'Y', 0, 10, 10, False, '$y', shape_result + shapes.name())
os.chdir(shape_result) # Sets current directory to path of new shapefiles
for layer in glob.glob("*.shp"): # Finds each .shp file and applies following actions
new_layer = QgsVectorLayer(layer, os.path.basename(layer), "ogr")
new_name = layer.replace('.shp', '')
csvpath = "C:/Users/You/Desktop/Testing/CSV results/" + new_name + ".csv" # Change path to where you want the csv(s) saved
QgsVectorFileWriter.writeAsVectorFormat(new_layer, csvpath, 'utf-8', None, "CSV")
Spero che questo ti aiuti!