[mapserver-users] distance between line and a given point using mapscript
Mark Brooks
mark_brooks at ncsu.edu
Fri Jul 24 13:01:51 PDT 2009
I'm trying to create several new line objects and then find the lines
that are within x miles of a given point. (i.e., how far away a given
point is from the track of a hurricane or the path of a ship). I'm
having some difficulties and I hope someone can help.
How do I query a layer with one or more line features to find all
features with X miles of a given point?
Here is how I'm approaching the problem in PHP:
// Test array of points. This is used to make a line.
$points[0]['lat'] = 35.5;
$points[0]['lon'] = -78.8;
$points[1]['lat'] = 36.5;
$points[1]['lon'] = -79.8;
$points[2]['lat'] = 37.5;
$points[2]['lon'] = -80.1;
// make new map object.
$map = ms_newMapObj('mapdata/mymap.map');
// Create new line
$my_line = ms_newLineObj();
// Loop through points, and add to the line
while (list(,$coordinate)=each($points)){
// Create new point object
$my_point = ms_newpointObj();
$my_point->setXY($coordinate['lon'],$coordinate['lat']);
// Add point to line object
$my_line->add($my_point);
}
// Now create a new shape
$my_shape = ms_newShapeObj(MS_SHAPE_LINE);
$my_shape->add($my_line);
// create new layer
$my_layer = ms_newLayerObj($map);
// set some fields for this new layer
//$my_layer->set("units","miles"); // does not seem to work as expected
// Create new class and add TEMPLATE. This is required for queries on
the layer to work?
$my_class = ms_newClassObj($my_layer);
$my_class->set("template",'/home/www/html/query_template.html');
// Add feature to the layer
$my_layer->addFeature($my_shape);
// Draw map. Query doesn't work if I don't do this now.
$map->draw();
// Make a new point object that will be our ref point
// to check how far away the line is from this point.
$my_point = ms_newpointObj();
$my_point->setXY(-82,36); // lon, lat
// Run a point query on the line object layer; layer must have a class
with a LAYER TEMPLATE value
// The third argument is the buffer tolerance. Default unit is pixels?
Must figure out how to make miles or km.
if ($my_layer->queryByPoint($my_point,MS_SINGLE,1)==MS_SUCCESS){
$my_result = $my_layer->getResult(0); // Get the result
$my_layer =
$my_layer->getShape($my_result->tileindex,$my_result->shapeindex); //
Get the shape
}
More information about the MapServer-users
mailing list