Converting lat/long distance to miles.

Camden Daily cdaily at GMAIL.COM
Thu Jan 27 09:29:10 PST 2005


Thanks for the suggestions everyone, but I still can't figure this out.

I've added projection blocks for all of my layers and an output
projection as well (epsg 4326 for lat/long).  The difficulties I'm
having seem to be mostly based on the fact that I initially need to
set my extents and place various points on the map using lat/long with
units of decimal degrees.  However, when I then calculate my
distances, I'd like the results in miles.

It seems to me that I should be able to set my extents, place my
points, then reproject both the points and the layer I'm searching
against to a projection that uses meters (which I can easily convert
to miles) for the distance calculations, but this just isn't working
for me.

Here's the code I'm using to try to find distances:

  // clone the layer
  $layer = $map->getLayerByName($layer_name);
  $temp_layer = ms_newLayerObj($map, $layer);

  // clone the point
  $temp_point = ms_newPointObj();
  $temp_point->setXY($point->x, $point->y);

  // project cloned point and layer 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"));
  $temp_layer->setProjection("init=epsg:26971");

  // the toleranceunits don't have to be the same as the projection
units to work
  $temp_layer->set("toleranceunits", MS_MILES);

  $tolerance = 1;
  $found_flag = false;
  while (!$found_flag AND ($tolerance < 30)) {
    $temp_layer->set("tolerance", $tolerance);
    $temp_layer->queryByPoint($temp_point, MS_MULTIPLE, -1);
    $num_results = $temp_layer->getNumResults();
    if ($num_results > 0) {
      $found_flag = true; }
    else {
      $tolerance += 1; }
  }

  echo $num_results;

If I comment out the two lines that reproject the point and layer, it
works perfectly, except that my distance is in dd.  With the
reprojections, I don't get any results at all.  I don't think it's an
issue with my toleranceunits being in miles, since it works fine that
way even when my projections are in units of dd.

Any insights would be greatly appreciated.



More information about the MapServer-users mailing list