[Qgis-developer] QGis and string replacement

Michele Mammini mamminim at yahoo.it
Tue Feb 2 05:11:06 EST 2010


Hi all,

I have another problem related to the string replacement in QGis.
I want to read a QGis Style file at runtime and write in another file 
some modifications.

If I try to execute the single command in Python Console everything 
is working and my line is changed, while if I execute it in my plugin 
nothing happen.

Do you know where i made a mistake?

This is a part of the file:
     ...
     <customColorRamp>
       <colorRampType>LINEAR</colorRampType>
       <colorRampEntry red="0" blue="143" value="1.000000" green="0" 
label="Color entry 1" />
       <colorRampEntry red="0" blue="159" value="2.000000" green="0" 
label="Color entry 2" />
       <colorRampEntry red="0" blue="175" value="3.000000" green="0" 
label="Color entry 3" />
     ...

This is what i exec in Python Console:
 >>> fIn = 
open("C:\\Users\\Michele\\.qgis\\python\\plugins\\KitICT\\style\\colormap64.qml","r")
...
 >>> szLine = fIn.readline()
 >>> delta = (54.0-42.0+1)/64.0
 >>> i=0
 >>> szToSearch = "value=\"" + str(i+1) + ".000000\""
 >>> szToReplace = "value=\"" + str(round(42.0 + i*delta,6)) + "\""
 >>> szLine.replace(szToSearch, szToReplace)
<colorRampEntry red="0" blue="143" value="42.0" green="0" 
label="Color entry 1" />

And this is my function:
     ...
     fIn  = open(szFileIn, "r")
     fOut = open(szFileOut, "w")

     i = 0
     delta = (nMax-nMin+1)/64

     szLine = fIn.readline()
     while len(szLine) > 0:
         if szLine.find("<colorRampEntry") > -1:
             szToSearch = "value=\"" + str(i+1) + ".000000\""
             szToReplace = "value=\"" + str(round(nMin + i*delta,6)) + "\""
             szLine = str(szLine)

             szLine.replace(szToSearch, szToReplace)
             fOut.write(szLine)
             i = i+1
         else:
             fOut.write(szLine)
         pass
         szLine = fIn.readline()
     pass

Where szFileIn and szFileOut are the path if the file to read/write 
and nMax/nMin are the minumun and maximum coming from bandStatistics.

I'm using QGis 1.0.2 Kore and I'm developing with Python.



______________________________________________________________________
Michele Mammini
Via dell'Arno, 54 - San Benedetto
56021
Cascina (PISA)

Tel:            +39 050 747037
Cell:           +39 328 9039640

email:  <mailto:mamminim at yahoo.it>mamminim at yahoo.it
Skype:  mamminim
______________________________________________________________________  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.osgeo.org/pipermail/qgis-developer/attachments/20100202/9831521f/attachment.html


More information about the Qgis-developer mailing list