= query_points2.pl = Improved perl mapscript 3.6.6 example of creating a queryable layer of point objects, by creating them as Layer Features without an associated shape file or database connection. The points are in the perl program. Of course the points could come from any database or text file or etc. The .map EXTENT must match the points you want to display. One other subtle point: The points layer in the map file must have a TEMPLATE for the layer to queryable, even though it is not used. See the comments in the code. ---- {{{ #!perl #!/usr/bin/perl # This is an example of creating a layer of point objects (actually buoys in the Gulf of Maine, U.S.) It should be # easily adaptable to any set of points. It creates the points as features in the layer which means that they # can still be retrieved using $layer->queryByPoint() even though no shape file or database connection exists # for them. # In this revised edition: # a) the points are drawn using $point->draw(). This allows us to do a getClass() for each point and set class values # such as $class->{size} for each point. # b) We still call addFeature() so that the layer is queryable, which is the main point. # c) We use a numeric key value as the $shape->{index} which maps to our "database". This will be returned by # queryByPoint(). # d) We no longer need to call $layer->draw() since we already called $point->draw(). Thus the features serve as # an invisible layer just for querying. # Author: Eric Bridger eric@gomoos.org eric@maine.com # Data: Thu Aug 28 10:56:06 EDT 2003 # This has been tested with mapserver 3.6.X not 4.0 use strict; use mapscript; use CGI ":cgi"; $ENV{MS_ERRORFILE} = '/path/to/mapserver.log'; my $q = new CGI; my $msg = ''; # A hash of points. First field is the key value used in addFeature() and returned by queryByPoint(). # This could come from any external database, etc. my %points = ( 10202 => {'longitude' => -67.0173, 'latitude' => 44.8911, 'size' => 5, 'label' => 'one', }, 20103 => {'longitude' => -66.0146, 'latitude' => 45.2045, 'size' => 10, 'label' => 'two', }, 30105 => {'longitude' => -68.3578, 'latitude' => 43.7148, 'size' => 12, 'label' => 'three', }, 40102 => {'longitude' => -66.5528, 'latitude' => 43.6243, 'size' => 18, 'label' => 'four', }, 50105 => {'longitude' => -68.9983, 'latitude' => 44.0555, 'size' => 20, 'label' => 'five', }, 60102 => {'longitude' => -67.8800, 'latitude' => 43.4900, 'size' => 10, 'label' => 'six', }, 70104 => {'longitude' => -70.5665, 'latitude' => 42.5185, 'size' => 10, 'label' => 'seven', }, 80103 => {'longitude' => -70.4278, 'latitude' => 43.1807, 'size' => 10, 'label' => 'eight', }, 90104 => {'longitude' => -68.1087, 'latitude' => 44.1058, 'size' => 8, 'label' => 'nine', }, 100202 => {'longitude' => -70.0578, 'latitude' => 43.5673, 'size' => 15, 'label' => 'ten', }, ); my $image_name = sprintf("%0.10d",rand(1000000000)) . ".png"; # see points.map my $map = new mapscript::mapObj("points2.map"); if(!$map){ warn "New mapObj() error: $mapscript::ms_error->{message}\n"; } # Create a point object representing the mouse click on the map. my ($x, $y) = get_click($q, $map); my $click_pt = undef; if($x != 0 && $y != 0){ $click_pt = new mapscript::pointObj(); $click_pt->{x} = $x; $click_pt->{y} = $y; } my $img = $map->prepareImage(); if(!$img){ warn "prepareImage() error: $mapscript::ms_error->{message}\n"; } my $layerObj = undef; # Add points as Features to the point layer. $layerObj = $map->getLayerByName('points'); my $point = new mapscript::pointObj(); # Queries will return index into this array. foreach my $point_id (keys %points){ $point->{x} = $points{$point_id}{longitude}; $point->{y} = $points{$point_id}{latitude}; # Features require shape objects, which require lines, so create a single point line. my $line = new mapscript::lineObj(); $line->add($point); my $shp = new mapscript::shapeObj($mapscript::MS_SHAPE_POINT); $shp->add($line); $shp->setBounds(); # Don't set any text, $point->draw() will draw the text. #$shp->{text} = $point_id; # set the shape index to our database key value. # the $shp->{index} can be any NUMERIC value. If our database key values were alphanumeric # we would need to use a lookup array and set $shp-{index} to 0,1,2,... # queryByPoint() results will return this value, but it must be numeric. $shp->{index} = $point_id; $layerObj->addFeature($shp); # we only have one class in this layer. my $class = $layerObj->getClass(0); # set symbol size based on a value from our "database" $class->{size} = $points{$point_id}{size}; $point->draw($map, $layerObj, $img, undef, $points{$point_id}{label}); } # Query based on the mouse click point. if($click_pt){ $msg .= "
\n";
$layerObj = $map->getLayerByName('points');
if($layerObj->queryByPoint($map,$click_pt,$mapscript::MS_SINGLE,0)){
$msg .= "No Points found
\n";
}else{
my $results = $layerObj->{resultcache};
# we only expect one result.
my $rslt = $layerObj->getResult(0);
# this is the numeric value we used for the shape passed to addFeature() above.
my $point_id = $rslt->{shapeindex};
$msg .= "Click found point: $point_id.
\n";
$msg .= "name is: $points{$point_id}{label}.
\n";
$msg .= "size is: $points{$point_id}{size}.
\n";
$msg .= "lat: $points{$point_id}{latitude} long: $points{$point_id}{longitude}.
\n";
}
$msg .= "