Beginner PHP, Map.php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
supersoup
Forum Newbie
Posts: 5
Joined: Sun Oct 18, 2009 1:52 pm

Beginner PHP, Map.php

Post by supersoup »

I am a complete beginner to PHP and am looking for some help with a program I am messing around with.

I found this incomplete source code, and I wanted to finish it, but I have no idea what I am doing.
It looks like all you need to do to complete it is write the Set functions in the 3rd file. I have been searching the internet for hours and cant find anything that seemed to help.
If someone could show me how to set these functions up that would be so great!

Code: Select all

 
//map.php
<html><title>
CS-430: Map
</title>
<style type=text/css>
.radH1  {
    border-right:1px solid black;
    border-bottom:1px solid black;
    color:#0000BB;
    font-family:serif;
    font-size:larger;
    font-weight:bolder;
    }
.radH2  {
    border-bottom:1px solid black;
    color:#880000;
    font-weight:bold;
    }
.radH3  {
    border-bottom:1px solid black;
    color:#0000BB;
    }
.radDiv {
    text-align:left;
    padding-right:3px;
    white-space:nowrap;
    }
.radE2  {
    color:#880000;
    font-weight:bold;
    }
.radE3  {
    color:#0000BB;
    }
.radTab {
    padding-right:1px;
    padding-left:1px;
    padding-top:0px;
    padding-bottom:0px;
    background-color:#FFFFBB;
    border:1px solid black;
    margin-top:0px;
    margin-bottom:0px;
    font-size:small;
    }
.pad0   {
    margin:0;
    padding:0;
    border:0;
    }
.divAll {
    margin-top:0px;
    overflow:hidden; 
    padding:0px; 
    border: 1px solid black;
    }
.divDot {
    margin:0;
    padding:0;
    border:0;
    position:relative;
    width:7px; 
    height:7px; 
    line-height:1px; 
    font-size:1px; 
    z-index:4;
    background-repeat:no-repeat;
    }
.divImg {
    position:relative; 
    top:-7; 
    left:0;
    z-index:1;
    }
.imgMap {
    margin:0px;
    }
.smll   {
    font-size:small;
    }
</style>
<body>
    <TABLE class=pad0>
    <TR><TD VALIGN=TOP ALIGN=CENTER>
    <h2>CS-430: Map </h2>
 
<?php
 //-------------------------------------------------------------
 // Locations are a value-pair, often stored in an array of two values:
 // [0] Longitude:  East/West location  (pixel column in an image)
 // [1] Latitude:   North/South Location    (pixel row in an image)
 //
 // Terms used for various ways of expressing locations:
 // Mix A pixel value within a map.
 // Tix A pixel value within a tile of a map.
 // UTM A location expressed as UTM value (in zone 15).
 // Rad A location expressed in radians.
 // Deg A location expressed in degrees.
 // All locations use WGS-84/NAD87 (which are very close) .
 //
 // Abbreviations used for image/tile corners: 
 // ul  UpperLeft
 // ur  UpperRight
 // lr  LowerRight
 // ll  LowerLeft
 //-------------------------------------------------------------
 
    //------------------------------------------------------------------
    //  Uncomment the following two lines to have PHP error messages
    //  display on the web page.  Use only for debugging.
    //------------------------------------------------------------------
    error_reporting(E_ALL);
    ini_set("display_errors", 1);
 
    //--------------------------------------------------------------------------
    // Global variables:
    //  ICON_URL:       URL of directory containing all icons
    //  IMAGES_URL:     URL of directory containing all images
    //  TILE_URL_FORMAT:    An sprintf format string for generating
    //              the URL of a "tile" image file given the
    //              following four paramters:
    //              zoom, type, upper-left-pix-col, upper-left-pix-row
    //  DISP_SZ:        Size in pixels of the image display window
    //  TILE_INC:       Size in pixels of the interval between tiles
    //  MAP_DATA:       Size and georeferencing data for each
    //              complete image, set in function SetMapData
    //--------------------------------------------------------------------------
    $ICON_URL = "http://w3.cs.jmu.edu/arch/crs/cs430-php/icons";
    $IMAGES_URL = "http://w3.cs.jmu.edu/arch/crs/cs430-php/images";
    $TILE_URL_FORMAT = "$IMAGES_URL" . '/%2$s-%1$s-tiles/%2$s-%1$s-%3$sx%4$s.png';
    $DISP_SZ = array(832, 608);
    $TILE_INC = array(64, 64);
 
    include 'lib/data.php';
    include 'lib/util.php';
    include 'lib/guts.php';
    include 'lib/loc.php';
    include 'lib/utm.php';
 
    SetMapData();
 
    $rtrn = SetMap("t", "64mp");
    $mapType = $rtrn[0];
    $mapZoom = $rtrn[1];
    $mapData = $MAP_DATA[$mapType][$mapZoom];
 
    $curDeg = SetDeg($mapData);
 
    $rtrn = GetTileInfo($curDeg, $mapType, $mapZoom);
    $tileUrl = $rtrn[0];
    $mixTile = $rtrn[1];
 
    OutputZoomTable($mapType, $mapZoom, $curDeg);
 
    echo ("<span class=smll>mp:meters/pixel</span>\n");
    echo ("<p><b>Location</b><br>Lon: $curDeg[0]<br>Lat: $curDeg[1]\n");
 
    echo ("</TD><TD>\n");
 
    GotoIsmap($curDeg, $mapData, $mapType, $mapZoom, $mixTile, $tileUrl);
 
    echo ("$tileUrl<br>\n");
