[mapserver-users] Can't figure out queries with Mapscript.
Ben Logan
ben at wblogan.net
Thu Jul 4 04:35:01 PDT 2002
Well, I'm sure I'm missing something simple here because I see other
people making queries work, but I simply can't figure it out.
I am using Mapserver 3.6.1 and PHP 4.0.6 on Redhat 7.1. PHP is
running as a CGI (thanks to David Norris for help with that).
The only way I can see to make use of the results of a query, is to
create a template that contains some PHP code which I can then
evaluate. To test things out, I created a simple template called
county.php which contains the following lines:
<?
$county = "[COUNTY]";
$state = "[STATE]";
echo "$county\n$state\n";
?>
(COUNTY and STATE are fields in the dbf file.)
In my map file, I have tried adding a
TEMPLATE "county.php"
line in the CLASS definition I'm querying. (I have also tried using
the full path to county.php.) When I call processQueryTemplate(), it
returns nothing. So I tried moving the TEMPLATE line into the LAYER
definition, but that didn't work either. The layer gets queried,
because the county I click is highlited in the query map. Of course,
I realize that the data which processQueryTemplate() returns will need
to be parsed by PHP in order for the variables to be set, but I can't
get the function to return anything at all.
I found a message in the list archives that said that TEMPLATE defs
needed to be contained inside QUERY defs. Like this
QUERY
TEMPLATE dummy
END
I tried that in both the LAYER and CLASS sections, but either way
Mapscript gives a fatal error parsing the map file.
Am I going about this all wrong? Even if this isn't the way to get
the query data, why can't I get templates to work?
One other thing: I had METADATA definitions in my LAYER sections that
looked like this
METADATA
title "My Layer Title"
END
I was using these to define layer titles that Mapscript could use, and
it was working with Mapserver 3.5. In 3.6.1, I had to comment all of
the defs out, because it wouldn't parse the map file. Can anyone tell
me why?
I would really appreciate any help with these problems (especially the
querying problems). I have included my map file and my php script
below. They are both terrible messes because I've been using them to
get a feel for Mapscript.
Thanks,
Ben
--
Ben Logan: ben at wblogan dot net
OpenPGP Key KeyID: A1ADD1F0
So many women; so little nerve.
----------php.map-----------
#
# Start of map file
#
NAME TEST
STATUS ON
SIZE 400 400
#EXTENT -80.633987 36.708718 -80.104413 37.123488
EXTENT -81.379427 36.542485 -80.041493 37.366792
UNITS DD
#SHAPEPATH "data"
IMAGECOLOR 255 255 255
FONTSET "/var/www/html/ben/mapserv_demo/test/fonts"
#PROJECTION
# "proj=latlong"
# "datum=NAD83"
#END
# you may need to change this to match your MapServer build
#IMAGETYPE PNG
#
# Projection definition, consult the PROJ.4 documentation for parameter discussion
#
#PROJECTION
# "proj=utm"
# "ellps=GRS80"
# "zone=15"
# "north"
# "no_defs"
#
# OR:
#
# "init=epsg:26915"
#END
#
# Start of web interface definition (including WMS enabling metadata)
#
WEB
#HEADER test_header.html
#TEMPLATE county.php
#FOOTER test_footer.html
LOG "/var/www/html/ben/mapserv_demo/local/test.log"
MINSCALE 1
MAXSCALE 2000000
IMAGEPATH "/var/www/html/ben/mapserv_demo/local/tmp/"
IMAGEURL "http://mach1/ben/mapserv_demo/local/tmp/"
METADATA
WMS_TITLE "A test map."
WMS_ABSTRACT "This is a test."
WMS_ACCESSCONSTRAINTS none
# change this value to match your setup
WMS_ONLINERESOURCE "http://mach1.log.cabin/ben/mapserv_demo/local/test_init.html"
WMS_SRS "EPSG:26915"
END
END
#
# Start of reference map
#
#REFERENCE
# IMAGE 'graphics/ref.png'
# EXTENT -80.633987 36.708718 -80.104413 37.123488
# SIZE 200 200
# STATUS ON
# COLOR -1 -1 -1
# OUTLINECOLOR 255 0 0
#END
#
# Start of legend
#
LEGEND
KEYSIZE 18 12
LABEL
TYPE BITMAP
SIZE MEDIUM
COLOR 0 0 89
END
STATUS ON
END
#
# Start of scalebar
#
SCALEBAR
IMAGECOLOR 255 255 255
LABEL
COLOR 0 255 0
SIZE tiny
END
STYLE 1
SIZE 80 2
COLOR 0 0 255
UNITS MILES
INTERVALS 1
TRANSPARENT TRUE
STATUS TRUE
END
#
# Start of symbol definitions (we're only using a few)
#
#
# Start of layer definitions
#
LAYER
NAME "County"
TYPE POLYGON
STATUS DEFAULT
TEMPLATE /var/www/html/ben/mapserv_demo/local/county.php
#DATA "tgr51063cty00"
TILEINDEX "/var/www/html/ben/mapserv_demo/local/county.shp"
TILEITEM "location"
DATA "county"
#HEADER "test_header.html"
#FOOTER "test_footer.html"
#METADATA
# title "County Boundaries"
#END
CLASS
NAME "County"
OUTLINECOLOR 0 0 0
COLOR 255 255 200
END
END
#LAYER
# NAME "Relief"
# TYPE RASTER
# STATUS DEFAULT
# DATA "floyd.tif"
# #PROJECTION
# # "proj=utm"
# # "ellps=WGS84"
# # "zone=17"
# # "north"
# # "no_defs"
# #END
#END
#LAYER
# NAME "lpy"
# TYPE POLYGON
# STATUS OFF
## CONNECTIONTYPE OGR
## CONNECTION "data/carroll, 0"
# DATA "tgr51063lpy"
#
# #CLASSITEM 'STATES'
# CLASS
# NAME "LandmarkPoly"
# OUTLINECOLOR 0 0 255
# #COLOR 255 255 200
# END
#END
LAYER
NAME "Places"
TYPE POLYGON
STATUS OFF
TILEINDEX "/var/www/html/ben/mapserv_demo/local/plc00.shp"
TILEITEM "location"
LABELITEM "NAME"
DATA "plc00"
LABELMAXSCALE 500000
#METADATA
# title "Designated Places"
#END
CLASS
NAME "Places"
OUTLINECOLOR 0 255 255
COLOR 240 240 180
LABEL
MINDISTANCE 150
POSITION AUTO
SIZE 9
TYPE truetype
FONT "arial"
COLOR 0 0 0
END
END
END
LAYER
NAME "majroads"
TYPE LINE
STATUS OFF
TILEINDEX "/var/www/html/ben/mapserv_demo/local/majroads.shp"
TILEITEM "location"
DATA "majroads"
LABELITEM "RTE_NUM"
LABELMAXSCALE 500000
#METADATA
# title "Major Roads"
#END
#CLASSITEM 'STATES'
CLASS
NAME "Major Roads"
OUTLINECOLOR 0 0 0
COLOR 255 0 0
#TEMPLATE "majroads.html"
LABEL
#ANGLE AUTO
#MINFEATURESIZE 40
MINDISTANCE 150
POSITION AUTO
#BACKGROUNDCOLOR 255 255 255
#BACKGROUNDSHADOWCOLOR 80 80 80
#OUTLINECOLOR 0 0 0
#BUFFER 2
SIZE 8
TYPE truetype
FONT "arial"
COLOR 0 0 0
END
END
END
LAYER
NAME "minroads"
TYPE LINE
STATUS OFF
TILEINDEX "/var/www/html/ben/mapserv_demo/local/minroads.shp"
TILEITEM "location"
DATA "minroads"
LABELITEM "NAME"
LABELMAXSCALE 200000
#METADATA
# title "Minor Roads"
#END
CLASS
NAME "Minor Roads"
OUTLINECOLOR 0 0 0
COLOR 0 255 0
#TEMPLATE "minroads.html"
LABEL
#ANGLE AUTO
#MINFEATURESIZE 40
MINDISTANCE 150
POSITION AUTO
#BACKGROUNDCOLOR 255 255 255
#BACKGROUNDSHADOWCOLOR 80 80 80
#OUTLINECOLOR 255 255 255
#BUFFER 2
SIZE 8
TYPE truetype
FONT "arial"
COLOR 0 0 0
END
END
END
QUERYMAP
STATUS ON
STYLE HILITE
COLOR 0 255 255
END
END # Map File
----------test.pmap-----------
<?
dl("php_mapscript.so");
function getLayerTitle($layerObj) {
# If the layer passed has a metadata string named "title," it will be
# returned. Otherwise, the name of the layer will be returned.
$title = $layerObj->getMetaData("title");
if($title == "") {
$title = $layerObj->name;
}
return $title;
}
function pixelToGeo($map,$x,$y) {
# Convert x,y pixel values to georeferenced coords. Returns a
# pointObj.
$minx = $map->extent->minx;
$miny = $map->extent->miny;
$maxx = $map->extent->maxx;
$maxy = $map->extent->maxy;
$dx = $maxx - $minx;
$dy = $maxy - $miny;
$width = $map->width;
$height = $map->height;
$dxpp = (double)$dx/$width;
$dypp = (double)$dy/$height;
$geox = $minx + $dxpp*$x;
$geoy = $maxy - $dypp*$y;
$pointObj = ms_newPointObj();
$pointObj->setXY($geox,$geoy);
return $pointObj;
}
$val_zsize = 3;
$check_pan = "CHECKED";
$map_path = "/var/www/html/ben/mapserv_demo/local/";
$map_file = "php.map";
$layer_chkbtn_name = "layers";
$map = ms_newMapObj($map_path.$map_file);
if(isset($HTTP_POST_VARS["update"]) || (isset($HTTP_POST_VARS["mapa_x"]) && isset($HTTP_POST_VARS["mapa_y"])
&& !isset($HTTP_POST_VARS["full"])) ) {
$extent_to_set = explode(" ",$HTTP_POST_VARS["extent"]);
$map->setextent($extent_to_set[0], $extent_to_set[1], $extent_to_set[2], $extent_to_set[3]);
$my_point = ms_newpointObj();
$my_point->setXY($HTTP_POST_VARS["mapa_x"],$HTTP_POST_VARS["mapa_y"]);
$my_extent = ms_newrectObj();
$my_extent->setextent($extent_to_set[0], $extent_to_set[1], $extent_to_set[2], $extent_to_set[3]);
if($HTTP_POST_VARS["zoom"] == 2) {
# Query
$zoom_factor = 1;
$check_pan = "";
$check_zout = "";
$check_zin = "";
$check_query = "CHECKED";
$res = $map->queryByPoint(pixelToGeo($map,$HTTP_POST_VARS["mapa_x"],$HTTP_POST_VARS["mapa_y"]),MS_SINGLE,0);
if($res == MS_SUCCESS) {
# Draw a query map
$q_image = $map->drawQuery();
# Save the map.
$q_url = $q_image->saveWebImage(MS_PNG,1,1,0);
$query_res = $map->processQueryTemplate(array());
}
} else {
$zoom_factor = $HTTP_POST_VARS["zoom"]*$HTTP_POST_VARS["zsize"];
if($zoom_factor == 0) {
$zoom_factor = 1;
$check_pan = "CHECKED";
$check_zout = "";
$check_zin = "";
$check_query = "";
} else if ($zoom_factor < 0) {
$check_pan = "";
$check_zout = "CHECKED";
$check_zin = "";
$check_query = "";
} else {
$check_pan = "";
$check_zout = "";
$check_zin = "CHECKED";
$check_query = "";
}
}
$val_zsize = abs($zoom_factor);
if(isset($HTTP_POST_VARS["mapa_x"]) && isset($HTTP_POST_VARS["mapa_y"]) && !($HTTP_POST_VARS["zoom"] == "2")) {
$map->zoompoint($zoom_factor,$my_point,$map->width,$map->height,$my_extent);
}
}
$image = $map->draw();
if(is_array($HTTP_POST_VARS[$layer_chkbtn_name])) {
# Turn on the necessary layers
$layers = $HTTP_POST_VARS[$layer_chkbtn_name];
foreach($layers as $layer) {
$l = $map->getLayer($layer);
$l->set("status",MS_ON);
$l->draw($image);
}
}
$map->embedScalebar($image);
$map->drawLabelCache($image);
$image_url = $image->saveWebImage(MS_PNG, 1, 1, 0);
$extent_to_html = $map->extent->minx." ".$map->extent->miny." ".$map->extent->maxx." ".$map->extent->maxy;
# Draw the legend.
$legend_image = $map->drawLegend();
$legend_url = $legend_image->saveWebImage(MS_PNG, 1, 1, 0);
?>
<html>
<head>
<title>Testing</title>
</head>
<body>
<center>
<form method=POST action=<? echo $PHP_SELF ?>>
<table>
<tr>
<td colspan=2 align=center>
<input type=image name="mapa" src="<? echo $image_url ?>">
</td>
</tr>
<tr>
<td colspan=2 align=center>
<img src="<?echo $legend_url ?>" alt="Legend">
</td>
</tr>
<tr>
<td>
Pan
</td>
<td>
<input type=radio name="zoom" value=0 <? echo $check_pan ?>>
</td>
</tr>
<tr>
<td>
Zoom In
</td>
<td>
<input type=radio name="zoom" value=1 <? echo $check_zin ?>>
</td>
</tr>
<tr>
<td>
Zoom Out
</td>
<td>
<input type=radio name="zoom" value=-1 <? echo $check_zout ?>>
</td>
</tr>
<tr>
<td>
Query
</td>
<td>
<input type=radio name="zoom" value=2 <? echo $check_query ?>>
</td>
</tr>
<tr>
<td>
Zoom Size
</td>
<td>
<input type=text name="zsize" value="<? echo $val_zsize ?>" size=2>
</td>
</tr>
<tr>
<td>
Full Extent
</td>
<td>
<input type=submit name="full" value="Go" size=2>
</td>
</tr>
<tr>
<td colspan=2>
<?
# Here we print the checkboxes for the layers.
for($i=0; $i<$map->numlayers; $i++) {
# Get the layerObj.
$l = $map->getLayer($i);
# If the layer has Default status, don't display it.
if( $l->status == MS_DEFAULT ) {
continue;
}
# Otherwise, we'll print an html form element.
$chk = "";
if( $l->status == MS_ON ) {
$chk = "checked";
}
$layerTitle = getLayerTitle($l);
print("<input type=\"checkbox\" name=\"".$layer_chkbtn_name."[]\" value=$i $chk>$layerTitle<br>\n");
}
?>
</td>
</tr>
<tr>
<td colspan=2>
<input type=submit name="update" value="Update">
</td>
</tr>
<?
if(isset($q_url)) {
?>
<tr>
<td colspan=2>
<img src="<? echo $q_url ?>">
</td>
</tr>
<?
}
?>
</table>
<input type=hidden name="extent" value="<? echo $extent_to_html ?>" >
</form>
</center>
<pre>
<?
printf("minx: %f\nminy: %f\nmaxx: %f\nmaxy: %f\n",$map->extent->minx,$map->extent->miny,$map->extent->maxx,$map->extent->maxy);
printf("width: %d\nheight: %d\n",$map->width,$map->height);
printf("Lat/pixel: %f\n",($map->extent->maxy - $map->extent->miny)/$map->height);
printf("Long/pixel: %f\n",($map->extent->maxx - $map->extent->minx)/$map->width);
print("Buffer: ".$query_res);
?>
</pre>
</body>
</html>
More information about the MapServer-users
mailing list