- https://www.mathworks.com/help/matlab/ref/jet.html
 - https://www.mathworks.com/help/matlab/ref/interp1.html
 
Generate a kml file plotting a multi-colored line
    13 views (last 30 days)
  
       Show older comments
    
I would like to generate a kml file plotting a line where its color is related to a mesaured value in that location. For example:
Lat   Lon     Concentration
10      70         57
11      71         58
12      72         59
13      73         57
The line should be blue in the begining and end, and red in the location (12;   72) as the concentratoin there is higher.
0 Comments
Answers (1)
  Suraj Kumar
 on 9 Sep 2024
        Hi Alessio, 
To generate a KML file using MATLAB to plot a line where the colour of the line segments is related to a measured concentration value, go through the following steps and attached code: 
1. Setup the data including the longitude, latitude and concentration values. Then normalize the concentration values to colours in a 0-1 range. 
minConc = min(concentration); 
maxConc = max(concentration); 
normalizedConc = (concentration - minConc) / (maxConc -     minConc); 
 2. Then you can map the concentration to the colours using ‘jet’ colourmap and interpolate the normalized concentration values to RGB colours using the ‘interp1’ function in MATLAB. 
cmap = jet(256);  
colors = interp1(linspace(0, 1, size(cmap, 1)), cmap, normalizedConc);
3. Create a new KML file for writing with a standard header structure and create a line segment for each point. 
kmlFileName = 'multicolored_line.kml'; 
fid = fopen(kmlFileName, 'w'); 
% KML Header 
fprintf(fid, '<?xml version="1.0" encoding="UTF-8"?>\n'); 
fprintf(fid, '<Document>\n'); 
4. For each line segment define a unique style in the KML file including its colour and width. 
fprintf(fid, '<Style id="lineStyle%d">\n', i); 
fprintf(fid, '  <LineStyle>\n'); 
fprintf(fid, '    <color>%s</color>\n', kmlColor); 
fprintf(fid, '    <width>2</width>\n'); 
fprintf(fid, '  </LineStyle>\n'); 
fprintf(fid, '</Style>\n'); 
5. Close the KML file with appropriate footer tags and output the script file. 
% KML Footer 
fprintf(fid, '</Document>\n'); 
fprintf(fid, '</kml>\n'); 
fclose(fid); 
To learn more about the ‘jet’ or ‘interp1’ function in MATLAB, kindly go through the documentations linked below: 
Hope this works for you! 
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!