?>
 
 
</TD></TR></TABLE>
</body></html>

Code: Select all

 
//  lib/data.php
<?php
 
//=====================================================================
// The global variable MAP_DATA defines data about the complete
// images for each type/zoom. $MAP_DATA is a 3d array.  
// * The first index indicates a map type (currently there is only one map 
//   type: 't').
// * The second index specifies an image file by its zoom. 
// * The third index specifies an array of 2 values as follows:
//  0: size of map in pixels (mix) (width, length)
//  1: upper-left degree (longitude, latitude) (e/w, n/s)
//  2: upper-right degree (longitude, latitude)
//  3: lower-right degree (longitude, latitude)
//  4: lower-left degree (longitude, latitude)
//  5: upper-left utm (longitude, latitude)
//  6: upper-right utm (longitude, latitude)
//  7: lower-right utm (longitude, latitude)
//  8: lower-left utm (longitude, latitude)
// This function initializes the array.
// The code at the end of this function computes entries 1-4 from 5-8.
//=====================================================================
function SetMapData()
{
global $MAP_DATA;
 
$MAP_DATA['t']['32mp'] = array(
    0 => array(3328,2432),
    5 => array(550996.855, 5397000.883),
    6 => array(657567.642, 5397000.883),
    7 => array(657567.642, 5319176.883),
    8 => array(550996.855, 5319176.883),
    0);
$MAP_DATA['t']['64mp'] = array(
    array(1664,1216),
    5 => array(550996.855, 5397000.883),
    6 => array(657567.642, 5397000.883),
    7 => array(657567.642, 5319176.883),
    8 => array(550996.855, 5319176.883),
    0);
$MAP_DATA['t']['128mp'] = array(
    array(832,608),
    5 => array(550996.855, 5397000.883),
    6 => array(657567.642, 5397000.883),
    7 => array(657567.642, 5319176.883),
    8 => array(550996.855, 5319176.883),
    0);
 
$tKeys = array_keys($MAP_DATA);
foreach ($tKeys as $tKey)
    {
    $zKeys = array_keys($MAP_DATA[$tKey]);
    foreach ($zKeys as $zKey)
        {
        for ($jj=1; $jj<=4; $jj++)
            $MAP_DATA[$tKey][$zKey][$jj] = UTM2deg($MAP_DATA[$tKey][$zKey][$jj+4]);
        }
    }
}

Code: Select all

 
//    lib/guts.php
<?php
 
