2

I have mentioned my QGIS script below. In that script, I have used "if condition".

When I open QGIS and run the script first time, after selecting the "Coordinate_System_SLD_99", it works accurately.

When I run it a second time, if I select "Coordinate_System_Kadawala" option, output comes according to the "Coordinate_System_SLD_99" option.

Once I close and reopen QGIS, it works accurately.

When I run the program again after selecting another option, same problem arise.

Can anyone help me to solve this?

##Dxf_to_Polygon(With Meta Data)=name
##Meta_Data_File=table
##Coordinate_System_SLD_99=boolean 
##Coordinate_System_Kadawala=boolean 
##Dxf_file=file
##Shape_File_Name=output vector
outputs_GRASS7V_IN_DXF_1=processing.runalg('grass7:v.in.dxf', Dxf_file,'1',True,False,True,False,False,True,'200000.000000,700000.000000,200000.000000,700000.000000',0,None)
outputs_GRASS7V_CLEAN_1=processing.runalg('grass7:v.clean', outputs_GRASS7V_IN_DXF_1['output'],1,0.1,'200000.000000,700000.000000,200000.000000,700000.000000',-1.0,0.0001,None,None)
outputs_QGISPOLYGONIZE_1=processing.runalg('qgis:polygonize', outputs_GRASS7V_CLEAN_1['output'],True,True,None)
outputs_SAGAPOLYGONDISSOLVEBYATTRIBUTE_1=processing.runalg('saga:polygondissolvebyattribute', outputs_QGISPOLYGONIZE_1['OUTPUT'],'area','layer','perimeter',False,None)
outputs_SAGAPOLYGONDISSOLVEALLPOLYGONS_1=processing.runalg('saga:polygondissolveallpolygons', outputs_SAGAPOLYGONDISSOLVEBYATTRIBUTE_1['DISSOLVED'],False,None)
outputs_QGISADDFIELDTOATTRIBUTESTABLE_1=processing.runalg('qgis:addfieldtoattributestable', outputs_SAGAPOLYGONDISSOLVEALLPOLYGONS_1['DISSOLVED'],'Req_No',2,30.0,0.0,None)
outputs_QGISADDFIELDTOATTRIBUTESTABLE_2=processing.runalg('qgis:addfieldtoattributestable', outputs_QGISADDFIELDTOATTRIBUTESTABLE_1['OUTPUT_LAYER'],'Surveyor_Name',2,25.0,0.0,None)
outputs_QGISADDFIELDTOATTRIBUTESTABLE_3=processing.runalg('qgis:addfieldtoattributestable', outputs_QGISADDFIELDTOATTRIBUTESTABLE_2['OUTPUT_LAYER'],'Survey_Date',2,20.0,0.0,None)
outputs_QGISADDFIELDTOATTRIBUTESTABLE_4=processing.runalg('qgis:addfieldtoattributestable', outputs_QGISADDFIELDTOATTRIBUTESTABLE_3['OUTPUT_LAYER'],'Plan_Name',2,20.0,0.0,None)
outputs_QGISADDFIELDTOATTRIBUTESTABLE_5=processing.runalg('qgis:addfieldtoattributestable', outputs_QGISADDFIELDTOATTRIBUTESTABLE_4['OUTPUT_LAYER'],'Method',2,20.0,0.0,None)
outputs_QGISADDFIELDTOATTRIBUTESTABLE_6=processing.runalg('qgis:addfieldtoattributestable', outputs_QGISADDFIELDTOATTRIBUTESTABLE_5['OUTPUT_LAYER'],'Cordi_Sys',2,20.0,0.0,None)
outputs_QGISADDFIELDTOATTRIBUTESTABLE_7=processing.runalg('qgis:addfieldtoattributestable', outputs_QGISADDFIELDTOATTRIBUTESTABLE_6['OUTPUT_LAYER'],'Purpose',2,20.0,0.0,None)

**if Coordinate_System_SLD_99 == True:
       processing.runalg('qgis:reprojectlayer',outputs_QGISADDFIELDTOATTRIBUTESTABLE_7['OUTPUT_LAYER'],'EPSG:5235',Shape_File_Name)
elif Coordinate_System_Kadawala == True:
       processing.runalg('qgis:reprojectlayer', outputs_QGISADDFIELDTOATTRIBUTESTABLE_7['OUTPUT_LAYER'],'EPSG:5234',Shape_File_Name)**

# #outputs_QGISREPROJECTLAYER_1=processing.runalg('qgis:reprojectlayer',outputs_QGISADDFIELDTOATTRIBUTESTABLE_7['OUTPUT_LAYER'],'EPSG:5235',Shape_File_Name)
#outputs_QGISREPROJECTLAYER_1=processing.runalg('qgis:reprojectlayer',               outputs_QGISADDFIELDTOATTRIBUTESTABLE_7['OUTPUT_LAYER'],'EPSG:5234',           Shape_File_Name)




