Jump to content

Convert Lat/Lon to X/Y points


TFD3

Recommended Posts

I would like to see if anyone can help me with converting Lat/Lon into X & Y points.

 

Im trying to get a polygon plotted on the map below:

http://www.alabamaweather.org/images2/basemap.png

 

An example of points are below with the first and last set being the same so they complete the polygon.

33.20 -99.20

34.43 -95.36

34.83 -95.45

33.20 -99.20

 

Any ideas on how to do this?

I got the Lat/Lon for the upper left corner and lower right if you need it.

 

Thanks for any help!

Link to comment
https://forums.phpfreaks.com/topic/110607-convert-latlon-to-xy-points/
Share on other sites

Here is the script that I am using. But the output is not lining up to where it should be.

 

<?php

// First we load the background/base map. We assume it's located in same dir

// as the script.
// This can be any format but we are using JPG in this example // We will also allocate the color for the marker 

$im = imagecreatefromjpeg("basemap.jpg");
$red = imagecolorallocate ($im, 255,0,0);

// Next need to find the base image size.
// We need these variables to be able scale the long/lat coordinates.

$scale_x = imagesx($im);
$scale_y = imagesy($im); 

$pt = getlocationcoords($lat, $long, $scale_x, $scale_y);

// Now mark the point on the map using a red 4 pixel rectangle 

imagefilledrectangle($im,$pt["x"]-1,$pt["y"]-1,$pt["x"]+1,$pt["y"]+1,$red);
imagestring($im,2,1,$scale_y-20,"Courtesy of www.staycanada.ca",$red);

// Return the map image. We are using a PNG format as it gives better final image quality than a JPG
header("Content-Type: image/png");
imagepng($im);
imagedestroy($im);

function getlocationcoords($lat, $lon, $width, $height)
{  
    $x = (($lon + 180) * (640 / 360));
    $y = ((($lat * -1) + 90) * (480 / 180));
    return array("x"=>round($x),"y"=>round($y));
}
// Now we convert the long/lat coordinates into screen coordinates
?>

 

http://www.alabamaweather.org/scripts/gd/maps/article_4.php?long=-89.077847&lat=29.081553

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.