//=========================================================================
// The web page uses two map parameters as follows:
//
// $_GET['type']    The type of the current map (a string).
// $_GET['zoom']    The zoom of the current map (a string).
//
// This function returns an array of two containing the type and zoom
// values of the current map.  If set, the values specified by the GET 
// parameters are used.  If a GET parameter is not specified, the
// default values are used.
//
// Return:
//  $result:    An array of two strings, the type and zoom values
//=========================================================================
 
function SetMap
(
    $defaultType,   // Default type (string)
    $defaultZoom    // Default zoom (string)
)   {
    global $_GET;
 
    // Replace this line with the code that implements this function.
    }
 
 
//=========================================================================
// The web page uses two location parameters as follows:
//
// $_GET['new'] The location that should be the display's new location. The value
//      is specified in the form 'col1,row1?col2,row2' where
//      'col1'/'row1' are the pixel in the map (mix) of
//      the upper-left corner of a tile and 'col2'/'row2' are the pixel
//      location within the tile (tix).
//
// $_GET['loc'] The maps previous location in the form 'long,lat' where
//      'long' is the longitude in degrees and 'lat' is the latitude 
//      in degrees.
//
// This function returns an array of two containing the degree 
// location for the dot on the map.  If 'new' is specifed, the dot is set
// to be the value specified by 'new' (the mix/tix value must be converted
// to degrees).  If 'new' is not specified, the value specified for 'loc' is 
// used.  If neither is specified, the center of the current map is used
// (the degree location is computed from the mix value of the center of 
// the map).
//
// Each returned degree value should be truncated to be at most 10 characters.
//
// Return:
//  $deg        The degree location (array of 2).
//=========================================================================
 
function SetDeg
(
    $mapData    // The map data (array of 9).
)   {
    global $_GET;
 
    // Replace this line with the code that implements this function.
    }
 
//============================================================================
// Return the URL and "tix" of the tile for this degree location for
// the specified map.
// The correct "tile" is the tile that best centers the current location
// in itself. Tiles are found on $TILE_INC boundaries of the complete map.
// 
// Return:
//   $rtrn[0] = URL of the "tile" file.
//   $rtrn[1] = pixel location in the map of the upper left corner of the tile.
//============================================================================
 
 function GetTileInfo
 (
    $deg,   // Get tile for this degree location (array of 2) 
    $type,  // Map type
    $zoom   // Map zoom
 )  {
    global $TILE_URL_FORMAT;
    global $MAP_DATA;
    global $DISP_SZ;
    global $TILE_INC;
 
    // Replace this line with the code that implements this function.
    }
 
 //---------------------------------------------------------------------------
 // Output HTML of the following form:
 //
 // <table class=radTab>
 // <tr>
 //     <th align=left class=radH1>Zoom</th>
 //     <td align=center class=RADH>TYPE</td>
 //     ...
 // </tr>
 // <div class=radDiv><tr>
 //     <td class=RADE>ZOOM</td>
 //     <td><a href='?type=TYPE&zoom=ZOOM&loc=LON,LAT' class=pad0>
 //         <img src=RADIO class=pad0>
 //     </a></td>
 //     ...
 // </tr></div>
 // ...
 // </table>
 //
 // The first row of the table has labels for the map types.
 // The first column of the table has labels for the zoom levels. 
 // The first and second ... above indicates  the <td>...</td> above it 
 // is repeated for each type of map. The last ... indicates there is a 
 // <div>...</div> entry for each zoom level.
 //
 // The ITEMS in all uppercase above represent the following:
 //
 // TYPE    The type for that column.
 // ZOOM    The zoom for that row.
 // RADH    The string "radH2" if the current type matches the column type,
 //     otherwise the string "radH3".
 // RADE    The string "radE2" if the current zoom matches the row zoom,
 //     otherwise the string "radE3".
 // LON The current degree longitude
 // LAT The current degree latitude
 // RADIO   The string "$ICON_URL/radio-on.png" if the current type and
 //     zoom match the col/row type/zoom, otherwise
 //     "$ICON_URL/radio-off.png".
 //---------------------------------------------------------------------------
 function OutputZoomTable
 (
    $mapType, 
    $mapZoom, 
    $curDeg
 )  {
    global $MAP_DATA;
    global $ICON_URL;
 
    // Replace this line with the code that implements this function.
    }
 
 
 //---------------------------------------------------------------------------
 // Output HTML of the following form:
 //
 // <Div align=left valign=top class=divAll style='width:WIDTH; height:HEIGHT; max-height:HEIGHT;'>
 // <!-- Center Dot -->
 // <div class=divDot 
 // style='left:DOTCOL; top:DOTROW; background-image:url($ICON_URL/mark-dot.png);' ><!----></div>
 // <!-- Map Image -->
 // <div class=divImg style='width:WIDTH; height:HEIGHT;'>
 // <a href=?type=TYPE&zoom=ZOOM&new=TILECOL,TILEROW>
 // <img ismap border=0 src=TILE_URL
 //  alt='[TILE_URL]' class=imgMap  style='width:WIDTH; height:HEIGHT;'></a>
 // </div></Div>
 
 // The ITEMS in all uppercase above represent the following:
 //
 // WIDTH   Width in pixels of the display area.
 // HEIGHT  Height in pixels of the display area.
 //
 // DOTCOL  The pixel location within the tile of the upper left corner
 // DOTROW  of the dot that indicates the current location.  The dot is
 //     8 pixels square.  So for example, if the center of the dot 
 //     should be at pixel 100,150 of the tile, the upper left corner
 //     of the pixel should at at 96,146.
 // TILECOL The pixel location within the map of the upper left corner of
 // TILEROW the tile.  
 //
 // TILEURL The URL of the tile to be displayed.
 // TYPE    The type of this map.
 // ZOOM    The zoom of this map.
 //---------------------------------------------------------------------------
 function GotoIsmap
