Converting lat/long distance to miles.
Camden Daily
cdaily at GMAIL.COM
Thu Jan 27 10:15:36 PST 2005
Ah, I seem to have it now. Instead of reprojectiong the point and
layer before my querybypoint calls, I reproject the point and the
individual shapes before I do my distancetoshape calls. I don't
exactly understand why the previous method didn't work, but I got it
now.
Here's the working function I'm using to find the closest shape to a
point and the distance in meters, as opposed to latlong:
function get_closest($layer, $point) {
global $map;
$layer = $map->getLayerByName($layer);
$layer->set("toleranceunits", MS_MILES);
// loop through, increasing the tolerance each time until we find some matches
$tolerance = 1;
$found_flag = false;
while (!$found_flag AND ($tolerance < 30)) {
$layer->set("tolerance", $tolerance);
$layer->queryByPoint($point, MS_MULTIPLE, -1);
$num_results = $layer->getNumResults();
if ($num_results > 0) {
$found_flag = true; }
else {
$tolerance += 1; }
}
if ($num_results == 0) {
return array(-1); }
else {
// clone the point
$temp_point = ms_newPointObj();
$temp_point->setXY($point->x, $point->y);
// project cloned point to NAD83 / Illinois East so that our
calculated distance are in meters, not dd
$temp_point->project(ms_newprojectionobj("init=epsg:4326"),
ms_newprojectionobj("init=epsg:26971"));
// set our min_distance to a very large value
$min_distance = 999999999999;
// loop through our results and see which on is closest
$layer->open();
for ($i=0; $i < $num_results; $i++) {
$result = $layer->getResult($i);
$shape = $layer->getShape($result->tileindex, $result->shapeindex);
// reproject our shape to the same projection as our cloned point
$shape->project(ms_newprojectionobj("init=epsg:4326"),
ms_newprojectionobj("init=epsg:26971"));
$distance = $temp_point->distanceToShape($shape);
if ($distance < $min_distance) {
$min_distance = $distance;
$closest = $shape; }
}
$layer->close();
return array($min_distance, $closest);
}
}
Thanks again for the help everyone!
More information about the MapServer-users
mailing list