[mapserver-users] quickly finding which point on map is nearest to mouse click

Vladimir Guzmán vguzman at bigfoot.com
Mon Apr 8 13:22:19 PDT 2002


Scott:

I've resolved the same situation doing the following with php-mapscript:

1. Get the user's click
2. Get the desired layer
3. Turn it on
4. Create a point Object with the user's click
5. Make a querybypoint, single, and with a medium tolerance.
	- It will find the nearest point -
6. Create a new shape with the new found point

My code looks like this:
(If you have questions on this, you can mail me)

$layer=$map->getLayerByName("transformadores");
$layer->set("status",1);

// 
To convert pixel coordinates to map coordinates:
$factor=($frm["maxx"]-$frm["minx"])/$ancho;
$px=$frm["minx"]+($ZOOM_x*$factor);
$py=$frm["maxy"]-($ZOOM_y*$factor);
// 
Create the point object:
$oPoint = ms_newPointObj();
$oPoint->setxy($px, $py);
// 
Make the query over the layer
@$results=$layer->queryByPoint($oPoint,MS_SINGLE,1000);
$oRes = $layer->getResult(0);

$map->setextent($frm["minx"], $frm["miny"], $frm["maxx"], $frm["maxy"]);
// 
If there is no points:
if($oRes=="") printf("There is no points near...");
else{
// 
Create the shapefile
	$layer->open($layer->shapepath);
	$oShape = $layer->getShape($oRes->tileindex,$oRes->shapeindex);
	$nuevo_id=$oShape->values["A02ID"];
	$nueva_long=$oShape->values["A02LONGITU"];
	$nueva_lat=$oShape->values["A02LATITUD"];
	$shpFname = $CFG->tmppath . "ali_" . session_id();
	$shpFile = ms_newShapeFileObj($shpFname, MS_SHP_POINT);
	$dbfFile = dbase_create($shpFname.".dbf", array(array("ID", "N", 10, 0)));
	include("funciones_shp.php");
	$campos=dbase_numfields($dbfFile);
	createPoint($nueva_long, $nueva_lat, $nuevo_id, $shpFile, $dbfFile);
	$shpFile->free();
	dbase_close($dbfFile);

	$shpFname = $CFG->tmppath . "ubi_" . session_id();
	$shpFile = ms_newShapeFileObj($shpFname, MS_SHP_POINT);
	$dbfFile = dbase_create($shpFname.".dbf", array(array("PROG_ID", "N", 5, 
0)));
	$campos=dbase_numfields($dbfFile);
	createPoint($px, $py, 1, $shpFile, $dbfFile);
	$shpFile->free();
	dbase_close($dbfFile);
}


Hope this helps.

-- 
Vladimir Guzmán R.
-----------------
www.main-task.com


Scott D Cogan wrote:

> I have a map with certain lat/lon coordinates marked as sites.  I would like
> users to be able to click on the map and have information for the nearest
> site become highlighted.  To do this, I have converted the mouse click
> coordinates to lat/lon coordinates which are mapped.  Then I am planning on
> calculating for each site, the distance between the click and the site, and
> storing the remembering the closest one.  Is there a quicker way to do this?
> Would it be beneficial to use the distanceToPoint function in mapscript?
> -scott
> 
> 
> 






More information about the MapServer-users mailing list