[Mapserver-users] mapscript: querying w/o shape files

Eric Bridger eric at gomoos.org
Thu Feb 6 07:20:03 PST 2003


Well, I did finally figure out how to do this so I will answer my own
question.  It seems like this might be a common thing to do.  I am using
Perl mapscript, but I imagine it would work with PHP mapscript as well.

You can create a layer of point objects which are queryable by creating
them as Features.  Using $layer->addFeature(shape) sets the connection
type to MS_INLINE, which makes the layer queryable w/o a shape file or
DB connection.  Some code snippets:

# points are in a hash: point_id => {latitude => y, longitude => x}
# Queries will return index into this array. so save the point name here
my @point_lookup = ();
my $i = 0;
my $layerObj = $map->getLayerByName('points');
my $point = new mapscript::pointObj();
# add point features to the layer
foreach my $point_id (keys %points){
    $point->{x} = $points{$point_id}{longitude};
    $point->{y} = $points{$point_id}{latitude};
    my $line = new mapscript::lineObj();
    $line->add($point);
    my $shp = new mapscript::shapeObj($mapscript::MS_SHAPE_POINT);
    $shp->add($line);
    $shp->setBounds();
    $shp->{text} = $point_id;
    $shp->{index} = $i;
    if($layerObj->addFeature($shp) == -1){
        warn "addFeature failed\n";
	next;
    }
    push @point_lookup, $point_id;
    $i++;
}

$layerObj->draw($map, $img);

# To query the layer assuming a point object containing the user's mouse
# click. $click_pt

if($layerObj->queryByPoint($map,$click_pt,$mapscript::MS_SINGLE,0)){
  print "No points found<br>\n";
}else{
   my $rslt = $layerObj->getResult(0);
   my $record = $rslt->{shapeindex};
   my $point_name = $point_lookup[$record];
   print "You clicked: $point_name.<br>\n";
}


And a minimal map file:  Note: you must have a TEMPLATE in the layer in
order for queryByPoint(), etc. to work.

MAP
  STATUS ON
  EXTENT -71.5 39.5 -63.0 46.0
  SIZE 504 385
  IMAGETYPE PNG
  UNITS DD

SYMBOL
  TYPE ELLIPSE
  NAME "circle"
  POINTS 1 1 END
  FILLED TRUE
END

LAYER
  NAME "points"
  TYPE POINT
  STATUS ON
  TOLERANCE 10
  # Need fake template for querys to work
  TEMPLATE "bogus.html"
  CLASS
    NAME "buoy"
    SYMBOL "circle"
    SIZE  8
    COLOR 255 0 0
    OUTLINECOLOR 0 0 0
    LABEL
      COLOR 255 0 0
      TYPE BITMAP
      SIZE MEDIUM
      POSITION AUTO
      PARTIALS FALSE
      BUFFER 2
    END # end of label
  END
END

END






On Wed, 2003-02-05 at 14:26, Eric Bridger wrote:
> 
> Using mapscript (mapserv 3.6.4) I am populating and drawing a layer with
> point objects.  I'm getting lat,lon and a text label from a mySQL
> database, in other words, from a data source or connection type not
> supported by MS. What are my options for using layer->queryByPoint() on
> this layer, if any? 
> 
> Since I know the order I've added the points I can track the record id
> of the points. Do I need to create a dummy *.dbf file, a real *.dbf
> file?  Is there some way to use MS_INLINE or perhaps layer->addFeature()
> or access the labels for the points created to allow me to query a layer
> created on the fly?
> 
> Eric Bridger
> eric at gomoos.org  Gulf of Maine Ocean Observing System www.gomoos.org
> 
> 
> 
> _______________________________________________
> Mapserver-users mailing list
> Mapserver-users at lists.gis.umn.edu
> http://lists.gis.umn.edu/mailman/listinfo/mapserver-users
> 





More information about the MapServer-users mailing list