[Mapserver-users] php query

Norbert Thieme norbert.thieme at ilmenau.baw.de
Wed Jul 14 04:35:34 EDT 2004


Michael Smith schrieb:
> Hi,
> 
> I am just starting to learn php.  I have mapserver up and running on windows
> IIS.  Does anyone have any php code to perform a search for a street and
> display it on a map in mapserver that they would be willing to share?  I
> would like my users to be able to type in a street name and zoom to it, I am
> just not able to do this and would like to learn how.  Any help will be
> useful for me.  Thank you.
> 
> Mike
> 
> _______________________________________________
> Mapserver-users mailing list
> Mapserver-users at lists.gis.umn.edu
> http://lists.gis.umn.edu/mailman/listinfo/mapserver-users
> 
> 


Hi,

I have not the whole code for you but I can give the direction to solve 
this yourself. Also I'm sure this is not the best way.

Because you first started with PHP you should play around a bit before 
going into the mapscript. After that the place to start is:

http://mapserver.gis.umn.edu/doc42/phpmapscript-class-guide.html

You should get familiar to this doc. There you find all the objects with 
their functions and parameters. You also find predefined keywords like 
"MS_SUCCESS". If you look through the doc you can often see at the 
beginning of an object part how to create such an object. There is 
always a set() function to change the parameters. Only for some 
parameters you will find specific functions.

Depending on the object you have different other functions. In front of 
the function you can see what type of object it returns. Then you see 
the name of the function and the possible/necessary input parameters and 
their types. For example:

int queryByAttributes(string qitem, string qstring, int mode)

which is the function to search a word in a field of the attribute table 
of the layer-data. You can use this function for your problem:

$layer->queryByAttributes("the field with the names", "the name to 
search", 0);

$layer is the object representing your street layer. The "->" is to 
address the function. The qitem/qstring you have to change to your needs 
but you have to put it in "" because these are strings. The 0 can also 
be chnaged to 1. The 0 means that the query mode is MS_SINGLE so only 
one result will be found. The 1 will find all features that have this 
criterion if there are more then one.

I think the qstring must be exact the same (case sensitive) like in the 
field of the attribute table. This could be a problem. If you want to 
get around that you can use php's dbase modul or odbc modul. But for the 
first application the mapscript should do it. Perhaps you should make a 
dropdown list of the possible names instead of a textfield to avoid 
problems.

To check if something is found you can use the function like the 
following because the results get saved in the resultcache of the layer. 
You can add the @ in front of $layer to disable the messages or you can 
just leave it:

if (@$layer->querybyattributes($sq_attr_field, $sq_attr_string, $q_mode) 
== MS_SUCCESS)
{
   $res_count = $layer->getnumresults(); // get the number of results

   for ($i = 0; $i < $res_count; $i++) // loop through the results -> 
only if you expect more then one result (not necessary in single mode)
   {
     $result = $layer->getResult($i); // get the result which includes 
the indexes of the found shape
     $shp_index  = $result->shapeindex;
     $tile_index = $result->tileindex;

     $layer->open(); // open the layer to get the shapeobject of the feature
     $shp = $layer->getshape($tile_index, $shp_index);

// FYI: if you want to get the items and the values to the items you 
ccan get that from that shapeobj --> so you can go through all the 
features and get the street names for the dropdown list - this is surely 
not the best method to do that but you can use it

     $layer->close(); // close the layer

     $shp_bound = $shp->bounds; // get the extent of the feature

     if ($shp_bound->project($layer_prj, $map_prj) == MS_SUCCESS) // 
change the projection of the layer to the one of the map
     {

// next: calculate a buffer for the extent ($res_rect_buf can be between 
1 and 100 -> %) --> I hope that there is nothing wrong so please check that

		  $res_buf_minx = $shp_bound->minx - ($shp_bound->maxx - 
$shp_bound->minx) * $res_rect_buf/200;
		  $res_buf_miny = $shp_bound->miny - ($shp_bound->maxy - 
$shp_bound->miny) * $res_rect_buf/200;
		  $res_buf_maxx = $shp_bound->maxx + ($shp_bound->maxx - 
$shp_bound->minx) * $res_rect_buf/200;
		  $res_buf_maxy = $shp_bound->maxy + ($shp_bound->maxy - 
$shp_bound->miny) * $res_rect_buf/200;

// next: add the buffer to the extent

		  $shp_bound->set(minx, $res_buf_minx);
		  $shp_bound->set(miny, $res_buf_miny);
		  $shp_bound->set(maxx, $res_buf_maxx);
		  $shp_bound->set(maxy, $res_buf_maxy);

// next: save the extent of the map to the bounds of the shape + buffer

                   $tmp_extent = $map->extent;
		  $map->setextent($shp_bound->minx, $shp_bound->miny, 
$shp_bound->maxx, $shp_bound->maxy);

		  $sq_img  = $map->drawquery(); // draw the image
		  $sq_path = $sq_img->savewebimage(); // get the path of the image

// next: set the extent of the map back --> only if you want that

		  $map->setextent($tmp_extent->minx, $tmp_extent->miny, 
$tmp_extent->maxx, $tmp_extent->maxy);
     }
     else
     {
       echo "<br>Problem with the projection.";
     }
   }
}

Good luck,
Norbert



More information about the mapserver-users mailing list