Convert Lat Long to php

OK, great to hear…

Next thing will be the color: it appears to be decimal r g b - is that correct? In your example it would be an almost pure red line.

Also, could you provide me with a complete file so that I can work out the extraction of individual polygons. I believe you can email me via my profile, or if the file is small enough, you could cut and paste it here.

I tried to add quite a few comments to the code, but feel free to ask about any parts of it.

Ya it should be 255 0 0 lol, that would be severe thunderstorm warning…the other potential color would be purple which 255 0 255 I believe…

www.southernontariochasing.ca/grpoly.txt Theres a text file. As you can see there are 2 polygons, one of them has 4 visible points, the other has 5…in the file that equals to 5 and 6 points respectively, due to the program needed the initial point to close the polygon.

OK, lets see how close this gets us…[php]<?php
$map = imagecreatefromstring(file_get_contents(‘http://www.southernontariochasing.ca/examples/countieswithlogo.png’));
$lineWidth = 1; // Default line width

$file = ‘http://www.southernontariochasing.ca/grpoly.txt’;

$polys = getPolys($file);

foreach ($polys as $poly)
{
$oldx = $oldy = NULL;
if(isset($poly[‘red’],$poly[‘green’],$poly[‘blue’])) $lineColor = imagecolorallocate($map, $poly[‘red’], $poly[‘green’], $poly[‘blue’]);
else $lineColor = imagecolorallocate($map, 255, 0, 0); // Default line color when not specified
if(isset($poly[‘lineWidth’])) $lineWidth = $poly[‘lineWidth’];
for($i=0;$i<count($poly[‘lat’]);$i++)
{
$result = getx_y($map,$poly[‘lat’][$i],$poly[‘lon’][$i],$oldx,$oldy,$lineColor,$lineWidth);
$map = $result[‘image’];
$oldx = $result[‘oldx’];
$oldy = $result[‘oldy’];
}
}

header(“Content-type: image/png”);
imagepng($map);

function getx_y($map,$pt_lat,$pt_lon,$oldx = NULL,$oldy = NULL,$color = NULL,$lineWidth = 1)
{
$width = imagesx($map);
$height = imagesy($map);

$mp_lat = 43.69514; // MidPoint Latitude
$mp_lon = -80.78972; // MidPoint Longitude

$mp_x = round($width/2); //MidPoint x coordinate
$mp_y = round($height/2); //MidPOint y coordinate

$scale = 1.33; // pixels per kilometer
$rhumb = rhumbDB($mp_lat,$pt_lat,$mp_lon,$pt_lon); // Get rhumb line distance and bearing

$dist = $rhumb['distance']; // Get rhumb line Distance from midpoint
$angle = $rhumb['bearing']+2.2; // Get rhumb line Bearing from midpoint & rotate angle 2.2

$h = $dist * $scale; // Get the scaled distance (in pixels) from midpoint
$o = sin(deg2rad($angle%90)) * $h; // Get the offset in one direction using basic trig
$a = sqrt(pow($h,2)-pow($o,2));  // Get the other offset using pythagorean's theorem

if($angle > 0 && $angle <= 90) // Angle is in the 12:00 to 3:00 quadrant
  {
    $pt_x = $mp_x + $o; //pt_x is to the right of the midpoint
    $pt_y = $mp_y - $a; //pt_y is above the midpoint
  }
elseif($angle > 90 && $angle <= 180) // Angle is in the 3:00 to 6:00 quadrant
  {
    $pt_x = $mp_x + $a; //pt_x is to the right of the midpoint
    $pt_y = $mp_y + $o; //pt_y is below the midpoint
  }
elseif($angle > 180 && $angle <= 270) // Angle is in the 6:00 to 9:00 quadrant
  {
    $pt_x = $mp_x - $o; //pt_x is to the left of the midpoint
    $pt_y = $mp_y + $a; //pt_y is below the midpoint
  }
elseif($angle > 270 && $angle <= 360) // Angle is in the 9:00 to 12:00 quadrant
  {
    $pt_x = $mp_x - $a; //pt_x is to the left of the midpoint
    $pt_y = $mp_y - $o; //pt_y is above the midpoint
  }

$pt_x = round($pt_x); // Make sure pt_x is an integer
$pt_y = round($pt_y); // Make sure pt_y is an integer
  
if(empty($oldx)) $oldx = $pt_x;
if(empty($oldy)) $oldy = $pt_y;

imagesetthickness($map, $lineWidth);
imageline($map,$oldx,$oldy,$pt_x,$pt_y,$color);

return array('image'=>$map,'oldx'=>$pt_x,'oldy'=>$pt_y);

}

/*
rhumbDB implements the formula for finding distance and bearing between two points.
The distance and bearing are calculated without regard to great circle routes.
Depending on the map, you may need to use the Haversine formula instead, which accounts
for great circle routes. To return results in miles, use $R=3956.09 instead.*/
function rhumbDB($lat1,$lat2,$lon1,$lon2,$R=6366.71)
{
$lat1 = deg2rad($lat1); // All trig functions need to be in radians
$lat2 = deg2rad($lat2); // All trig functions need to be in radians
$lon1 = deg2rad($lon1); // All trig functions need to be in radians
$lon2 = deg2rad($lon2); // All trig functions need to be in radians
$deltlat = $lat2 - $lat1;
$deltlon = $lon2 - $lon1;

  $deltPhi = log(tan($lat2/2+M_PI/4)/tan($lat1/2+M_PI/4));
  
  if(is_numeric($deltlat/$deltPhi)) $q = $deltlat/$deltPhi;
  else $q = cos($lat1);
  
  if(abs($deltlon) > M_PI)
    {
        if($deltlon > 0) $deltlon = -(2*M_PI-$deltlon);
        else $deltlon = 2*M_PI+$deltlon;
    }
  $d = sqrt($deltlat*$deltlat + $q*$q*$deltlon*$deltlon)*$R;
  $brng = fmod(rad2deg(atan2($deltlon,$deltPhi))+360,360);
  
  return array('distance'=>$d,'bearing'=>$brng);

}

function getPolys($file)
{
$fh = fopen($file, “r”);

$i=0;
while (!feof($fh))
  {
    $line = trim(fgets($fh));
    if(substr($line,0,6) == 'Color:') list($poly[$i]['red'],$poly[$i]['green'],$poly[$i]['blue']) = explode(' ',substr($line,7));
    elseif(substr($line,0,5) == 'Line:')
      {
        preg_match('/\d+/',$line,$match);
        $poly[$i]['lineWidth'] = $match[0];
      }
    elseif(substr($line,0,4) == 'End:') $i++;
    elseif(preg_match('/^\d+(.)+([0-9]+)*$/',$line)) list($poly[$i]['lat'][],$poly[$i]['lon'][]) = explode(',',$line);
  }
fclose($fh);

return $poly;

}[/php]

OMG, works amazing. I added another polygon to the text file, worked fine.

I guess the only thing now would be…is possible to have the polygon filled with a 105 alpha transparency of the given color? If not how bout solid? Once again thanks for the help…works great!!!

Awesome! Glad to hear it.

I’m going to mark this thread solved, but please let me know if there is anything else that needs to be done.
Also, let me know if you have any questions regarding the code. There is a lot going on and the code is a mess - I may try to clean it up some.

Best,

jay

Sounds good…once again thanks…do you have any thoughts on the 105 alpha transparency fill?

I do,

Sorry I missed your post. New code coming soon…

Lets try this:[php]<?php
$map = imagecreatefromstring(file_get_contents(‘http://www.southernontariochasing.ca/examples/countieswithlogo.png’));
$lineWidth = 1; // Default line width
$width = imagesx($map);
$height = imagesy($map);

$file = ‘http://www.southernontariochasing.ca/grpoly.txt’;

$polys = getPolys($file);

foreach ($polys as $poly)
{
$coords = array();
if(isset($poly[‘red’],$poly[‘green’],$poly[‘blue’]))
{
$borderColor = imagecolorallocate($map, $poly[‘red’], $poly[‘green’], $poly[‘blue’]);
$fillColor = imagecolorallocatealpha($map, $poly[‘red’], $poly[‘green’], $poly[‘blue’], 105);
}
else
{
$borderColor = imagecolorallocate($map, 255, 0, 0); // Default line color when not specified
$fillColor = imagecolorallocatealpha($map, 255, 0, 0, 105); // Default fill color when not specified
}
if(isset($poly[‘lineWidth’])) $lineWidth = $poly[‘lineWidth’];
for($i=0;$i<count($poly[‘lat’]);$i++)
{
$result = getx_y($poly[‘lat’][$i],$poly[‘lon’][$i],$width,$height);
$coords[] = $result[‘x’];
$coords[] = $result[‘y’];
}

imagesetthickness($map, $lineWidth);
imagefilledpolygon($map, $coords, count($coords)/2, $fillColor);
imagepolygon($map, $coords, count($coords)/2, $borderColor);

}

header(“Content-type: image/png”);
imagepng($map);

function getx_y($pt_lat,$pt_lon,$width,$height)
{
$mp_lat = 43.69514; // MidPoint Latitude
$mp_lon = -80.78972; // MidPoint Longitude

$mp_x = round($width/2); //MidPoint x coordinate
$mp_y = round($height/2); //MidPOint y coordinate

$scale = 1.33; // pixels per kilometer
$rhumb = rhumbDB($mp_lat,$pt_lat,$mp_lon,$pt_lon); // Get rhumb line distance and bearing

$dist = $rhumb['distance']; // Get rhumb line Distance from midpoint
$angle = $rhumb['bearing']+2.2; // Get rhumb line Bearing from midpoint & rotate angle 2.2

$h = $dist * $scale; // Get the scaled distance (in pixels) from midpoint
$o = sin(deg2rad($angle%90)) * $h; // Get the offset in one direction using basic trig
$a = sqrt(pow($h,2)-pow($o,2));  // Get the other offset using pythagorean's theorem

if($angle > 0 && $angle <= 90) // Angle is in the 12:00 to 3:00 quadrant
  {
    $pt_x = $mp_x + $o; //pt_x is to the right of the midpoint
    $pt_y = $mp_y - $a; //pt_y is above the midpoint
  }
elseif($angle > 90 && $angle <= 180) // Angle is in the 3:00 to 6:00 quadrant
  {
    $pt_x = $mp_x + $a; //pt_x is to the right of the midpoint
    $pt_y = $mp_y + $o; //pt_y is below the midpoint
  }
elseif($angle > 180 && $angle <= 270) // Angle is in the 6:00 to 9:00 quadrant
  {
    $pt_x = $mp_x - $o; //pt_x is to the left of the midpoint
    $pt_y = $mp_y + $a; //pt_y is below the midpoint
  }
elseif($angle > 270 && $angle <= 360) // Angle is in the 9:00 to 12:00 quadrant
  {
    $pt_x = $mp_x - $a; //pt_x is to the left of the midpoint
    $pt_y = $mp_y - $o; //pt_y is above the midpoint
  }

$pt_x = round($pt_x); // Make sure pt_x is an integer
$pt_y = round($pt_y); // Make sure pt_y is an integer
  
return array('x'=>$pt_x,'y'=>$pt_y);

}

/*
rhumbDB implements the formula for finding distance and bearing between two points.
The distance and bearing are calculated without regard to great circle routes.
Depending on the map, you may need to use the Haversine formula instead, which accounts
for great circle routes. To return results in miles, use $R=3956.09 instead.*/
function rhumbDB($lat1,$lat2,$lon1,$lon2,$R=6366.71)
{
$lat1 = deg2rad($lat1); // All trig functions need to be in radians
$lat2 = deg2rad($lat2); // All trig functions need to be in radians
$lon1 = deg2rad($lon1); // All trig functions need to be in radians
$lon2 = deg2rad($lon2); // All trig functions need to be in radians
$deltlat = $lat2 - $lat1;
$deltlon = $lon2 - $lon1;

  $deltPhi = log(tan($lat2/2+M_PI/4)/tan($lat1/2+M_PI/4));
  
  if(is_numeric($deltlat/$deltPhi)) $q = $deltlat/$deltPhi;
  else $q = cos($lat1);
  
  if(abs($deltlon) > M_PI)
    {
        if($deltlon > 0) $deltlon = -(2*M_PI-$deltlon);
        else $deltlon = 2*M_PI+$deltlon;
    }
  $d = sqrt($deltlat*$deltlat + $q*$q*$deltlon*$deltlon)*$R;
  $brng = fmod(rad2deg(atan2($deltlon,$deltPhi))+360,360);
  
  return array('distance'=>$d,'bearing'=>$brng);

}

function getPolys($file)
{
$fh = fopen($file, “r”);

$i=0;
while (!feof($fh))
  {
    $line = trim(fgets($fh));
    if(substr($line,0,6) == 'Color:') list($poly[$i]['red'],$poly[$i]['green'],$poly[$i]['blue']) = explode(' ',substr($line,7));
    elseif(substr($line,0,5) == 'Line:')
      {
        preg_match('/\d+/',$line,$match);
        $poly[$i]['lineWidth'] = $match[0];
      }
    elseif(substr($line,0,4) == 'End:') $i++;
    elseif(preg_match('/^\d+(.)+([0-9]+)*$/',$line)) list($poly[$i]['lat'][],$poly[$i]['lon'][]) = explode(',',$line);
  }
fclose($fh);

return $poly;

}[/php]

Looks awesome. That is exactly 100% what I need…Can I add imagepng($im,'southernontarioallwarn.png'); header('Content-type: image/png'); imagepng($im); To have the script create a image onto the server?

Almost,

You actually just need to add:[php]imagepng($map,‘southernontarioallwarn.png’);[/php]
I would put it right above this line:[php]header(“Content-type: image/png”);[/php]

Thanks, I must be doing something wrong. I add those and it still doesn’t create the image.

Did you add it the one I provided with the $map variable? The one you posted was for an image resource $im instead of $map. When we put together the code for combining the images I had used $im for the image resource, but on this one, I was using $map. The principal is the same, but the resource names are different. If you prefer, you could switch all occurrence of $map with $im to keep things more consistent.

I did change it to map, doesn’t want to work on my end. Oh well, where we are at now is perfectly fine…thanks for all the help once again.

Try putting it after the header… I didn’t think it needed the header to save to file, but lets give it a shot:[php]header(“Content-type: image/png”);
imagepng($map,‘southernontarioallwarn.png’);[/php]

I would say to check your permissions, but you have successfully stored the other images, so that shouldn’t be it.

I tried it a few ways…heres the one that should work
[php]imagepng($map,‘southernontarioexperimental.png’);
header(“Content-type: image/png”);
imagepng($map);[/php]
But it doesnt lol

I just tried it on my server and it saves fine. Not sure what to think now.

Are you saving to the same directory that the other script saved to? If not, see if it will save in that directory. The only thing I can think of is a permissions issue, even though that seems to be ruled out by the prior success. You might also try giving it a different filename, something like ‘southontexp.png’ I see nothing wrong with the filename that you are using, but I would try it if nothing else works…

Got it…had it before the } instead of after…thanks, Im pretty set now…Thanks for all the help

Sponsor our Newsletter | Privacy Policy | Terms of Service