Reputation: 1
I'm trying to plot weather data to image. The source of data is XML file. I display the data on image with php code below. Everything seems to be going well except for the wind barbs. My idea is that the wind direction has to be plotted as arrows, from where the wind blows. The xml data for the wind direction are given with characters:
N - north, S - south, E - east, W - west, NW - northwest, NE - northeast, SW - southwest, SE - southeast
The question is how to change the php code below so that an arrow can be plotted into the image?
Basically I want to change the letter NW (northwest) on image into arrow (arrow from the northwest direction).
Here is the code:
<?php
$rss = simplexml_load_file('city_1.xml');
$rss2 = simplexml_load_file('city_2.xml');
$rss3 = simplexml_load_file('city_3.xml');
$rss4 = simplexml_load_file('city_4.xml');
$rss5 = simplexml_load_file('city_5.xml');
$title = $rss->metData->tsValid_issued_UTC;
$data = $rss->metData->dd_icon;
$data2 = $rss2->metData->dd_icon;
$data3 = $rss3->metData->dd_icon;
$data4 = $rss4->metData->dd_icon;
$data5 = $rss5->metData->dd_icon;
//PHP's GD class functions can create a variety of output image
header("Content-Type: image/png");
//open up the image you want to put text over
$im = ImageCreateFrompng("country_map.png");
//The numbers are the RGB values of the color you want to use
$purple = ImageColorAllocate($im, 215, 4, 168);
$black = ImageColorAllocate($im, 0, 0, 0);
// Image title
$title_x = 5;
$title_y = 15;
// City 1
$start_x = 172;
$start_y = 390;
// City 2
$start_x1 = 229;
$start_y1 = 257;
// City 3
$start_x2 = 170;
$start_y2 = 340;
// City 4
$start_x3 = 124;
$start_y3 = 368;
// City 5
$start_x4 = 43;
$start_y4 = 411;
//Copyrights
$start_x35 = 428;
$start_y35 = 475;
$start_x36 = 428;
$start_y36 = 460;
//This writes your text on the image
Imagettftext($im, 11, 0, $title_x, $title_y, $black, 'verdana.ttf', "Wind direction $title");
Imagettftext($im, 10, 0, $start_x, $start_y, $purple, 'verdana.ttf', "$data");
Imagettftext($im, 10, 0, $start_x1, $start_y1, $purple, 'verdana.ttf', "$data2");
Imagettftext($im, 10, 0, $start_x2, $start_y2, $purple, 'verdana.ttf', "$data3");
Imagettftext($im, 10, 0, $start_x3, $start_y3, $purple, 'verdana.ttf', "$data4");
Imagettftext($im, 10, 0, $start_x4, $start_y4, $purple, 'verdana.ttf', "$data5");
Imagettftext($im, 10, 0, $start_x35, $start_y35, $black, 'verdana.ttf', "Data: meteo");
Imagettftext($im, 10, 0, $start_x36, $start_y36, $black, 'verdana.ttf', "Copyright 2020");
//Creates the image
Imagepng($im, 'png/wind.png');
ImageDestroy($im);
?>
...and here is the XML file:
<data id="MeteoSI_WebMet_observation_xml">
<metData>
<domain_meteosiId>NOVO-MES_</domain_meteosiId>
<domain_id>64783785</domain_id>
<valid>09.02.2020 21:00 CET</valid>
<valid_UTC>09.02.2020 20:00 UTC</valid_UTC>
<t_var_desc>Temperature</t_var_desc>
<t_var_unit>°C</t_var_unit>
<t>7</t>
<t_degreesC>7</t_degreesC>
<rh_var_desc>Humidity</rh_var_desc>
<rh_var_unit>%</rh_var_unit>
<rh>57</rh>
<td_var_desc>Dewpoint Temperature</td_var_desc>
<td_var_unit>°C</td_var_unit>
<td>0</td>
<td_degreesC>0</td_degreesC>
<dd_var_desc>Wind Direction</dd_var_desc>
<dd_var_unit>°</dd_var_unit>
<dd_val>266</dd_val>
<dd_icon>NW</dd_icon>
<dd_decodeText>NW</dd_decodeText>
<ff_var_desc>Wind Speed</ff_var_desc>
<ff_var_unit>m/s</ff_var_unit>
<ff_val>3.1</ff_val>
<ff_val_kmh>11</ff_val_kmh>
<ff_value>3</ff_value>
<ff_value_kmh>11</ff_value_kmh>
<ff_minimum>0</ff_minimum>
<ff_minimum_kmh>0</ff_minimum_kmh>
<ff_maximum>5</ff_maximum>
<ff_maximum_kmh>18</ff_maximum_kmh>
<ff_icon>light</ff_icon>
<domain_geoType>general</domain_geoType>
<note/>
</metData>
</data>
Upvotes: 0
Views: 98
Reputation: 1
What about if I try to replace Wind direction text with numbers? then I can generate image with arrows font, every number for defined arrow direction? For example:
N -> 1 W -> 2 E -> 3 S -> 4 NW-> 5 ...
I try to put the if statement into the my php code, but the generated image is always displayed with original text from the XML file?
$rss2 = simplexml_load_file('city_2.xml');
$data2 = $rss2->metData->ddavg_icon;
$se = "SE";
$n = "N";
$wind = $data2;
if($wind = $se){
print "1";
} elseif($wind = $n){
print "2";
}
//This writes your text on the image
Imagettftext($im, 12, 0, $title_x, $title_y, $black, 'verdana.ttf', "Wind direction $title");
Imagettftext($im, 9, 0, $start_x, $start_y, $purple, 'verdana.ttf', "$data1");
Imagettftext($im, 9, 0, $start_x1, $start_y1, $purple, 'verdana.ttf', $wind);
Imagettftext($im, 9, 0, $start_x2, $start_y2, $purple, 'verdana.ttf', "$data3");
Upvotes: 0