displaying xy coordinates from database

Geoff Rowland geoffrey.rowland at NOAA.GOV
Mon Jun 13 13:05:35 EDT 2005


Here is the code I use (using phpmapscript with MySQL)....this example 
assumes that the coords stored in the database are pixels. There is 
probably a better way to do it, but this works.

//Convert pixels to map units
function pix2geo($click_x, $click_y) {
    global $map;
    $e= &$map->extent; //for saving writing
    $x_pct = ($click_x / $map->width);
    $y_pct = 1 - ($click_y / $map->height);
    $x_map = $e->minx + ( ($e->maxx - $e->minx) * $x_pct);
    $y_map = $e->miny + ( ($e->maxy - $e->miny) * $y_pct);
    return array($x_map, $y_map);
}

$sql = "SELECT x,y,city FROM my_table'";
$result = mysql_query($sql, $linkID);

//Draw the map and get the point layer
$img = $map->draw();
//MYPOINTS is the name of the point layer in the .MAP file
$layer = $map->getLayerByName('MYPOINTS');

for ($j=0; $j < mysql_num_rows($result); $j++)
    {
        $row = mysql_fetch_assoc($result);
        // convert pixels to geocoded location
        $map_pt = pix2geo($row[x],$row[y],$map->extent);
        //Create the point
        $pt = "pt" . $j;
        //create dynamic variables based on num of mysql rows returned
        ${$pt} = ms_newPointObj();
        ${$pt}->setXY($map_pt[0],$map_pt[1]);
        ${$pt}->draw($map, $layer, $img, 0 ,$row[city]);
    }

//draw the labels
$map->drawLabelCache($img);
//save image to disk - can specify name later
$url = $img->saveWebImage();

Cheers,
Geoff

Jessica Deegan wrote:

>Greetings list,
>
>A newbie to PHP/Mapscript, I am trying to display XY coordinates from a
>selected set of points (the XY to be displayed are a different location from
>selected point).
>
>As a test, I was successful in adding a dynamic point where a user clicks on
>the map using the sample found at
>http://mapserver.gis.umn.edu/cgi-bin/wiki.pl?PHPMapScriptAddPoint , but I'm
>at a loss to where to begin this process from a database perspective.  My
>data are stored in a PostgreSQL database.
>
>Any suggestions or starting points would be highly appreciated.
>thanks,
>Jessica
>  
>


-- 
Geoffrey Rowland
Technical Officer
Climate Information Project
NOAA Research
Office of  Global Programs
http://www.cip.ogp.noaa.gov
Contractor, 
RGII Technologies, Inc., a Computer Horizons Company
http://www.rg2.com
Phone: 301-427-2344
Fax : 301-427-2082
geoffrey.rowland at noaa.gov



More information about the mapserver-users mailing list