Jump to content

PHP GIS mapping help needed


BoldStepDesign

Recommended Posts

Okay.

 

I just finished reading the following tutorial.

http://www.web-max.ca/PHP/article_1.php

Basically it's a map that converts Lon and Lat into x and y coordinates. I reset some of the varibles and set x and y to standard coordinates. But now I want to change from that little red dot to using a picture.

Here is their code.

 <?php
function getlocationcoords($lat, $lon, $width, $height)
{
   $x = (($lon + 180) * ($width / 360));
   $y = ((($lat * -1) + 90) * ($height / 180));
   return array("x"=>round($x),"y"=>round($y));
}

// These are the coordinates the location we wish to plot.
// These are being passed in the URL, but we will set them to a default if nothing is passed.

if(empty($long))$long = -63.10774861954596;
if(empty($lat)) $lat = 46.2899306519141;

// 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("earth_310.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);

// Now we convert the long/lat coordinates into screen coordinates

$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"]-2,$pt["y"]-2,$pt["x"]+2,$pt["y"]+2,$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);

?>

 

My code:

(Very few mods)

<?php
function getlocationcoords($lat, $lon, $width, $height) 
{ 
   $x = 108; 
   $y = 336; 
   return array("x"=>round($x),"y"=>round($y)); 
}


// 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 

$mainImage = imagecreatefromjpeg("map.jpg"); 
$DotScreen = imagecolorallocate ($mainImage, 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($mainImage);
$scale_y = imagesy($mainImage); 

// Now we convert the long/lat coordinates into screen coordinates 

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

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

imagefilledrectangle($mainImage,$pt["x"]-2,$pt["y"]-2,$pt["x"]+2,$pt["y"]+2,$DotScreen);

// 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($mainImage); 
imagedestroy($mainImage); 

?>

 

 

Basically two things here.

 

$DotScreen = imagecolorallocate ($mainImage, 255,0,0);
imagefilledrectangle($mainImage,$pt["x"]-2,$pt["y"]-2,$pt["x"]+2,$pt["y"]+2,$DotScreen);

 

The first one is $DotScreen that creates the color, the second is imagefilledrectangle which plots the point. Is there a way to use like a gif rather this  red dot?

 

 

Link to comment
https://forums.phpfreaks.com/topic/114618-php-gis-mapping-help-needed/
Share on other sites

header ("Content-type: image/png");

imagepng($im);

imagedestroy($im);

$im = imagecreatefromjpeg("earth_310.jpg");

$red = imagecolorallocate ($im, 255,0,0);

 

your image is jpg and your telling the header that it is an PNG file.

 

You see.

 

content image/png

earth_310.jpg

the file types conflict

Place 16x16 image at 30,40 on map

[pre]

  +-----------------------------------+

  |                                  |

  |                                  |

  |        30, 40                    |          0,0

  |          <-----------------------------------  +---+

  |                                  |            |  |

  |                                  |            +---+ 16,16

  |                                  |            src

  |                                  |

  |                                  |

  +-----------------------------------+

                dst

 

[/pre]

    $dst = imagecreatefromjpeg("map.jpg");                  // jpeg converted to gd image
    $src = imagecreatefromjpeg("locator.jpg");
    imagecopy($dst, $src, 30, 40, 0, 0, 16, 16) ;
    header ("content-type: image/png");
    imagepng($dst);                                                 // gd image converted to png
    imagedestroy($dst);
    imagedestroy($src);

 

That worked.

 

I rewrote the code before trying that and it's cleaner and works better.

 

<?php
header ("Content-type: image/png");
// Defining the background image
$background = imagecreatefromjpeg("map.jpg");

// Defining the overlay image to be added or combined.
$Locator = imagecreatefromjpeg("locator.jpg");

// Select the first pixel of the overlay image (at 0,0) and use
// it's color to define the transparent color
imagecolortransparent($Locator,imagecolorat($Locator ,0,0));

// Get overlay image width and hight for later use
$Locator_x = imagesx($Locator);
$Locator_y = imagesy($Locator);

// Positining of image 
$x=600;
$y=100;

// Combine the images into a single output image.
imagecopymerge($background,$Locator,$x,$y,0,0,$Locator_x,$Locator_y,100);

/* first set of numbers after $background,$Locator, is the x cooridnate, 
Second set of numbers after $background,$Locator, is the y, The reamaining two are 
padding */
imagejpeg($background,"",100);
?>

 

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.