Does anyone have any practical examples of programmatically drawing icons as vectors in KML? Specifically, I have data with a magnitude and an azimuth at given coordinates, and I would like to have icons (or another graphical element) generated based on these values.
Some thoughts on how I might approach it:
Image directory (a brute force way): Make an image director of 360 different image files (probably batch rotate a single image) each pointing in a cooresponding azimuth. I've seen things like "Excel to KML," but am looking for code that I can use within a program, rather than a web utility. Issue: Arrow does not contain magnitude context, so that would have to be a label. I'd rather dynamically lengthen the arrow.
Line creation in KML: Perhaps create a formula that creates a line with the origin at the coordinate points, with the length of the line proportional to the magnitute, and angled according to azimuth. There would then be two more lines, perhaps 30 degrees or so extending from the end of the previous line to make the arrow head. Issues: Not a separate image icon, so not sure how it would work in KML. Also not sure how easy it would be to generate this algorithm.
Separate image generation: Perhaps create a PHP file that uses imagemagick or something similar to dynamically generate a .png file in a similar method to the above, and then link to the icon using the URI "domain.tld/imagegen.php?magnitude=magvalue&azimuth=azmvalue". Issue: Still have the problem of actually writing the algorithm for image generation.
So, the question: has anyone else come up with solutions for programmatic vector (rather than merely arrow) generation?
I've seen this done (in Fortran!) with a variant of option 1, but using only one image of an arrow and setting the size and direction using the KML IconStyle element.
The trick is to first convert your vectors from whatever format you have them in into headings (in degrees clockwise from north so between 0 and 360 degrees) and the vector lengths into a scale factor (a float in decimal notation). You also need to give each vector a unique name. Then for each vector create a chunk of KML, say for vector "my-vector-1" with a scale of 1.8 and a heading of 90.0 (i.e. east):
<IconStyle id="my-vector-1">
<scale>1.8</scale>
<heading>90.0</heading>
<Icon>
<href>/path/to/arrow.png</href>
</Icon>
</IconStyle>
and then to place the vector on the map reference the IconStyle:
<Placemark>
<styleUrl>#my-vector-1</styleUrl>
<Point>
<coordinates>-10.5,10.8</coordinates>
</Point>
wrap all that up in:
<kml><Document> ... </Document></kml>
and you should be displaying a bunch of vector. Getting the scale factor correct may take some experimentation (and will depend on the size of your arrow image), but this approach should be a lot quicker then loading lots of different images. If it's any help, I could probably dig out the Fortran to do this...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With