wiki:SWIGMapScriptQuery

MS4W Version 5.x Query From .MAP File Sample

original source: https://gist.github.com/andrewteg/81b8d45948d67003f5f1170e51d66e61

<html><head><title>query sample</title></head><body><PRE>
<?php

// sample filter strings to search on... 'name' is field in Attribute Table for Layer
$filter_string = '("[name]" == "Canada")';              //exact (use ==) - case-sensitive
//$filter_string = '("[name]" ~* "^ca.*")';                     //starts Ga (^ for starting) not case-sensitive
//$filter_string = '("[name]" ~* ".*CA.*")';            //like Ga (.* maybe not needed?!?) not case-sensitive

$map = new mapObj("/ms4w/apps/local-demo/local.map");

$layer = $map->getLayer(1);                                     //second layer in mapfile
$layer->setFilter($filter_string);                              //filter results
$layer->template = true;                                        //for historical reasons, the query attribute must be non-NULL for a layer to be queryable RE: https://www.mapserver.org/mapscript/mapscript.html#layerobj-attributes

$ext = $layer->getExtent();
$query = $layer->queryByRect($map, $ext);                       //must query before getting $layer numitems
$numResults = $layer->getNumResults();

$layername = $layer->name;
$layeritems = $layer->numitems;                                 //count of items in attribute table
//echo 'Layer '.$layername.' has '.$layeritems.' items<br>';
for ($x=0;$x<$layeritems;$x++) {
        $layer_headers[$x] = $layer->getItem($x);               //for the layer, this gets the attribute field names
}
//print_r($layer_headers);

if($query == MS_SUCCESS) {
        echo 'Query has '.$layer->getNumResults().' Results:<br>';
        if($layer->getNumResults()>0) {
                for ($i=0; $i < $layer->getNumResults(); $i++) {
                        $result = $layer->getResult($i); //print_r($result);
                        if($result === FALSE) {
                                error_log('No result '.$i);
                        } else {
                                $shape = $layer->getShape($result); //print_r($shape);
                                //echo ("#$i  OBJECTID = ".$shape->values['OBJECTID'].'<br>');  //ms4w 3.x
                                //echo 'vals='.$shape->numvalues;
                                echo ("#$i  OBJECTID = ".$shape->getValue(0));                  //ms4w 4.x
                                $vals = array();
                                echo '<blockquote>';
                                for ($x=0;$x<$shape->numvalues;$x++) {
                                        //echo $x.'='.$shape->getValue($x).'<br>';
                                        $vals[$layer_headers[$x]] = $shape->getValue($x);
                                }
                                print_r($vals);
                                //echo 'WKT='.$shape->toWKT().'<br>';
                                print_r($shape->values);
                                echo '</blockquote>';
                        }
                }
        }
} else {
        echo('Query Returned No Results');
}
echo '<hr>END<hr>'; ?>
</PRE></body></html>
Last modified 7 months ago Last modified on 2021-12-21T17:35:09-04:00
Note: See TracWiki for help on using the wiki.