[Qgis-user] kmz export sed script

José G Moya Y. josemoya at gmail.com
Tue Sep 2 06:12:06 PDT 2014


Hi!

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.

That's the reason I wrote a small script to generate kmz icons from a
qgis-generated kml.

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.

Someone could try to adapt it to python, but I don't know how to make
python scripts.

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.


-----------------------------------------------------------------------------------------------------------------------------------
SCRIPT 1 ---- Generate icons from "name" field

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",
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".

The icons should be placed in a folder called "images", and zipped along
with the modified kml file in a file with kmz extension.

The script should be invoked like this:

sed mykmlfile.kml -f kmzfromname.sed > newkmlfile.kml

After that, you should use your preferred zip manager to zip together the
files. I use this:

7z -a -tzip newkmlfile.kmz newkmlfilekml images/*.png

but the syntax would be different if you use a different zip manager.

----kmziconfromname.sed------------------CUT
HERE------------------------------------------
#!/bin/sed

#Replace name tag with Cueva or cueva with icon and name tag

/name>[^>]*[Cc]ueva/{;s/<name>/<icon><href>images\/Cueva.png<\/href><\/icon><name>/;};

#Replace name tag with Sima or sima with icon and name tag

/name>[^>]*[Ss]ima/{;s/<name>/<icon><href>images\/Cueva.png<\/href><\/icon><name>/;};

#Replace name tag with Icnita or icnita with icon and name tag

/name>[^>]*[Ii]cnita/{;s/<name>/<icon><href>images\/Icnitas.png<\/href><\/icon><name>/;}

#Replace name tag with F?sil or f?sil (probably, fosil or fósil) with icon
and name tag

/name>[^>]*[Ff].sil/{;s/<name>/<icon><href>images\/Fosil.png<\/href><\/icon><name>/;}

#Replace name tag with Mina or mina with icon and name tag

/name>[^>]*[Mm]ina/{;s/<name>/<icon><href>images\/Mina.png<\/href><\/icon><name>/;}

#Previous code for icons was a pseudocode. Replace it with the real code.

s!<icon>!<Style><IconStyle><scale>1.0</scale><Icon>!;

s!</icon>!</Icon></IconStyle><LabelStyle><scale>1.0</scale></LabelStyle><LineStyle><width>1,0</width></LineStyle></Style>!

#I don't know why, but replacing folder with document allows oruxmaps put a
name (instead of a date) to this layer.

s!Folder>!Document>!

----kmziconfromname.sed-------END OF
FILE-----------------------------------------------------------


SCRIPT 2 ----- Generate icons from a custom field (not "name")

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.

So I could use that field instead.

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.

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.

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.

The usage would be again,

sed mykmlfile.kml -f kmzfromfield.sed > newkmlfile.kml

After that, you should use your preferred zip manager to zip together the
files. I use this:

7z -a -tzip newkmlfile.kmz newkmlfilekml images/*.png

but the syntax would be different if you use a different zip manager.

---kmziconfromfield.sed----------------------CUT
HERE------------------------------------------------------
#!/usr/bin/sed

#Example: how to make icon from field using SED

#Replace T006_006_C with fieldname

:a

N

#Do not put spaces after the backslash in the next line:

s/\
/ /g


/\/ExtendedData/!ba

s/\(<ExtendedData.*SimpleData
name=.T006_006_C.>\)\([^<]*\)\(.*<\/ExtendedData>\)/\1\2\3<!-- Icon_For:\2
-->/

#Step 2: Replace <!-- Icon_For:\2 --> with code for icon
(<Style><IconStyle... etc) (See script 1)

#Now you have to change all categories with their icon

#This is an example on how to change "Icon fror:Modelado K*" to the
"Cueva.png" icon:

s/<!-- Icon for:Modelado [Kk].*-->/<icon>Cueva.png<\/icon>/

#Now we change <Icon> pseudocode with real icon code.

s!<icon>!<Style><IconStyle><scale>1.0</scale><Icon>!;

s!</icon>!</Icon></IconStyle><LabelStyle><scale>1.0</scale></LabelStyle><LineStyle><width>1,0</width></LineStyle></Style>!

#Last of all, we change the <Folder> tag with the <Document> Tag.

#It gives a name for kml layer in oruxmaps.

s!Folder>!Document>!

--kmziconfromfield.sed----------------------END OF
FILE-----------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20140902/9b5a0d39/attachment.html>


More information about the Qgis-user mailing list