Finding which shape polygon a point is in with mapscript

Camden Daily cdaily at GMAIL.COM
Wed Feb 2 17:05:58 EST 2005


Why is everything so much harder than it seems?

I'm trying to take a point, and find which shape within a polygon
layer contains that point.

The code I'm using is this:

function get_containing($layer, $longitude, $latitude) {
  global $map;
  $point = ms_newPointObj();
  $point->setXY($longitude, $latitude);
  $layer = $map->getLayerByName($layer);
  $layer->open();
  for ($i=0; $layer->getShape(-1, $i) != -1; $i++) {
    $shape = $layer->getShape(-1, $i);
    if ($shape->contains($point)) {
      $layer->close();
      return $shape; }
  }
  $layer->close();
  return -1;
}

and the map layer I'm using is school districts:

  LAYER
    NAME "school_elm"
    TYPE POLYGON
    STATUS OFF
    DATA "tgr17031elm"
    TEMPLATE dummy
    PROJECTION
      "init=epsg:4326"
    END
  END

The error I receive (on the line that calls getShape):
   Fatal error: [MapServer Error]: msLayerGetShape(): Shapefile layer
has not been opened.

Which doesn't make much sense, as I'm calling layer->open and it
doesn't return an error on that.

Can anyone see something wrong with this, or tell me an effective way
to loop through all of the shapes within a layer?  Is there a way to
get the number of shapes in a layer?

-Camden Daily



More information about the mapserver-users mailing list