Friday, 24 February 2017

Converting ArcGIS raster colormap file (*.clr) to QGIS style file (*.qml)


I have an ArcGIS colormap (*.clr) and I want to open it in QGIS to apply a predefined style.


The *.clr file is really simple, only has four columns representing [value] [red] [green] [blue], as this example:


19 161 161 161
21 152 181 129
22 114 168 144

23 124 142 173

Is there a way to read these columns and convert them in a QGIS style file (*.qml)?



Answer



Nice answer from @whyzar! You could load .clr files into QGIS and then save it as a .qml file. As described in this post, the standard text format is:


Value R G B Alpha Label

So in your case, you could create a text file with:


19,161,161,161,255,19
21,152,181,129,255,21

22,114,168,144,255,22
23,124,142,173,255,23

And load it from the menu:


Result


You can then edit and save the style as a .qml.




From @Stefan's comment, I've included some quick code which reformats the input text file in a way that QGIS can read it (hopefully!):


main_path = 'path/to/directory/'
with open(main_path + 'infile.txt') as infile, open(main_path + 'outfile.txt', 'w') as outfile:

for line in infile:
value = line.split(' ')[0]
line = line.replace(' ', ',').rstrip('\n') + ',255,' + value + '\n'
outfile.write(line)

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...