(
    $deg,       // The current location in degrees
    $mapData,   // The array of data about the current map
    $mapType,   // The current map type.
    $mapZoom,   // The current map zoom
    $mixTile,   // The pixel location in the map of this tile.
    $tileUrl    // The URL of the tile image
)   {
    global $ICON_URL;
    global $DISP_SZ;
 
    $dotSz = 8;
 
    // Replace this line with the code that implements this function.
    }
?>
There are 3 other files that come with this code. I didn't think they were important to the set functions, but if anyone would like to see them, let me know.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Beginner PHP, Map.php

Post by requinix »

Some of this will be rather complex for someone just starting out. I suggest putting this aside for now and finding something simpler to tackle.
supersoup
Forum Newbie
Posts: 5
Joined: Sun Oct 18, 2009 1:52 pm

Re: Beginner PHP, Map.php

Post by supersoup »

I understand that this is a little complex but I feel like once I understand how to write the set functions it will all come together.
I am a beginner with php, but more like an intermediate at programming.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Beginner PHP, Map.php

Post by requinix »

Here's a couple examples:

Code: Select all

//=========================================================================
// The web page uses two map parameters as follows:
//
// $_GET['type']    The type of the current map (a string).
// $_GET['zoom']    The zoom of the current map (a string).
//
// This function returns an array of two containing the type and zoom
// values of the current map.  If set, the values specified by the GET
// parameters are used.  If a GET parameter is not specified, the
// default values are used.
//
// Return:
//  $result:    An array of two strings, the type and zoom values
//=========================================================================
 
function SetMap
(
    $defaultType,   // Default type (string)
    $defaultZoom    // Default zoom (string)
)   {
    global $_GET;
 
    // Replace this line with the code that implements this function.
}
The documentation describes exactly what the function should do. It should check for those $_GET arguments and use the $default* values if one of them isn't present. Then it returns an array.

Code: Select all

