3

How in QGIS 3.16 to convert from CRS EPSG:4326 to CRS EPSG:32635 using Python?

I got this error:

exec(open('C:/Users/aaa/AppData/Local/Temp/tmpovpzt0d0.py'.encode('utf-8')).read()) Traceback (most recent call last): File "C:\PROGRA~1\QGIS31~1.8\apps\Python39\lib\code.py", line 90, in runcode exec(code, self.locals) File "", line 1, in File "", line 17, in AttributeError: module 'processing' has no attribute 'runalg'

This is the code:

#ADDING CSV file D:\1.csv
import os
import processing

# added file name (1.csv)
Add_file_name='1.csv'
# path + added file name  (D:\1.csv)
Add_path = 'file:/' + 'D:' + Add_file_name
# (angle,angle,meter)(XYZ) (E,N,Height) (LLH at EPSG:4326) 
uri = '{}?type=csv&xField={}&yField={}&crs={}'.format(
   Add_path, 'Longitude', 'Latitude','Height','Q', 'EPSG:4326')
## name added layer with added file name 1.csv) 
iface.addVectorLayer(uri, Add_file_name, 'delimitedtext')

#EXPORTINGI CSV file D:\UTM_1.csv
activelayer = iface.activeLayer()
out_file_name='utm_1.csv'
out_path = 'file:/' + 'D:' + out_file_name
processing.runalg('qgis:reprojectlayer', activelayer, 32635, out_path)
# exported file name = 'UTM_'+ Add_file_name = (UTM_1.csv)
# here add export file name and path

with open(r'D:\\'+ 'UTM_'+ Add_file_name , 'w') as output_file:
    fieldnames = [field.name() for field in activelayer.fields()]
# write header
    line = ','.join(name for name in fieldnames) + '\n'
    output_file.write(line)
# write feature attributes
    for f in activelayer.getFeatures():
        line = ','.join(str(f[name]) for name in fieldnames) + '\n'
        output_file.write(line)
4
  • 2
    Well, what is the error you get thrown, or which you observe? Currently your question is no question, but a statement.
    – Erik
    Jul 26, 2021 at 20:47
  • While there is an achievement for deleting down-voted questions, you aren't going to get any awards for deleting the previous question content and reposting it as a new Question without attempting to improve it. In fact, automated anti-spam defenses may suspend your account. While you do have code, there doesn't seem to be an attempt to implement your goal, so a code sample could still be expected.
    – Vince
    Jul 26, 2021 at 21:09
  • I have problem in processing.runalg I do not know how to use it. any suggestion?
    – DA53
    Jul 26, 2021 at 22:11
  • Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Your questions should as much as possible describe not just what you want to do, but precisely what you have tried and where you are stuck trying that. Please check out our short tour for more about how the site works
    – Ian Turton
    Jul 27, 2021 at 7:33

2 Answers 2

3

You need to use processing.run instead of processing.runalg. The second is now not working due to deprecation e.g Using processing.runalg in QGIS 3

That's why it's throwing the error

AttributeError: module 'processing' has no attribute 'runalg'

To learn a bit more about using processing algorithms in PyQGIS, go to the section "Calling algorithms from the Python console" from the official doc

0

Here is the working code using QGIS 3.16.8

1- Enable Qgis macro Settings>>Options>>General>>(Enable Macros Always)

2- Create a batch file (runQGIS.bat) and paste this


"C:\Program Files\QGIS 3.16.8\bin\qgis-ltr-bin.exe" --code "c:\Users\me\Desktop\xyz\utm.py"


3- Or create a new shortcut to (qgis-ltr-bin.exe) then right-click >> properties>> in target paste this


"C:\Program Files\QGIS 3.16.8\bin\qgis-ltr-bin.exe" --code c:\Users\me\Desktop\xyz\utm.py


4- Crate folder xyz in me Desktop and put your CSV file in this format

y,x,z

22.956425942,11.708518637,92.7

22.256725748,11.508518344,99.6

5- Paste the code into utm.py and save in the same xyz folder

6- You can delete the last line in the code if you do not want to auto-close Qgis

7- Warning You have to choose the right resultant file from 5 zones
Take care this code does not work if your points fall in 2 zones or out of the 5 zones.

You have to modify this code to change change to path of windows user Desktop/xyz and aqgis-ltr-bin.exe path and your county CRS (5 zones max)