from PyQt4.QtCore import QVariant
from qgis.utils import iface

import qgis
from qgis.core import *
from PyQt4.QtCore import *
#vl = iface.activeLayer()----->this is the corret code
#qgis.utils.iface.setActiveLayer(vl)
#vl = qgis.utils.iface.activeLayer()
vl = processing.getObject(Shape_File_Name)
vl.startEditing()
for field in vl.fields():
    print field.name()
    vl.startEditing()


metafile= processing.getObject(Meta_Data_File)
metafile.startEditing()
b=[]
features = metafile.getFeatures()
for feat in features:
    attrs = feat.attributes()
    b.append(attrs[1])




features = vl.getFeatures() 
for feat in features:
    attrs = feat.attributes()
    print attrs[1]
    print attrs[2]
    print attrs[3]
    print attrs[4]

selection=vl.getFeatures(QgsFeatureRequest().setFilterExpression(' "OID"=0'))
for feat in selection:
  vl.changeAttributeValue(feat.id(),1, b[1])
  vl.changeAttributeValue(feat.id(),2, b[10])
  vl.changeAttributeValue(feat.id(),3, b[6])
  vl.changeAttributeValue(feat.id(),4, b[3])
  vl.changeAttributeValue(feat.id(),5, b[8])
  vl.changeAttributeValue(feat.id(),6, b[9])
  vl.changeAttributeValue(feat.id(),7, b[2])
  vl.commitChanges()
 #selection=vl.getFeatures(QgsFeatureRequest().setFilterExpression(' "Req_No"= 5'))
#for feat in selection:
  #vl.changeAttributeValue(feat.id(),1, '45')
  #vl.commitChanges()
0

3 Answers 3

2

Once i open the QGIS,default OTF was wgs 84 EPSG:4326.At the very first,when i run my script,after selecting Coordinate_System as SLD_99, the OTF at the bottom of the QGIS window changed in to EPSG:5235 automatically.Once i save my out put as a KML file,out put of the KML positioned at the correct place in our country map.Then again when i run the script,by selecting the coordinate system as Kandawala,OTF didn't change into EPSG:5234. It exist as EPSG:5235 .Once i create a kml from the output, it does not positioned at the correct place.Actually,that was my problem.

To overcome that issue what i did was, before change the coordinate system in my sript,I change the OTF at the bottom of the QGIS window,according to the coordinate system which i am going to select in my script.That means ,if i select Kandawala in my script,i change OTF into EPSG:5234.Then result kml positioned at the correct place.

Above two answers are correct.It helps to assign coordinates system into my shape file automatically.But i feel, i have not convey may problem properly.I apologize for it.Anyway both your answers help me to over come a half of my problem.Thank both of you again for your valuable thoughts.

1

I think a more effective method would be to create a drop-down list and allow the user to select the CRS instead of using two booleans:

Drop-down list


In your script, replace the following:

##Coordinate_System_SLD_99=boolean 
##Coordinate_System_Kadawala=boolean 

with:

##Coordinate_system=selection SLD_99;Kadawala

This drop-down list returns an integer value (i.e. 0 for the first item, 1 for the second item etc.). So for your if statement, you could use the following logic:

if Coordinate_system == 0:
    processing.runalg('qgis:reprojectlayer',outputs_QGISADDFIELDTOATTRIBUTESTABLE_7['OUTPUT_LAYER'],'EPSG:5235',Shape_File_Name)
else:
    processing.runalg('qgis:reprojectlayer', outputs_QGISADDFIELDTOATTRIBUTESTABLE_7['OUTPUT_LAYER'],'EPSG:5234',Shape_File_Name)
4
  • @ShanakaHerath - Near the beginning of your code, you need to remove the boolean parameters (##Coordinate_System_SLD_99=boolean ##Coordinate_System_Kadawala=boolean) and add ##Coordinate_system=selection SLD_99;Kadawala. Save the script then run it, you should see the list menu from the interface.
    – Joseph
    Aug 21, 2018 at 10:45
  • still problem exist.It will work once i close the qgis and reopen only.then again it will not work for the other option Aug 21, 2018 at 11:06
  • @ShanakaHerath - Apologies but I can't replicate your problem. A simple script works for me, tested on QGIS 2.18.17 on Win7 64-bit.
    – Joseph
    Aug 21, 2018 at 12:57
  • Anyway, thanks a lot for the interest shown regarding my question. highly appreciate. I am using QGIS 2.18.19 on win 7 64-bit. Aug 21, 2018 at 14:58
1

When you select a coordinate system, make sure to set the boolean value of the other coordinate system variable to false.

--> when selecting Coordinate_System_Kadawala, set Coordinate_System_SLD_99 to false

1
  • i normally do it.thought i do it ,it gives that wrong result Aug 21, 2018 at 10:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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