Finding which shape polygon a point is in with mapscript

Stephen Woodbridge woodbri at SWOODBRIDGE.COM
Wed Feb 2 14:31:00 PST 2005


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
>



More information about the MapServer-users mailing list