<div dir="ltr">Hi!<div><br></div><div>As I said in a previous message, I use qgis to put together many geographic information sources prior to downloading the data as kmz to my mobile.</div><div><br></div><div>That's the reason I wrote a small script to generate kmz icons from a qgis-generated kml.</div>

<div><br></div><div>That script is a "sed" script, meaning it has to be processed with the "sed" utility included in Linux. If you have a Windows version of qgis bundled with Msys (such as osgeo4win), you could probably execute it from the msys command line.</div>

<div><br></div><div>Someone could try to adapt it to python, but I don't know how to make python scripts.</div><div><br></div><div>Please note: Lines are long and they could be wrapped when sendind as email. I will use an empty line (two carriage returns) to mark new line.</div>

<div><br></div><div><br></div><div>-----------------------------------------------------------------------------------------------------------------------------------</div><div>SCRIPT 1 ---- Generate icons from "name" field</div>

<div><br></div><div>This script takes a qgis-generated kml file, and adds an icon (called "Cueva.png") to every feature with a name containing "Cueva" or "Sima", another icon (called ."Icnitas.png") to every feature with a name containing "icnita" or "Icnita", </div>

<div>another icon (called "Fosil.png") to every feature with a name like "Fosil", "Fósil", "fosil" or "fósil", and another icon (called "Mina.png") to every feature with a name containing "Mina".</div>

<div><br></div><div>The icons should be placed in a folder called "images", and zipped along with the modified kml file in a file with kmz extension.</div><div><br></div><div>The script should be invoked like this:</div>

<div><br></div><div><div>sed mykmlfile.kml -f kmzfromname.sed > newkmlfile.kml</div><div><br></div><div>After that, you should use your preferred zip manager to zip together the files. I use this:</div><div><br></div>
<div>
7z -a -tzip newkmlfile.kmz newkmlfilekml images/*.png</div><div><br></div><div>but the syntax would be different if you use a different zip manager.</div><div><br></div><div>----kmziconfromname.sed------------------CUT HERE------------------------------------------</div>

<div>#!/bin/sed</div><div><br></div><div>#Replace name tag with Cueva or cueva with icon and name tag</div><div><br></div><div>/name>[^>]*[Cc]ueva/{;s/<name>/<icon><href>images\/Cueva.png<\/href><\/icon><name>/;};</div>

<div><br></div><div><div>#Replace name tag with Sima or sima with icon and name tag</div></div><div><br></div><div>/name>[^>]*[Ss]ima/{;s/<name>/<icon><href>images\/Cueva.png<\/href><\/icon><name>/;};</div>

<div><br></div><div>#Replace name tag with Icnita or icnita with icon and name tag<br></div><div><br></div><div>/name>[^>]*[Ii]cnita/{;s/<name>/<icon><href>images\/Icnitas.png<\/href><\/icon><name>/;}</div>

<div><br></div><div>#Replace name tag with F?sil or f?sil (probably, fosil or fósil) with icon and name tag<br></div><div><br></div><div>/name>[^>]*[Ff].sil/{;s/<name>/<icon><href>images\/Fosil.png<\/href><\/icon><name>/;}</div>

<div><br></div><div>#Replace name tag with Mina or mina with icon and name tag<br></div><div><br></div><div>/name>[^>]*[Mm]ina/{;s/<name>/<icon><href>images\/Mina.png<\/href><\/icon><name>/;}</div>

<div><br></div><div>#Previous code for icons was a pseudocode. Replace it with the real code.</div><div><br></div><div>s!<icon>!<Style><IconStyle><scale>1.0</scale><Icon>!;</div><div><br>

</div><div>s!</icon>!</Icon></IconStyle><LabelStyle><scale>1.0</scale></LabelStyle><LineStyle><width>1,0</width></LineStyle></Style>!</div><div><br>
</div>
<div>#I don't know why, but replacing folder with document allows oruxmaps put a name (instead of a date) to this layer.</div><div><br></div><div>s!Folder>!Document>!</div></div><div><br></div><div>----kmziconfromname.sed-------END OF FILE-----------------------------------------------------------</div>

<div><br></div><div><br></div><div>SCRIPT 2 ----- Generate icons from a custom field (not "name")</div><div><br></div><div>This is a skeleton script. In the previous sample, I used the name to detect the class of each feature, but my kml file has a field caled T006_006_C that stores the actual feature class.</div>

<div><br></div><div>So I could use that field instead.</div><div><br></div><div>But, in order to do that, I have to read the field inside the <ExtendedData> tag and, after that, put a new flag outside that tag.</div>

<div><br></div><div>Doing that with SED is tricky, because SED uses to read one line at a time. We have to tell it to group the whole <ExtendedData> group in a single line.</div><div><br></div><div>Please notice you should adapt this script to your needs, i.e., replace T006_006_C with the field name you want, add code to replace the <!---icon --> tags with your preferred icon, and so on.</div>

<div><br></div><div>The usage would be again,</div><div><br></div><div><div>sed mykmlfile.kml -f kmzfromfield.sed > newkmlfile.kml</div><div><br></div><div>After that, you should use your preferred zip manager to zip together the files. I use this:</div>

<div><br></div><div>7z -a -tzip newkmlfile.kmz newkmlfilekml images/*.png</div><div><br></div><div>but the syntax would be different if you use a different zip manager.</div></div><div><br></div><div>---kmziconfromfield.sed----------------------CUT HERE------------------------------------------------------ </div>

<div><div>#!/usr/bin/sed</div><div><br></div><div>#Example: how to make icon from field using SED</div><div><br></div><div>#Replace T006_006_C with fieldname</div><div><br></div><div>:a</div><div><br></div><div>N</div><div>

<br></div><div>#Do not put spaces after the backslash in the next line:</div><div><br></div><div>s/\</div><div>/ /g<br></div><div><br></div><div><br></div><div>/\/ExtendedData/!ba</div><div><br></div><div>s/\(<ExtendedData.*SimpleData name=.T006_006_C.>\)\([^<]*\)\(.*<\/ExtendedData>\)/\1\2\3<!-- Icon_For:\2 -->/</div>

<div><br></div><div>#Step 2: Replace <!-- Icon_For:\2 --> with code for icon (<Style><IconStyle... etc) (See script 1)</div><div><br></div><div>#Now you have to change all categories with their icon</div><div>

<br></div><div>#This is an example on how to change "Icon fror:Modelado K*" to the "Cueva.png" icon:</div><div><br></div><div>s/<!-- Icon for:Modelado [Kk].*-->/<icon>Cueva.png<\/icon>/</div>

<div><br></div><div>#Now we change <Icon> pseudocode with real icon code.</div><div><br></div><div>s!<icon>!<Style><IconStyle><scale>1.0</scale><Icon>!;</div><div><br></div><div>
s!</icon>!</Icon></IconStyle><LabelStyle><scale>1.0</scale></LabelStyle><LineStyle><width>1,0</width></LineStyle></Style>!</div>
<div><br></div><div>#Last of all, we change the <Folder> tag with the <Document> Tag.</div><div><br></div><div>#It gives a name for kml layer in oruxmaps.</div><div><br></div><div>s!Folder>!Document>!</div>

</div><div><br></div><div><div>--kmziconfromfield.sed----------------------END OF FILE----------------------------------------------- </div></div></div>