Finding which shape polygon a point is in with mapscript

Eric Bridger eric at GOMOOS.ORG
Wed Feb 2 17:39:49 EST 2005


I have some 3.6 Perl Mapscript code which does something similiar, which I can send you if you like.

A quick look at the code shows that I always did a
$layer->open($layer->{data});
i.e. passed an explicit shape file name to open.

Eric


At 05:31 PM 02/02/2005 -0500, Stephen Woodbridge wrote:
>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