[mapserver-users] distance between line and a given point usingmapscript
Mark Brooks
mark_brooks at ncsu.edu
Tue Jul 28 05:49:05 PDT 2009
I got the looping through each result. However, all distances are
calculated to -1, which I know can't be right. In debugging, I'm also
echo'ing the shape index for each result, but it's always the same. Am I
constructing the layer correctly?
// create array of points from database query results.
for ($i=0; $row=mysql_fetch_assoc($result); $i++){
$points[$row['snum']][$i]['lat'] = $row['lat'];
$points[$row['snum']][$i]['lon'] = $row['lon'];
} // End loop through points
// create map object
$map = ms_newMapObj('wms.map');
// create new layer for hurricane tracks.
$my_layer = ms_newLayerObj($map);
// set some fields for this new layer
$my_layer->set("toleranceunits","miles");
$my_layer->set("tolerance","20");
// Create new class and add TEMPLATE.
$my_class = ms_newClassObj($my_layer);
$my_class->set("template",'query_template.html');
// Loop through points, create line for each storm, and add track points
to new line object.
while (list($snum,$coordinates)=each($points)){
// Create new line object
$my_line = ms_newLineObj();
// Loop through each point coordinate for this snum
while (list($num,$coordinate)=each($coordinates)){
// 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);
} // done adding this storm's points to the line object
// Now create a new shape object
$my_shape = ms_newShapeObj(MS_SHAPE_LINE);
$my_shape->add($my_line);
// Add feature to the layer
$my_layer->addFeature($my_shape);
} // end creating and adding points to line objects and line objects to
the layer.
// Draw map.
$map->draw();
// Make a new point object that will be our reference point.
$my_ref_point = ms_newpointObj();
$my_ref_point->setXY(-78.7,35.4); // user-entered coordinates of where we
are querying from.
// Run a point query on the line object layer.
if (@$my_layer->queryByPoint($my_point,MS_MULTIPLE,-1)==MS_SUCCESS){
// Loop through results.
$num_results = $my_layer->getNumResults();
echo "# results = $num_results\n\n";
// Loop through each result
for ($num=0; $num<$num_results; $num++){
echo "result # $num:\n";
// Get this result
$my_result = $my_layer->getResult($num);
//$my_shape =
$my_layer->getShape($my_result->tileindex,$my_result->shapeindex);
// Get the shape
echo "shapeindex = ".$my_result->shapeindex."\n";
$my_shape = $my_layer->getFeature($my_result->shapeindex);
// Get the feature
echo "distanceToShape =
".$my_ref_point->distanceToShape($my_shape)."\n\n"; //
calculate distance from ref point to feature
}
}
On Mon, July 27, 2009 11:54 pm, Steve Lime wrote:
> There are lots of samples that show you how to loop through a set of
> search
> results. There's a getNumResults() method for layer objects and then a
> getResult()
> method to retrieve each one individually.
>
> Steve
>
>>>> Mark Brooks <mark_brooks at ncsu.edu> 07/27/09 9:50 AM >>>
> Steve,
>
> This is a huge help. The only thing I had not figured out was the
> 'toleranceunits' and 'tolerance' fields. I appreciate your help!
>
> I intend to add many, possible hundreds, lines to a layer. Then query
> it with a given reference point. How do I get the number of results and
> then loop through each one?
>
> Best Regards,
> Mark
>
>
> Steve Lime wrote:
>>> 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