AussieKlutz Posted December 2, 2008 Share Posted December 2, 2008 Hey guys. I was going through some old site code while doing a major recode and rebuild, when I found some code I used earlier on in the year that I want to tidy up and put out there for the community. The story is that I was moving from far north-west queensland, australia to tasmania, australia. A roadtrip of about 4000 Km. I wanted to be able to post to my blog en-route and provide a map so that my family and friends could see where I was at. The first thing was a simple little php script that logged into a pop3 mailbox and parsed a email into a blog post. I also had a similar script that parsed and stored a gps location. Being kinda poor, owning an old phone with no wap and sms' being so cheap, I found a free sms to email gateway. This allowed me to post my blogs as well as update my location in a database. The second piece of code took the gps points out of a route (from .... to ....) in a google maps kml file (the compressed file in a kmz file) and drew them using the image functions in php. I also drew some other kml files into the image to give me state borders, etc. I also drew some city points and names, giving a result something like this: Quote Link to comment Share on other sites More sharing options...
DingleBerries Posted December 2, 2008 Share Posted December 2, 2008 Do we get to see the code? Quote Link to comment Share on other sites More sharing options...
AussieKlutz Posted December 2, 2008 Author Share Posted December 2, 2008 its a bit of a mess mashup atm... i'll strip some email addresses and pwds and so on tonight and post teh code update: first gen code: Not sure if this is working, my host is down, i gotta install wamp and test it tonight <?php //function get coords function getCoords($filename, $linebreak = true) { // open kml file $mapfile = file($filename); $coordsindex = 0; $nextline = false; // step throught kml file and extract all coordinates foreach ($mapfile as $line) { if($nextline) { $thesepoints = explode(' ', $line); foreach ($thesepoints as $point) { $thesecoords = explode(',', $point); // convert to absolute values for($i=0;$i<count($thesecoords);$i++) { $thesecoords[$i] = abs($thesecoords[$i]); } // build coords array if(count($thesecoords) == 3) { $coords[$coordsindex] = $thesecoords; //print_r($thesecoords); //echo('<br />' . '/n'); $coordsindex++; } } if($linebreak) { $coords[$coordsindex] = 'fred'; $coordsindex++; } } if(strstr($line, '<coordinates>')) { $nextline = true; } else { $nextline = false; } } return $coords; } // function drawRoute() function drawRoute($coords, $map, $ratio, $latoffset, $lonoffset, $color, $points, $repair = false) { //echo('----<br />'); $longcolor = imagecolorallocate($map, 255, 0, 0); // draw route $previouspoint = $coords[0]; $point = 0; $breakpoints = false; foreach($coords as $currentpoint) { if($currentpoint == 'fred') { $breakpoints = true; } else { if($breakpoints) { $previouspoint = $currentpoint; } $pointdistance = round(sqrt(((($ratio * ($currentpoint[0] + $latoffset))-($ratio * ($previouspoint[0] + $latoffset)))*(($ratio * ($currentpoint[0] + $latoffset))-($ratio * ($previouspoint[0] + $latoffset)))) + ((($ratio * ($currentpoint[1] + $lonoffset))-($ratio * ($previouspoint[1] + $lonoffset)))*(($ratio * ($currentpoint[1] + $lonoffset))-($ratio * ($previouspoint[1] + $lonoffset)))))); if($pointdistance > 7 && (round($ratio * ($previouspoint[0] + $latoffset) != round($ratio * ($currentpoint[0] + $latoffset))) && (round($ratio * ($previouspoint[1] + $lonoffset)) != round($ratio * ($currentpoint[1] + $lonoffset))))) { if($repair) { $previouspoint = $currentpoint; } } if($point < $points) { imageline($map, round($ratio * ($previouspoint[0] + $latoffset)), round($ratio * ($previouspoint[1] + $lonoffset)), round($ratio * ($currentpoint[0] + $latoffset)), round($ratio * ($currentpoint[1] + $lonoffset)), $color); $point++; $breakpoints = false; } /* $pointdistance = round(sqrt(((($ratio * ($currentpoint[0] + $latoffset))-($ratio * ($previouspoint[0] + $latoffset)))*(($ratio * ($currentpoint[0] + $latoffset))-($ratio * ($previouspoint[0] + $latoffset)))) + ((($ratio * ($currentpoint[1] + $lonoffset))-($ratio * ($previouspoint[1] + $lonoffset)))*(($ratio * ($currentpoint[1] + $lonoffset))-($ratio * ($previouspoint[1] + $lonoffset)))))); if($pointdistance > 7 && (round($ratio * ($previouspoint[0] + $latoffset) != round($ratio * ($currentpoint[0] + $latoffset))) && (round($ratio * ($previouspoint[1] + $lonoffset)) != round($ratio * ($currentpoint[1] + $lonoffset))))) { echo('Long point: ' . $pointdistance . '<br />'); print_r($currentpoint); echo('<br />'); print_r($previouspoint); echo('<br />(' . $point . '/' . count($coords) . ')<br />'); imageline($map, round($ratio * ($previouspoint[0] + $latoffset)), round($ratio * ($previouspoint[1] + $lonoffset)), round($ratio * ($currentpoint[0] + $latoffset)), round($ratio * ($currentpoint[1] + $lonoffset)), $longcolor); } */ //debugging //if(!(round($ratio * ($previouspoint[0] + $latoffset)) < $width && round($ratio * ($previouspoint[0] + $latoffset)) > 1)) echo(' 1 ' . round($ratio * ($previouspoint[0] + $latoffset)) . '<br />'); //if(!(round($ratio * ($previouspoint[1] + $lonoffset)) < $height && round($ratio * ($previouspoint[1] + $lonoffset)) > 1)) echo(' 2 ' . round($ratio * ($previouspoint[1] + $lonoffset)) . '<br />'); //if(!(round($ratio * ($currentpoint[0] + $latoffset)) < $width && round($ratio * ($currentpoint[0] + $latoffset)) > 1)) echo(' 3 ' . round($ratio * ($currentpoint[0] + $latoffset)) . '<br />'); //if(!(round($ratio * ($currentpoint[1] + $lonoffset)) < $height && round($ratio * ($currentpoint[1] + $lonoffset)) > 1)) echo(' 4 ' . round($ratio * ($currentpoint[1] + $lonoffset)) . '<br />'); $previouspoint = $currentpoint; } } } //function drawplaces function drawplaces2($filename, $map, $ratio, $latoffset, $lonoffset, $color) { // open coma delimited file $mapfile = file($filename); $previousxposition = 0; $previousyposition = 0; $previousname = ''; // step throught kml file and extract all coordinates foreach ($mapfile as $line) { $datum = explode(',', $line); $place = $datum[0]; $latitude = abs($datum[4]); $longitude = abs($datum[3]); $currentxposition = round($ratio * ($latitude + $latoffset)); $currentyposition = round($ratio * ($longitude + $lonoffset)); if((($currentxposition > $previousxposition) && ($currentxposition < ($previousxposition+(imagefontwidth(2)*strlen($previousname))))) && (($currentyposition > ($previousyposition-(imagefontheight(2)))) && ($currentyposition < ($previousyposition+(imagefontheight(2)))))) { $overlapoffset = imagefontheight(2); } else { $overlapoffset = 0; } imagefilledellipse($map, round($ratio * ($latitude + $latoffset)), round($ratio * ($longitude + $lonoffset)), 8, 8, $color); imagestring($map, 2, round($ratio * ($latitude + $latoffset))+10, round($ratio * ($longitude + $lonoffset))+10+$overlapoffset, $place, $color); $previousxposition = $currentxposition; $previousyposition = $currentyposition; $previousname = $place; } } // function colorPoints() function colorPoints($coords, $map, $ratio, $latoffset, $lonoffset, $color, $points) { foreach($coords as $currentpoint) { if($currentpoint != 'fred') { if($point < $points) { imagefill($map, round($ratio * ($currentpoint[0] + $latoffset)), round($ratio * ($currentpoint[1] + $lonoffset)), $color); } $point++; } } } // settings $imagewidth = 500; $imageheight = 500; $frames = 1; // retrieve main route $coords = getCoords($kmlfile); // determine max and min lat and lon values $minlat = 1000; $maxlat = -1000; $minlon = 1000; $maxlon = -1000; foreach($coords as $gpscoord) { if($gpscoord != 'fred') { if($gpscoord[0] < $minlat) $minlat = $gpscoord[0]; if($gpscoord[0] > $maxlat) $maxlat = $gpscoord[0]; if($gpscoord[1] < $minlon) $minlon = $gpscoord[1]; if($gpscoord[1] > $maxlon) $maxlon = $gpscoord[1]; } } //echo('Minlat: ' . $minlat . '<br />'); //echo('Maxlat: ' . $maxlat . '<br />'); //echo('Minlon: ' . $minlon . '<br />'); //echo('Maxlon: ' . $maxlon . '<br />'); // determine total lat and lon range $totlat = $maxlat - $minlat; $totlon = $maxlon - $minlon; //echo('Totlat: ' . $totlat . '<br />'); //echo('Totlon: ' . $totlon . '<br />'); // calculate ideal image ratio $gpsratio = $totlat / $totlon; if($gpsratio >= 4/3) $ratio = $imagewidth / ($totlat * 2); if($gpsratio < 4/3) $ratio = $imageheight / ($totlon * 2); //echo('Original Ratio (w/h): ' . $gpsratio . '<br />'); //echo('Ratio (pixels/degree): ' . $ratio . '<br />'); // determine image width and height $width = round($ratio * $totlat * 2); $height = round($ratio * $totlon * 2); //echo('Width: ' . $width . '<br />'); //echo('Height: ' . $height . '<br />'); // determine offset constants $latoffset = -($minlat) + ($totlat * 0.5) + ((($imagewidth - $width)/2)/$ratio); $lonoffset = -($minlon) + ($totlon * 0.5) + ((($imageheight - $height)/2)/$ratio); //echo('Lat Offset: ' . $latoffset . '<br />'); //echo('Lon Offset: ' . $lonoffset . '<br />'); // calculate points per frame $pointsperframe = count($coords) / $frames; // begin animation loop for($frame = 0; $frame < $frames; $frame++) { // create image $map = ImageCreateTrueColor($imagewidth, $imageheight); $white = imagecolorallocate($map, 255, 255, 255); $black = imagecolorallocate($map, 0, 0, 0); $aabbcc = imagecolorallocate($map, 0xaa, 0xbb, 0xcc); $aaccbb = imagecolorallocate($map, 0xaa, 0xcc, 0xbb); $nnccbb = imagecolorallocate($map, 0x00, 0xcc, 0xbb); $red = imagecolorallocate($map, 255, 0, 0); imagefill($map, 0, 0, $white); imagesetthickness($map, 3); // draw states $aus1 = getCoords('aus1.kml'); $aus2 = getCoords('aus2.kml'); $aus3 = getCoords('aus3.kml'); $aus4 = getCoords('aus4.kml'); $aus5 = getCoords('aus5.kml'); $aus6 = getCoords('aus6.kml'); $aus7 = getCoords('aus7.kml'); drawRoute($aus1, $map, $ratio, $latoffset, $lonoffset, $aabbcc, count($aus1)); drawRoute($aus2, $map, $ratio, $latoffset, $lonoffset, $aabbcc, count($aus2)); drawRoute($aus3, $map, $ratio, $latoffset, $lonoffset, $aabbcc, count($aus3)); drawRoute($aus4, $map, $ratio, $latoffset, $lonoffset, $aabbcc, count($aus4)); drawRoute($aus5, $map, $ratio, $latoffset, $lonoffset, $aabbcc, count($aus5)); drawRoute($aus6, $map, $ratio, $latoffset, $lonoffset, $aabbcc, count($aus6)); drawRoute($aus7, $map, $ratio, $latoffset, $lonoffset, $aabbcc, count($aus7)); // color map //$states = getCoords('states.kml'); //colorPoints($states, $map, $ratio, $latoffset, $lonoffset, $nnccbb, count($states)); //draw places drawplaces2($locationsfile, $map, $ratio, $latoffset, $lonoffset, $black); // draw main route imagesetthickness($map, 4); drawRoute($coords, $map, $ratio, $latoffset, $lonoffset, $aaccbb, ($pointsperframe*($frame+1))); //draw places drawplaces2($locationsfile, $map, $ratio, $latoffset, $lonoffset, $black); // fetch most recent position //include('../includes/dbvars.php'); //mysql_connect($dbhost, $dbuser, $dbpassword); //mysql_select_db($dbname); //$posquerystring = 'SELECT * FROM `gpsposition` ORDER BY `time` DESC LIMIT 0 , 1'; //$posquery = mysql_query($posquerystring); //$pos = mysql_fetch_assoc($posquery); //print_r($pos); // draw recent position /* imagesetthickness($map, 5); imageellipse($map, round($ratio * ($pos['latitude'] + $latoffset)), round($ratio * ($pos['longitude'] + $lonoffset)), 15, 15, $red); imageellipse($map, round($ratio * ($pos['latitude'] + $latoffset)), round($ratio * ($pos['longitude'] + $lonoffset)), 14, 14, $red); imageellipse($map, round($ratio * ($pos['latitude'] + $latoffset)), round($ratio * ($pos['longitude'] + $lonoffset)), 13, 13, $red); imageellipse($map, round($ratio * ($pos['latitude'] + $latoffset)), round($ratio * ($pos['longitude'] + $lonoffset)), 12, 12, $red); imageellipse($map, round($ratio * ($pos['latitude'] + $latoffset)), round($ratio * ($pos['longitude'] + $lonoffset)), 11, 11, $red); imageellipse($map, round($ratio * ($pos['latitude'] + $latoffset)), round($ratio * ($pos['longitude'] + $lonoffset)), 10, 10, $red); imageellipse($map, round($ratio * ($pos['latitude'] + $latoffset)), round($ratio * ($pos['longitude'] + $lonoffset)), 9, 9, $red); imageellipse($map, round($ratio * ($pos['latitude'] + $latoffset)), round($ratio * ($pos['longitude'] + $lonoffset)), 2, 2, $red); imageellipse($map, round($ratio * ($pos['latitude'] + $latoffset)), round($ratio * ($pos['longitude'] + $lonoffset)), 3, 3, $red); */ // save image imagejpeg($map, 'map' . $frame . '.jpg', 75); header("Content-type: image/jpeg"); imagejpeg($map, NULL, 75); imagedestroy($map); $links[$frame] = '<img src="map' . $frame . '.jpg" /><br />'; //echo($frame . '<br />'); // end animation loop } // output links //foreach ($links as $link) //{ //echo($link); //} ?> This was originally going to be used to draw a series of images with the route drawing through. The end result to be turned into video for home videos. This is a static, single frame version. Quote Link to comment Share on other sites More sharing options...
AussieKlutz Posted December 3, 2008 Author Share Posted December 3, 2008 support files: http://headlessmoron.com/randomcomplexity/...ort%20files.zip usage is : s_statickmlrender.php?kmlfile=day1.kml&locationsfile=day1.txt kmlfile is the main route you are viewing locationsfile is a comma delimited text file with the place names I plan to rewrite/ clean up the code over the next few days. Quote Link to comment Share on other sites More sharing options...
AussieKlutz Posted December 10, 2008 Author Share Posted December 10, 2008 This is gonna take a bit longer than I had originally planned. I want to find a better way to extract the co-ordinates from the kml files. Also trying to rewrite my blog code, so it might take a while Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.