[mapserver-users] user map from mysql table: Have a look at my code. Can`t add layer with user data to map
Jan Mantkowski
jan.mantkowski at gmx.de
Tue Mar 19 08:10:00 PST 2002
Hi all,
i run into a problem i can not solve by myself.
I like to display names (at the right locations) of users as an additional
layer to an existing map.
I use:
Windows 98
Apache (newest)
Mapserver 3.5
PHP/mapscript
php 4.0.6
mysql(newest)
The following parts work right now:
get test data from mysql table
display test data on screen for check
make users.dbf, users.shp, users.shx
display map on screen
The problem is, that the user names will NOT be displayed at the map.
Please help.
I am using the first simple europe map and code from the example at
http://mapserver.gis.umn.edu/doc/phpmapscript-byexample-howto.html
This is the test data stored in a mysql table:
51.5063 -0.1271 Jan London
40.4203 -3.7057 Jose Madrid
48.8547 2.3453 Madeleine Paris
53.5548 9.9898 Fritz Hamburg
The last name gives a hint, where the user should be displayed.
I will post two files: europe.map and index.php please have a look at them:
This is the map file i use. Its the original map file from the europe
example. I only added the Layer PostNukeUsers.
(I grabbed the state park layer from the tutorial3.5, example4.map file and
changed STATUS to ON, also i deleted the SYMBOLSCALE 250000 line)
I do not "really" understand what i did, so bear with me.
NAME FIRSTMAP
SIZE 400 400
STATUS ON
EXTENT -5696501 1923039 5696501 11022882
UNITS METERS
SHAPEPATH "data"
WEB
IMAGEPATH "c:\Apache\htdocs\tmp\"
IMAGEURL "/tmp/"
END
LAYER
NAME world
TYPE POLYGON
STATUS ON
DATA europa
CLASS
TEMPLATE void
COLOR 110 50 100
OUTLINECOLOR 200 200 200
#END
END
TOLERANCE 10
END
LAYER
NAME "Nations"
DATA europa
STATUS ON
TYPE ANNOTATION
CLASS
COLOR -1 -1 -1 # no marker will be drawn
LABEL
SIZE MEDIUM
COLOR 132 31 31
OUTLINECOLOR 255 255 255
BUFFER 4
END
END
END
END
LAYER
NAME "PostNukeUsers"
DATA users
STATUS ON
TYPE ANNOTATION
LABELITEM "text"
CLASS
COLOR -1 -1 -1 # no marker will be drawn
LABEL
SIZE MEDIUM
COLOR 132 31 31
OUTLINECOLOR 255 255 255
BUFFER 4
END
END
END
END
This is the index-standalone.php file, that does all the work:
<?php
// load .dll`s, this makes sure that you do not need to edit the php.ini.
dl("php_mapscript_35.dll");
dl("php_proj.dll");
dl("php_dbase.dll");
// start preparation files
$shpPath = "c:/Apache/htdocs/pn_71/html/modules/map/data/"; // Do not forget
the / in the end of the line!
$shpFileName ="users";
$shpFname = $shpPath.$shpFileName;
$shpFile = ms_newShapeFileObj($shpFname, MS_SHP_POINT);
$def =
array(
array("name", "C", 20), // in the map.dbf there is no need for lat and lon
);
$dbfFile = dbase_create( $shpFname.".dbf", $def);
// end preparation files
// lets do it
$db = mysql_connect("localhost", "root", "");
mysql_select_db("Rogue",$db);
$result = mysql_query("SELECT * FROM nuke_map",$db);
if ($result === false) die("failed");
echo " This is the test data from the mysql table that should be displayed.
The last name gives a hint, where the user should be displayed.";
echo "<br>";
echo "<br>";
echo
"lat
lon n
ame<br>\n";
while ($fields = mysql_fetch_row($result)) {
$name=$fields[0]; //i know that there are 3 columns in the mysql
table
$lat=$fields[1];
$lon=$fields[2];
out($lat, $lon, $name); //prints data from mysql table on screen for
check
createPoint($lat, $lon, $name); //creates map.dbf, map.shp, map.shx
}
display_map(); //displays a map of europe, this is where
the users will be displayed
//end
function display_map (){
$map_path="/data/";
$map = ms_newMapObj($map_path."europe.map");
$image=$map->draw();
$image_url=$image->saveWebImage(MS_PNG,1,1,0);
echo "<img src=\"".$image_url."\">";
}
function out($lat, $lon, $name){
echo
$lat." ".$lon." ".$name."<br>\
n";
}
function createPoint($lat, $lon, $name){
GLOBAL $shpFile, $dbfFile;
// Create shape
$oShp = ms_newShapeObj(MS_SHP_POINT);
$oLine = ms_newLineObj();
$oLine->addXY($lat, $lon);
$oShp->add( $oLine );
$shpFile->addShape($oShp);
// Write attribute record
dbase_add_record($dbfFile, array($name));
}
// clean up
$shpFile->free();
#dbase_close($dbfFile);
//$result->close(); // this line is deaktivated, because it causes this
error:
//Fatal error: Call to a member function on a non-object in ....
//$db->close(); // this line is deaktivated, because it causes this error:
//Fatal error: Call to a member function on a non-object in....
// Any help is apereciated to fix this.
// I believe it is because of changes from php3.0 to 4.0, but i know nothing
about classes and methods, so i can not fix it by my own.
?>
Thank for your help.
Jan
More information about the MapServer-users
mailing list