Querying in MapScript
| Author: | Sean Gillies |
|---|---|
| Contact: | sgillies@frii.com |
| Revision: | 1.2 |
| Date: | 2004-09-25 |
1 Introduction
All examples in this document refer to the mapfile and testing layers distributed with MapServer 4.2+ and found under mapserver/tests.
1.1 Pseudocode
All examples will use a pseudocode that is consistent with the language independent API reference. Each line is a statement. For object attributes and methods we use the dot, '.', operator. Creation and deletion of objects will be indicated by 'new' and 'del' keywords. Other than that, the pseudocode looks a lot like Python.
2 Querying Overview
2.1 The Query Result Set
Map layers can be queried to select features using spatial query methods or the attribute query method. Ignoring for the moment whether we are executing a spatial or attribute query, results are obtained like so:
layer.query() # not an actual method! results = layer.getResults()
In the case of a failed query or query with zero results, 'getResults' returns NULL.
2.2 Result Set Members
Individual members of the query results are obtained like:
... # continued
if results:
for i in range(results.numresults): # iterate over results
result = results.getResult(i)
This result object is a handle, of sorts, for a feature of the layer, having 'shapeindex' and 'tileindex' attributes that can be used as arguments to 'getFeature'.
2.3 Resulting Features
The previous example code can now be extended to the case of obtaining all queried features:
layer.query()
results = layer.getResults()
if results:
# open layer in preparation of reading shapes
layer.open()
for i in range(results.numresults):
result = results.getResult(i)
layer.getFeature(result.shapeindex, result.tileindex)
... # do something with this feature
# Close when done
layer.close()
2.4 Backwards Compatibility
Scripts using the 4.2 API can continue to access query result members through layer methods:
for i in range(layer.getNumResults()):
result = layer.getResult(0)
but should adopt the new API for use in new work.
3 Attribute Queries
3.1 By Attributes
queryByAttributes()
4 Spatial Queries
4.1 By Rectangle
queryByRect()
4.2 By Point
queryByRect()
4.3 By Shape
queryByShape()
4.4 By Selection
queryByFeatures()
This How-to applies to: Any version.
How to query with Perl MapScript
my $queryLayer = $mapObj->getLayerByName('parcel');
#$mapObj->prepareQuery(); #not sure what this does
if ($queryLayer->queryByPoint($mapObj, $queryPoint,
$mapscript::MS_SINGLE, 0)
== $mapscript::MS_SUCCESS) {
my $queryResults = $queryLayer->getResults();
# warn("Num of results = ", $queryResults->{numresults});
my $result = $queryResults->getResult(0);
# warn("shapeindex = ", $result->{shapeindex});
$queryLayer->open();
my $feature = $queryLayer->getFeature($result->{shapeindex});
# warn("Query results: ",Dumper(%{$feature}));
# can't get this to show anything
# warn("Num of attributes = ", $feature->{numvalues});
# now do something with the feature(s)
# such as push the attributes to an HTML::Template variable
foreach (1 .. $feature->{numvalues}-1 ) {
# warn("Attribute $_ = ", $feature->getValue($_));
if (my $attr = $feature->getValue($_)) {
push @queryAttrs, { attr => $attr };
}
}
$queryLayer->close();
} else {
warn("queryByPoint => MS_FAILURE");
}
It worked for me but some aspects are still a little mysterious, like why I couldn't use Data::Dumper to show the results of the query, likely due to my own ignorance of one or both of the interfaces. Has anyone tried that?