//=========================================================================
// The web page uses two location parameters as follows:
//
// $_GET['new'] The location that should be the display's new location. The value
//      is specified in the form 'col1,row1?col2,row2' where
//      'col1'/'row1' are the pixel in the map (mix) of
//      the upper-left corner of a tile and 'col2'/'row2' are the pixel
//      location within the tile (tix).
//
// $_GET['loc'] The maps previous location in the form 'long,lat' where
//      'long' is the longitude in degrees and 'lat' is the latitude
//      in degrees.
//
// This function returns an array of two containing the degree
// location for the dot on the map.  If 'new' is specifed, the dot is set
// to be the value specified by 'new' (the mix/tix value must be converted
// to degrees).  If 'new' is not specified, the value specified for 'loc' is
// used.  If neither is specified, the center of the current map is used
// (the degree location is computed from the mix value of the center of
// the map).
//
// Each returned degree value should be truncated to be at most 10 characters.
//
// Return:
//  $deg        The degree location (array of 2).
//=========================================================================
 
function SetDeg
(
    $mapData    // The map data (array of 9).
)   {
    global $_GET;
 
    // Replace this line with the code that implements this function.
}
This one's a bit harder. If $_GET["new"] is present the code should convert the "mix/tix" value into a latitude/longitude pair. Otherwise it uses the $_GET["loc"] value, if present. If that's not present either it uses $mapData to return the center of the current map.


All of them are a matter of getting familiar with the system and how it works, then taking the function and providing code that does exactly what it says.
supersoup
Forum Newbie
Posts: 5
Joined: Sun Oct 18, 2009 1:52 pm

Re: Beginner PHP, Map.php

Post by supersoup »

something like this?

Code: Select all

 
function SetMap
(
        $defaultType,   // Default type (string)
        $defaultZoom    // Default zoom (string)
)       {
        global $_GET;
 
        if($defaultType = "t" && $defaultZoom = "64mp")
        {
                $result = array($_GET['t'],$_GET['64mp']);
        }
        return $result;
}
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Beginner PHP, Map.php

Post by requinix »

To my understanding, no. More like

Code: Select all

function SetMap
(
    $defaultType,   // Default type (string)
    $defaultZoom    // Default zoom (string)
)   {
    global $_GET;
 
    if (isset($_GET["type"])) {
        // "If set, the values specified by the GET parameters are used."
        $type = $_GET["type"];
    } else {
        // "If a GET parameter is not specified, the default values are used."
        $type = $defaultType;
    }
    if (isset($_GET["zoom"])) {
        // "If set, the values specified by the GET parameters are used."
        $zoom = $_GET["zoom"];
    } else {
        // "If a GET parameter is not specified, the default values are used."
        $zoom = $defaultZoom;
    }
 
    // "This function returns an array of two containing the type and zoom values of the current map"
    return array($type, $zoom);
}
supersoup
Forum Newbie
Posts: 5
Joined: Sun Oct 18, 2009 1:52 pm

Re: Beginner PHP, Map.php

Post by supersoup »

ok that makes sense.
im now working on the next method.
im not sure i understand this one.
this is what i have:

Code: Select all

function SetDeg
(
        $mapData        // The map data (array of 9).
)       {
        global $_GET;
 
        if (isset($_GET["new"]))
        {
                $new = $_GET["new"];
        }
        elseif (!isset($_GET["new"]))
        {
                $new = $_GET["loc"];
        }
        else
        {
                $new = //center of the map
        }
 
        $deg = array($long, $lat);
        return $deg;
        }
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Beginner PHP, Map.php

Post by requinix »

You're partway there.

First,

Code: Select all

       if (isset($_GET["new"]))
        {
        }
        elseif (!isset($_GET["new"]))
Between those two conditions you've caught every possible situation: the else you've given will never execute.
Second: you check for $_GET["new"]; if there isn't one then you check for $_GET["loc"]; if there's neither then you use the existing data.

The numbers in $_GET["new"] need to be converted into a <longitude, latitude> pair. Use math to convert the mix/tix values into degrees: $mapData can tell you the map width and height as well as the longitude and latitude for all four corners.
$_GET["loc"] is (as far as I can tell) the same <longitude, latitude> pair the function would return, so nothing extra needs to happen there.
For the current map, look in $mapData and get the <longitude, latitude> of the center of the map.
Post Reply