Finding which shape polygon a point is in with mapscript

Lowell Filak lfilak at MEDINACO.ORG
Thu Feb 3 08:21:09 EST 2005


The following message was sent by Stephen Woodbridge
<woodbri at SWOODBRIDGE.COM> on Wed, 2 Feb 2005 17:31:00 -0500.

> Well that is one way to attempt this feat, the other way would be to
> queryByPoint the polygon layer and let mapserver do the heavy lifting
> for you. I thought there were some example mapscript code for doing
> queries on the wiki or the website, but I couldn't find them doing a
> quick browse of it.
>
> Can someone point Camden at some examples of mapscript queries.
>
> -Steve W.
>
> Camden Daily wrote:
> > 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
> >

Camden,

There are some examples under the wiki with the newest one being for 4.2
though.
http://mapserver.gis.umn.edu/cgi-bin/wiki.pl?PerlMapScriptExamples35
HTH

Lowell



More information about the mapserver-users mailing list