################################################
#----------  You May Change This    -----------#
################################################
your_working_folder="xyz"
your_working_file="1.csv"
in_epsg="EPSG:4326"
################################################
my_crs1="EPSG:32630"
my_crs2="EPSG:32631"
my_crs3="EPSG:32632"
my_crs4="EPSG:32633"
my_crs5="EPSG:32634"
################################################
import os
import processing
from qgis.core import *
from qgis.utils import iface
import platform
################################################
userhome = os.path.expanduser('~')
in_path ='file:/'+userhome + '/Desktop/'+ your_working_folder +'/'
out_path =userhome + '/Desktop/'+ your_working_folder +'/'
################################################
in_all=in_path+your_working_file
f= '{}?type=csv&xField={}&yField={}&zField={}&crs='+ in_epsg
uri = f.format(in_all,  'x','y','z','q','sat')
#(q= GPS quality from PPK RTK correction) (sat= number of satellites)
iface.addVectorLayer(uri, your_working_file, 'delimitedtext')
activelayer = iface.activeLayer()
#################################################
cut_this = "EPSG:"
#################################################
my_layer_name= my_crs1.replace(cut_this, '')
my_memory_layer="memory:"+" XYZ         ( " +my_crs1+" )"
parameter = {'INPUT': iface.activeLayer(), 'TARGET_CRS': my_crs1,'OUTPUT': my_memory_layer}
result = processing.run('native:reprojectlayer', parameter)
QgsProject.instance().addMapLayer(result['OUTPUT'])
activelayer = iface.activeLayer()
out_write= out_path + my_layer_name+"    "+your_working_file
QgsVectorFileWriter.writeAsVectorFormat(activelayer,out_write,"utf-8",driverName = "CSV" , layerOptions = ['GEOMETRY=AS_XYZ'])
#################################################
my_layer_name= my_crs2.replace(cut_this, '')
my_memory_layer="memory:"+" XYZ         ( " +my_crs2+" )"
parameter = {'INPUT': iface.activeLayer(), 'TARGET_CRS': my_crs2,'OUTPUT': my_memory_layer}
result = processing.run('native:reprojectlayer', parameter)
QgsProject.instance().addMapLayer(result['OUTPUT'])
activelayer = iface.activeLayer()
out_write= out_path + my_layer_name+"    "+your_working_file
QgsVectorFileWriter.writeAsVectorFormat(activelayer,out_write,"utf-8",driverName = "CSV" , layerOptions = ['GEOMETRY=AS_XYZ'])
##############################################
my_layer_name= my_crs3.replace(cut_this, '')
my_memory_layer="memory:"+" XYZ         ( " +my_crs3+" )"
parameter = {'INPUT': iface.activeLayer(), 'TARGET_CRS': my_crs3,'OUTPUT': my_memory_layer}
result = processing.run('native:reprojectlayer', parameter)
QgsProject.instance().addMapLayer(result['OUTPUT'])
activelayer = iface.activeLayer()
out_write= out_path + my_layer_name+"    "+your_working_file
QgsVectorFileWriter.writeAsVectorFormat(activelayer,out_write,"utf-8",driverName = "CSV" , layerOptions = ['GEOMETRY=AS_XYZ'])
##############################################
my_layer_name= my_crs4.replace(cut_this, '')
my_memory_layer="memory:"+" XYZ         ( " +my_crs4+" )"
parameter = {'INPUT': iface.activeLayer(), 'TARGET_CRS': my_crs4,'OUTPUT': my_memory_layer}
result = processing.run('native:reprojectlayer', parameter)
QgsProject.instance().addMapLayer(result['OUTPUT'])
activelayer = iface.activeLayer()
out_write= out_path + my_layer_name+"    "+your_working_file
QgsVectorFileWriter.writeAsVectorFormat(activelayer,out_write,"utf-8",driverName = "CSV" , layerOptions = ['GEOMETRY=AS_XYZ'])
##############################################
my_layer_name= my_crs5.replace(cut_this, '')
my_memory_layer="memory:"+" XYZ         ( " +my_crs5+" )"
parameter = {'INPUT': iface.activeLayer(), 'TARGET_CRS': my_crs5,'OUTPUT': my_memory_layer}
result = processing.run('native:reprojectlayer', parameter)
QgsProject.instance().addMapLayer(result['OUTPUT'])
activelayer = iface.activeLayer()
out_write= out_path + my_layer_name+"    "+your_working_file
QgsVectorFileWriter.writeAsVectorFormat(activelayer,out_write,"utf-8",driverName = "CSV" , layerOptions = ['GEOMETRY=AS_XYZ'])
##############################################
#iface.actionExit().trigger()
os._exit(0)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.