[mapserver-users] distance between line and a given point usingmapscript
Steve Lime
Steve.Lime at dnr.state.mn.us
Sat Jul 25 07:36:57 PDT 2009
> How do I query a layer with one or more line features to find all
> features with X miles of a given point?
Your line layer should like so:
LAYER
NAME 'lines'
TYPE LINE
...other junk...
TEMPLATE 'dummy' # to be queryable
TOLERANCEUNITS MILES
TOLERANCE X
END
Then in mapscript you'd do a
$my_layer->queryByPoint($my_point, MS_MULTIPLE, -1);
The buffer parameter is used to override a layer tolerance so setting to -1 tells
the method to ignore it. If you do use it then it's given in the units of the layer
being searched (before any projection).
Does that help?
Steve
>>> Mark Brooks <mark_brooks at ncsu.edu> 07/24/09 3:26 PM >>>
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
}
_______________________________________________
mapserver-users mailing list
mapserver-users at lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/mapserver-users
More information about the MapServer-users
mailing list